001/* An actor that delays the input for a certain amount of real time. 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.actor.TypedIOPort; 031import ptolemy.data.LongToken; 032import ptolemy.data.Token; 033import ptolemy.data.expr.Parameter; 034import ptolemy.data.type.BaseType; 035import ptolemy.kernel.CompositeEntity; 036import ptolemy.kernel.util.IllegalActionException; 037import ptolemy.kernel.util.NameDuplicationException; 038 039/////////////////////////////////////////////////////////////////// 040//// Sleep 041 042/** 043 An actor that calls Thread.sleep() on the current thread the first 044 time fire() is called. The sleep delays the inputs for a certain 045 amount of real time, specified by the <i>sleepTime</i> input. 046 047 <p>Note that one way to slow down the execution of a model while running 048 inside vergil is to turn on animation. 049 050 <p>If the width of the output port is less than that of the input port, 051 the tokens in the extra channels are lost. 052 053 @author Yang Zhao, based on Sleep by Jie Liu, Christopher Hylands 054 @version $Id$ 055 @since Ptolemy II 1.0 056 @Pt.ProposedRating Yellow (cxh) 057 @Pt.AcceptedRating Yellow (cxh) 058 */ 059public class VariableSleep extends Transformer { 060 /** Construct an actor with the given container and name. 061 * @param container The container. 062 * @param name The name of this actor. 063 * @exception IllegalActionException If the actor cannot be contained 064 * by the proposed container. 065 * @exception NameDuplicationException If the container already has an 066 * actor with this name. 067 */ 068 public VariableSleep(CompositeEntity container, String name) 069 throws NameDuplicationException, IllegalActionException { 070 super(container, name); 071 defaultSleepTime = new Parameter(this, "defaultSleepTime", 072 new LongToken(0)); 073 defaultSleepTime.setTypeEquals(BaseType.LONG); 074 075 // Data type polymorphic, multiports. 076 input.setMultiport(true); 077 output.setMultiport(true); 078 sleepTime = new TypedIOPort(this, "sleepTime", true, false); 079 sleepTime.setTypeEquals(BaseType.LONG); 080 } 081 082 /////////////////////////////////////////////////////////////////// 083 //// ports and parameters //// 084 085 /** The sleepTime amount, in milliseconds 086 * This parameter must contain a LongToken. 087 * The default value of this parameter is 0, meaning 088 * that this actor will not sleep the current thread at all. 089 */ 090 public Parameter defaultSleepTime; 091 092 /** An input port receives the value of sleep time. 093 */ 094 public TypedIOPort sleepTime; 095 096 /////////////////////////////////////////////////////////////////// 097 //// public methods //// 098 099 /** Call Thread.sleep() the first time fire is called and then 100 * transfer tokens from inputs to outputs, one token from each 101 * channel. If fire() is called twice in a row without an 102 * intervening call to either postfire() or prefire(), then no 103 * output is produced. 104 * <p>If the width of the output port is less than 105 * that of the input port, the tokens in the extra channels 106 * are lost. 107 * @exception IllegalActionException Not thrown in this base class */ 108 @Override 109 public void fire() throws IllegalActionException { 110 super.fire(); 111 if (!_wasSleepCalledInFireYet) { 112 try { 113 if (sleepTime.hasToken(0)) { 114 _sleepTime = ((LongToken) sleepTime.get(0)).longValue(); 115 } else { 116 _sleepTime = ((LongToken) defaultSleepTime.getToken()) 117 .longValue(); 118 } 119 120 if (_debugging) { 121 _debug(getName() + ": Wait for " + _sleepTime 122 + " milliseconds."); 123 } 124 125 Thread.sleep(_sleepTime); 126 } catch (InterruptedException e) { 127 // Ignore... 128 } 129 130 // Pull these out of the loop so we do not call them 131 // more than once. 132 int inputWidth = input.getWidth(); 133 int outputWidth = output.getWidth(); 134 135 for (int i = 0; i < inputWidth; i++) { 136 if (input.hasToken(i)) { 137 Token inToken = input.get(i); 138 139 if (i < outputWidth) { 140 output.send(i, inToken); 141 } 142 } 143 } 144 } 145 } 146 147 /** Reset the flag that fire() checks so that fire() only sleeps once. 148 * @exception IllegalActionException If the parent class throws it. 149 * @return Whatever the superclass returns (probably true). 150 */ 151 @Override 152 public boolean postfire() throws IllegalActionException { 153 _wasSleepCalledInFireYet = false; 154 return super.postfire(); 155 } 156 157 /** Reset the flag that fire() checks so that fire() only sleeps once. 158 * @exception IllegalActionException If the parent class throws it. 159 * @return Whatever the superclass returns (probably true). 160 */ 161 @Override 162 public boolean prefire() throws IllegalActionException { 163 _wasSleepCalledInFireYet = false; 164 return super.prefire(); 165 } 166 167 /////////////////////////////////////////////////////////////////// 168 //// private variables //// 169 // True if sleep was called in fire(). Sleep should only 170 // be called once in fire(). 171 private boolean _wasSleepCalledInFireYet = false; 172 173 private long _sleepTime; 174}