|
|||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES All Classes | ||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Objectptolemy.kernel.util.NamedObj
ptolemy.kernel.InstantiableNamedObj
ptolemy.kernel.Entity
ptolemy.kernel.ComponentEntity
ptolemy.actor.AtomicActor
ptolemy.actor.TypedAtomicActor
ptolemy.actor.lib.python.PythonScript
public class PythonScript
An actor of this class executes a Python script. There are two versions of this actor provided in the Vergil libraries. The one called "PythonActor" has an input port and an output port; to view or edit its Python script, look inside the actor. The second version is called "PythonScript" and has no ports; to view or edit its Python script, select Configure (or double click on the icon).
Upon creation, this actor has no ports, and no parameters other than
script
; The script parameter has visibility
EXPERT, and therefore does not normally show up in a configure dialog
for the actor. To make the script visible and editable, you have two
options. Including an instance of an attribute of class
TextEditorConfigureFactory (with its attributeName parameter
set to script) results in behavior like that of the Vergil
"PythonScript." That is, to edit the script, you Configure the actor.
If instead you include an instance of TextEditorTableauFactory,
then to edit the script you look inside the actor. Use the latter
if you wish to add additional attributes to the actor and hide the
script from the users. Use the former if the script is the main
means by which users interact with the actor.
Upon creation, this actor has no ports, and no parameters other than
script
; The script parameter has visibility
EXPERT, and therefore does not normally show up in a configure dialog
for the actor. To make the script visible and editable, you have two
options. Including an instance of an attribute of class
TextEditorConfigureFactory (with its attributeName parameter
set to script) results in behavior like that of the Vergil
"PythonScript." That is, to edit the script, you Configure the actor.
If instead you include an instance of TextEditorTableauFactory,
then to edit the script you look inside the actor. Use the latter
if you wish to add additional attributes to the actor and hide the
script from the users. Use the former if the script is the main
means by which users interact with the actor.
The functionality of an actor of this type is given by a Python script.
As an example, a simplified version of the
Scale
actor can be implemented by the following script:
1. class Main : 2. "scale" 3. def fire(self) : 4. if not self.input.hasToken(0) : 5. return 6. s = self.scale.getToken() 7. t = self.input.get(0) 8. self.output.broadcast(s.multiply(t))Line 1 defines a Python class Main. This name is fixed. An instance of this class is created when the actor is initialized. Line 2 is a description of the purpose of the script. Lines 3-8 define the fire() method, which is called by the
fire()
method of this actor. In the method body,
input and output are ports that have to have been
added to the actor, and scale is a parameter that has to have been
added to the actor (these can be added in the XML that defines the
actor instance in an actor library). The Main class can provide other
methods in the Executable
interface
as needed.
In the script, use self.actor
to access the actor. For example,
self.actor.getDirector()
returns the current director of the
actor. For debugging, use self.actor.debug(someMessage)
. The
final message sent to the debug listeners of the actor will have the string
"From script: " inserted at the beginning. To avoid generating the debug
message when there are no listeners, use:
if self.actor.isDebugging() : self.actor.debug(someMessage)
This class relies on Jython, which is a Java implementation of Python.
As of 1/2/2006, $PTII/lib/jython.jar was based on Jython 1.1 .
The Jython Registry - information about how to set the search path.
Follow the links below for more information about the Python language, licensing, downloads, etc.
Red (reviewmoderator) |
Yellow (liuxj) |
Nested Class Summary |
---|
Nested classes/interfaces inherited from class ptolemy.kernel.Entity |
---|
Entity.ContainedObjectsIterator |
Field Summary | |
---|---|
private org.python.core.PyClass |
_class
|
private static java.lang.String |
_CLASS_NAME
|
private static org.python.util.PythonInterpreter |
_interpreter
|
private static java.lang.String[] |
_METHOD_NAMES
|
private java.util.HashMap |
_methodMap
|
private org.python.core.PyObject |
_object
|
StringAttribute |
script
The script that specifies the function of this actor. |
Fields inherited from class ptolemy.actor.AtomicActor |
---|
_actorFiringListeners, _initializables, _notifyingActorFiring, _stopRequested |
Fields inherited from class ptolemy.kernel.util.NamedObj |
---|
_changeListeners, _changeLock, _changeRequests, _debugging, _debugListeners, _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 | |
---|---|
PythonScript(CompositeEntity container,
java.lang.String name)
Construct an actor with the given container and name. |
Method Summary | |
---|---|
private org.python.core.PyObject |
_createObject()
|
private void |
_evaluateScript()
|
private org.python.core.PyObject |
_invokeMethod(java.lang.String methodName,
java.lang.Object[] args)
|
private java.lang.String |
_mangleName(java.lang.String name)
|
private void |
_reportScriptError(org.python.core.PyException ex,
java.lang.String messagePrefix)
|
void |
attributeChanged(Attribute attribute)
If script is changed, invoke the python interpreter to evaluate the script. |
java.lang.Object |
clone(Workspace workspace)
Clone the actor into the specified workspace. |
void |
debug(java.lang.String message)
Send the message to all registered debug listeners. |
void |
fire()
Invoke the fire() method if defined in the script. |
void |
initialize()
Invoke the initialize() method if defined in the script. |
boolean |
isDebugging()
Return true if this actor has at least one debug listener. |
boolean |
postfire()
Invoke the postfire() method if defined in the script. |
boolean |
prefire()
Invoke the prefire() method if defined in the script. |
void |
preinitialize()
Create an instance of the Main class defined in the script. |
void |
stop()
Invoke the stop() method if defined in the script. |
void |
stopFire()
Invoke the stopFire() method if defined in the script. |
void |
terminate()
Invoke the terminate() method if defined in the script. |
void |
wrapup()
Invoke the wrapup() method if defined in the script. |
Methods inherited from class ptolemy.actor.TypedAtomicActor |
---|
_addPort, _fireAt, _fireAt, attributeTypeChanged, clone, newPort, typeConstraintList, typeConstraints |
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 |
---|
_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 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.actor.Executable |
---|
isFireFunctional, isStrict, iterate |
Methods inherited from interface ptolemy.actor.Initializable |
---|
addInitializable, removeInitializable |
Methods inherited from interface ptolemy.kernel.util.Nameable |
---|
description, getContainer, getDisplayName, getFullName, getName, getName, setName |
Methods inherited from interface ptolemy.kernel.util.Derivable |
---|
getDerivedLevel, getDerivedList, propagateValue |
Field Detail |
---|
public StringAttribute script
private org.python.core.PyClass _class
private static org.python.util.PythonInterpreter _interpreter
private java.util.HashMap _methodMap
private org.python.core.PyObject _object
private static java.lang.String _CLASS_NAME
private static final java.lang.String[] _METHOD_NAMES
Constructor Detail |
---|
public PythonScript(CompositeEntity container, java.lang.String name) throws NameDuplicationException, IllegalActionException
container
- The container.name
- The name of this actor.
NameDuplicationException
- If the container already
has an actor with this name.
IllegalActionException
- If the actor cannot be contained
by the proposed container.Method Detail |
---|
public void attributeChanged(Attribute attribute) throws IllegalActionException
attributeChanged
in class NamedObj
attribute
- The attribute that changed.
IllegalActionException
- If there is any error in evaluating
the script.public java.lang.Object clone(Workspace workspace) throws java.lang.CloneNotSupportedException
clone
in class AtomicActor
workspace
- 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)
public void debug(java.lang.String message)
self.actor.debug()
to call this method.
message
- The debug message.public void fire() throws IllegalActionException
fire
in interface Executable
fire
in class AtomicActor
IllegalActionException
- If there is any error in calling the
fire() method defined by the script.public void initialize() throws IllegalActionException
initialize
in interface Initializable
initialize
in class AtomicActor
IllegalActionException
- If there is any error in calling the
initialize() method defined by the script.public boolean isDebugging()
public boolean postfire() throws IllegalActionException
postfire
in interface Executable
postfire
in class AtomicActor
IllegalActionException
- If there is any error in calling the
postfire() method defined by the script.public boolean prefire() throws IllegalActionException
prefire
in interface Executable
prefire
in class AtomicActor
IllegalActionException
- If there is any error in calling the
prefire() method.public void preinitialize() throws IllegalActionException
preinitialize
in interface Initializable
preinitialize
in class AtomicActor
IllegalActionException
- If there is any error in creating
an instance of the Main class defined in the script.public void stop()
stop
in interface Executable
stop
in class AtomicActor
public void stopFire()
stopFire
in interface Executable
stopFire
in class AtomicActor
public void terminate()
terminate
in interface Executable
terminate
in class AtomicActor
public void wrapup() throws IllegalActionException
wrapup
in interface Initializable
wrapup
in class AtomicActor
IllegalActionException
- If there is any error in calling the
wrapup() method defined in the script.private org.python.core.PyObject _createObject() throws IllegalActionException
IllegalActionException
private void _evaluateScript() throws IllegalActionException
IllegalActionException
private org.python.core.PyObject _invokeMethod(java.lang.String methodName, java.lang.Object[] args) throws IllegalActionException
IllegalActionException
private java.lang.String _mangleName(java.lang.String name)
private void _reportScriptError(org.python.core.PyException ex, java.lang.String messagePrefix) throws IllegalActionException
IllegalActionException
|
|||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES All Classes | ||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |