001/* A timed actor that outputs the current 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.data.BooleanToken; 031import ptolemy.data.DoubleToken; 032import ptolemy.data.expr.Parameter; 033import ptolemy.data.type.BaseType; 034import ptolemy.kernel.CompositeEntity; 035import ptolemy.kernel.util.IllegalActionException; 036import ptolemy.kernel.util.NameDuplicationException; 037 038/////////////////////////////////////////////////////////////////// 039//// CurrentTime 040 041/** 042 Produce an output token on each firing with a value that is 043 the current time. The output is of type double. 044 By default, this uses the local notion of time, which may 045 lag behind the global notion of time if this actor is used 046 inside a modal model. Under the Continuous director, it is 047 essential to use local time, and not global time 048 because of the speculative executions during numerical 049 ODE solving. 050 051 @author Jie Liu and Edward A. Lee 052 @version $Id$ 053 @since Ptolemy II 0.3 054 @Pt.ProposedRating Yellow (eal) 055 @Pt.AcceptedRating Yellow (cxh) 056 */ 057public class CurrentTime extends TimedSource { 058 /** Construct an actor with the given container and name. 059 * 060 * @param container The container. 061 * @param name The name of this actor. 062 * @exception IllegalActionException If the actor cannot be contained 063 * by the proposed container. 064 * @exception NameDuplicationException If the container already has an 065 * actor with this name. 066 */ 067 public CurrentTime(CompositeEntity container, String name) 068 throws NameDuplicationException, IllegalActionException { 069 super(container, name); 070 071 // set the type constraints. 072 output.setTypeEquals(BaseType.DOUBLE); 073 074 useLocalTime = new Parameter(this, "useLocalTime"); 075 useLocalTime.setTypeEquals(BaseType.BOOLEAN); 076 useLocalTime.setExpression("true"); 077 078 // Override the clock to make it look a bit 079 // different from the DiscreteClock and PoissonClock. 080 _attachText("_iconDescription", "<svg>\n" + "<rect x=\"-20\" y=\"-20\" " 081 + "width=\"40\" height=\"40\" " + "style=\"fill:lightGrey\"/>\n" 082 + "<circle cx=\"0\" cy=\"0\" r=\"17\"" 083 + "style=\"fill:black\"/>\n" 084 + "<line x1=\"0\" y1=\"-15\" x2=\"0\" y2=\"-13\" style=\"stroke:white\"/>\n" 085 + "<line x1=\"0\" y1=\"14\" x2=\"0\" y2=\"16\" style=\"stroke:white\"/>\n" 086 + "<line x1=\"-15\" y1=\"0\" x2=\"-13\" y2=\"0\" style=\"stroke:white\"/>\n" 087 + "<line x1=\"14\" y1=\"0\" x2=\"16\" y2=\"0\" style=\"stroke:white\"/>\n" 088 + "<line x1=\"0\" y1=\"-8\" x2=\"0\" y2=\"0\" style=\"stroke:white\"/>\n" 089 + "<line x1=\"0\" y1=\"0\" x2=\"11.26\" y2=\"-6.5\" style=\"stroke:white\"/>\n" 090 + "</svg>\n"); 091 } 092 093 /////////////////////////////////////////////////////////////////// 094 //// parameters //// 095 096 /** If true, use the model time reported by the input port, 097 * which is normally the model time of the local director. 098 * If false (the default), use the model time reported by 099 * the top-level director. Local time may differ 100 * from global time inside modal models and certain domains 101 * that manipulate time. 102 */ 103 public Parameter useLocalTime; 104 105 /////////////////////////////////////////////////////////////////// 106 //// public methods //// 107 108 /** Send the current time to the output. If there are trigger inputs, 109 * then the current time is the minimum of the times of each of the 110 * input tokens (currently, these can be different only in the DT 111 * domain). Otherwise, current time is that reported by the director. 112 * @exception IllegalActionException If send() throws it. 113 */ 114 @Override 115 public void fire() throws IllegalActionException { 116 // For domain polymorphism getCurrentTime(channel_number) has 117 // to be called before get(channel_number). Currently, 118 // the only domain in which the two versions of getCurrentTime 119 // are different is in DT... getCurrentTime() on the director 120 // returns the "start of iteration" time, whereas getCurrentTime() 121 // on the channel returns the time of the current sample. 122 double currentTimeValue = Double.POSITIVE_INFINITY; 123 124 if (trigger.isOutsideConnected()) { 125 // Trigger port is connected. 126 // If there is a token in a channel of the trigger port, 127 // output the current time (that is associated with the token). 128 for (int i = 0; i < trigger.getWidth(); i++) { 129 if (trigger.hasToken(i)) { 130 boolean localTime = ((BooleanToken) useLocalTime.getToken()) 131 .booleanValue(); 132 if (localTime) { 133 currentTimeValue = Math.min(currentTimeValue, 134 trigger.getModelTime(i).getDoubleValue()); 135 } else { 136 currentTimeValue = Math.min(currentTimeValue, 137 getDirector().getGlobalTime().getDoubleValue()); 138 } 139 140 // Do not consume the token... It will be consumed 141 // in the superclass fire(). 142 // trigger.get(i); 143 output.send(0, new DoubleToken(currentTimeValue)); 144 } 145 } 146 } else { 147 // Trigger port is not connected. 148 boolean localTime = ((BooleanToken) useLocalTime.getToken()) 149 .booleanValue(); 150 if (localTime) { 151 currentTimeValue = getDirector().getModelTime() 152 .getDoubleValue(); 153 } else { 154 currentTimeValue = getDirector().getGlobalTime() 155 .getDoubleValue(); 156 } 157 output.send(0, new DoubleToken(currentTimeValue)); 158 } 159 160 super.fire(); 161 } 162}