001/* An actor that limits the input to a specified range.
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.ScalarToken;
031import ptolemy.data.expr.Parameter;
032import ptolemy.data.type.BaseType;
033import ptolemy.kernel.CompositeEntity;
034import ptolemy.kernel.util.IllegalActionException;
035import ptolemy.kernel.util.NameDuplicationException;
036import ptolemy.kernel.util.Workspace;
037
038///////////////////////////////////////////////////////////////////
039//// Limiter
040
041/**
042 Produce an output token on each firing with a value that is
043 equal to the input if the input lies between the <i>bottom</i> and
044 <i>top</i> parameters.  Otherwise, if the input is greater than <i>top</i>,
045 output <i>top</i>.  If the input is less than <i>bottom</i>, output
046 <i>bottom</i>.  This actor operates on scalar types only.
047
048 @author Edward A. Lee
049 @version $Id$
050 @since Ptolemy II 2.0
051 @Pt.ProposedRating Yellow (eal)
052 @Pt.AcceptedRating Red (yuhong)
053 */
054public class Limiter extends Transformer {
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 Limiter(CompositeEntity container, String name)
064            throws NameDuplicationException, IllegalActionException {
065        super(container, name);
066        bottom = new Parameter(this, "bottom");
067        bottom.setExpression("0.0");
068
069        top = new Parameter(this, "top");
070        top.setExpression("1.0");
071
072        input.setTypeAtMost(BaseType.SCALAR);
073        input.setTypeAtLeast(top);
074        input.setTypeAtLeast(bottom);
075
076        output.setTypeAtLeast(input);
077    }
078
079    ///////////////////////////////////////////////////////////////////
080    ////                     ports and parameters                  ////
081
082    /** The bottom of the limiting range.  This is a scalar with default
083     *  value 0.0.
084     */
085    public Parameter bottom;
086
087    /** The top of the limiting range.  This is a scalar with default
088     *  value 1.0.
089     */
090    public Parameter top;
091
092    ///////////////////////////////////////////////////////////////////
093    ////                         public methods                    ////
094
095    /** Override the base class to set type constraints.
096     *  @param workspace The workspace for the cloned object.
097     *  @exception CloneNotSupportedException If cloned ports cannot have
098     *   as their container the cloned entity (this should not occur), or
099     *   if one of the attributes cannot be cloned.
100     *  @return A new instance of Sleep.
101     */
102    @Override
103    public Object clone(Workspace workspace) throws CloneNotSupportedException {
104        Limiter newObject = (Limiter) super.clone(workspace);
105        newObject.input.setTypeAtMost(BaseType.SCALAR);
106        newObject.input.setTypeAtLeast(newObject.top);
107        newObject.input.setTypeAtLeast(newObject.bottom);
108        newObject.output.setTypeAtLeast(newObject.input);
109        return newObject;
110    }
111
112    /** Compute the output and send it to the output port. If there is
113     *  no input, then produce no output.
114     *  @exception IllegalActionException If there is no director.
115     */
116    @Override
117    public void fire() throws IllegalActionException {
118        super.fire();
119        if (input.hasToken(0)) {
120            ScalarToken in = (ScalarToken) input.get(0);
121            if (in.isLessThan((ScalarToken) bottom.getToken()).booleanValue()) {
122                output.send(0, bottom.getToken());
123            } else if (in.isGreaterThan((ScalarToken) top.getToken())
124                    .booleanValue()) {
125                output.send(0, top.getToken());
126            } else {
127                output.send(0, in);
128            }
129        }
130    }
131}