ptolemy.actor.lib
Class Expression

java.lang.Object
  extended by ptolemy.kernel.util.NamedObj
      extended by ptolemy.kernel.InstantiableNamedObj
          extended by ptolemy.kernel.Entity
              extended by ptolemy.kernel.ComponentEntity
                  extended by ptolemy.actor.AtomicActor
                      extended by ptolemy.actor.TypedAtomicActor
                          extended by ptolemy.actor.lib.Expression
All Implemented Interfaces:
java.io.Serializable, java.lang.Cloneable, Actor, Executable, FiringsRecordable, Initializable, TypedActor, Changeable, Debuggable, DebugListener, Derivable, Instantiable, ModelErrorHandler, MoMLExportable, Moveable, Nameable

public class Expression
extends TypedAtomicActor

On each firing, evaluate an expression that may include references to the inputs, current time, and a count of the firing. The ports are referenced by the identifiers that have the same name as the port. To use this class, instantiate it, then add ports (instances of TypedIOPort). In vergil, you can add ports by right clicking on the icon and selecting "Configure Ports". In MoML you can add ports by just including ports of class TypedIOPort, set to be inputs, as in the following example:

 <entity name="exp" class="ptolemy.actor.lib.Expression">
 <port name="in" class="ptolemy.actor.TypedIOPort">
 <property name="input"/>
 </port>
 </entity>
 

This actor is type-polymorphic. The types of the inputs can be arbitrary and the types of the outputs are inferred from the expression based on the types inferred for the inputs.

The expression parameter specifies an expression that can refer to the inputs by name. By default, the expression is empty, and attempting to execute the actor without setting it triggers an exception. This actor can be used instead of many of the arithmetic actors, such as AddSubtract, MultiplyDivide, and TrigFunction. However, those actors will be usually be more efficient, and sometimes more convenient to use.

The expression language understood by this actor is the same as that used to set any parameter value, with the exception that the expressions evaluated by this actor can refer to the values of inputs, and to the current time by the identifier name "time", and to the current iteration count by the identifier named "iteration."

This actor requires its all of its inputs to be present. If inputs are not all present, then an exception will be thrown.

NOTE: There are a number of limitations in the current implementation. Primarily, multiports are not supported.

Since:
Ptolemy II 0.2
Version:
$Id: Expression.java 57040 2010-01-27 20:52:32Z cxh $
Author:
Xiaojun Liu, Edward A. Lee, Steve Neuendorffer
See Also:
Serialized Form
Accepted Rating:
Green (neuendor)
Proposed Rating:
Green (neuendor)

Nested Class Summary
private  class Expression.OutputTypeFunction
           
private  class Expression.VariableScope
           
 
Nested classes/interfaces inherited from class ptolemy.kernel.Entity
Entity.ContainedObjectsIterator
 
Field Summary
private  int _iterationCount
           
private  ASTPtRootNode _parseTree
           
private  ParseTreeEvaluator _parseTreeEvaluator
           
private  Expression.VariableScope _scope
           
private  java.util.Map _tokenMap
           
 StringAttribute expression
          The expression that is evaluated to produce the output.
 TypedIOPort output
          The output port.
 
Fields inherited from class ptolemy.actor.AtomicActor
_actorFiringListeners, _initializables, _notifyingActorFiring, _stopRequested
 
Fields inherited from class ptolemy.kernel.util.NamedObj
_changeListeners, _changeLock, _changeRequests, _debugging, _debugListeners, _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
Expression(CompositeEntity container, java.lang.String name)
          Construct an actor with the given container and name.
 
Method Summary
private  void _setOutputTypeConstraint()
           
 void attributeChanged(Attribute attribute)
          React to a change in the value of an attribute.
 java.lang.Object clone(Workspace workspace)
          Clone the actor into the specified workspace.
 void fire()
          Evaluate the expression and send its result to the output.
 void initialize()
          Initialize the iteration count to 1.
 boolean postfire()
          Increment the iteration count.
 boolean prefire()
          Prefire this actor.
 void preinitialize()
          Preinitialize this actor.
 
Methods inherited from class ptolemy.actor.TypedAtomicActor
_addPort, _fireAt, _fireAt, attributeTypeChanged, clone, newPort, typeConstraintList, typeConstraints
 
Methods inherited from class ptolemy.actor.AtomicActor
_actorFiring, _actorFiring, addActorFiringListener, addInitializable, connectionsChanged, createReceivers, declareDelayDependency, getCausalityInterface, getDirector, getExecutiveDirector, getManager, inputPortList, isFireFunctional, isStrict, iterate, newReceiver, outputPortList, 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
_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, _debug, _debug, _debug, _debug, _debug, _getIndentPrefix, _isMoMLSuppressed, _markContentsDerived, _propagateValue, _recordDecoratedAttributes, _removeAttribute, _splitName, _stripNumericSuffix, addChangeListener, addDebugListener, attributeList, attributeList, 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, propagateValue, propagateValues, removeChangeListener, removeDebugListener, 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.actor.Executable
isFireFunctional, isStrict, iterate, stop, stopFire, terminate
 
Methods inherited from interface ptolemy.actor.Initializable
addInitializable, removeInitializable, wrapup
 
Methods inherited from interface ptolemy.kernel.util.Nameable
description, getContainer, getDisplayName, getFullName, getName, getName, setName
 
Methods inherited from interface ptolemy.kernel.util.Derivable
getDerivedLevel, getDerivedList, propagateValue
 

Field Detail

output

public TypedIOPort output
The output port.


expression

public StringAttribute expression
The expression that is evaluated to produce the output.


_iterationCount

private int _iterationCount

_parseTree

private ASTPtRootNode _parseTree

_parseTreeEvaluator

private ParseTreeEvaluator _parseTreeEvaluator

_scope

private Expression.VariableScope _scope

_tokenMap

private java.util.Map _tokenMap
Constructor Detail

Expression

public Expression(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 the actor cannot be contained by the proposed container.
NameDuplicationException - If the container already has an actor with this name.
Method Detail

attributeChanged

public void attributeChanged(Attribute attribute)
                      throws IllegalActionException
React to a change in the value of an attribute.

Overrides:
attributeChanged in class NamedObj
Parameters:
attribute - The attribute whose type changed.
Throws:
IllegalActionException - Not thrown in this base class.

clone

public java.lang.Object clone(Workspace workspace)
                       throws java.lang.CloneNotSupportedException
Clone the actor into the specified workspace. This calls the base class and then creates new ports and parameters.

Overrides:
clone in class AtomicActor
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
Evaluate the expression and send its result to the output.

Specified by:
fire in interface Executable
Overrides:
fire in class AtomicActor
Throws:
IllegalActionException - If the evaluation of the expression triggers it, or the evaluation yields a null result, or the evaluation yields an incompatible type, or if there is no director, or if a connected input has no tokens.

initialize

public void initialize()
                throws IllegalActionException
Initialize the iteration count to 1.

Specified by:
initialize in interface Initializable
Overrides:
initialize in class AtomicActor
Throws:
IllegalActionException - If the parent class throws it.

postfire

public boolean postfire()
                 throws IllegalActionException
Increment the iteration count.

Specified by:
postfire in interface Executable
Overrides:
postfire in class AtomicActor
Returns:
True if execution can continue into the next iteration.
Throws:
IllegalActionException - If the superclass throws it.

prefire

public boolean prefire()
                throws IllegalActionException
Prefire this actor. Return false if an input port has no data, otherwise return true.

Specified by:
prefire in interface Executable
Overrides:
prefire in class AtomicActor
Returns:
True if this actor is ready for firing, false otherwise.
Throws:
IllegalActionException - If the superclass throws it.

preinitialize

public void preinitialize()
                   throws IllegalActionException
Preinitialize this actor.

Specified by:
preinitialize in interface Initializable
Overrides:
preinitialize in class AtomicActor
Throws:
IllegalActionException - Not thrown in this base class.

_setOutputTypeConstraint

private void _setOutputTypeConstraint()