001/* Output true if the input is present, false otherwise.
002
003 Copyright (c) 1997-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.logic;
029
030import ptolemy.actor.lib.Transformer;
031import ptolemy.data.BooleanToken;
032import ptolemy.data.type.BaseType;
033import ptolemy.kernel.CompositeEntity;
034import ptolemy.kernel.util.IllegalActionException;
035import ptolemy.kernel.util.NameDuplicationException;
036
037///////////////////////////////////////////////////////////////////
038//// TrueGate
039
040/**
041 On each firing, output true if the input is present and true.
042 Otherwise, output nothing.
043 The type of the input and output ports is boolean.
044 The width of the input is expected to match the width of the output.
045 Note that the utility of this actor varies by domain.  In PN, for
046 example, the input is always present (by definition). In SDF, it is
047 expected to be always present, but may not be.  In DE, the actor is
048 only triggered if one of the input channels has data.  The actor is
049 probably most useful in synchronous domains like SR and Giotto.
050
051 @author Edward A. Lee
052 @version $Id$
053 @since Ptolemy II 8.0
054 @Pt.ProposedRating Yellow (eal)
055 @Pt.AcceptedRating Red (eal)
056 */
057public class TrueGate extends Transformer {
058    /** Construct an actor in the specified container with the specified
059     *  name.
060     *  @param container The container.
061     *  @param name The name of this actor within the container.
062     *  @exception IllegalActionException If the actor cannot be contained
063     *   by the proposed container.
064     *  @exception NameDuplicationException If the name coincides with
065     *   an actor already in the container.
066     */
067    public TrueGate(CompositeEntity container, String name)
068            throws IllegalActionException, NameDuplicationException {
069        super(container, name);
070        input.setTypeEquals(BaseType.BOOLEAN);
071        input.setMultiport(true);
072        output.setTypeEquals(BaseType.BOOLEAN);
073        output.setMultiport(true);
074
075        input.setWidthEquals(output, true);
076
077        _attachText("_iconDescription",
078                "<svg>\n" + "<rect x=\"-15\" y=\"-15\" "
079                        + "width=\"40\" height=\"30\" "
080                        + "style=\"fill:white\"/>\n" + "<text x=\"-10\" y=\"4\""
081                        + "style=\"font-size:14\">true?</text>\n" + "</svg>\n");
082    }
083
084    ///////////////////////////////////////////////////////////////////
085    ////                         public methods                    ////
086
087    /** Consume at most one token from each input channel, and output
088     *  a boolean on the corresponding output channel (if there is one).
089     *  The value of the boolean is true if the input is present and
090     *  false otherwise.
091     */
092    @Override
093    public void fire() throws IllegalActionException {
094        super.fire();
095        int outputWidth = output.getWidth();
096
097        for (int i = 0; i < input.getWidth(); i++) {
098            if (input.hasToken(i)) {
099                // Consume the token.
100                boolean inputValue = ((BooleanToken) input.get(i))
101                        .booleanValue();
102
103                if (i < outputWidth && inputValue) {
104                    output.send(i, BooleanToken.TRUE);
105                }
106            }
107        }
108    }
109}