001/* An actor that counts true inputs.
002
003 Copyright (c) 2003-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.domains.sdf.lib;
029
030import ptolemy.data.BooleanToken;
031import ptolemy.data.IntToken;
032import ptolemy.data.Token;
033import ptolemy.data.expr.Parameter;
034import ptolemy.data.type.BaseType;
035import ptolemy.kernel.CompositeEntity;
036import ptolemy.kernel.util.IllegalActionException;
037import ptolemy.kernel.util.NameDuplicationException;
038import ptolemy.kernel.util.Workspace;
039
040///////////////////////////////////////////////////////////////////
041//// CountTrues
042
043/**
044 Read the given number of input booleans and output the number that are true.
045
046 @author Steve Neuendorffer
047 @version $Id$
048 @since Ptolemy II 4.0
049 @Pt.ProposedRating Red (eal)
050 @Pt.AcceptedRating Red (yuhong)
051 */
052public class CountTrues extends SDFTransformer {
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 CountTrues(CompositeEntity container, String name)
062            throws NameDuplicationException, IllegalActionException {
063        super(container, name);
064
065        input.setTypeEquals(BaseType.BOOLEAN);
066        output.setTypeEquals(BaseType.INT);
067
068        blockSize = new Parameter(this, "blockSize", new IntToken(1));
069        blockSize.setTypeEquals(BaseType.INT);
070
071        input_tokenConsumptionRate.setExpression("blockSize");
072    }
073
074    ///////////////////////////////////////////////////////////////////
075    ////                     ports and parameters                  ////
076
077    /** The number of input tokens to read before outputting a count.
078     *  The input is of type integer and has a default value of 1.
079     */
080    public Parameter blockSize;
081
082    ///////////////////////////////////////////////////////////////////
083    ////                         public methods                    ////
084
085    /** Clone the actor into the specified workspace. This calls the
086     *  base class and then sets the type constraints.
087     *  @param workspace The workspace for the new object.
088     *  @return A new actor.
089     *  @exception CloneNotSupportedException If a derived class has
090     *   an attribute that cannot be cloned.
091     */
092    @Override
093    public Object clone(Workspace workspace) throws CloneNotSupportedException {
094        CountTrues newObject = (CountTrues) super.clone(workspace);
095        return newObject;
096    }
097
098    /** Read at most one token from each input channel and broadcast the one
099     *  with the largest value to the <i>maximumValue</i>output.
100     *  In addition, broadcast its channel number to the <i>channelNumber</i>
101     *  output.  If there is no input, then produce no output.
102     *  @exception IllegalActionException If there is no director.
103     */
104    @Override
105    public void fire() throws IllegalActionException {
106        super.fire();
107        int count = ((IntToken) blockSize.getToken()).intValue();
108        Token[] inputBlock = input.get(0, count);
109        int trueCount = 0;
110
111        for (int i = 0; i < count; i += 1) {
112            if (((BooleanToken) inputBlock[i]).booleanValue()) {
113                trueCount++;
114            }
115        }
116
117        output.send(0, new IntToken(trueCount));
118    }
119}