001/* An actor that converts a string to a boolean
002
003 Copyright (c) 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.StringToken;
032import ptolemy.data.type.BaseType;
033import ptolemy.kernel.CompositeEntity;
034import ptolemy.kernel.util.IllegalActionException;
035import ptolemy.kernel.util.NameDuplicationException;
036
037///////////////////////////////////////////////////////////////////
038/// StringToBoolean
039
040/**
041 Convert a string to a boolean.
042
043 <p>This actor is a simpler version of
044 {@link ptolemy.actor.lib.conversions.ExpressionToToken}, which can perform
045 the same conversion, but since ExpressionToToken just evaluates an
046 expression, it is much more powerful.</p>
047
048 @author Christopher Brooks, Long Le
049 @version $Id$
050 @since Ptolemy II 10.0
051 @Pt.ProposedRating Red (longle1)
052 @Pt.AcceptedRating Red (longle1)
053 */
054public class StringToBoolean extends Converter {
055    /** Construct an actor with the given container and name.
056     *  @param container The container.
057     *  @param name The name of this actor.
058     *  @exception IllegalActionException If the actor cannot be contained
059     *   by the proposed container.
060     *  @exception NameDuplicationException If the container already has an
061     *   actor with this name.
062     */
063    public StringToBoolean(CompositeEntity container, String name)
064            throws NameDuplicationException, IllegalActionException {
065        super(container, name);
066
067        input.setTypeEquals(BaseType.STRING);
068
069        output.setTypeEquals(BaseType.BOOLEAN);
070    }
071
072    ///////////////////////////////////////////////////////////////////
073    ////                         public methods                    ////
074
075    /** Consume one token, get the string value and convert it to a
076     * BooleanToken.
077     *
078     *  @exception IllegalActionException If thrown while getting
079     *  or sending a token.
080     */
081    @Override
082    public void fire() throws IllegalActionException {
083        super.fire();
084        String inputValue = ((StringToken) input.get(0)).stringValue();
085
086        Boolean value = Boolean.parseBoolean(inputValue);
087
088        output.send(0, new BooleanToken(value));
089    }
090
091    /** Return false if the input port has no token, otherwise return
092     *  what the superclass returns (presumably true).
093     *  @exception IllegalActionException If there is no director.
094     */
095    @Override
096    public boolean prefire() throws IllegalActionException {
097        if (!input.hasToken(0)) {
098            return false;
099        }
100
101        return super.prefire();
102    }
103}