public class Javascript extends ScriptEngineActor
The following example defines an actor in Javascript that computes factorials:
1. importClass(Packages.ptolemy.data.type.BaseType);
2. importClass(Packages.ptolemy.data.IntToken);
3.
4. function Actor() {};
5.
6. // implement the "fire" function to be called each time the actor executes
7. Actor.prototype.fire = function() {
8.
9. // read from the "input" port
10. var val = this.input.get(0).intValue();
11.
12. if(val < 0) {
13. error = "Input must be greater than or equal to 0";
14. } else {
15. var total = 1;
16. while(val > 1) {
17. total *= val;
18. val--;
19. }
20. // write the result to the "output" port
21. this.output.broadcast(new IntToken(total));
22. }
23. };
24.
25. // define the "preinitialize" function to be called once when the workflow starts.
26. Actor.prototype.preinitialize = function() {
27. // set the data types for the "input" and "output" ports.
28. this.input.setTypeEquals(BaseType.INT);
29. this.output.setTypeAtLeast(this.input);
30. };
Lines 1 and 2 import the Java classes used by the actor. Line 4 defines the Actor object and is required in every Javascript. Lines 7-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 "input". A port or parameter called "foo" can be accessed in the Javascript script by using "this.foo". Lines 12-19 compute the factorial of the integer read from the port. Line 21 writes the result to the output port called "output". Lines 26-29 define the preinitialize() method of this actor, which is executed once when the workflow starts. Lines 28-29 set the types for the input and output ports.
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 |
|---|
Javascript(CompositeEntity container,
java.lang.String name)
Construct a new Javascript with the given container and name.
|
Javascript(Workspace workspace)
Construct a new Javascript for a specified workspace.
|
_createActorInstance, _evaluateScript, _putObjectToActorInstance, _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, 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 Javascript(Workspace workspace)
public Javascript(CompositeEntity container, java.lang.String name) throws IllegalActionException, NameDuplicationException