001/* An actor that outputs a scaled version of the input.
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;
029
030import ptolemy.data.BooleanToken;
031import ptolemy.data.Token;
032import ptolemy.data.expr.Parameter;
033import ptolemy.kernel.CompositeEntity;
034import ptolemy.kernel.util.IllegalActionException;
035import ptolemy.kernel.util.NameDuplicationException;
036import ptolemy.kernel.util.Workspace;
037
038///////////////////////////////////////////////////////////////////
039//// Scale
040
041/**
042 Produce an output token on each firing with a value that is
043 equal to a scaled version of the input.  The actor is polymorphic
044 in that it can support any token type that supports multiplication
045 by the <i>factor</i> parameter.  The output
046 type is constrained to be at least as general as both the input and the
047 <i>factor</i> parameter.
048 For data types where multiplication is not commutative (such
049 as matrices), whether the factor is multiplied on the left is controlled
050 by the <i>scaleOnLeft</i> parameter. Setting the parameter to true means
051 that the factor is  multiplied on the left, and the input
052 on the right. Otherwise, the factor is multiplied on the right.
053
054 @author Edward A. Lee, Steve Neuendorffer
055 @version $Id$
056 @since Ptolemy II 0.3
057 @Pt.ProposedRating Yellow (eal)
058 @Pt.AcceptedRating Yellow (yuhong)
059 */
060public class Scale extends Transformer {
061    /** Construct an actor with the given container and name.
062     *  @param container The container.
063     *  @param name The name of this actor.
064     *  @exception IllegalActionException If the actor cannot be contained
065     *   by the proposed container.
066     *  @exception NameDuplicationException If the container already has an
067     *   actor with this name.
068     */
069    public Scale(CompositeEntity container, String name)
070            throws NameDuplicationException, IllegalActionException {
071        super(container, name);
072        factor = new Parameter(this, "factor");
073        factor.setExpression("1");
074        scaleOnLeft = new Parameter(this, "scaleOnLeft");
075        scaleOnLeft.setExpression("true");
076
077        // set the type constraints.
078        output.setTypeAtLeast(input);
079        output.setTypeAtLeast(factor);
080
081        // icon
082        _attachText("_iconDescription",
083                "<svg>\n" + "<polygon points=\"-30,-20 30,-4 30,4 -30,20\" "
084                        + "style=\"fill:white\"/>\n" + "</svg>\n");
085    }
086
087    ///////////////////////////////////////////////////////////////////
088    ////                     ports and parameters                  ////
089
090    /** The factor.
091     *  This parameter can contain any scalar token that supports
092     *  multiplication.  The default value of this parameter is the
093     *  IntToken 1.
094     */
095    public Parameter factor;
096
097    /** Multiply on the left.
098     *  This parameter controls whether the scale factor is multiplied
099     *  on the left. The default value is a boolean token of value true.
100     *  Setting is to false will multiply the factor on the right.
101     */
102    public Parameter scaleOnLeft;
103
104    ///////////////////////////////////////////////////////////////////
105    ////                         public methods                    ////
106
107    /** Clone the actor into the specified workspace. This calls the
108     *  base class and then sets the type constraints.
109     *  @param workspace The workspace for the new object.
110     *  @return A new actor.
111     *  @exception CloneNotSupportedException If a derived class has
112     *   an attribute that cannot be cloned.
113     */
114    @Override
115    public Object clone(Workspace workspace) throws CloneNotSupportedException {
116        Scale newObject = (Scale) super.clone(workspace);
117        newObject.output.setTypeAtLeast(newObject.input);
118        newObject.output.setTypeAtLeast(newObject.factor);
119        return newObject;
120    }
121
122    /** Compute the product of the input and the <i>factor</i>.
123     *  If there is no input, then produce no output.
124     *  @exception IllegalActionException If there is no director.
125     */
126    @Override
127    public void fire() throws IllegalActionException {
128        super.fire();
129        if (input.hasToken(0)) {
130            Token in = input.get(0);
131            Token factorToken = factor.getToken();
132            Token result;
133
134            if (((BooleanToken) scaleOnLeft.getToken()).booleanValue()) {
135                // Scale on the left.
136                result = factorToken.multiply(in);
137            } else {
138                // Scale on the right.
139                result = in.multiply(factorToken);
140            }
141            output.send(0, result);
142        }
143    }
144}