001/* An actor that outputs the maximum value that it has received since the start
002of execution.
003
004 Copyright (c) 2008-2014 The Regents of the University of California.
005 All rights reserved.
006 Permission is hereby granted, without written agreement and without
007 license or royalty fees, to use, copy, modify, and distribute this
008 software and its documentation for any purpose, provided that the above
009 copyright notice and the following two paragraphs appear in all copies
010 of this software.
011
012 IN NO EVENT SHALL THE UNIVERSITY OF CALIFORNIA BE LIABLE TO ANY PARTY
013 FOR DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES
014 ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF
015 THE UNIVERSITY OF CALIFORNIA HAS BEEN ADVISED OF THE POSSIBILITY OF
016 SUCH DAMAGE.
017
018 THE UNIVERSITY OF CALIFORNIA SPECIFICALLY DISCLAIMS ANY WARRANTIES,
019 INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
020 MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE
021 PROVIDED HEREUNDER IS ON AN "AS IS" BASIS, AND THE UNIVERSITY OF
022 CALIFORNIA HAS NO OBLIGATION TO PROVIDE MAINTENANCE, SUPPORT, UPDATES,
023 ENHANCEMENTS, OR MODIFICATIONS.
024
025 PT_COPYRIGHT_VERSION_2
026 COPYRIGHTENDKEY
027
028 */
029package ptolemy.actor.lib;
030
031import ptolemy.data.ScalarToken;
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 On each firing, this actor consumes exactly one scalar token at its input port.
040 The value of the token is compared to the maximum value maintained since the
041 start of the execution. The greater of the two is output to the output port in
042 the same firing, and the maximum value is set with that greater value in
043 postfire().
044
045 @author Thomas Huining Feng
046 @version $Id$
047 @since Ptolemy II 8.0
048 @Pt.ProposedRating Yellow (tfeng)
049 @Pt.AcceptedRating Red (tfeng)
050 @deprecated Use RunningMaximum.
051 */
052@Deprecated
053public class MovingMaximum extends Transformer {
054
055    /** Construct an actor with the specified container and name.
056     *
057     *  @param container The composite actor to contain this one.
058     *  @param name The name of this actor.
059     *  @exception IllegalActionException If the entity cannot be contained
060     *   by the proposed container.
061     *  @exception NameDuplicationException If the container already has an
062     *   actor with this name.
063     */
064    public MovingMaximum(CompositeEntity container, String name)
065            throws NameDuplicationException, IllegalActionException {
066        super(container, name);
067
068        input.setTypeAtMost(BaseType.SCALAR);
069        output.setTypeAtLeast(input);
070    }
071
072    /** Clone this actor into the specified workspace. The new actor is
073     *  <i>not</i> added to the directory of that workspace (you must do this
074     *  yourself if you want it there).
075     *  The result is a new actor with the same ports as the original, but
076     *  no connections and no container.  A container must be set before
077     *  much can be done with this actor.
078     *
079     *  @param workspace The workspace for the cloned object.
080     *  @exception CloneNotSupportedException If cloned ports cannot have
081     *   as their container the cloned entity (this should not occur), or
082     *   if one of the attributes cannot be cloned.
083     *  @return A new ComponentEntity.
084     */
085    @Override
086    public Object clone(Workspace workspace) throws CloneNotSupportedException {
087        MovingMaximum newObject = (MovingMaximum) super.clone(workspace);
088        newObject.input.setTypeAtMost(BaseType.SCALAR);
089        newObject.output.setTypeAtLeast(newObject.input);
090        return newObject;
091    }
092
093    /** Consume a token at the input port, and produce the greater of that value
094     *  and the maintained maximum value to the output port.
095     *
096     *  @exception IllegalActionException If getting token from input or
097     *  sending token to output throws it.
098     */
099    @Override
100    public void fire() throws IllegalActionException {
101        super.fire();
102
103        _value = (ScalarToken) input.get(0);
104        if (_maximum == null || _value.isGreaterThan(_maximum).booleanValue()) {
105            output.broadcast(_value);
106        } else {
107            output.broadcast(_maximum);
108        }
109    }
110
111    /** Initialize the maintained maximum value to be null so it will be set
112     *  with the first input at the input port.
113     *
114     *  @exception IllegalActionException If the initialize() method of the
115     *  superclass throws it.
116     */
117    @Override
118    public void initialize() throws IllegalActionException {
119        super.initialize();
120
121        _maximum = null;
122    }
123
124    /** Commit the maximum value observed since the start of execution to the
125     *  maximum field to be compared with later inputs.
126     *
127     *  @exception IllegalActionException If the postfire() method of the
128     *  superclass throws it.
129     */
130    @Override
131    public boolean postfire() throws IllegalActionException {
132        boolean result = super.postfire();
133
134        if (_maximum == null || _value.isGreaterThan(_maximum).booleanValue()) {
135            _maximum = _value;
136        }
137
138        return result;
139    }
140
141    /** Return true if the prefire() method of the superclass returns true, and
142     *  there is at least one token at the input port.
143     *
144     *  @exception IllegalActionException If the prefire() method of the
145     *  superclass throws it.
146     */
147    @Override
148    public boolean prefire() throws IllegalActionException {
149        return super.prefire() && input.hasToken(0);
150    }
151
152    // The maximum value observed so far.
153    private ScalarToken _maximum;
154
155    // The value observed in the current firing.
156    private ScalarToken _value;
157}