001/* An actor that converts input tokens to specified units.
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.actor.lib.Transformer;
031import ptolemy.data.DoubleToken;
032import ptolemy.data.expr.Parameter;
033import ptolemy.data.type.BaseType;
034import ptolemy.kernel.CompositeEntity;
035import ptolemy.kernel.util.IllegalActionException;
036import ptolemy.kernel.util.NameDuplicationException;
037
038///////////////////////////////////////////////////////////////////
039//// InUnitsOf
040
041/**
042 <p>An actor that converts input tokens to specified units by dividing the
043 input by the value of the <i>units</i> parameter.  This actor is designed
044 to be used with a <i>unit system</i>, which must be included in the
045 model (note that some Ptolemy II applications do not include unit systems).
046 </p><p>
047 The units are specified by the <i>units</i> parameter, which contains a
048 DoubleToken with units. The input tokens and the token in the <i>unit</i>
049 parameter must have the same unit category. Otherwise, an exception
050 will be thrown in the fire() method. Unit categories include the ones
051 defined in the MoML file, such as length, time, mass, and the composite
052 ones formed through the base categories, such as length/time (speed),
053 and length * length (area). The output token is a DoubleToken without
054 units.</p>
055
056 @author Yuhong Xiong, Xiaojun Liu, Edward Lee
057 @version $Id$
058 @since Ptolemy II 2.0
059 @Pt.ProposedRating Red (yuhong)
060 @Pt.AcceptedRating Red (cxh)
061 */
062public class InUnitsOf extends Transformer {
063    /** Construct an actor with the given container and name.
064     *  In addition to invoking the base class constructors, construct
065     *  the <i>units</i> parameter. Initialize <i>units</i>
066     *  to DoubleToken with value 1.0.
067     *  @param container The container.
068     *  @param name The name of this actor.
069     *  @exception IllegalActionException If the actor cannot be contained
070     *   by the proposed container.
071     *  @exception NameDuplicationException If the container already has an
072     *   actor with this name.
073     */
074    public InUnitsOf(CompositeEntity container, String name)
075            throws NameDuplicationException, IllegalActionException {
076        super(container, name);
077        units = new Parameter(this, "units", new DoubleToken(1.0));
078
079        // set the type constraints.
080        input.setTypeEquals(BaseType.DOUBLE);
081        output.setTypeEquals(BaseType.DOUBLE);
082        units.setTypeEquals(BaseType.DOUBLE);
083    }
084
085    ///////////////////////////////////////////////////////////////////
086    ////                     ports and parameters                  ////
087
088    /** The units to which the input tokens will be converted.
089     *  The default value of this parameter is the double 1.0.
090     */
091    public Parameter units;
092
093    ///////////////////////////////////////////////////////////////////
094    ////                         public methods                    ////
095
096    /** Convert the input to the units specified by the <i>units</i>
097     *  parameter.  If there is no input, then produce no output.
098     *  @exception IllegalActionException If there is no director.
099     */
100    @Override
101    public void fire() throws IllegalActionException {
102        super.fire();
103        if (input.hasToken(0)) {
104            DoubleToken in = (DoubleToken) input.get(0);
105            DoubleToken out = (DoubleToken) in
106                    .inUnitsOf((DoubleToken) units.getToken());
107            output.send(0, out);
108        }
109    }
110
111    /** Return false if the input port has no token, otherwise
112     *  return what the superclass returns (presumably true).
113     *  @exception IllegalActionException If there is no director.
114     */
115    @Override
116    public boolean prefire() throws IllegalActionException {
117        if (!input.hasToken(0)) {
118            return false;
119        }
120
121        return super.prefire();
122    }
123}