public class Manager extends NamedObj implements java.lang.Runnable
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.
Modifier and Type | Class and Description |
---|---|
static class |
Manager.State
Instances of this class represent phases of execution, or the
state of the manager.
|
NamedObj.ContainedObjectsIterator
Modifier and Type | Field and Description |
---|---|
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 long |
SHUTDOWN_TIME
The amount of time to allow for the model to terminate
gracefully before shutting it down when the JVM is shut down
due to control-C, user logging out, etc.
|
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.
|
_changeListeners, _changeLock, _changeRequests, _debugging, _debugListeners, _deferChangeRequests, _elementName, _isPersistent, _verbose, _workspace, ATTRIBUTES, CLASSNAME, COMPLETE, CONTENTS, DEEP, FULLNAME, LINKS
Constructor and Description |
---|
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.
|
Modifier and Type | Method and Description |
---|---|
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 |
_registerShutdownHook()
Register a shutdown hook to gracefully stop the execution of a model
if the JVM is shut down (by control-C, the user logging out, etc.).
|
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.
|
long |
elapsedTimeSinceStart()
Return the elapsed time (in milliseconds) since the start of execution
of the model.
|
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).
|
long |
getPreinitializeVersion()
Return the workspace version on the last completion of
preinitializeAndResolveTypes().
|
long |
getRealStartTime()
Get the real time at which the model began executing.
|
Manager.State |
getState()
Return the current state of execution of the manager.
|
java.lang.String |
getStatusMessage()
Return any messages, such as the amount of time consumed.
|
java.lang.Thread |
getWaitingThread()
Return a thread that is waiting and can be
interrupted in the event that a change request is made,
or null to indicate that there is no thread waiting.
|
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.
|
static void |
preinitializeThenWrapup(Actor actor)
Invoke preinitialize() and wrapup().
|
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.
|
void |
setStatusMessage(java.lang.String message)
Set the status message, such as the amount of time consumed.
|
void |
setWaitingThread(java.lang.Thread thread)
Indicate that the specified thread is waiting and can be
interrupted in the event that a change request is made.
|
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.
|
_addAttribute, _adjustOverride, _attachText, _cloneFixAttributeFields, _containedDecorators, _copyChangeRequestList, _debug, _debug, _debug, _debug, _debug, _description, _executeChangeRequests, _exportMoMLContents, _getContainedObject, _getIndentPrefix, _isMoMLSuppressed, _markContentsDerived, _notifyHierarchyListenersAfterChange, _notifyHierarchyListenersBeforeChange, _propagateExistence, _propagateValue, _removeAttribute, _splitName, _stripNumericSuffix, _validateSettables, addChangeListener, addDebugListener, addHierarchyListener, attributeChanged, attributeDeleted, attributeList, attributeList, attributeTypeChanged, clone, clone, containedObjectsIterator, decorators, deepContains, depthInHierarchy, description, description, event, executeChangeRequests, exportMoML, exportMoML, exportMoML, exportMoML, exportMoML, exportMoMLPlain, getAttribute, getAttribute, getAttributes, getChangeListeners, getClassName, getDecoratorAttribute, getDecoratorAttributes, getDerivedLevel, getDerivedList, getDisplayName, getElementName, getFullName, getModelErrorHandler, getName, getName, getPrototypeList, getSource, handleModelError, isDeferringChangeRequests, isOverridden, isPersistent, lazyContainedObjectsIterator, message, moveDown, moveToFirst, moveToIndex, moveToLast, moveUp, notifyOfNameChange, propagateExistence, propagateValue, propagateValues, removeAttribute, removeChangeListener, removeDebugListener, removeHierarchyListener, requestChange, setClassName, setDeferringChangeRequests, setDerivedLevel, setDisplayName, setModelErrorHandler, setName, setPersistent, setSource, sortContainedObjects, toplevel, toString, uniqueName, validateSettables, workspace
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)
public static long SHUTDOWN_TIME
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.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 long elapsedTimeSinceStart()
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 long getRealStartTime()
public NamedObj getContainer()
getContainer
in interface Nameable
getContainer
in class NamedObj
public int getIterationCount()
public long getPreinitializeVersion()
preinitializeAndResolveTypes()
.public Manager.State getState()
public java.lang.String getStatusMessage()
setStatusMessage(String)
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 java.lang.Thread getWaitingThread()
setWaitingThread(Thread)
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 while preinitializing
and resolving types.public static void preinitializeThenWrapup(Actor actor) throws KernelException
If the model has not been preinitialized since the last change to its structure, that must be done now for the result to be accurate. This is because higher-order components and Publisher and Subscriber connections may not have yet been created.
actor
- The actor upon which to call preinitialize() on
its toplevel.KernelException
- If thrown when a Manager is added to
the top level or if preinitialize() fails.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 void setStatusMessage(java.lang.String message)
message
- The message to be displayed.getStatusMessage()
public void setWaitingThread(java.lang.Thread thread)
thread
- The thread that is waiting, or null to indicate
that no thread is waiting.getWaitingThread()
@Deprecated public static java.lang.String shortDescription(java.lang.Throwable throwable)
throwable
- The throwablepublic void startRun() throws IllegalActionException
IllegalActionException
- If the model is already running,
e.g. the state is not IDLE.public void stop()
Note that in general, it is best to get that director and call finish() and stopFire() on the director than it is to call Manager.stop().
The reason that it is better to call Director.finish() is that if Manager.stop() is called inside a RunCompositeActor then only the inside execution will be stopped, not the outside one. It is also not correct to call stop() on the directory because Director.stop() requests immediate stopping. To give determinate stopping, call Director.finish() so that the current iteration is completed.
In multithreaded domains, Director.stopFire is called to request that all actors conclude ongoing firings.
@Deprecated 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.@Deprecated protected void _notifyListenersOfCompletion()
_notifyListenersOfSuccessfulCompletion()
protected void _notifyListenersOfSuccessfulCompletion()
protected void _notifyListenersOfStateChange()
protected void _registerShutdownHook()
protected void _setState(Manager.State newState)
newState
- The new state.