001/* Base class for simple source actors. 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 java.util.HashSet; 031import java.util.Set; 032 033import ptolemy.actor.TypedAtomicActor; 034import ptolemy.actor.TypedIOPort; 035import ptolemy.data.BooleanToken; 036import ptolemy.data.expr.SingletonParameter; 037import ptolemy.data.type.BaseType; 038import ptolemy.data.type.TypeConstant; 039import ptolemy.graph.Inequality; 040import ptolemy.kernel.CompositeEntity; 041import ptolemy.kernel.util.IllegalActionException; 042import ptolemy.kernel.util.NameDuplicationException; 043 044/////////////////////////////////////////////////////////////////// 045//// Source 046 047/** 048 Base for simple data sources. This provides an output 049 port and a trigger input port, both exposed as public variables. 050 The trigger port is a multiport with undeclared type, meaning that 051 you can supply it with any data type. The trigger port can also be 052 left unconnected. The purpose of the trigger input is to 053 (optionally) supply events that cause the actor to fire. If the 054 port is connected to something, then this actor will check it 055 for a token and return false from prefire() if there is no token. 056 each channel of the trigger input, if any, and then discards the 057 token. 058 <p> 059 Some derived classes may attach additional significance to an input 060 on the trigger port. For example, they might fix the type and attach 061 some significance to the value. Note that it is not recommend to 062 use getWidth() on the port to determine whether the port is connected, 063 since the width may be greater than zero even if there 064 is no actual source of data. This can occur, for example, if a trigger port 065 is connected to the inside of a port of an opaque composite actor, and 066 there is nothing connected to the outside of that port. It is not 067 recommended to make the behavior of an actor dependent on a global 068 property such as whether there is ultimately a source of data.</p> 069 <p> 070 Any type of data on is accepted on the trigger port, therefore no 071 type is declared. Instead, the type resolution algorithm finds 072 the least fixed point. If backward type inference is enabled, and 073 no type has been declared for the trigger, it is constrained to be 074 equal to <code>BaseType.GENERAL</code>. This will result in upstream 075 ports resolving to the most general type rather than the most specific. 076 </p> 077 078 079 @author Edward A. Lee 080 @version $Id$ 081 @since Ptolemy II 0.3 082 @Pt.ProposedRating Green (eal) 083 @Pt.AcceptedRating Green (bilung) 084 */ 085public abstract class Source extends TypedAtomicActor { 086 /** Construct an actor with the given container and name. 087 * The output and trigger ports are also constructed. 088 * @param container The container. 089 * @param name The name of this actor. 090 * @exception IllegalActionException If the entity cannot be contained 091 * by the proposed container. 092 * @exception NameDuplicationException If the container already has an 093 * actor with this name. 094 */ 095 public Source(CompositeEntity container, String name) 096 throws NameDuplicationException, IllegalActionException { 097 super(container, name); 098 output = new TypedIOPort(this, "output", false, true); 099 trigger = new TypedIOPort(this, "trigger", true, false); 100 101 // NOTE: It used to be that trigger was set to GENERAL, but this 102 // isn't really what we want. What we want is an undeclared type 103 // that can resolve to anything. EAL 12/31/02 104 // trigger.setTypeEquals(BaseType.GENERAL); 105 trigger.setMultiport(true); 106 107 // Parameter to get Vergil to label the trigger port. 108 new SingletonParameter(trigger, "_showName") 109 .setToken(BooleanToken.TRUE); 110 } 111 112 /////////////////////////////////////////////////////////////////// 113 //// ports and parameters //// 114 115 /** The output port. The type of this port is unspecified. 116 * Derived classes may set it. 117 */ 118 public TypedIOPort output = null; 119 120 /** The trigger port. The type of this port is undeclared, meaning 121 * that it will resolve to any data type. 122 */ 123 public TypedIOPort trigger = null; 124 125 /////////////////////////////////////////////////////////////////// 126 //// public methods //// 127 128 /** Read at most one input token from each channel of the trigger 129 * input and discard it. If the trigger input is not connected 130 * or has no actual sources (it might be connected to other 131 * inputs, for example, or to an unconnected input port at 132 * a higher level in the hierarchy) then this method does 133 * nothing. Derived classes should be 134 * sure to call super.fire(), or to consume the trigger input 135 * tokens themselves, so that they aren't left unconsumed. 136 * @exception IllegalActionException Not thrown in this base class. 137 */ 138 @Override 139 public void fire() throws IllegalActionException { 140 super.fire(); 141 142 // Note that the following derived classes do not call 143 // super.fire(): WallClockTime. 144 // if significant changes are made to this method, please review 145 // the above classes. 146 147 // NOTE: It might seem that using trigger.numberOfSources() is 148 // correct here, but it is not. It is possible for channels 149 // to be connected, for example, to other output ports or 150 // even back to this same trigger port, in which case higher 151 // numbered channels will not have their inputs read. 152 for (int i = 0; i < trigger.getWidth(); i++) { 153 // FIXME: Should this be: 154 // if (trigger.isKnown(i) && trigger.hasToken(i)) { 155 // DiscreteClock.fire() was checking if the trigger was known 156 // before DiscreteClock.fire() was calling super.fire() 157 if (trigger.hasToken(i)) { 158 trigger.get(i); 159 _triggered = true; 160 } 161 } 162 } 163 164 /** If the trigger input is connected and it has no input or an unknown 165 * state, then return false. Otherwise, return true. 166 * @return True, unless the trigger input is connected 167 * and has no input. 168 * @exception IllegalActionException If checking the trigger for 169 * a token throws it or if the super class throws it. 170 */ 171 @Override 172 public boolean prefire() throws IllegalActionException { 173 if (trigger.numberOfSources() > 0) { 174 // Have to consume all trigger inputs. 175 boolean returnValue = false; 176 for (int i = 0; i < trigger.getWidth(); i++) { 177 if (trigger.isKnown(i) && trigger.hasToken(i)) { 178 returnValue = true; 179 } 180 } 181 if (_debugging) { 182 _debug("Called prefire(), which returns " + returnValue); 183 } 184 return returnValue; 185 } 186 187 return super.prefire(); 188 } 189 190 /////////////////////////////////////////////////////////////////// 191 //// protected methods //// 192 193 /** Set the input port greater than or equal to 194 * <code>BaseType.GENERAL</code> in case backward type inference is 195 * enabled and the input port has no type declared. 196 * 197 * @return A set of inequalities. 198 */ 199 @Override 200 protected Set<Inequality> _customTypeConstraints() { 201 HashSet<Inequality> result = new HashSet<Inequality>(); 202 if (isBackwardTypeInferenceEnabled() 203 && trigger.getTypeTerm().isSettable()) { 204 result.add(new Inequality(new TypeConstant(BaseType.GENERAL), 205 trigger.getTypeTerm())); 206 } 207 return result; 208 } 209 210 /////////////////////////////////////////////////////////////////// 211 //// protected variables //// 212 213 /** Indicator of whether trigger inputs have arrived 214 * since the last output. 215 */ 216 protected transient boolean _triggered; 217 218}