edu.memphis.ccrg.lida.framework.tasks
Class TaskManager

java.lang.Object
  extended by edu.memphis.ccrg.lida.framework.tasks.TaskManager
All Implemented Interfaces:
GuiEventProvider

public class TaskManager
extends java.lang.Object
implements GuiEventProvider

All tasks in the Agent system are executed by this class. Controls the decay of all the FrameworkModules in Agent. Keeps track of the current tick, the unit of time in the application. Maintains a task queue where each position represents the time (in ticks) when a task will be executed. Multiple tasks can be scheduled for the same tick. Uses an ExecutorService to obtain the threads to run all the tasks scheduled in one tick concurrently.

Author:
Javier Snaider, Ryan J. McCall

Field Summary
static int DEFAULT_NUMBER_OF_THREADS
          Default number of threads in the ExecutorService
static int DEFAULT_TICK_DURATION
          Default minimum duration of a tick in real time (ms)
 
Constructor Summary
TaskManager(int tickDuration, int maxPoolSize)
          Constructs a new TaskManager.
 
Method Summary
 void addFrameworkGuiEventListener(FrameworkGuiEventListener listener)
          Must be able to register FrameworkGuiEvent listeners
 void addTicksToExecute(long ticks)
          Sets a number of ticks to execute when the system is in interval execution mode.
 boolean cancelTask(FrameworkTask task)
          Cancels the task from the Task Queue.
static long getCurrentTick()
          Current tick in the system.
 long getEndOfNextInterval()
          This attribute is used for interval execution mode.
 int getGuiEventsInterval()
           
 long getMaxTick()
          Returns max tick.
 java.util.Map<java.lang.Long,java.util.Set<FrameworkTask>> getTaskQueue()
           
 int getTickDuration()
           
 boolean isInIntervalMode()
           
 boolean isTasksPaused()
           
 void pauseTasks()
          Finish the executions of all tasks scheduled for the currentTick and pauses all further tasks executions.
(package private)  void reset()
          This method clean up the Task Queue and reset to 0 the currentTick and the maxTick.
 void resumeTasks()
          Resumes the execution of tasks in the queue.
 boolean scheduleTask(FrameworkTask task, long inXTicks)
          Schedules the task for execution in currentTick + inXTicks If inXTicks is negative or 0, the task is not scheduled.
 void sendEventToGui(FrameworkGuiEvent event)
          This is a convenience method to send GUI events to listeners.
 void setDecayingModules(java.util.Collection<FrameworkModule> modules)
          Set the Collection of modules for decaying.
 void setGuiEventsInterval(int i)
          Sets guiEventsInterval
 void setInIntervalMode(boolean intervalMode)
          Sets inIntervalMode.
 void setTickDuration(int d)
          Sets tickDuration
 void stopRunning()
          This method stops all tasks executing and prevents further tasks from being executed.
 java.lang.String toString()
           
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

DEFAULT_TICK_DURATION

public static final int DEFAULT_TICK_DURATION
Default minimum duration of a tick in real time (ms)

See Also:
Constant Field Values

DEFAULT_NUMBER_OF_THREADS

public static final int DEFAULT_NUMBER_OF_THREADS
Default number of threads in the ExecutorService

See Also:
Constant Field Values
Constructor Detail

TaskManager

public TaskManager(int tickDuration,
                   int maxPoolSize)
Constructs a new TaskManager.

Parameters:
tickDuration - - length of time of 1 tick in milliseconds
maxPoolSize - - max number of threads used by the ExecutorService
Method Detail

getCurrentTick

public static long getCurrentTick()
Current tick in the system. Tasks scheduled for this tick have been executed or they are being executed.

Returns:
current tick

getMaxTick

public long getMaxTick()
Returns max tick.

Returns:
the farthest tick in the future that has a scheduled task. in other words, the highest tick position in the task queue that has scheduled task(s).

getEndOfNextInterval

public long getEndOfNextInterval()
This attribute is used for interval execution mode. Returns endOfNextInterval

Returns:
the absolute tick when the current execution interval ends

setTickDuration

public void setTickDuration(int d)
Sets tickDuration

Parameters:
d - the new tick duration, the length of time of 1 tick in milliseconds. The actual time that the tick unit represents. In practice tickDuration affects the speed of tasks in the simulation.

getTickDuration

public int getTickDuration()
Returns:
tickDuration

setGuiEventsInterval

public void setGuiEventsInterval(int i)
Sets guiEventsInterval

Parameters:
i - set a new guiEventsInterval, the number of ticks between gui events generated by the TaskManager. Used to refresh the GUI panels. with 0, no events are generated.

getGuiEventsInterval

public int getGuiEventsInterval()
Returns:
guiEventsInterval

isInIntervalMode

public boolean isInIntervalMode()
Returns:
true if system is in interval execution mode

setInIntervalMode

public void setInIntervalMode(boolean intervalMode)
Sets inIntervalMode.

Parameters:
intervalMode - true to set the system to interval execution mode, false to exit.

getTaskQueue

public java.util.Map<java.lang.Long,java.util.Set<FrameworkTask>> getTaskQueue()
Returns:
UnmodifiableMap of the task queue

isTasksPaused

public boolean isTasksPaused()
Returns:
true if tasks are paused

pauseTasks

public void pauseTasks()
Finish the executions of all tasks scheduled for the currentTick and pauses all further tasks executions.


resumeTasks

public void resumeTasks()
Resumes the execution of tasks in the queue.


cancelTask

public boolean cancelTask(FrameworkTask task)
Cancels the task from the Task Queue. This is only possible if the tick for which the task is scheduled has not been reached.

Parameters:
task - The task to cancel.
Returns:
true if it was , false otherwise.

addTicksToExecute

public void addTicksToExecute(long ticks)
Sets a number of ticks to execute when the system is in interval execution mode. The system will execute all tasks scheduled in the queue until currentTick + ticks.

Parameters:
ticks - the number of ticks to use as an interval.

scheduleTask

public boolean scheduleTask(FrameworkTask task,
                            long inXTicks)
Schedules the task for execution in currentTick + inXTicks If inXTicks is negative or 0, the task is not scheduled.

Parameters:
task - the task to schedule
inXTicks - the number of ticks in the future that the task will be scheduled for execution.
Returns:
true if the task was scheduled.

setDecayingModules

public void setDecayingModules(java.util.Collection<FrameworkModule> modules)
Set the Collection of modules for decaying.

Parameters:
modules - a Collection with the FrameworkModules

stopRunning

public void stopRunning()
This method stops all tasks executing and prevents further tasks from being executed. It is used to shutdown the entire system. Method shuts down all tasks, the executor service, waits, and exits.


addFrameworkGuiEventListener

public void addFrameworkGuiEventListener(FrameworkGuiEventListener listener)
Description copied from interface: GuiEventProvider
Must be able to register FrameworkGuiEvent listeners

Specified by:
addFrameworkGuiEventListener in interface GuiEventProvider
Parameters:
listener - receiver of GuiEvents, typically a GuiPanel

sendEventToGui

public void sendEventToGui(FrameworkGuiEvent event)
Description copied from interface: GuiEventProvider
This is a convenience method to send GUI events to listeners.

Specified by:
sendEventToGui in interface GuiEventProvider
Parameters:
event - GuiEvent

reset

void reset()
This method clean up the Task Queue and reset to 0 the currentTick and the maxTick. All TaskSpawner must be reset also. This method is intended to be used only when the Agent is reset. Currently used only for testing. To be implemented in the framework in the future.


toString

public java.lang.String toString()
Overrides:
toString in class java.lang.Object