001/* Output the length of an array.
002
003 Copyright (c) 1998-2014 The Regents of the University of California.
004 All rights reserved.
005 Permission is hereby granted, without written agreement and without
006 license or royalty fees, to use, copy, modify, and distribute this
007 software and its documentation for any purpose, provided that the above
008 copyright notice and the following two paragraphs appear in all copies
009 of this software.
010
011 IN NO EVENT SHALL THE UNIVERSITY OF CALIFORNIA BE LIABLE TO ANY PARTY
012 FOR DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES
013 ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF
014 THE UNIVERSITY OF CALIFORNIA HAS BEEN ADVISED OF THE POSSIBILITY OF
015 SUCH DAMAGE.
016
017 THE UNIVERSITY OF CALIFORNIA SPECIFICALLY DISCLAIMS ANY WARRANTIES,
018 INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
019 MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE
020 PROVIDED HEREUNDER IS ON AN "AS IS" BASIS, AND THE UNIVERSITY OF
021 CALIFORNIA HAS NO OBLIGATION TO PROVIDE MAINTENANCE, SUPPORT, UPDATES,
022 ENHANCEMENTS, OR MODIFICATIONS.
023
024 PT_COPYRIGHT_VERSION_2
025 COPYRIGHTENDKEY
026
027 */
028package ptolemy.actor.lib;
029
030import ptolemy.data.ArrayToken;
031import ptolemy.data.IntToken;
032import ptolemy.data.type.ArrayType;
033import ptolemy.data.type.BaseType;
034import ptolemy.kernel.CompositeEntity;
035import ptolemy.kernel.util.IllegalActionException;
036import ptolemy.kernel.util.NameDuplicationException;
037import ptolemy.kernel.util.Workspace;
038
039///////////////////////////////////////////////////////////////////
040//// ArrayLength
041
042/**
043 Output the length of an array.  This actor reads an ArrayToken from the
044 <i>input</i> port and sends its length to the <i>output</i> port.
045
046 @author Edward A. Lee
047 @version $Id$
048 @since Ptolemy II 1.0
049 @Pt.ProposedRating Green (celaine)
050 @Pt.AcceptedRating Green (cxh)
051 */
052public class ArrayLength extends Transformer {
053    /** Construct an actor with the given container and name.
054     *  @param container The container.
055     *  @param name The name of this actor.
056     *  @exception IllegalActionException If the actor cannot be contained
057     *   by the proposed container.
058     *  @exception NameDuplicationException If the container already has an
059     *   actor with this name.
060     */
061    public ArrayLength(CompositeEntity container, String name)
062            throws NameDuplicationException, IllegalActionException {
063        super(container, name);
064
065        // set type constraints.
066        input.setTypeAtLeast(ArrayType.ARRAY_BOTTOM);
067        output.setTypeEquals(BaseType.INT);
068    }
069
070    ///////////////////////////////////////////////////////////////////
071    ////                         public methods                    ////
072
073    /** Clone the actor into the specified workspace. This calls the
074     *  base class and then sets up the type constraints.
075     *  @param workspace The workspace for the new object.
076     *  @return A new actor.
077     *  @exception CloneNotSupportedException If a derived class contains
078     *   an attribute that cannot be cloned.
079     */
080    @Override
081    public Object clone(Workspace workspace) throws CloneNotSupportedException {
082        ArrayLength newObject = (ArrayLength) super.clone(workspace);
083
084        // Set the type constraints.
085        newObject.input.setTypeAtLeast(ArrayType.ARRAY_BOTTOM);
086
087        return newObject;
088    }
089
090    /** Consume at most one array from the input port and produce
091     *  its length on the output port.  If there is no token
092     *  on the input, then no output is produced.
093     *  @exception IllegalActionException If there is no director.
094     */
095    @Override
096    public void fire() throws IllegalActionException {
097        super.fire();
098        if (input.hasToken(0)) {
099            ArrayToken token = (ArrayToken) input.get(0);
100            output.broadcast(new IntToken(token.length()));
101        }
102    }
103}