Class SetVariable
- java.lang.Object
-
- All Implemented Interfaces:
java.lang.Cloneable
,Actor
,Executable
,FiringsRecordable
,Initializable
,TypedActor
,ExplicitChangeContext
,Changeable
,ChangeListener
,Debuggable
,DebugListener
,Derivable
,Instantiable
,ModelErrorHandler
,MoMLExportable
,Moveable
,Nameable
- Direct Known Subclasses:
SequencedSharedMemoryActor
public class SetVariable extends TypedAtomicActor implements ChangeListener, ExplicitChangeContext
Set the value of a variable. If there is a variable or parameter in scope with a name matching variableName, then that variable is the one to be set. If there is no such variable, then one will be created in the container of this actor. A variable is in scope if it is contained by the container, the container's container, or any container above in the hierarchy. NOTE: We recommend always creating a parameter with a name matching variableName, because then the model is explicit about which variable will be set. It also makes it easier to monitor the variable updates.
The update to the variable may occur at two different times, depending on the value of the delayed parameter. If delayed is true, then the change to the value of the variable is implemented in a change request, and consequently will not take hold until the end of the current top-level iteration. This helps ensure that users of value of the variable will see changes to the value deterministically (independent of the schedule of execution of the actors), assuming there is only a single instance of SetVariable writing to the variable. If delayed is false, then the change to the value of the variable is performed immediately in the fire() method. This allows more frequent reconfiguration. However, this can result in nondeterminism if the variable values are observed by any other actor in the system. If you are trying to communicate with another actor without wiring, use the Publisher and Subscriber actors instead.
If delayed is false, then the output port produces the same token provided at the input port when the actor fires, after the specified variable has been set. This can be used, even with delayed set to false, to ensure determinacy by triggering downstream actions only after the variable has been set.
If delayed is true, then the output port produces the current value of the referenced variable. If the referenced variable does not exist on the first firing, or is not an instance of Variable, then no output is produced on the first firing.
The variable can be any attribute that implements the Settable interface, which includes Parameter. If it is in addition an instance of Variable or Parameter, then the input token is used directly to set the value, and the type of the variable is constrained to be the same as the type of the input. Otherwise, then input token is converted to a string and the setExpression() method on the variable is used to set the value.
For efficiency, the variable update does not automatically trigger a repaint in Vergil. If the variable value is being used to create an animation in Vergil, then you should include in the model an instance of RepaintController, which can be found under Utilities in the library.
- Since:
- Ptolemy II 4.0
- Version:
- $Id$
- Author:
- Edward A. Lee, Steve Neuendorffer, Contributor: Blanc, Bert Rodiers
- See Also:
Publisher
,Subscriber
- Pt.AcceptedRating:
- Red (cxh)
- Pt.ProposedRating:
- Red (yuhong)
-
-
Nested Class Summary
-
Nested classes/interfaces inherited from class ptolemy.kernel.Entity
Entity.ContainedObjectsIterator
-
-
Field Summary
Fields Modifier and Type Field Description Parameter
delayed
Parameter that determines when reconfiguration occurs.TypedIOPort
input
The input port.TypedIOPort
output
The output port.StringAttribute
variableName
The name of the variable in the container to set.-
Fields inherited from class ptolemy.actor.TypedAtomicActor
_typesValid
-
Fields inherited from class ptolemy.actor.AtomicActor
_actorFiringListeners, _initializables, _notifyingActorFiring, _stopRequested
-
Fields inherited from class ptolemy.kernel.util.NamedObj
_changeListeners, _changeLock, _changeRequests, _debugging, _debugListeners, _deferChangeRequests, _elementName, _isPersistent, _verbose, _workspace, ATTRIBUTES, CLASSNAME, COMPLETE, CONTENTS, DEEP, FULLNAME, LINKS
-
Fields inherited from interface ptolemy.actor.Executable
COMPLETED, NOT_READY, STOP_ITERATING
-
-
Constructor Summary
Constructors Constructor Description SetVariable(CompositeEntity container, java.lang.String name)
Construct an actor with the given container and name.SetVariable(Workspace workspace)
Construct an actor in the specified workspace with an empty string as a name.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description protected java.util.Set<Inequality>
_customTypeConstraints()
Return a constraint requiring that if there is a specified variable to modify, the type of that variable is less than or equal to the type of the output port.void
changeExecuted(ChangeRequest change)
Do nothing.void
changeFailed(ChangeRequest change, java.lang.Exception exception)
React to the fact that a change failed by setting a flag that causes an exception to be thrown in next call to prefire() or wrapup().java.lang.Object
clone(Workspace workspace)
Clone the actor into the specified workspace.void
fire()
Read at most one token from the input port and issue a change request to update variables as indicated by the input.Entity
getContext()
Return the change context being made explicit.Attribute
getModifiedVariable()
Return the (presumably Settable) attribute modified by this actor.java.util.List
getModifiedVariables()
Return a list of variables that this entity modifies.boolean
postfire()
Read at most one token from the input port and issue a change request to update variables as indicated by the input.void
preinitialize()
If there is no variable with the specified name, then create one.-
Methods inherited from class ptolemy.actor.TypedAtomicActor
_containedTypeConstraints, _defaultTypeConstraints, _fireAt, _fireAt, attributeTypeChanged, clone, isBackwardTypeInferenceEnabled, newPort, typeConstraintList, typeConstraints
-
Methods inherited from class ptolemy.actor.AtomicActor
_actorFiring, _actorFiring, _declareDelayDependency, addActorFiringListener, addInitializable, connectionsChanged, createReceivers, declareDelayDependency, getCausalityInterface, getDirector, getExecutiveDirector, getManager, initialize, inputPortList, isFireFunctional, isStrict, iterate, newReceiver, outputPortList, prefire, pruneDependencies, recordFiring, removeActorFiringListener, removeDependency, removeInitializable, setContainer, stop, stopFire, terminate, wrapup
-
Methods inherited from class ptolemy.kernel.ComponentEntity
_adjustDeferrals, _checkContainer, _getContainedObject, _propagateExistence, getContainer, instantiate, isAtomic, isOpaque, moveDown, moveToFirst, moveToIndex, moveToLast, moveUp, propagateExistence, setName
-
Methods inherited from class ptolemy.kernel.Entity
_addPort, _description, _exportMoMLContents, _removePort, _validateSettables, connectedPortList, connectedPorts, containedObjectsIterator, getAttribute, getPort, getPorts, linkedRelationList, linkedRelations, portList, removeAllPorts, setClassDefinition, uniqueName
-
Methods inherited from class ptolemy.kernel.InstantiableNamedObj
_setParent, exportMoML, getChildren, getElementName, getParent, getPrototypeList, isClassDefinition, isWithinClassDefinition
-
Methods inherited from class ptolemy.kernel.util.NamedObj
_addAttribute, _adjustOverride, _attachText, _cloneFixAttributeFields, _containedDecorators, _copyChangeRequestList, _debug, _debug, _debug, _debug, _debug, _executeChangeRequests, _getIndentPrefix, _isMoMLSuppressed, _markContentsDerived, _notifyHierarchyListenersAfterChange, _notifyHierarchyListenersBeforeChange, _propagateValue, _removeAttribute, _splitName, _stripNumericSuffix, addChangeListener, addDebugListener, addHierarchyListener, attributeChanged, attributeDeleted, attributeList, attributeList, decorators, deepContains, depthInHierarchy, description, description, event, executeChangeRequests, exportMoML, exportMoML, exportMoML, exportMoML, exportMoMLPlain, getAttribute, getAttributes, getChangeListeners, getClassName, getDecoratorAttribute, getDecoratorAttributes, getDerivedLevel, getDerivedList, getDisplayName, getFullName, getModelErrorHandler, getName, getName, getSource, handleModelError, isDeferringChangeRequests, isOverridden, isPersistent, lazyContainedObjectsIterator, message, notifyOfNameChange, propagateValue, propagateValues, removeAttribute, removeChangeListener, removeDebugListener, removeHierarchyListener, requestChange, setClassName, setDeferringChangeRequests, setDerivedLevel, setDisplayName, setModelErrorHandler, setPersistent, setSource, sortContainedObjects, toplevel, toString, validateSettables, workspace
-
Methods inherited from class java.lang.Object
equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
-
Methods inherited from interface ptolemy.actor.Actor
createReceivers, getCausalityInterface, getDirector, getExecutiveDirector, getManager, inputPortList, newReceiver, outputPortList
-
Methods inherited from interface ptolemy.kernel.util.Derivable
getDerivedLevel, getDerivedList, propagateValue
-
Methods inherited from interface ptolemy.actor.Executable
isFireFunctional, isStrict, iterate, prefire, stop, stopFire, terminate
-
Methods inherited from interface ptolemy.actor.Initializable
addInitializable, initialize, removeInitializable, wrapup
-
Methods inherited from interface ptolemy.kernel.util.Nameable
description, getContainer, getDisplayName, getFullName, getName, getName, setName
-
-
-
-
Field Detail
-
delayed
public Parameter delayed
Parameter that determines when reconfiguration occurs.
-
input
public TypedIOPort input
The input port.
-
output
public TypedIOPort output
The output port.
-
variableName
public StringAttribute variableName
The name of the variable in the container to set.
-
-
Constructor Detail
-
SetVariable
public SetVariable(Workspace workspace)
Construct an actor in the specified workspace with an empty string as a name. You can then change the name with setName(). If the workspace argument is null, then use the default workspace. The object is added to the workspace directory. Increment the version number of the workspace.- Parameters:
workspace
- The workspace that will list the entity.
-
SetVariable
public SetVariable(CompositeEntity container, java.lang.String name) throws NameDuplicationException, IllegalActionException
Construct an actor with the given container and name.- Parameters:
container
- The container.name
- The name of this actor.- Throws:
IllegalActionException
- If this actor cannot be contained by the proposed container.NameDuplicationException
- If the container already has an actor with this name.
-
-
Method Detail
-
changeExecuted
public void changeExecuted(ChangeRequest change)
Do nothing.- Specified by:
changeExecuted
in interfaceChangeListener
- Parameters:
change
- The change that executed.
-
changeFailed
public void changeFailed(ChangeRequest change, java.lang.Exception exception)
React to the fact that a change failed by setting a flag that causes an exception to be thrown in next call to prefire() or wrapup().- Specified by:
changeFailed
in interfaceChangeListener
- Parameters:
change
- The change request.exception
- The exception that resulted.
-
clone
public java.lang.Object clone(Workspace workspace) throws java.lang.CloneNotSupportedException
Clone the actor into the specified workspace.- Overrides:
clone
in classTypedAtomicActor
- Parameters:
workspace
- The workspace for the new object.- Returns:
- A new actor.
- Throws:
java.lang.CloneNotSupportedException
- If a derived class contains an attribute that cannot be cloned.- See Also:
NamedObj.exportMoML(Writer, int, String)
,NamedObj.setDeferringChangeRequests(boolean)
-
fire
public void fire() throws IllegalActionException
Read at most one token from the input port and issue a change request to update variables as indicated by the input.- Specified by:
fire
in interfaceExecutable
- Overrides:
fire
in classAtomicActor<TypedIOPort>
- Throws:
IllegalActionException
- If thrown reading the input.
-
getContext
public Entity getContext()
Return the change context being made explicit. In this case, the change context returned is this actor.- Specified by:
getContext
in interfaceExplicitChangeContext
- Returns:
- The change context being made explicit
-
getModifiedVariable
public Attribute getModifiedVariable() throws IllegalActionException
Return the (presumably Settable) attribute modified by this actor. This is the attribute in the container of this actor with the name given by the variableName attribute. If no such attribute is found, then this method creates a new variable in the actor's container with the correct name. This method gets write access on the workspace.- Returns:
- The attribute modified by this actor.
- Throws:
IllegalActionException
- If the variable cannot be found.
-
getModifiedVariables
public java.util.List getModifiedVariables() throws IllegalActionException
Return a list of variables that this entity modifies. The variables are assumed to have a change context of the given entity.- Specified by:
getModifiedVariables
in interfaceExplicitChangeContext
- Returns:
- A list of variables.
- Throws:
IllegalActionException
- If the list of modified variables cannot be returned.
-
postfire
public boolean postfire() throws IllegalActionException
Read at most one token from the input port and issue a change request to update variables as indicated by the input.- Specified by:
postfire
in interfaceExecutable
- Overrides:
postfire
in classAtomicActor<TypedIOPort>
- Returns:
- True if execution can continue into the next iteration.
- Throws:
IllegalActionException
- If thrown reading the input.
-
preinitialize
public void preinitialize() throws IllegalActionException
If there is no variable with the specified name, then create one. This is done in preinitialize() so that we can set up a type constraint that ensures that the type of the variable is at least that of the input port.- Specified by:
preinitialize
in interfaceInitializable
- Overrides:
preinitialize
in classAtomicActor<TypedIOPort>
- Throws:
IllegalActionException
- If the superclass throws it, or if there is no container.
-
_customTypeConstraints
protected java.util.Set<Inequality> _customTypeConstraints()
Return a constraint requiring that if there is a specified variable to modify, the type of that variable is less than or equal to the type of the output port.- Overrides:
_customTypeConstraints
in classTypedAtomicActor
- Returns:
- A set of type constraints.
-
-