public class Scheduler extends Agent
Scheduler is the core agent for defining multi-agent based simulations. This class defines a generic threaded agent which is in charge of activating the simulated agents usingActivator
.Activator
are tool objects which are able to trigger any available method belonging to the agents that have a role within a group. The purpose of this approach is to allow the manipulation of agents regardless of their concrete Java classes. 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. As of MaDKit 5.3, two different temporal schemes could used:
BigDecimal
which is incremented at will. BigDecimal
is 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.
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 is ChronoUnit.SECONDS
).
This mode also allow to handle agents that evolve considering different time scales or during specific period of the day.
Those two modes are exclusive and can be selected using the corresponding constructor of the schedulerActivator
,
BigDecimal
,
LocalDateTime
Modifier and Type | Class and Description |
---|---|
static class |
Scheduler.SimulationState
A simulation state.
|
static interface |
Scheduler.SimulationTime |
AbstractAgent.ReturnCode, AbstractAgent.State
Constructor and Description |
---|
Scheduler()
Constructs a
Scheduler using a tick-based Scheduler.SimulationTime . |
Scheduler(double endTick)
Constructs a
Scheduler using a tick-based that will end the simulation for the specified tick. |
Scheduler(java.time.LocalDateTime initialDate)
Constructs a
Scheduler using a date-based time which relies on LocalDateTime |
Modifier and Type | Method and Description |
---|---|
void |
addActivator(Activator<? extends AbstractAgent> activator)
Adds an activator to the simulation engine.
|
protected void |
checkMail(Message m)
Changes my state according to a
SchedulingMessage and sends a reply to the sender as acknowledgment. |
void |
doSimulationStep()
Defines a default simulation loop which is automatically during the scheduler's life.
|
protected void |
end()
This method corresponds to the last behavior which is called by the MaDKit kernel.
|
void |
executeAndLog(Activator<? extends AbstractAgent> activator,
java.lang.Object... args)
Triggers the execute method of this
activator and logs it using the Level.FINER logging level |
int |
getDelay()
Returns the delay between two simulation steps
|
double |
getGVT()
Deprecated.
as of MaDKit 5.3, replaced by
AbstractAgent.getSimulationTime() |
javax.swing.JLabel |
getGVTLabel()
Returns a label giving the simulation time
|
javax.swing.JMenu |
getSchedulerMenu()
Returns a menu which could be used in any GUI.
|
javax.swing.JLabel |
getSchedulerStatusLabel()
Returns a label giving some information on the simulation process
|
javax.swing.JToolBar |
getSchedulerToolBar()
Returns a toolbar which could be used in any GUI.
|
double |
getSimulationDuration()
Deprecated.
as of MDK 5.3, replaced by
AbstractAgent.getSimulationTime() available methods |
Scheduler.SimulationState |
getSimulationState()
The state of the simulation.
|
protected void |
live()
Scheduler's default behavior. default code is:
while (isAlive()) {
if (GVT > getSimulationDuration()) {
if (logger !
|
void |
logActivationStep()
Logs the current simulation step value.
|
protected void |
paused()
Runs
checkMail(Message) every 1000 ms. |
void |
removeActivator(Activator<? extends AbstractAgent> activator)
Removes an activator from the simulation engine.
|
void |
removeAllActivators()
Remove all the activators which have been previously added
|
void |
setDelay(int delay)
Sets the delay between two simulation steps.
|
void |
setGVT(double value)
Deprecated.
as of MaDKit 5.3, replaced by
AbstractAgent.getSimulationTime() |
void |
setSimulationDuration(double endTime)
Deprecated.
as of MDK 5.3, replaced by
AbstractAgent.getSimulationTime() available methods |
protected void |
setSimulationState(Scheduler.SimulationState newState)
Changes the state of the scheduler
|
void |
setupFrame(AgentFrame frame)
Setup the default Scheduler GUI when launched with the default MaDKit GUI mechanism.
|
broadcastMessageWithRoleAndWaitForReplies, getThreadPriority, isDaemon, killAgent, pause, sendMessageAndWaitForReply, sendMessageAndWaitForReply, sendMessageAndWaitForReply, sendMessageAndWaitForReply, sendMessageWithRoleAndWaitForReply, sendMessageWithRoleAndWaitForReply, sendMessageWithRoleAndWaitForReply, sendMessageWithRoleAndWaitForReply, sendReplyAndWaitForReply, sendReplyAndWaitForReply, sendReplyWithRoleAndWaitForReply, sendReplyWithRoleAndWaitForReply, setThreadPriority, waitAnswer, waitAnswer, waitNextMessage, waitNextMessage, waitNextMessage, waitNextMessage
activate, broadcastMessage, broadcastMessageWithRole, bucketModeCreateGroup, bucketModeRequestRole, checkAgentAddress, compareTo, createGroup, createGroup, createGroup, createGroupIfAbsent, createGroupIfAbsent, createGroupIfAbsent, createGUIOnStartUp, destroyCommunity, destroyGroup, destroyRole, executeThisAgent, executeThisAgent, executeThisAgent, getAgentAddressIn, getAgentsWithRole, getAgentsWithRole, getAgentWithRole, getDistantAgentWithRole, getExistingCommunities, getExistingGroups, getExistingRoles, getKernelAddress, getLastReceivedMessage, getLastReceivedMessage, getLogger, getMadkitConfig, getMadkitProperty, getMadkitProperty, getMyGroups, getMyRoles, getName, getNetworkID, getOrganizationSnapShot, getReplyTo, getServerInfo, getSimpleNetworkID, getSimulationTime, getState, hasGUI, hashCode, hasRole, isAlive, isCommunity, isGroup, isKernelOnline, isMadkitPropertyTrue, isMessageBoxEmpty, isRole, killAgent, launchAgent, launchAgent, launchAgent, launchAgent, launchAgent, launchAgent, launchAgent, launchAgent, launchAgentBucket, launchAgentBucket, launchAgentBucket, launchAgentBucket, launchNode, launchXmlAgents, leaveGroup, leaveRole, nextMessage, nextMessage, nextMessages, proceedEnumMessage, purgeMailbox, receiveMessage, reload, requestRole, requestRole, sendMessage, sendMessage, sendMessageWithRole, sendMessageWithRole, sendReply, sendReplyWithRole, setLogLevel, setMadkitProperty, setMadkitProperty, setName, toString
public Scheduler()
Scheduler
using a tick-based Scheduler.SimulationTime
.public Scheduler(double endTick)
Scheduler
using a tick-based that will end the simulation for the specified tick.endTick
- the tick at which the simulation will automatically stoppublic Scheduler(java.time.LocalDateTime initialDate)
Scheduler
using a date-based time which relies on LocalDateTime
initialDate
- the date at which the simulation should begin e.g. LocalDateTime.of(1, 1, 1, 0, 0)
the initial date of the simulationpublic int getDelay()
public void setDelay(int delay)
doSimulationStep()
. Using the Scheduler's GUI, the value can be adjusted from 0 to 400.delay
- the real time pause between two steps in milliseconds, an integer between 0 and 400: O is max speed.public double getGVT()
AbstractAgent.getSimulationTime()
public void setGVT(double value)
AbstractAgent.getSimulationTime()
value
- the current simulation timepublic void setupFrame(AgentFrame frame)
setupFrame
in class AbstractAgent
frame
- the default frame which has been created by MaDKit for this agent.AbstractAgent.setupFrame(AgentFrame)
public void addActivator(Activator<? extends AbstractAgent> activator)
activator
- an activator.public void removeActivator(Activator<? extends AbstractAgent> activator)
activator
- an activator.public void doSimulationStep()
Activator.execute(Object...)
, and then increments the simulation time by one unit. Default implementation is:
logActivationStep(); for (final Activator extends AbstractAgent> activator : activators) { executeAndLog(activator); } getSimulationTime().addOneTimeUnit();By default logs are displayed only if
AbstractAgent.getLogger()
is set above Level.FINER
.public void logActivationStep()
AbstractAgent.getLogger()
is set above Level.FINER
.public void executeAndLog(Activator<? extends AbstractAgent> activator, java.lang.Object... args)
activator
and logs it using the Level.FINER
logging levelactivator
- the activator to triggerargs
- the args that will be passed to the targeted methodprotected void end()
AbstractAgent
Here is a typical example:
@Override protected void end() { AbstractAgent.ReturnCode returnCode = leaveRole("a community", "a group", "my role"); if (returnCode == AbstractAgent.ReturnCode.SUCCESS){ if(logger != null) logger.info("I am leaving the artificial society"); } else{ if(logger != null) logger.warning("something wrong when ending, return code is "+returnCode); } if(logger != null) logger.info("I am done"); } }
end
in class AbstractAgent
public Scheduler.SimulationState getSimulationState()
Scheduler.SimulationState
protected void setSimulationState(Scheduler.SimulationState newState)
newState
- the new stateprotected void live()
while (isAlive()) { if (GVT > getSimulationDuration()) { if (logger != null) logger.info("Simulation has reached end time -> " + getSimulationDuration()); return; } pause(getDelay()); checkMail(nextMessage()); switch (getSimulationState()) { case RUNNING: doSimulationStep(); break; case PAUSED: paused(); break; case STEP: simulationState = PAUSED; doSimulationStep(); break; case SHUTDOWN: return; // shutdown default: getLogger().severe("state not handled " + getSimulationState); } }
live
in class Agent
Agent.live()
protected void checkMail(Message m)
SchedulingMessage
and sends a reply to the sender as acknowledgment.m
- the received messageprotected void paused()
checkMail(Message)
every 1000 ms.public void removeAllActivators()
@Deprecated public void setSimulationDuration(double endTime)
AbstractAgent.getSimulationTime()
available methodsendTime
- the end time to setpublic double getSimulationDuration()
AbstractAgent.getSimulationTime()
available methodspublic javax.swing.JToolBar getSchedulerToolBar()
public javax.swing.JMenu getSchedulerMenu()
public javax.swing.JLabel getSchedulerStatusLabel()
public javax.swing.JLabel getGVTLabel()
Fabien Michel, Olivier Gutknecht, Jacques Ferber - September 9 2019