Class Scheduler<T extends SimuTimer<?>>
java.lang.Object
madkit.kernel.Agent
madkit.simulation.SimuAgent
madkit.kernel.Scheduler<T>
- Type Parameters:
T- the type of the simulation time. It should be a subclass of either aDateBasedTimeror aTickBasedTimer. Two different temporal schemes could used:- tick-based: The time is represented as a
BigDecimalwhich is incremented at will.BigDecimalis used to avoid rounding errors that may happen when working with double values. This is the preferred choice if the simulation is based on simple loop following a discrete time approach. - date-based: The time is represented using
LocalDateTime. This is far more convenient when the model refers to a real-world case for which representing usual temporal units such as hours or weeks is required (the default used unit isChronoUnit.SECONDS). This mode also allow to handle agents that evolve considering different time scales or during specific period of the day.
- tick-based: The time is represented as a
- Direct Known Subclasses:
DateBasedDiscreteEventScheduler,TickBasedScheduler
Scheduler is the core agent for defining multi-agent based simulations in MaDKit.This class defines a generic threaded agent which is in charge of activating the simulated agents usingActivator.Activatorare tool objects which are able to trigger any available method belonging to agents having a role within a group.The interest of this approach is twofold:
- Firstly it allows the manipulation of agents regardless of their concrete Java classes.
- Secondly it allows to define complex scheduling policies by defining different activators.
So a scheduler holds a collection of activators that target specific groups and roles and thus allow to define very complex scheduling policies if required. A default behavior is implemented and corresponds to the triggering of the activators according to the order in which they have been added to the scheduler engine usingaddActivator(Activator).The default state of a scheduler isScheduler.SimulationState.PAUSED.The default delay between two simulation steps is 0 milliseconds (max speed).Default GUI components are defined for this agent and they could be easily integrated in any GUI.
- See Also:
-
Nested Class Summary
Nested ClassesNested classes/interfaces inherited from class madkit.kernel.Agent
Agent.ReturnCode -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionbooleanaddActivator(Activator activator) Adds an activator to the simulation engine.Adds the default activator for environment and returns it.Adds the default activator for viewers and returns it.protected abstract voidDefines a simulation step.protected voidexecuteAndLog(Activator activator, Object... args) Triggers the execute method of thisactivatorand logs it using theLevel.FINERlogging level.Returns the actions that can be used to control the simulation.Gets the list of activators that are currently added to the simulation engine by this scheduler.intgetPause()Returns the delay between two simulation steps.Gets aSliderthat can manipulate the pause between two simulation steps.Returns the actions group that can be used to control the simulation.Returns theSimuTimerof the scheduler.Returns a Label displaying the time of the simulation.Returns aToolBarmade of scheduling actions, a pause slider and a label displaying the current simulation time.voidLogs the current simulation time value.protected voidOn activation, by default the scheduler requests the roleSimuOrganization.SCHEDULER_ROLEin the groupSimuOrganization.ENGINE_GROUP.protected voidonEnd()Called when the simulation ends.protected voidonLive()The main loop of the scheduler agent.voidCalled when the simulation starts, or when the user clicks on the onsimulationrestart button.voidremoveActivator(Activator activator) Removes an activator from the simulation engine.voidRemove all the activators which have been previously added.voidsetPause(int delay) Sets the delay between two simulation steps.protected voidsetSimulationTime(T simulationTime) Sets the simulation time of the simulation.Methods inherited from class madkit.simulation.SimuAgent
createSimuGroup, getCommunity, getEngineGroup, getEnvironment, getLauncher, getModel, getModelGroup, getNextEventDate, getScheduler, getViewers, launchAgent, leaveRole, leaveSimuGroup, leaveSimuRole, playRole, prng, requestSimuRoleMethods inherited from class madkit.kernel.Agent
activate, broadcast, broadcastWithRole, broadcastWithRoleWaitForReplies, checkAgentAddress, createGroup, createGroup, createGroup, end, equals, executeThisAgent, executeThisAgent, exitOnKill, getAgentsWithRole, getAgentWithRole, getKernelAddress, getKernelConfig, getLogger, getMailbox, getName, getNetworkID, getOrganization, getReplyTo, handleRequestActionMessage, hashCode, isAlive, killAgent, killAgent, launchAgent, launchAgent, leaveGroup, leaveRole, live, nextMessage, pause, receiveMessage, reload, reply, replyWithRole, requestRole, requestRole, send, send, sendWaitReply, sendWaitReply, sendWithRole, sendWithRole, sendWithRoleWaitReply, sendWithRoleWaitReply, setupDefaultGUI, toString, waitAnswer, waitAnswer, waitNextMessage, waitNextMessage
-
Constructor Details
-
Scheduler
protected Scheduler()Default constructor. It automatically initializes the simulation time according to the type of the simulation time.
-
-
Method Details
-
onActivation
protected void onActivation()On activation, by default the scheduler requests the roleSimuOrganization.SCHEDULER_ROLEin the groupSimuOrganization.ENGINE_GROUP.- Overrides:
onActivationin classAgent
-
onLive
protected void onLive()The main loop of the scheduler agent. It is automatically called by the MaDKit kernel once the agent is activated. Firstly, the scheduler waits for the first message to start the simulation. Then, it enters an infinite loop where it waits for messages, checks the simulation state, and processes the simulation step accordingly. The loop is interrupted when the simulation reaches the end time or when a SHUTDOWN message is received. -
onSimulationStart
public void onSimulationStart()Called when the simulation starts, or when the user clicks on the onsimulationrestart button. By default, it resets the simulation time and calls theonSimulationStart()method of the environment agent- Overrides:
onSimulationStartin classSimuAgent
-
onEnd
-
doSimulationStep
protected abstract void doSimulationStep()Defines a simulation step. This method should be overridden to define a customized scheduling policy. -
getPause
public int getPause()Returns the delay between two simulation steps.- Returns:
- the pause between two steps
-
setPause
public void setPause(int delay) Sets the delay between two simulation steps.- Parameters:
delay- the pause between two steps
-
getPauseSlider
-
getToolBar
-
getShedulerActions
Returns the actions group that can be used to control the simulation.- Returns:
- the shedulingActions as an ActionGroup
-
getActions
-
getTimeLabel
Returns a Label displaying the time of the simulation.- Returns:
- the current simulation state
-
setSimulationTime
Sets the simulation time of the simulation.- Parameters:
simulationTime- the simulationTime to set
-
getSimuTimer
Returns theSimuTimerof the scheduler.- Overrides:
getSimuTimerin classSimuAgent- Returns:
- the
SimuTimerof the scheduler
-
getActivators
-
addViewersActivator
Adds the default activator for viewers and returns it. The default activator for viewers is an activator that triggers the methodViewer.display()of agents playing the roleSimuOrganization.VIEWER_ROLEin the groupSimuOrganization.ENGINE_GROUP- Returns:
- an activator that triggers viewers display method
-
addEnvironmentActivator
Adds the default activator for environment and returns it. The default activator for environment is an activator that triggers the method update of agents playing the roleSimuOrganization.ENVIRONMENT_ROLEin the groupSimuOrganization.ENGINE_GROUP- Returns:
- an activator that triggers the environment update method
-
addActivator
Adds an activator to the simulation engine. This has to be done before the simulation starts. It is possible to add several activators to the simulation engine. Once added, the activator can be triggered by the scheduler agent using theActivator.execute(Object...)method.- Parameters:
activator- an activator to add to the simulation engine.- Returns:
trueif the activator has been added,falseif it was already added
-
removeActivator
Removes an activator from the simulation engine.- Parameters:
activator- an activator.
-
logCurrrentTime
public void logCurrrentTime()Logs the current simulation time value. logs are displayed only ifAgent.getLogger()is set aboveLevel.FINE. -
executeAndLog
Triggers the execute method of thisactivatorand logs it using theLevel.FINERlogging level.- Parameters:
activator- the activator to triggerargs- the args that will be passed to the targeted method
-
removeAllActivators
public void removeAllActivators()Remove all the activators which have been previously added.
-