/* An actor that outputs the sum of the elements of an input array. Copyright (c) 2007 The Regents of the University of California. All rights reserved. Permission is hereby granted, without written agreement and without license or royalty fees, to use, copy, modify, and distribute this software and its documentation for any purpose, provided that the above copyright notice and the following two paragraphs appear in all copies of this software. IN NO EVENT SHALL THE UNIVERSITY OF CALIFORNIA BE LIABLE TO ANY PARTY FOR DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF THE UNIVERSITY OF CALIFORNIA HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. THE UNIVERSITY OF CALIFORNIA SPECIFICALLY DISCLAIMS ANY WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE PROVIDED HEREUNDER IS ON AN "AS IS" BASIS, AND THE UNIVERSITY OF CALIFORNIA HAS NO OBLIGATION TO PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS. PT_COPYRIGHT_VERSION_2 COPYRIGHTENDKEY */ package ptolemy.actor.lib; import ptolemy.data.ArrayToken; import ptolemy.data.Token; import ptolemy.data.type.ArrayType; import ptolemy.kernel.CompositeEntity; import ptolemy.kernel.util.IllegalActionException; import ptolemy.kernel.util.InternalErrorException; import ptolemy.kernel.util.NameDuplicationException; import ptolemy.kernel.util.Workspace; ////////////////////////////////////////////////////////////////////////// //// ArraySum /** Compute the sum of the elements in an array. This actor reads an array from the <i>input</i> port and sends the sum of its elements to the <i>output</i> port. The output data type is at least the type of the elements of the input array. The elements of the input array have to support addition, or an exception will be thrown in the fire() method. This actor is similar to ArrayAverage, except that it supports data types that do not support division by an integer. @author Edward A. Lee, Christine Avanessians @version $Id$ @since Ptolemy II 6.1 @see ArrayAverage @Pt.ProposedRating Red (cxh) @Pt.AcceptedRating Red (cxh) */ public class ArraySum extends Transformer { /** Construct an actor with the given container and name. * @param container The container. * @param name The name of this actor. * @exception IllegalActionException If the actor cannot be contained * by the proposed container. * @exception NameDuplicationException If the container already has an * actor with this name. */ public ArraySum(CompositeEntity container, String name) throws NameDuplicationException, IllegalActionException { super(container, name); // set type constraints. output.setTypeAtLeast(ArrayType.elementType(input)); } /////////////////////////////////////////////////////////////////// //// public methods //// /** Override the base class to set type constraints. * @param workspace The workspace for the new object. * @return A new instance of ArraySum. * @exception CloneNotSupportedException If a derived class contains * an attribute that cannot be cloned. */ public Object clone(Workspace workspace) throws CloneNotSupportedException { ArraySum newObject = (ArraySum) super.clone(workspace); try { newObject.output.setTypeAtLeast(ArrayType .elementType(newObject.input)); } catch (IllegalActionException e) { // Should have been caught before. throw new InternalErrorException(e); } return newObject; } /** Consume at most one array from the input port and produce * the average of its elements on the <i>output</i> port. * If there is no token on the input, or if the input array * is empty, then no output is produced. * @exception IllegalActionException If there is no director. */ public void fire() throws IllegalActionException { super.fire(); if (input.hasToken(0)) { ArrayToken token = (ArrayToken) input.get(0); if (token.length() == 0) { return; } Token sum = token.getElement(0); for (int i = 1; i < token.length(); i++) { sum = sum.add(token.getElement(i)); } output.send(0, sum); } } }