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