001/* A clock source where the period is given as an input. 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.DoubleToken; 032import ptolemy.data.Token; 033import ptolemy.data.type.BaseType; 034import ptolemy.kernel.CompositeEntity; 035import ptolemy.kernel.util.IllegalActionException; 036import ptolemy.kernel.util.NameDuplicationException; 037 038/////////////////////////////////////////////////////////////////// 039//// VariableClock 040 041/** 042 This actor is identical to Clock except that it has an additional 043 input port, <i>periodControl</i>. If this port has a token when the actor 044 fires, then the value read from that port is used to set the parameter 045 value. The initial value of the <i>period</i> parameter is used before 046 any input is observed on <i>periodControl</i>. 047 <p> 048 This actor can be fairly tricky to use with multiple values and 049 offsets because of the constraint that all offsets must be less 050 than the period. Thus, the default values and offsets are different 051 from those of the base class. The output value, by default, is 052 just the constant integer 1, and the offset is 0.0. The default 053 value of <i>period</i> is changed to 1.0. This gives 054 a very simply behavior, where the output is always the same, but 055 the time between outputs is controlled by the <i>periodControl</i> 056 input. 057 058 @author Edward A. Lee 059 @version $Id$ 060 @since Ptolemy II 0.4 061 @Pt.ProposedRating Yellow (eal) 062 @Pt.AcceptedRating Yellow (yuhong) 063 @deprecated Use Clock instead. 064 */ 065@Deprecated 066public class VariableClock extends Clock { 067 /** Construct an actor with the specified container and name. 068 * @param container The container. 069 * @param name The name of this actor. 070 * @exception IllegalActionException If the entity cannot be contained 071 * by the proposed container. 072 * @exception NameDuplicationException If the container already has an 073 * actor with this name. 074 */ 075 public VariableClock(CompositeEntity container, String name) 076 throws NameDuplicationException, IllegalActionException { 077 super(container, name); 078 079 periodControl = new TypedIOPort(this, "periodControl", true, false); 080 periodControl.setTypeEquals(BaseType.DOUBLE); 081 082 // Change default values from the base class. 083 offsets.setExpression("{0.0}"); 084 values.setExpression("{1}"); 085 period.setToken(new DoubleToken(1.0)); 086 } 087 088 /////////////////////////////////////////////////////////////////// 089 //// ports and parameters //// 090 091 /** The port that controls the value of the <i>period</i> parameter. 092 */ 093 public TypedIOPort periodControl; 094 095 /////////////////////////////////////////////////////////////////// 096 //// public methods //// 097 098 /** If there is an input on the <i>periodControl</i> port, read it 099 * and set the value of the <i>period</i> parameter. 100 * Then call the base class fire() method. 101 * @exception IllegalActionException If the input is not positive, 102 * or if the base class throws it. 103 */ 104 @Override 105 public void fire() throws IllegalActionException { 106 // FIXME 107 //double time = getDirector().getModelTime().getDoubleValue(); 108 if (periodControl.isOutsideConnected() && periodControl.hasToken(0)) { 109 Token in = periodControl.get(0); 110 period.setToken(in); 111 } 112 113 super.fire(); 114 } 115}