001/* A base class for SDF actors that transform an input stream into an
002 output stream.
003
004 Copyright (c) 1998-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.domains.sdf.lib;
030
031import ptolemy.actor.TypedAtomicActor;
032import ptolemy.actor.TypedIOPort;
033import ptolemy.actor.lib.SequenceActor;
034import ptolemy.data.IntToken;
035import ptolemy.data.Token;
036import ptolemy.data.expr.Parameter;
037import ptolemy.data.type.BaseType;
038import ptolemy.kernel.CompositeEntity;
039import ptolemy.kernel.util.IllegalActionException;
040import ptolemy.kernel.util.NameDuplicationException;
041import ptolemy.kernel.util.Settable;
042
043///////////////////////////////////////////////////////////////////
044//// SDFTransformer
045
046/**
047 This is an abstract base class for actors that transform an input
048 stream into an output stream.  It provides an input and an output
049 port.
050
051 @author Edward A. Lee, Steve Neuendorffer
052 @version $Id$
053 @since Ptolemy II 1.0
054 @Pt.ProposedRating Yellow (eal)
055 @Pt.AcceptedRating Yellow (neuendor)
056 */
057public class SDFTransformer extends TypedAtomicActor implements SequenceActor {
058    /** Construct an actor with the given container and name.
059     *  @param container The container.
060     *  @param name The name of this actor.
061     *  @exception IllegalActionException If the actor cannot be contained
062     *   by the proposed container.
063     *  @exception NameDuplicationException If the container already has an
064     *   actor with this name.
065     */
066    public SDFTransformer(CompositeEntity container, String name)
067            throws NameDuplicationException, IllegalActionException {
068        super(container, name);
069
070        input = new TypedIOPort(this, "input", true, false);
071        output = new TypedIOPort(this, "output", false, true);
072
073        input_tokenConsumptionRate = new Parameter(input,
074                "tokenConsumptionRate");
075        input_tokenConsumptionRate.setExpression("1");
076        input_tokenConsumptionRate.setVisibility(Settable.NOT_EDITABLE);
077        input_tokenConsumptionRate.setTypeEquals(BaseType.INT);
078        input_tokenConsumptionRate.setPersistent(false);
079
080        output_tokenProductionRate = new Parameter(output,
081                "tokenProductionRate");
082        output_tokenProductionRate.setExpression("1");
083        output_tokenProductionRate.setVisibility(Settable.NOT_EDITABLE);
084        output_tokenProductionRate.setTypeEquals(BaseType.INT);
085        output_tokenProductionRate.setPersistent(false);
086
087        output_tokenInitProduction = new Parameter(output,
088                "tokenInitProduction");
089        output_tokenInitProduction.setExpression("0");
090        output_tokenInitProduction.setVisibility(Settable.NOT_EDITABLE);
091        output_tokenInitProduction.setTypeEquals(BaseType.INT);
092        output_tokenInitProduction.setPersistent(false);
093    }
094
095    ///////////////////////////////////////////////////////////////////
096    ////                         public methods                    ////
097
098    /** Return true if the number of available tokens on the <i>input</i>
099     *  port is at least the declared consumption rate for the port.
100     *  Otherwise return false.
101     *  @exception IllegalActionException If it is thrown accessing the port.
102     *  @return True if there are enough tokens.
103     */
104    @Override
105    public boolean prefire() throws IllegalActionException {
106        Token rateToken = input_tokenConsumptionRate.getToken();
107        int required = ((IntToken) rateToken).intValue();
108
109        // Derived classes may convert the input port to a multiport.
110        for (int i = 0; i < input.getWidth(); i++) {
111            if (!input.hasToken(i, required)) {
112                if (_debugging) {
113                    _debug("Called prefire(), "
114                            + " input tokenConsumptionRate = " + required
115                            + ", input.hasToken(" + i + ", " + required
116                            + ") is false, prefire() returning false");
117                }
118
119                return false;
120            }
121        }
122
123        return super.prefire();
124    }
125
126    ///////////////////////////////////////////////////////////////////
127    ////                     ports and parameters                  ////
128
129    /** The input port.  This base class imposes no type constraints except
130     *  that the type of the input cannot be greater than the type of the
131     *  output.
132     */
133    public TypedIOPort input;
134
135    /** The rate parameter for the input port.
136     */
137    public Parameter input_tokenConsumptionRate;
138
139    /** The output port. By default, the type of this output is constrained
140     *  to be at least that of the input.
141     */
142    public TypedIOPort output;
143
144    /** The rate parameter for the output port.
145     */
146    public Parameter output_tokenProductionRate;
147
148    /** The rate parameter for the output port that declares the
149     *  initial production.
150     */
151    public Parameter output_tokenInitProduction;
152}