Class DevsSimulator<T extends Number & Comparable<T>>
java.lang.Object
org.djutils.event.LocalEventProducer
nl.tudelft.simulation.dsol.simulators.Simulator<T>
nl.tudelft.simulation.dsol.simulators.DevsSimulator<T>
- Type Parameters:
T
- the simulation time type based on the absolute and relative time.
- All Implemented Interfaces:
Serializable
,Runnable
,Remote
,DevsSimulatorInterface<T>
,SimulatorInterface<T>
,EventProducer
- Direct Known Subclasses:
DevDessSimulator
,DevsAnimator
public class DevsSimulator<T extends Number & Comparable<T>>
extends Simulator<T>
implements DevsSimulatorInterface<T>
The DEVS defines the interface of the DEVS simulator. DEVS stands for the Discrete Event System Specification. More
information on Discrete Event Simulation can be found in "Theory of Modeling and Simulation" by Bernard Zeigler et.al.
Copyright (c) 2002-2024 Delft University of Technology, Jaffalaan 5, 2628 BX Delft, the Netherlands. All rights reserved. See for project information https://simulation.tudelft.nl. The DSOL project is distributed under a three-clause BSD-style license, which can be found at https://https://simulation.tudelft.nl/dsol/docs/latest/license.html.
- Since:
- 1.5
- Author:
- Peter Jacobs , Alexander Verbraeck
- See Also:
-
Nested Class Summary
Nested classes/interfaces inherited from class nl.tudelft.simulation.dsol.simulators.Simulator
Simulator.SimulatorWorkerThread
-
Field Summary
Modifier and TypeFieldDescriptionprotected EventListInterface<T>
eventList represents the future event list.Fields inherited from class nl.tudelft.simulation.dsol.simulators.Simulator
model, replication, replicationState, runflag, runState, runUntilIncluding, runUntilTime, semaphore, simulatorTime, worker
Fields inherited from interface nl.tudelft.simulation.dsol.simulators.DevsSimulatorInterface
EVENTLIST_CHANGED_EVENT
Fields inherited from interface org.djutils.event.EventProducer
FIRST_POSITION, LAST_POSITION
Fields inherited from interface nl.tudelft.simulation.dsol.simulators.SimulatorInterface
START_EVENT, STARTING_EVENT, STOP_EVENT, STOPPING_EVENT, TIME_CHANGED_EVENT
-
Constructor Summary
-
Method Summary
Modifier and TypeMethodDescriptionboolean
cancelEvent
(SimEventInterface<T> event) cancels an event from the event list.void
End the replication before the official ending time.returns the eventlist of the simulator.void
initialize
(DsolModel<T, ? extends SimulatorInterface<T>> model, Replication<T> replication) Initializes the simulator with a replication for a model.boolean
Deprecated.void
run()
The run method defines the actual time step mechanism of the simulator.scheduleEvent
(SimEventInterface<T> event) Method scheduleEvent schedules an event on the eventlist.scheduleEventAbs
(T absoluteTime, short priority, Object target, String method, Object[] args) schedules a methodCall at an absolute time.scheduleEventAbs
(T absoluteTime, short priority, Executable executable) schedules a lambda expression at an absolute time.scheduleEventAbs
(T absoluteTime, Object target, String method, Object[] args) schedules a methodCall at an absolute time.scheduleEventAbs
(T absoluteTime, Executable executable) schedules a lambda expression at an absolute time.scheduleEventNow
(short priority, Object target, String method, Object[] args) schedules a methodCall immediately.scheduleEventNow
(short priority, Executable executable) schedules a lambda expression immediately.scheduleEventNow
(Object target, String method, Object[] args) schedules a methodCall immediately.scheduleEventNow
(Executable executable) schedules a lambda expression immediately.scheduleEventRel
(T relativeDelay, short priority, Object target, String method, Object[] args) schedules a methodCall at a relative duration.scheduleEventRel
(T relativeDelay, short priority, Executable executable) schedules a lambda expression at a relative duration.scheduleEventRel
(T relativeDelay, Object target, String method, Object[] args) schedules a methodCall at a relative duration.scheduleEventRel
(T relativeDelay, Executable executable) schedules a lambda expression at a relative duration.void
setEventList
(EventListInterface<T> eventList) Method setEventList sets the eventlist.void
setPauseOnError
(boolean pauseOnError) Deprecated.protected void
stepImpl()
The implementation body of the step() method.Methods inherited from class nl.tudelft.simulation.dsol.simulators.Simulator
addScheduledMethodOnInitialize, cleanUp, fireTimedEvent, getErrorLogLevel, getErrorStrategy, getLogger, getModel, getReplication, getReplicationState, getRunState, getSimulatorTime, handleSimulationException, runUpTo, runUpToAndIncluding, setErrorLogLevel, setErrorStrategy, setErrorStrategy, start, startImpl, step, stop, stopImpl, warmup
Methods inherited from class org.djutils.event.LocalEventProducer
addListener, addListener, addListener, addListener, fireEvent, fireEvent, fireEvent, fireTimedEvent, fireTimedEvent, fireTimedEvent, fireUnverifiedEvent, fireUnverifiedEvent, fireUnverifiedTimedEvent, fireUnverifiedTimedEvent, getEventListenerMap, getEventTypesWithListeners, getListenerReferences, hasListeners, numberOfListeners, removeAllListeners, removeAllListeners, removeListener
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
Methods inherited from interface nl.tudelft.simulation.dsol.simulators.DevsSimulatorInterface
scheduleEventAbs, scheduleEventAbs, scheduleEventNow, scheduleEventNow, scheduleEventRel, scheduleEventRel
Methods inherited from interface org.djutils.event.EventProducer
addListener, addListener, addListener, addListener, fireEvent, fireEvent, fireEvent, fireTimedEvent, fireTimedEvent, fireTimedEvent, fireUnverifiedEvent, fireUnverifiedEvent, fireUnverifiedTimedEvent, fireUnverifiedTimedEvent, getEventListenerMap, getEventTypesWithListeners, getListenerReferences, hasListeners, numberOfListeners, removeAllListeners, removeAllListeners, removeListener
Methods inherited from interface nl.tudelft.simulation.dsol.simulators.SimulatorInterface
addScheduledMethodOnInitialize, addScheduledMethodOnInitialize, cleanUp, getErrorLogLevel, getErrorStrategy, getLogger, getModel, getReplication, getReplicationState, getRunState, getSimulatorTime, isInitialized, isStartingOrRunning, isStoppingOrStopped, runUpTo, runUpToAndIncluding, setErrorLogLevel, setErrorStrategy, setErrorStrategy, start, step, stop
-
Field Details
-
eventList
eventList represents the future event list.
-
-
Constructor Details
-
DevsSimulator
Constructs a new DevsSimulator.- Parameters:
id
- the id of the simulator, used in logging and firing of events.
-
-
Method Details
-
cancelEvent
cancels an event from the event list.- Specified by:
cancelEvent
in interfaceDevsSimulatorInterface<T extends Number & Comparable<T>>
- Parameters:
event
- SimEventInterface<T>; a simulation event to be canceled.- Returns:
- boolean the succes of the operation.
-
getEventList
returns the eventlist of the simulator.- Specified by:
getEventList
in interfaceDevsSimulatorInterface<T extends Number & Comparable<T>>
- Returns:
- the eventlist.
-
initialize
public void initialize(DsolModel<T, ? extends SimulatorInterface<T>> model, Replication<T> replication) throws SimRuntimeExceptionInitializes the simulator with a replication for a model. It immediately fires a START_REPLICATION_EVENT and a TIME_CHANGED_EVENT with the starting time. It does not yet fire a WARMUP_EVENT in case the warmup time is zero; this will only be done after the simulator has been started. Note that the listeners of all statistics objects are removed when the simulator is initialized with the replication. Connecting the statistics objects to the simulation should be done between the initialize(...) method and starting the simulator, or could even be delayed till the WARMUP_EVENT has been fired.- Specified by:
initialize
in interfaceSimulatorInterface<T extends Number & Comparable<T>>
- Overrides:
initialize
in classSimulator<T extends Number & Comparable<T>>
- Parameters:
model
- DsolModel<T, S>; the model to initializereplication
- Replication<T, ? extends SimulatorInterface<T>>; the replication to use for running the model- Throws:
SimRuntimeException
- when the simulator is running
-
scheduleEvent
Method scheduleEvent schedules an event on the eventlist.- Specified by:
scheduleEvent
in interfaceDevsSimulatorInterface<T extends Number & Comparable<T>>
- Parameters:
event
- SimEventInterface<T>; a simulation event- Returns:
- the simulation event so it can be cancelled later
- Throws:
SimRuntimeException
- whenever event is scheduled in past.
-
scheduleEventRel
public SimEventInterface<T> scheduleEventRel(T relativeDelay, short priority, Object target, String method, Object[] args) throws SimRuntimeException schedules a methodCall at a relative duration. The executionTime is thus simulator.getSimulatorTime()+relativeDuration.- Specified by:
scheduleEventRel
in interfaceDevsSimulatorInterface<T extends Number & Comparable<T>>
- Parameters:
relativeDelay
- T; the relativeDelay in timeUnits of the simulator.priority
- short; the priority compared to other events scheduled at the same time.target
- Object; the targetmethod
- String; the methodargs
- Object[]; the arguments.- Returns:
- the simulation event so it can be cancelled later
- Throws:
SimRuntimeException
- whenever the event is scheduled in the past.
-
scheduleEventRel
public SimEventInterface<T> scheduleEventRel(T relativeDelay, Object target, String method, Object[] args) throws SimRuntimeException schedules a methodCall at a relative duration. The executionTime is thus simulator.getSimulatorTime()+relativeDuration.- Specified by:
scheduleEventRel
in interfaceDevsSimulatorInterface<T extends Number & Comparable<T>>
- Parameters:
relativeDelay
- T; the relativeDelay in timeUnits of the simulator.target
- Object; the targetmethod
- String; the methodargs
- Object[]; the arguments.- Returns:
- the simulation event so it can be cancelled later
- Throws:
SimRuntimeException
- whenever the event is scheduled in the past.
-
scheduleEventAbs
public SimEventInterface<T> scheduleEventAbs(T absoluteTime, short priority, Object target, String method, Object[] args) throws SimRuntimeException schedules a methodCall at an absolute time.- Specified by:
scheduleEventAbs
in interfaceDevsSimulatorInterface<T extends Number & Comparable<T>>
- Parameters:
absoluteTime
- T; the exact time to schedule the method on the simulator.priority
- short; the priority compared to other events scheduled at the same time.target
- Object; the targetmethod
- String; the methodargs
- Object[]; the arguments.- Returns:
- the simulation event so it can be cancelled later
- Throws:
SimRuntimeException
- whenever the event is scheduled in the past.
-
scheduleEventAbs
public SimEventInterface<T> scheduleEventAbs(T absoluteTime, Object target, String method, Object[] args) throws SimRuntimeException schedules a methodCall at an absolute time.- Specified by:
scheduleEventAbs
in interfaceDevsSimulatorInterface<T extends Number & Comparable<T>>
- Parameters:
absoluteTime
- T; the exact time to schedule the method on the simulator.target
- Object; the targetmethod
- String; the methodargs
- Object[]; the arguments.- Returns:
- the simulation event so it can be cancelled later
- Throws:
SimRuntimeException
- whenever the event is scheduled in the past.
-
scheduleEventNow
public SimEventInterface<T> scheduleEventNow(short priority, Object target, String method, Object[] args) throws SimRuntimeException schedules a methodCall immediately.- Specified by:
scheduleEventNow
in interfaceDevsSimulatorInterface<T extends Number & Comparable<T>>
- Parameters:
priority
- short; the priority compared to other events scheduled at the same time.target
- Object; the targetmethod
- String; the methodargs
- Object[]; the arguments.- Returns:
- the simulation event so it can be cancelled later
- Throws:
SimRuntimeException
- whenever the event is scheduled in the past.
-
scheduleEventNow
public SimEventInterface<T> scheduleEventNow(Object target, String method, Object[] args) throws SimRuntimeException schedules a methodCall immediately.- Specified by:
scheduleEventNow
in interfaceDevsSimulatorInterface<T extends Number & Comparable<T>>
- Parameters:
target
- Object; the targetmethod
- String; the methodargs
- Object[]; the arguments.- Returns:
- the simulation event so it can be cancelled later
- Throws:
SimRuntimeException
- whenever the event is scheduled in the past.
-
scheduleEventRel
public SimEventInterface<T> scheduleEventRel(T relativeDelay, short priority, Executable executable) throws SimRuntimeException schedules a lambda expression at a relative duration. The executionTime is thus simulator.getSimulatorTime()+relativeDuration.- Specified by:
scheduleEventRel
in interfaceDevsSimulatorInterface<T extends Number & Comparable<T>>
- Parameters:
relativeDelay
- T; the relativeDelay in timeUnits of the simulator.priority
- short; the priority compared to other events scheduled at the same time.executable
- Executable; the lambda expression to execute- Returns:
- the simulation event so it can be cancelled later
- Throws:
SimRuntimeException
- whenever the event is scheduled in the past.
-
scheduleEventRel
public SimEventInterface<T> scheduleEventRel(T relativeDelay, Executable executable) throws SimRuntimeException schedules a lambda expression at a relative duration. The executionTime is thus simulator.getSimulatorTime()+relativeDuration.- Specified by:
scheduleEventRel
in interfaceDevsSimulatorInterface<T extends Number & Comparable<T>>
- Parameters:
relativeDelay
- T; the relativeDelay in timeUnits of the simulator.executable
- Executable; the lambda expression to execute- Returns:
- the simulation event so it can be cancelled later
- Throws:
SimRuntimeException
- whenever the event is scheduled in the past.
-
scheduleEventAbs
public SimEventInterface<T> scheduleEventAbs(T absoluteTime, short priority, Executable executable) throws SimRuntimeException schedules a lambda expression at an absolute time.- Specified by:
scheduleEventAbs
in interfaceDevsSimulatorInterface<T extends Number & Comparable<T>>
- Parameters:
absoluteTime
- T; the exact time to schedule the method on the simulator.priority
- short; the priority compared to other events scheduled at the same time.executable
- Executable; the lambda expression to execute- Returns:
- the simulation event so it can be cancelled later
- Throws:
SimRuntimeException
- whenever the event is scheduled in the past.
-
scheduleEventAbs
public SimEventInterface<T> scheduleEventAbs(T absoluteTime, Executable executable) throws SimRuntimeException schedules a lambda expression at an absolute time.- Specified by:
scheduleEventAbs
in interfaceDevsSimulatorInterface<T extends Number & Comparable<T>>
- Parameters:
absoluteTime
- T; the exact time to schedule the method on the simulator.executable
- Executable; the lambda expression to execute- Returns:
- the simulation event so it can be cancelled later
- Throws:
SimRuntimeException
- whenever the event is scheduled in the past.
-
scheduleEventNow
public SimEventInterface<T> scheduleEventNow(short priority, Executable executable) throws SimRuntimeException schedules a lambda expression immediately.- Specified by:
scheduleEventNow
in interfaceDevsSimulatorInterface<T extends Number & Comparable<T>>
- Parameters:
priority
- short; the priority compared to other events scheduled at the same time.executable
- Executable; the lambda expression to execute- Returns:
- the simulation event so it can be cancelled later
- Throws:
SimRuntimeException
- whenever the event is scheduled in the past.
-
scheduleEventNow
schedules a lambda expression immediately.- Specified by:
scheduleEventNow
in interfaceDevsSimulatorInterface<T extends Number & Comparable<T>>
- Parameters:
executable
- Executable; the lambda expression to execute- Returns:
- the simulation event so it can be cancelled later
- Throws:
SimRuntimeException
- whenever the event is scheduled in the past.
-
setEventList
Method setEventList sets the eventlist.- Specified by:
setEventList
in interfaceDevsSimulatorInterface<T extends Number & Comparable<T>>
- Parameters:
eventList
- EventListInterface<T>; the eventList for the simulator.
-
stepImpl
protected void stepImpl()The implementation body of the step() method. The stepImpl() method should fire the TIME_CHANGED_EVENT before the execution of the simulation event, or before executing the integration of the differential equation for the next timestep. So the time is changed first to match the logic carried out for that time, and then the action for that time is carried out. This is INDEPENDENT of the fact whether the time changes or not. The TIME_CHANGED_EVENT is always fired. -
run
public void run()The run method defines the actual time step mechanism of the simulator. The implementation of this method depends on the formalism. Where discrete event formalisms loop over an event list, continuous simulators take predefined time steps. Make sure that:
- SimulatorInterface.TIME_CHANGED_EVENT is fired when the time of the simulator changes
- the warmup() method is called when the warmup period has expired (through an event or based on simulation time)
- the endReplication() method is called when the replication has ended
- the simulator runs until the runUntil time, which is also set by the start() method. -
endReplication
public void endReplication()End the replication before the official ending time. When the simulation is running, it will fire a STOP_EVENT followed by an END_REPLICATION_EVENT, and will stop the running of the simulator, moving the RunState and the ReplicatioNState both to the ENDED state. When the simulation time is not equal to or larger than the length of the replication, a logger warning is given, but the method is fully executed. In that case it does set the simulation time to the end time of the replication, to avoid restarting of the simulator.- Specified by:
endReplication
in interfaceSimulatorInterface<T extends Number & Comparable<T>>
- Overrides:
endReplication
in classSimulator<T extends Number & Comparable<T>>
-
isPauseOnError
Deprecated.DEPRECATED: Replaced by getErrorHandler() and getErrorLogLevel().- Specified by:
isPauseOnError
in interfaceDevsSimulatorInterface<T extends Number & Comparable<T>>
- Returns:
- pauseOnError whether we pause on an error or not.
-
setPauseOnError
Deprecated.DEPRECATED: Replaced by setErrorHandler() and setErrorLogLevel(). Set the boolean whether we pause on an error or not.- Specified by:
setPauseOnError
in interfaceDevsSimulatorInterface<T extends Number & Comparable<T>>
- Parameters:
pauseOnError
- boolean; set true or false.
-