public class Groovy extends ScriptEngineActor
The following example defines an actor in Groovy that computes factorials:
1. import ptolemy.data.IntToken 2. 3. // Groovy actor to compute factorials. 4. 5. public class Actor { 6. def fire() { 7. int val = input.get(0).intValue() 8. if(val < 0) { 9. actor.error("Input must be non-negative."); 10. return 11. } 12. int total = factorial(val) 13. output.broadcast(new IntToken(total)) 14. } 15. 16. def factorial = { n -> n == 0 ? 1 : n * factorial(n - 1) } 17.}
Line 1 imports the Java class used by the actor. Lines 5-17 define the Actor object; the actor object must be named "Actor" in Groovy scripts. Lines 6-14 define the fire() method of the actor, which is called each time this actor executes in the workflow. Line 7 reads an integer from the input port called "input". A port or parameter called "foo" can be accessed in the Groovy script by using the same name. Line 12 calls the closure defined on line 16 that recursively computes the factorial of the integer read from the port. Line 13 writes the result to the output port called "output".
Entity.ContainedObjectsIterator
_ACTOR_INSTANCE_NAME, _actorObject, _editorFactory, _engine, _evaluatedScriptObject, _invokeMethodWithReflection, _manager, actorClassName, language, script
_typesValid
_actorFiringListeners, _initializables, _notifyingActorFiring, _stopRequested
_changeListeners, _changeLock, _changeRequests, _debugging, _debugListeners, _deferChangeRequests, _elementName, _isPersistent, _verbose, _workspace, ATTRIBUTES, CLASSNAME, COMPLETE, CONTENTS, DEEP, FULLNAME, LINKS
COMPLETED, NOT_READY, STOP_ITERATING
Constructor and Description |
---|
Groovy(CompositeEntity container,
java.lang.String name)
Construct a new Groovy with the given container and name.
|
Groovy(Workspace workspace)
Construct a new Groovy for a specified workspace.
|
Modifier and Type | Method and Description |
---|---|
protected void |
_putObjectToActorInstance(java.lang.String name,
java.lang.Object object)
Put the given object to the actor instance in the script.
|
_createActorInstance, _evaluateScript, _registerEngineFactory, attributeChanged, clone, debug, error, fire, initialize, postfire, prefire, preinitialize, stop, stopFire, terminate, wrapup
_containedTypeConstraints, _customTypeConstraints, _defaultTypeConstraints, _fireAt, _fireAt, attributeTypeChanged, clone, isBackwardTypeInferenceEnabled, newPort, typeConstraintList, typeConstraints
_actorFiring, _actorFiring, _declareDelayDependency, addActorFiringListener, addInitializable, connectionsChanged, createReceivers, declareDelayDependency, getCausalityInterface, getDirector, getExecutiveDirector, getManager, inputPortList, isFireFunctional, isStrict, iterate, newReceiver, outputPortList, pruneDependencies, recordFiring, removeActorFiringListener, removeDependency, removeInitializable, setContainer
_adjustDeferrals, _checkContainer, _getContainedObject, _propagateExistence, getContainer, instantiate, isAtomic, isOpaque, moveDown, moveToFirst, moveToIndex, moveToLast, moveUp, propagateExistence, setName
_addPort, _description, _exportMoMLContents, _removePort, _validateSettables, connectedPortList, connectedPorts, containedObjectsIterator, getAttribute, getPort, getPorts, linkedRelationList, linkedRelations, portList, removeAllPorts, setClassDefinition, uniqueName
_setParent, exportMoML, getChildren, getElementName, getParent, getPrototypeList, isClassDefinition, isWithinClassDefinition
_addAttribute, _adjustOverride, _attachText, _cloneFixAttributeFields, _containedDecorators, _copyChangeRequestList, _debug, _debug, _debug, _debug, _debug, _executeChangeRequests, _getIndentPrefix, _isMoMLSuppressed, _markContentsDerived, _notifyHierarchyListenersAfterChange, _notifyHierarchyListenersBeforeChange, _propagateValue, _removeAttribute, _splitName, _stripNumericSuffix, addChangeListener, addDebugListener, addHierarchyListener, 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
equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
createReceivers, getCausalityInterface, getDirector, getExecutiveDirector, getManager, inputPortList, newReceiver, outputPortList
isFireFunctional, isStrict, iterate
addInitializable, removeInitializable
description, getContainer, getDisplayName, getFullName, getName, getName, setName
getDerivedLevel, getDerivedList, propagateValue
public Groovy(CompositeEntity container, java.lang.String name) throws IllegalActionException, NameDuplicationException
protected void _putObjectToActorInstance(java.lang.String name, java.lang.Object object) throws javax.script.ScriptException
_putObjectToActorInstance
in class ScriptEngineActor
name
- the name of the object to put.object
- the object to put.javax.script.ScriptException