Class Expression
- java.lang.Object
-
- All Implemented Interfaces:
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 send an expression for evaluation to a matlabEngine
. The expression is any valid matlab expression, e.g.:[out1, out2, ... ] = SomeMatlabFunctionOrExpression( in1, in2, ... );...
The expression may include references to the input port names, current time (time), and a count of the firing (iteration). This is similar to Expression. To refer to parameters in scope, use $name or ${name} within the expression.
The matlab engine is opened (started) during prefire() by the first matlab Expression actor. Subsequent open()s simply increment a use count.
At the start of fire(), clear variables;clear globals commands are sent to matlab to clear its workspace. This helps detect errors where the matlab expression refers to a matlab variable not initialized from the input ports of this actor instance.
After the evaluation of the matlab expression is complete, the fire() method iterates through names of output ports and converts matlab variables with corresponding names to Tokens that are sent to the corresponding output ports. Incorrect expressions are usually first detected at this point by not finding the expected variables. If an output port variable is not found in the matlab
Engine
, an exception is thrown. The exception description string contains the last stdout of the matlab engine that usually describes the error.The
get1x1asScalars
andgetIntegerMatrices
control data conversion (seeEngine
andEngine.ConversionParameters
).A Parameter named packageDirectories may be added to this actor to augment the search path of the matlab engine during the firing of this actor. The value of this parameter should evaluate to a StringToken, e.g.:
"path1, path2, ..."
containing a comma-separated list of paths to be prepended to the matlab engine search path before expression is evaluated. The list may contain paths relative to the directory in which ptolemy was started, or any directory listed in the current classpath (in that order, first match wins). See
UtilityFunctions.findFile(String)
. After evaluation, the previous search path is restored.A Parameter named _debugging may be used to turn on debug print statements to stdout from
Engine
and the ptmatlab JNI. An IntToken with a value of 1 turns on Engine debug statements, a value of 2 adds ptmatlab debug statements as well. A value of 0 or the absence of the _debugging parameter yields normal operation.For this actor to work, Matlab must be installed on your local machine. In addition, your environment must be set properly. The
$PTII/bin/vergil
script does this for you, below are instructions for users, such as Eclipse users, who are not using$PTII/bin/vergil
.Add the Matlab shared libraries to your the library path. In the examples below
$MATLAB
should be the location of your Matlab installation. For example, if /usr/bin/matlab is a link:bash-3.2$ which matlab /usr/bin/matlab bash-3.2$ ls -l /usr/bin/matlab lrwxr-xr-x 1 root wheel 42 Jan 15 20:57 /usr/bin/matlab -> /Applications/MATLAB_R2009b.app/bin/matlab bash-3.2$
Then $MATLAB would be /Applications/MATLAB_R2009b.app- 32 Bit Mac (10.5?, Edit ~/.MacOSX/environment.plist)
export DYLD_LIBRARY_PATH=$MATLAB/bin/maci
- 64 Bit Mac (10.6?, Edit ~/.MacOSX/environment.plist)
export DYLD_LIBRARY_PATH=$MATLAB/bin/maci64
- 32 Bit Linux (Edit
export LD_LIBRARY_PATH=$MATLAB/bin/gnlx86
- 64 Bit Linux
export LD_LIBRARY_PATH=$MATLAB/bin/glnxa64
- Windows (Start|My Computer|Properties|Advanced Environment Variables| Add the directory that contains matlab.exe to your path)
Be sure that the matlab binary is in your path
Notes about Matlab 10.8 and later
Note that under recent version of MacOS, the DYLD_LIBRARY_PATH is no longer used. The workaround is to be sure that matlab is in your path and to run ./configure.
For example:
bash-3.2$ bash-3.2$ which matlab /Applications/MATLAB_R2018a.app/bin/matlab
Then, running:
./configure --enable-verbose
shows:
checking for matlab... /Applications/MATLAB_R2018a.app/bin/matlab checking the value of the Matlab root directory ... '/Applications/MATLAB_R2018a.app' checking for gcc... checking for gcc... /usr/bin/gcc checking for malloc_size... yes checking the extension for Matlab .mex files... mexmaci64 checking which major type of OS we are running under... MacOSX using '/Applications/MATLAB_R2018a.app/bin/maci' for Matlab's engine libraries
Then run
cd $PTII ant cd $PTII/bin make cd $PTII/ptolemy/matlab make clean; make
Here's what libraries are used by libptmatlab.jnilib
bash-3.2$ otool -L libptmatlab.jnilib libptmatlab.jnilib: libptmatlab64.jnilib (compatibility version 0.0.0, current version 0.0.0)
- Since:
- Ptolemy II 2.0
- Version:
- $Id$
- Author:
- Zoltan Kemenczy and Sean Simmons, Research in Motion Limited
- Pt.AcceptedRating:
- Red (cxh)
- Pt.ProposedRating:
- Yellow (zkemenczy)
-
-
Nested Class Summary
-
Nested classes/interfaces inherited from class ptolemy.kernel.Entity
Entity.ContainedObjectsIterator
-
-
Field Summary
Fields Modifier and Type Field Description Parameter
clearEnvironment
If true, clear variables and globals before each execution.StringParameter
expression
The parameter that is evaluated to produce the output.Parameter
get1x1asScalars
If true (checked), 1x1 matrix results are converted to ScalarTokens instead of a 1x1 MatrixToken, default is true.Parameter
getIntegerMatrices
If true, all double-valued matrix results are checked to see if all elements represent integers, and if so, an IntMatrixToken is returned, default is false for performance reasons.TypedIOPort
output
The output port.-
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 Expression(CompositeEntity container, java.lang.String name)
Construct an actor with the given container and name.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description protected java.util.Set<Inequality>
_defaultTypeConstraints()
Default type constraints do not apply in this case, since the input type may be totally unrelated to the output type and cannot be inferred; return null.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()
Return true if all input ports have at least one token.void
preinitialize()
Open a matlab engine.void
wrapup()
Close matlab engine if it was open.-
Methods inherited from class ptolemy.actor.TypedAtomicActor
_containedTypeConstraints, _customTypeConstraints, _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, inputPortList, isFireFunctional, isStrict, iterate, newReceiver, outputPortList, pruneDependencies, recordFiring, removeActorFiringListener, removeDependency, removeInitializable, setContainer, stop, stopFire, terminate
-
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, stop, stopFire, terminate
-
Methods inherited from interface ptolemy.actor.Initializable
addInitializable, removeInitializable
-
Methods inherited from interface ptolemy.kernel.util.Nameable
description, getContainer, getDisplayName, getFullName, getName, getName, setName
-
-
-
-
Field Detail
-
output
public TypedIOPort output
The output port.
-
expression
public StringParameter expression
The parameter that is evaluated to produce the output. Typically, this parameter evaluates an expression involving the inputs. To refer to parameters in scope within the expression, use $name or ${name}, where "name" is the name of the parameter.
-
get1x1asScalars
public Parameter get1x1asScalars
If true (checked), 1x1 matrix results are converted to ScalarTokens instead of a 1x1 MatrixToken, default is true.
-
getIntegerMatrices
public Parameter getIntegerMatrices
If true, all double-valued matrix results are checked to see if all elements represent integers, and if so, an IntMatrixToken is returned, default is false for performance reasons.
-
clearEnvironment
public Parameter clearEnvironment
If true, clear variables and globals before each execution.
-
-
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
-
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 sets theiteration
public member to the parameters of the new actor.- 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)
-
preinitialize
public void preinitialize() throws IllegalActionException
Open a matlab engine.- Specified by:
preinitialize
in interfaceInitializable
- Overrides:
preinitialize
in classAtomicActor<TypedIOPort>
- Throws:
IllegalActionException
- If matlab engine not found.
-
initialize
public void initialize() throws IllegalActionException
Initialize the iteration count to 1.- Specified by:
initialize
in interfaceInitializable
- Overrides:
initialize
in classAtomicActor<TypedIOPort>
- Throws:
IllegalActionException
- If the parent class throws it.
-
prefire
public boolean prefire() throws IllegalActionException
Return true if all input ports have at least one token.- Specified by:
prefire
in interfaceExecutable
- Overrides:
prefire
in classAtomicActor<TypedIOPort>
- Returns:
- True if this actor is ready for firing, false otherwise.
- Throws:
IllegalActionException
- Not thrown in this base class.
-
fire
public void fire() throws IllegalActionException
Evaluate the expression and send its result to the output.- Specified by:
fire
in interfaceExecutable
- Overrides:
fire
in classAtomicActor<TypedIOPort>
- 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.
-
postfire
public boolean postfire() throws IllegalActionException
Increment the iteration count.- Specified by:
postfire
in interfaceExecutable
- Overrides:
postfire
in classAtomicActor<TypedIOPort>
- Returns:
- True if execution can continue into the next iteration.
- Throws:
IllegalActionException
- If the superclass throws it.
-
wrapup
public void wrapup() throws IllegalActionException
Close matlab engine if it was open.- Specified by:
wrapup
in interfaceInitializable
- Overrides:
wrapup
in classAtomicActor<TypedIOPort>
- Throws:
IllegalActionException
- Not thrown in this base class.
-
_defaultTypeConstraints
protected java.util.Set<Inequality> _defaultTypeConstraints()
Default type constraints do not apply in this case, since the input type may be totally unrelated to the output type and cannot be inferred; return null.- Overrides:
_defaultTypeConstraints
in classTypedAtomicActor
- Returns:
- null
-
-