public class JRuby extends ScriptEngineActor
NOTE:The JRuby jar must be downloaded separately since it is released with the GPL license.
The following example defines an actor in JRuby that computes factorials:
1. include Java 2. 3. import Java::ptolemy.data.IntToken 4. 5. class Actor 6. # create accessor methods for self field 7. # the java actor will be assigned to this field. 8. attr_accessor :self 9. 10. def fire 11. # read the input value from the port "input" 12. val = @self.getPort('input').get(0).intValue 13. 14. # calculate the factorial 15. total = 1 16. if val < 0 17. @self.error("Input must be greater than or equal to 0") 18. else 19. while val > 1 do 20. total *= val 21. val -= 1 22. end 23. end 24. 25. # write the factorial to the port "output" 26. @self.getPort('output').broadcast(IntToken.new(total)) 27. end 28. end
Line 1 includes the module for the JVM, and line 3 imports the Java class used by the actor. Lines 5-28 define the Actor object; the actor object must be named "Actor" in JRuby scripts. Line 8 specifies that accessor methods should be created for a field called "self". This field is a reference to the Java object of this actor, and can be used to access ports and parameters. Lines 10-27 define the fire() method of the actor, which is called each time this actor executes in the workflow. Line 12 reads an integer from the input port called "input". A port or parameter called "foo" can be accessed in the JRuby script by using
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 |
---|
JRuby(CompositeEntity container,
java.lang.String name)
Construct a new JRuby with the given container and name.
|
JRuby(Workspace workspace)
Construct a new JRuby for a specified workspace.
|
Modifier and Type | Method and Description |
---|---|
protected java.lang.Object |
_createActorInstance(java.lang.String actorClassNameStr)
Create an instance of the actor object in the script.
|
protected void |
_putObjectToActorInstance(java.lang.String name,
java.lang.Object object)
Put the given object to the actor instance in the script.
|
_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 JRuby(CompositeEntity container, java.lang.String name) throws IllegalActionException, NameDuplicationException
protected java.lang.Object _createActorInstance(java.lang.String actorClassNameStr) throws javax.script.ScriptException
_createActorInstance
in class ScriptEngineActor
javax.script.ScriptException
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