001/* An actor that converts a boolean token into any other data type.
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.conversions;
029
030import ptolemy.data.BooleanToken;
031import ptolemy.data.IntToken;
032import ptolemy.data.expr.Parameter;
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//// BooleanToAnything
041
042/**
043 <p>This actor converts a boolean input token into any data type.</p>
044 <p>A <i>true</i> at the input results in an output with value given
045 by the <i>trueValue</i> parameter.
046 A <i>false</i> at the input results in an output with value given
047 by the <i>falseValue</i> parameter.
048 </p>
049 @author Edward A. Lee
050 @version $Id$
051 @since Ptolemy II 2.0
052 @Pt.ProposedRating Green (eal)
053 @Pt.AcceptedRating Red (cxh)
054
055 @see ptolemy.data.BooleanToken
056 */
057public class BooleanToAnything extends Converter {
058    /** Construct an actor with the given container and name.
059     *  @param container The container.
060     *  @param name The name of this actor.
061     *  @exception IllegalActionException If the actor cannot be contained
062     *   by the proposed container.
063     *  @exception NameDuplicationException If the container already has an
064     *   actor with this name.
065     */
066    public BooleanToAnything(CompositeEntity container, String name)
067            throws NameDuplicationException, IllegalActionException {
068        super(container, name);
069
070        falseValue = new Parameter(this, "falseValue", new IntToken(0));
071        trueValue = new Parameter(this, "trueValue", new IntToken(1));
072
073        input.setTypeEquals(BaseType.BOOLEAN);
074        output.setTypeAtLeast(trueValue);
075        output.setTypeAtLeast(falseValue);
076    }
077
078    ///////////////////////////////////////////////////////////////////
079    ////                     ports and parameters                  ////
080
081    /** The value produced at the output when a <i>false</i> input is read. */
082    public Parameter falseValue;
083
084    /** The value produced at the output when a <i>true</i> input is read. */
085    public Parameter trueValue;
086
087    ///////////////////////////////////////////////////////////////////
088    ////                         public methods                    ////
089
090    /** Clone the actor into the specified workspace. This calls the
091     *  base class and then sets the value public variable in the new
092     *  object to equal the cloned parameter in that new object.
093     *  @param workspace The workspace for the new object.
094     *  @return A new actor.
095     *  @exception CloneNotSupportedException If a derived class contains
096     *   an attribute that cannot be cloned.
097     */
098    @Override
099    public Object clone(Workspace workspace) throws CloneNotSupportedException {
100        BooleanToAnything newObject = (BooleanToAnything) super.clone(
101                workspace);
102
103        // Set the type constraint.
104        newObject.output.setTypeAtLeast(newObject.trueValue);
105        newObject.output.setTypeAtLeast(newObject.falseValue);
106        return newObject;
107    }
108
109    /** Read exactly one token from the input and output the token
110     *  given by either the <i>falseValue</i> or <i>trueValue</i>
111     *  parameter.
112     *  @exception IllegalActionException If there is no director.
113     */
114    @Override
115    public void fire() throws IllegalActionException {
116        super.fire();
117        BooleanToken inputToken = (BooleanToken) input.get(0);
118
119        if (inputToken.booleanValue()) {
120            output.send(0, trueValue.getToken());
121        } else {
122            output.send(0, falseValue.getToken());
123        }
124    }
125
126    /** Return false if the input port has no token, otherwise return
127     *  what the superclass returns (presumably true).
128     *  @exception IllegalActionException If there is no director.
129     */
130    @Override
131    public boolean prefire() throws IllegalActionException {
132        if (!input.hasToken(0)) {
133            return false;
134        }
135
136        return super.prefire();
137    }
138}