001/* An SDF test actor.
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.domains.sdf.kernel.test;
029
030import ptolemy.actor.lib.Transformer;
031import ptolemy.data.IntToken;
032import ptolemy.data.expr.Parameter;
033import ptolemy.domains.sdf.kernel.SDFDirector;
034import ptolemy.kernel.CompositeEntity;
035import ptolemy.kernel.util.IllegalActionException;
036import ptolemy.kernel.util.NameDuplicationException;
037import ptolemy.kernel.util.Workspace;
038
039///////////////////////////////////////////////////////////////////
040//// SDFTestZeroRate1
041
042/**
043 A test actor for HDF/SDF. This actor contains parameters that make it
044 easy to set the rates of the input and output ports. This actor
045 simply discards whatever it reads in and outputs the contents of
046 the <i>value</i> parameter.
047
048 @author Brian K. Vogel
049 @version $Id$
050 @since Ptolemy II 2.0
051 @Pt.ProposedRating Red (vogel)
052 @Pt.AcceptedRating Red (vogel)
053 */
054public class SDFTestZeroRate1 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 SDFTestZeroRate1(CompositeEntity container, String name)
064            throws NameDuplicationException, IllegalActionException {
065        super(container, name);
066        value = new Parameter(this, "value", new IntToken(1));
067
068        input_rate1 = new Parameter(this, "input_rate1", new IntToken(1));
069        input_tokenConsumptionRate = new Parameter(input,
070                "tokenConsumptionRate");
071        input_tokenConsumptionRate.setExpression("input_rate1");
072
073        output_rate1 = new Parameter(this, "output_rate1", new IntToken(1));
074        output_tokenProductionRate = new Parameter(output,
075                "tokenProductionRate");
076        output_tokenProductionRate.setExpression("output_rate1");
077
078        // Set the type constraint.
079        output.setTypeAtLeast(value);
080    }
081
082    ///////////////////////////////////////////////////////////////////
083    ////                     ports and parameters                  ////
084
085    /** The value produced by this constant source.
086     *  By default, it contains an IntToken with value 1.  If the
087     *  type of this token is changed during the execution of a model,
088     *  then the director will be asked to redo type resolution.
089     */
090    public Parameter value;
091
092    public Parameter input_rate1;
093
094    public Parameter output_rate1;
095
096    public Parameter input_tokenConsumptionRate;
097
098    public Parameter output_tokenProductionRate;
099
100    ///////////////////////////////////////////////////////////////////
101    ////                         public methods                    ////
102
103    /** Clone the actor into the specified workspace. This calls the
104     *  base class and then sets the type constraints.
105     *  @param workspace The workspace for the new object.
106     *  @return A new actor.
107     *  @exception CloneNotSupportedException If a derived class has
108     *   an attribute that cannot be cloned.
109     */
110    @Override
111    public Object clone(Workspace workspace) throws CloneNotSupportedException {
112        SDFTestZeroRate1 newObject = (SDFTestZeroRate1) super.clone(workspace);
113
114        // Set the type constraint.
115        newObject.output.setTypeAtLeast(newObject.value);
116        return newObject;
117    }
118
119    /** Discard tokens received. Send the token in the value parameter.
120     *  @exception IllegalActionException If there is no director.
121     */
122    @Override
123    public void fire() throws IllegalActionException {
124        for (int i = 0; i < ((IntToken) input_rate1.getToken())
125                .intValue(); i++) {
126            input.get(0);
127        }
128
129        for (int i = 0; i < ((IntToken) output_rate1.getToken())
130                .intValue(); i++) {
131            output.send(0, value.getToken());
132        }
133    }
134
135    /**
136     * for debugging only...
137     *
138     *  @exception IllegalActionException If a derived class throws it.
139     */
140    @Override
141    public void initialize() throws IllegalActionException {
142        super.initialize();
143
144        // debug sdf schedules:
145        SDFDirector dir = (SDFDirector) getDirector();
146        /*SDFScheduler scheduler = (SDFScheduler)*/dir.getScheduler();
147
148        // For debugging the SDF scheduler...
149        //StreamListener sa = new StreamListener();
150        //scheduler.addDebugListener(sa);
151        //
152        // Get the SDF Director's scheduler.
153        //        Scheduler s = dir.getScheduler();
154        //Iterator allActors = s.getSchedule().actorIterator();
155        //while (allActors.hasNext()) {
156        //    Actor actor = (Actor)allActors.next();
157        //   String schedActName = ((Nameable)actor).getName();
158        //    System.out.println("Actor in scheduler: " + schedActName);
159        //}
160    }
161
162    ///////////////////////////////////////////////////////////////////
163    ////                         private variables                 ////
164}