public class Java extends ScriptEngineActor
There are several differences between this actor and the BeanShell Java actor. This actor requires:
The following example defines an actor in Java that computes factorials:
1. import ptolemy.actor.*;
2. import ptolemy.data.*;
3. import ptolemy.kernel.util.IllegalActionException;
4.
5. import org.kepler.scriptengine.Java;
6.
7. public class Actor {
8. public void fire() throws IllegalActionException {
9.
10. int val = ((IntToken)in.get(0)).intValue();
11.
12. if(val < 0) {
13. actor.error("Input must be non-negative.");
14. }
15.
16. int total = 1;
17. while(val > 1) {
18. total *= val;
19. val--;
20. }
21.
22. out.broadcast(new IntToken(total));
23. }
24.
25. public TypedIOPort in;
26. public TypedIOPort out;
27. public Java actor;
28.}
Lines 1-3 import the Java classes used by the actor. Line 7 defines the Actor object. Lines 8-23 define the fire() method of the actor; this method is called each time this actor executes in the workflow. Line 10 reads an integer from the input port called "in". A port or parameter called "foo" can be accessed in the Java code by simply using "foo". Lines 16-20 compute the factorial of the integer read from the port. Line 22 writes the result to the output port called "out".
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, LINKSCOMPLETED, NOT_READY, STOP_ITERATING| Constructor and Description |
|---|
Java(CompositeEntity container,
java.lang.String name)
Create a new Java with the given container and name.
|
Java(Workspace workspace)
Create a new Java in the 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 java.lang.Object |
_evaluateScript()
Evaluate the script.
|
protected void |
_putObjectToActorInstance(java.lang.String name,
java.lang.Object object)
Put the given object to the actor instance in the script.
|
protected void |
_registerEngineFactory()
Register the Java ScriptEngine factory with in the
ScriptEngineManager since it is not loaded in a jar.
|
java.lang.Object |
clone(Workspace workspace)
Clone the actor into the specified workspace.
|
attributeChanged, 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, workspaceequals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, waitcreateReceivers, getCausalityInterface, getDirector, getExecutiveDirector, getManager, inputPortList, newReceiver, outputPortListisFireFunctional, isStrict, iterateaddInitializable, removeInitializabledescription, getContainer, getDisplayName, getFullName, getName, getName, setNamegetDerivedLevel, getDerivedList, propagateValuepublic Java(CompositeEntity container, java.lang.String name) throws IllegalActionException, NameDuplicationException
public java.lang.Object clone(Workspace workspace) throws java.lang.CloneNotSupportedException
clone in class ScriptEngineActorworkspace - The workspace for the new object.java.lang.CloneNotSupportedException - If a derived class contains
an attribute that cannot be cloned.NamedObj.exportMoML(Writer, int, String),
NamedObj.setDeferringChangeRequests(boolean)protected java.lang.Object _evaluateScript() throws javax.script.ScriptException, IllegalActionException
_evaluateScript in class ScriptEngineActorjavax.script.ScriptExceptionIllegalActionExceptionprotected java.lang.Object _createActorInstance(java.lang.String actorClassNameStr) throws javax.script.ScriptException
_createActorInstance in class ScriptEngineActorjavax.script.ScriptExceptionprotected void _putObjectToActorInstance(java.lang.String name, java.lang.Object object) throws javax.script.ScriptException
_putObjectToActorInstance in class ScriptEngineActorname - the name of the object to put.object - the object to put.javax.script.ScriptExceptionprotected void _registerEngineFactory()
_registerEngineFactory in class ScriptEngineActor