|
|||||||||
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.actor.Manager
public class Manager
A Manager governs the execution of a model in a domain-independent way. Its methods are designed to be called by a GUI, an applet, a command-line interface, or the top-level code of an application. The manager can execute the model in the calling thread or in a separate thread. The latter is useful when the caller wishes to remain live during the execution of the model.
There are three methods that can be used to start execution of a system attached to the manager. The execute() method is the most basic way to execute a model. The model will be executed synchronously, meaning that the execute() method will return when execution has completed. Any exceptions that occur will be thrown by the execute method to the calling thread, and will not be reported to any execution listeners. The run() method also initiates synchronous execution of a model, but additionally catches all exceptions and passes them to the notifyListenersOfException() method without throwing them to the calling thread. The startRun() method, unlike the previous two techniques, begins asynchronous execution of a model. This method starts a new thread for execution of the model and then returns immediately. Exceptions are reported using the notifyListenersOfException() method.
In addition, execution can be manually driven, one phase at a time, using the methods initialize(), iterate() and wrapup(). This is most useful for testing purposes. For example, a type system check only needs to get the resolved types, which are found during initialize, so the test can avoid actually executing the system. Also, when testing mutations, the model can be examined after each toplevel iteration to ensure the proper behavior.
A manager provides services for cleanly handling changes to the topology. These include such changes as adding or removing an entity, port, or relation, creating or destroying a link, and changing the value or type of a parameter. Collectively, such changes are called mutations. Usually, mutations cannot safely occur at arbitrary points in the execution of a model. Models can queue mutations with any object in the hierarchy or with the manager using the requestChange() method. An object in the hierarchy simply delegates the request to its container, so the request propagates up the hierarchy until it gets to the top level composite actor, which delegates to the manager, which performs the change at the earliest opportunity. In this implementation of Manager, the changes are executed between iterations.
A service is also provided whereby an object can be registered with the composite actor as a change listener. A change listener is informed when mutations that are requested via requestChange() are executed successfully, or when they fail with an exception.
Manager can optimize the performance of an execution by making the workspace write protected during an iteration, if all relevant directors permit this. This removes some of the overhead of obtaining read and write permission on the workspace. By default, directors do not permit this, but many directors explicitly relinquish write access to allow faster execution. Such directors are declaring that they will not make changes to the topology during execution. Instead, any desired mutations are delegated to the manager via the requestChange() method.
Many domains make use of static analyses for performing, e.g., static scheduling of actor firings. In some cases, these analyses must make use of global information. The class provides a centralized mechanism for managing such global analyses. During preinitialize, domains can invoke the getAnalysis and addAnalysis methods to create a global analysis. It is up to the users of this mechanism to ensure that a particular type of analysis is only created once, if that is what is required. After preinitialize, the manager clears the list of analyses, to avoid unnecessary memory usage, and to ensure that the analyses are performed again on the next invocation of the model. This is somewhat preferable to tying a cache of analysis information to the version of the workspace, since the version number of the workspace itself may change during preinitialize as domains add annotation to the model.
Yellow (cxh) |
Green (neuendor) |
Nested Class Summary | |
---|---|
static class |
Manager.State
Instances of this class represent phases of execution, or the state of the manager. |
Nested classes/interfaces inherited from class ptolemy.kernel.util.NamedObj |
---|
NamedObj.ContainedObjectsIterator |
Field Summary | |
---|---|
private java.util.List<Actor> |
_actorsToInitialize
|
private CompositeActor |
_container
|
private java.lang.Object |
_executionIdentifier
An execution identifier. |
private java.util.List<java.lang.ref.WeakReference<ExecutionListener>> |
_executionListeners
|
private boolean |
_exitAfterWrapup
|
private boolean |
_finishRequested
|
private int |
_iterationCount
|
private java.util.HashMap<java.lang.String,java.lang.Object> |
_nameToAnalysis
|
private boolean |
_pauseRequested
|
private long |
_preinitializeVersion
|
private boolean |
_printTimeAndMemory
|
private boolean |
_resumeNotifyWaiting
|
private Manager.State |
_state
|
private PtolemyThread |
_thread
|
private java.util.Map<java.lang.Throwable,java.lang.Object> |
_throwableToExecutionIdentifier
|
private boolean |
_typesResolved
|
static Manager.State |
CORRUPTED
Indicator that the model may be corrupted. |
static Manager.State |
EXITING
Indicator that the execution is in the wrapup phase and about to exit. |
static Manager.State |
IDLE
Indicator that there is no currently active execution. |
static Manager.State |
INFERING_WIDTHS
Indicator that width inference is being done. |
static Manager.State |
INITIALIZING
Indicator that the execution is in the initialize phase. |
static Manager.State |
ITERATING
Indicator that the execution is in an iteration. |
static int |
minimumStatisticsTime
The minimum amount of time that may elapse during certain operations before statistics are generated. |
static Manager.State |
PAUSED
Indicator that the execution is paused. |
static Manager.State |
PAUSED_ON_BREAKPOINT
Indicator that the execution is paused on a breakpoint. |
static Manager.State |
PREINITIALIZING
Indicator that the execution is in the preinitialize phase. |
static Manager.State |
RESOLVING_TYPES
Indicator that type resolution is being done. |
static Manager.State |
THROWING_A_THROWABLE
Indicator that the execution is throwing a throwable. |
static Manager.State |
WRAPPING_UP
Indicator that the execution is in the wrapup phase. |
Fields inherited from class ptolemy.kernel.util.NamedObj |
---|
_changeListeners, _changeLock, _changeRequests, _debugging, _debugListeners, _elementName, _isPersistent, _verbose, _workspace, ATTRIBUTES, CLASSNAME, COMPLETE, CONTENTS, DEEP, FULLNAME, LINKS |
Constructor Summary | |
---|---|
Manager()
Construct a manager in the default workspace with an empty string as its name. |
|
Manager(java.lang.String name)
Construct a manager in the default workspace with the given name. |
|
Manager(Workspace workspace,
java.lang.String name)
Construct a manager in the given workspace with the given name. |
Method Summary | |
---|---|
private void |
_inferWidths()
Infer the width of the relations for which no width has been specified yet. |
protected void |
_makeManagerOf(CompositeActor compositeActor)
Make this manager the manager of the specified composite actor. |
protected void |
_notifyListenersOfCompletion()
Deprecated. Incomplete name, use _notifyListenersOfSuccessfulCompletion() |
protected void |
_notifyListenersOfStateChange()
Propagate the state change event to all the execution listeners. |
protected void |
_notifyListenersOfSuccessfulCompletion()
Notify listeners that execution has completed successfully. |
protected void |
_setState(Manager.State newState)
Set the state of execution and notify listeners if the state actually changes. |
void |
addAnalysis(java.lang.String name,
java.lang.Object analysis)
Add a static analysis to this manager. |
void |
addExecutionListener(ExecutionListener listener)
Add a listener to be notified when the model execution changes state. |
void |
enablePrintTimeAndMemory(boolean enabled)
Enable or disable printing time and memory usage at the end of model execution. |
void |
execute()
Execute the model. |
void |
exitAfterWrapup()
Cause the system to exit after wrapup(). |
void |
finish()
If the state is not IDLE, set a flag to request that execution stop and exit in a completely deterministic fashion at the end of the next toplevel iteration. |
java.lang.Object |
getAnalysis(java.lang.String name)
Get the analysis with the given name, or return null if no such analysis exists. |
NamedObj |
getContainer()
Return the top-level composite actor for which this manager controls execution. |
java.lang.Object |
getExecutionIdentifier(java.lang.Throwable throwable)
Get the execution identifier object for a throwable. |
int |
getIterationCount()
Return the iteration count, which is the number of iterations that have been started (but not necessarily completed). |
Manager.State |
getState()
Return the current state of execution of the manager. |
void |
initialize()
Initialize the model. |
void |
invalidateResolvedTypes()
Indicate that resolved types in the system may no longer be valid. |
boolean |
isExitingAfterWrapup()
Return true if exitAfterWrapup() was called. |
boolean |
iterate()
Invoke one iteration of the model. |
void |
notifyListenersOfException(java.lang.Exception exception)
Notify all the execution listeners of an exception. |
void |
notifyListenersOfThrowable(java.lang.Throwable throwable)
Notify all the execution listeners of a Throwable. |
void |
pause()
Set a flag requesting that execution pause at the next opportunity (between iterations). |
void |
pauseOnBreakpoint(java.lang.String breakpointMessage)
The thread that calls this method will wait until resume() has been called. |
void |
preinitializeAndResolveTypes()
Preinitialize the model. |
void |
preinitializeIfNecessary()
If the workspace version has changed since the last invocation of preinitializeAndResolveTypes(), then invoke it now and set the state of the Manager to IDLE upon completion. |
void |
removeExecutionListener(ExecutionListener listener)
Remove a listener from the list of listeners that are notified of execution events. |
void |
requestInitialization(Actor actor)
Queue an initialization request. |
void |
resolveTypes()
Check types on all the connections and resolve undeclared types. |
void |
resume()
If the model is paused, resume execution. |
void |
run()
Execute the model, catching all exceptions. |
void |
setExecutionIdentifier(java.lang.Object executionIdentifier)
Set the execution identifier object. |
static java.lang.String |
shortDescription(java.lang.Throwable throwable)
Deprecated. Instead ptolemy.util.MessageHandler.shortDescription() |
void |
startRun()
Start an execution in another thread and return. |
void |
stop()
If the state is not IDLE, set a flag to request that execution stop and exit in a completely deterministic fashion at the end of the next toplevel iteration. |
void |
terminate()
Deprecated. |
static java.lang.String |
timeAndMemory(long startTime)
Return a string with the elapsed time since startTime, and the amount of memory used. |
static java.lang.String |
timeAndMemory(long startTime,
long totalMemory,
long freeMemory)
Return a string with the elapsed time since startTime, and the amount of memory used. |
void |
waitForCompletion()
If there is an active thread created by startRun(), then wait for it to complete and return. |
void |
wrapup()
Wrap up the model by invoking the wrapup method of the toplevel composite actor. |
Methods inherited from class java.lang.Object |
---|
equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait |
Field Detail |
---|
public static final Manager.State CORRUPTED
public static final Manager.State IDLE
public static final Manager.State INFERING_WIDTHS
public static final Manager.State INITIALIZING
public static final Manager.State ITERATING
public static final Manager.State PAUSED
public static final Manager.State PAUSED_ON_BREAKPOINT
public static final Manager.State PREINITIALIZING
public static final Manager.State RESOLVING_TYPES
public static final Manager.State THROWING_A_THROWABLE
public static final Manager.State WRAPPING_UP
public static final Manager.State EXITING
public static int minimumStatisticsTime
CompositeEntity.statistics(String)
private java.util.List<Actor> _actorsToInitialize
private CompositeActor _container
private java.lang.Object _executionIdentifier
private java.util.List<java.lang.ref.WeakReference<ExecutionListener>> _executionListeners
private boolean _exitAfterWrapup
private boolean _finishRequested
private int _iterationCount
private java.util.HashMap<java.lang.String,java.lang.Object> _nameToAnalysis
private boolean _pauseRequested
private long _preinitializeVersion
private boolean _printTimeAndMemory
private boolean _resumeNotifyWaiting
private volatile Manager.State _state
private PtolemyThread _thread
private java.util.Map<java.lang.Throwable,java.lang.Object> _throwableToExecutionIdentifier
private boolean _typesResolved
Constructor Detail |
---|
public Manager()
public Manager(java.lang.String name) throws IllegalActionException
name
- Name of this Manager.
IllegalActionException
- If the name has a period.public Manager(Workspace workspace, java.lang.String name) throws IllegalActionException
workspace
- Object for synchronization and version tracking.name
- Name of this Manager.
IllegalActionException
- If the name has a period.Method Detail |
---|
public void addAnalysis(java.lang.String name, java.lang.Object analysis)
name
- The name of the analysis.analysis
- The analysis to record.getAnalysis(String)
public void addExecutionListener(ExecutionListener listener)
listener
- The listener.removeExecutionListener(ExecutionListener)
public void enablePrintTimeAndMemory(boolean enabled)
enabled
- Whether time and memory printing is enabled.public void execute() throws KernelException, IllegalActionException
The execution is performed in the calling thread (the current thread), so this method returns only after execution finishes. If you wish to perform execution in a new thread, use startRun() instead. Even if an exception occurs during the execution, the wrapup() method is called (in a finally clause).
If an exception occurs during the execution, delegate to the exception handlers (if there are any) to handle these exceptions. If there are no exception handlers, it is up to the caller to handle (e.g. report) the exception. If you do not wish to handle exceptions, but want to execute within the calling thread, use run().
KernelException
- If the model throws it.
IllegalActionException
- If the model is already running, or
if there is no container.run()
public void exitAfterWrapup()
public void finish()
Executable.stop()
public java.lang.Object getAnalysis(java.lang.String name)
name
- The name of the analysis.
addAnalysis(String, Object)
public NamedObj getContainer()
getContainer
in interface Nameable
getContainer
in class NamedObj
public int getIterationCount()
public Manager.State getState()
public java.lang.Object getExecutionIdentifier(java.lang.Throwable throwable)
An execution identifier is an object given to the Manager via setExecutionIdentifier(), and cleared during preinitialize(). If the Manager notifies a listener of a model error, getExecutionIdentifier() can be used to map that error back to the executionIdentifier object.
throwable
- The throwable.
setExecutionIdentifier(Object)
public void initialize() throws KernelException, IllegalActionException
KernelException
- If the model throws it.
IllegalActionException
- If the model is already running, or
if there is no container.public void invalidateResolvedTypes()
public boolean isExitingAfterWrapup()
public boolean iterate() throws KernelException
KernelException
- If the model throws it, or if there
is no container.public void notifyListenersOfException(java.lang.Exception exception)
notifyListenersOfThrowable(Throwable)
.
exception
- The exception.public void notifyListenersOfThrowable(java.lang.Throwable throwable)
throwable
- The throwablepublic void pause()
Executable.stopFire()
public void pauseOnBreakpoint(java.lang.String breakpointMessage)
Note: This method will block. It should only be called from the executing thread (the thread that is executing the model). Do not call this method from the same thread that will call resume().
breakpointMessage
- The message to print when paused on a
breakpoint.public void preinitializeAndResolveTypes() throws KernelException
KernelException
- If the model throws it.
IllegalActionException
- If the model is already running, or
if there is no container.public void preinitializeIfNecessary() throws KernelException
KernelException
- If thrown wil preinitializing
and resolving types.public void removeExecutionListener(ExecutionListener listener)
listener
- The listener to remove.addExecutionListener(ExecutionListener)
public void requestInitialization(Actor actor)
actor
- The actor to initialize.public void resolveTypes() throws TypeConflictException
TypeConflictException
- If a type conflict is detected.public void resume()
public void run()
run
in interface java.lang.Runnable
public void setExecutionIdentifier(java.lang.Object executionIdentifier)
executionIdentifier
- The execution identifier objectgetExecutionIdentifier(Throwable)
public static java.lang.String shortDescription(java.lang.Throwable throwable)
throwable
- The throwable
public void startRun() throws IllegalActionException
IllegalActionException
- If the model is already running,
e.g. the state is not IDLE.public void stop()
public void terminate()
If the model execution was started in a separate thread (using startRun()), then that thread is killed unceremoniously (using a method that is now deprecated in Java, for obvious reasons). This method also calls terminate on the toplevel composite actor.
This method is not synchronized because we want it to execute as soon as possible.
public static java.lang.String timeAndMemory(long startTime)
startTime
- The start time in milliseconds. For example,
the value returned by (new Date()).getTime()
.
public static java.lang.String timeAndMemory(long startTime, long totalMemory, long freeMemory)
(new Date()).getTime()
.
startTime
- The start time in milliseconds. For example,
the value returned by (new Date()).getTime()
.totalMemory
- The total amount of memory used in kilobytes.freeMemory
- The total amount of memory free in kilobytes.
public void waitForCompletion()
startRun()
public void wrapup() throws KernelException, IllegalActionException
KernelException
- If the model throws it.
IllegalActionException
- If the model is idle or already
wrapping up, or if there is no container.protected void _makeManagerOf(CompositeActor compositeActor)
compositeActor
- The composite actor that this manager will
manage.protected void _notifyListenersOfCompletion()
_notifyListenersOfSuccessfulCompletion()
protected void _notifyListenersOfSuccessfulCompletion()
protected void _notifyListenersOfStateChange()
protected void _setState(Manager.State newState)
newState
- The new state.private void _inferWidths() throws IllegalActionException
IllegalActionException
- If the widths of the relations at
port are not consistent or if the width cannot be inferred
for a relation.
|
|||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES All Classes | ||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |