etomica.action.activity
Class Controller

java.lang.Object
  extended by etomica.action.Activity
      extended by etomica.action.activity.ActivityGroupSeries
          extended by etomica.action.activity.Controller
All Implemented Interfaces:
ActionGroup, ActivityGroup, IController, IAction, Serializable

public class Controller
extends ActivityGroupSeries
implements Serializable, IController

Organizer and executor of actions performed by the simulation. The Controller holds a series of Actions which it performs in series, using a thread that it launches when start() is invoked. A simple Action will run on the Controller's thread. An Activity is a subclass of Action that runs on its own thread, and can be paused or terminated (halted) before it completes naturally.

The Controller thread will idle while an Activity is running. While idling, the Controller will respond to "urgent actions" that another thread (typically a gui) will ask it to perform. In response, the Controller will pause the current Activity (or complete the Action if not an Activity), perform the urgent action, and continue where it left off. This capability is needed for a user to interact with a simulation that is in progress, without colliding with an integrator running on this Controller.

The queue for pending Actions may be altered (given additions or deletions) while Actions are being executed. As Actions are completed they are moved into a list of completed Actions. When all Actions are done, the Controller exits, but it may be restarted if Actions are added and start() is invoked again.

Each Simulation holds a single, final instance of a Controller.

See Also:
Action, Activity, Serialized Form

Nested Class Summary
static class Controller.ActionStatus
          Enumerated type describing the status of an action.
protected static class Controller.UrgentWaitObject
           
protected static class Controller.WaitObject
           
 
Field Summary
protected  HashMap<IAction,Throwable> actionExceptionMap
           
protected  HashMap<IAction,Controller.ActionStatus> actionStatusMap
           
protected  Controller.UrgentWaitObject urgentWaitObject
           
protected  Controller.WaitObject waitObject
           
 
Fields inherited from class etomica.action.activity.ActivityGroupSeries
completedActions, currentAction, numActions, pauseAfterEachAction, pendingActions
 
Fields inherited from class etomica.action.Activity
haltRequested, isActive, pauseRequested
 
Constructor Summary
Controller()
           
 
Method Summary
 void addAction(IAction newAction)
          Adds the given action to the list of actions.
 void doActionNow(IAction action)
          Pauses current activity, executes given action, then resumes current activity.
 Controller.ActionStatus getActionStatus(IAction action)
          Returns the status of an action held by the controller.
 IEventManager getEventManager()
          Returns the event manager used by the controller to notify listeners of individual actions events as well as events related to the controller itself.
 Throwable getException(IAction action)
          Returns the exception thrown by an action held by the controller.
 boolean isRepeatCurrentAction()
          Returns value of repeatCurrentAction flag.
 boolean removeAction(IAction oldAction)
          Removes the given action from the list of actions performed by this controller.
 void reset()
          Marks all actions as pending.
protected  void run()
          Causes uncompleted actions added to this group to be run in sequence.
 void setRepeatCurrentAction(boolean repeatCurrentAction)
          Flag indicating whether the current action should be repeated indefinitely before moving on to the next action.
 String toString()
           
 
Methods inherited from class etomica.action.activity.ActivityGroupSeries
getAllActions, getCompletedActions, getCurrentActions, getPendingActions, halt, isPauseAfterEachAction, isPaused, pause, setPauseAfterEachAction, unPause
 
Methods inherited from class etomica.action.Activity
actionPerformed, doContinue, doWait, isActive
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 
Methods inherited from interface etomica.action.activity.ActivityGroup
getCompletedActions, getCurrentActions, getPendingActions
 
Methods inherited from interface etomica.action.ActionGroup
getAllActions
 
Methods inherited from interface etomica.action.IAction
actionPerformed
 

Field Detail

urgentWaitObject

protected final Controller.UrgentWaitObject urgentWaitObject

waitObject

protected final Controller.WaitObject waitObject

actionStatusMap

protected final HashMap<IAction,Controller.ActionStatus> actionStatusMap

actionExceptionMap

protected final HashMap<IAction,Throwable> actionExceptionMap
Constructor Detail

Controller

public Controller()
Method Detail

addAction

public void addAction(IAction newAction)
Description copied from class: ActivityGroupSeries
Adds the given action to the list of actions. If action is already in list of actions to be performed, method returns without doing anything.

Specified by:
addAction in interface ActionGroup
Overrides:
addAction in class ActivityGroupSeries

removeAction

public boolean removeAction(IAction oldAction)
Description copied from class: ActivityGroupSeries
Removes the given action from the list of actions performed by this controller. If action is currently running, or is not in list of actions, method returns without doing anything.

Specified by:
removeAction in interface ActionGroup
Overrides:
removeAction in class ActivityGroupSeries
Returns:
true if the action was removed

reset

public void reset()
Description copied from class: ActivityGroupSeries
Marks all actions as pending. Previously completed actions are put first in the new list of pending actions. The Controller must be inactive or halted before calling this method.

Specified by:
reset in interface IController
Overrides:
reset in class ActivityGroupSeries

getActionStatus

public Controller.ActionStatus getActionStatus(IAction action)
Returns the status of an action held by the controller. Returns null for actions not held by the controller.

Specified by:
getActionStatus in interface IController

getException

public Throwable getException(IAction action)
Returns the exception thrown by an action held by the controller. Returns null if the given action did not throw an exception or is not held by the controller.

Specified by:
getException in interface IController

run

protected void run()
Causes uncompleted actions added to this group to be run in sequence. Should not be executed directly, but instead it is executed by a thread made upon invoking the start method.

Overrides:
run in class ActivityGroupSeries

doActionNow

public void doActionNow(IAction action)
Pauses current activity, executes given action, then resumes current activity. If current action is already paused, it is not resumed by this method.

Specified by:
doActionNow in interface IController
Parameters:
action - Action to be performed right away; cannot be an Activity.

setRepeatCurrentAction

public void setRepeatCurrentAction(boolean repeatCurrentAction)
Flag indicating whether the current action should be repeated indefinitely before moving on to the next action. Setting this to true also sets pauseAfterEachAction to true (setting false then has no effect on pauseAfterEachAction). In repeating an action, when an action is completed it is placed again at the head of the pending action list (for an Action the same instance is used; for an Activity a copy is made).

Default is false.


isRepeatCurrentAction

public boolean isRepeatCurrentAction()
Returns value of repeatCurrentAction flag.


toString

public String toString()
Overrides:
toString in class Object

getEventManager

public IEventManager getEventManager()
Description copied from interface: IController
Returns the event manager used by the controller to notify listeners of individual actions events as well as events related to the controller itself.

Specified by:
getEventManager in interface IController
See Also:
ControllerEvent