Class Engine
- All Implemented Interfaces:
Timeable
The engine can be run both synchronously and asynchronously. Synchronous execution can be used for running models fast in background without visualization. Asynchronous execution is typically used for running models with visualization.
- Author:
- Andrey Malykhanov
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionvoid
addStateChangedHandler
(Runnable handler) Adds the specified handler of this engine state change event.Returns the begin date, i.e.boolean
Returns the value of exceptions catching flag.date()
Returns the current simulation date.int
Returns the number of events that are scheduled but not yet executedIf this engine is running, executes the action at current simulation time with the specified sequence.If this engine is running, executes the action at current simulation time with maximum priority.long
Returns the number of events completely executed by the engine since its creation or last resetboolean
fastMode()
Checks whether fast mode flag is setReturns the unmodifiable list of events that are scheduled but not yet executeddouble
Returns the ratio of events execution time and total run time, i.e.Returns the unmodifiable list of exceptions thrown in events action code during simulation run.double
Returns the total duration of execution of all simulation events since last engine start, in millisecondsdouble
Returns the total duration of run since last engine start, in milliseconds.boolean
Checks whether this engine is currently runningboolean
Checks if this engine is running in fast mode.void
reset()
Resets the simulation engine, sets its time to zero.void
run()
Starts or resumes running the engine asynchronously and returns immediately.void
run
(boolean blockUntilStopped) Starts or resumes running the engine either synchronously or asynchronously, depending on the value of the argument.scheduleAbsolute
(double absoluteTime, int sequence, Runnable runnable) Schedules an event for execution at the specified absolute time with the specified priorityscheduleAbsolute
(double absoluteTime, Runnable runnable) Schedules an event for execution at the specified absolute time with the priority equal to 0scheduleRelative
(double timeout, int sequence, Runnable runnable) Schedules an event for execution in the specified timeout from the current simulation time with the specified priority.scheduleRelative
(double timeout, Runnable runnable) Schedules an event for execution in the specified timeout from the current simulation time with the priority equal to 0.scheduleStop
(double absoluteTime, int sequence, String description) Schedules a stoppage of this engine at the specified absolute time with the specified priorityscheduleStop
(double absoluteTime, String description) Schedules a stoppage of this engine at the specified absolute time with the priority equal to 0void
setCatchExceptions
(boolean catchExceptions) Sets the value of exceptions catching flag.void
setFastMode
(boolean fastMode) Switches between the fast and normal modes of events execution.void
setTemporal
(LocalDateTime beginDate, ChronoUnit timeUnit) Sets the temporal parameters of the engine.void
setTimeScale
(double timeScale) Sets the new simulation time scale.void
setTraceEvents
(boolean traceEvents) Sets the value of event tracing flag.void
stop()
Stops running this engine.double
time()
Returns the current simulation timedouble
Returns current simulation time scale, i.e.timeUnit()
Returns the simulation time unit, i.e.boolean
Returns the value of event tracing flag.void
Enqueues the request for a visualization procedure.Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
Methods inherited from interface com.amalgamasimulation.utils.time.Timeable
dateToTime, dateToTime, day, duration, hour, minute, second, timeToDate, week, wholeDays, wholeDays, wholeHours, wholeHours, wholeUnits
-
Constructor Details
-
Engine
public Engine()Creates a new instance of the simulation engine
-
-
Method Details
-
reset
public void reset()Resets the simulation engine, sets its time to zero. If this engine is running, stops the engine before resetting. -
setTemporal
Sets the temporal parameters of the engine. The temporal parameters are used to map model time to model date and backwards. Such conversion is made byTimeable.dateToTime(LocalDateTime)
andTimeable.timeToDate(double)
methods.Conceptually, simulation calendar date is calculated with the following formula:
It is impossible to set the temporal parameters if the engine is running or is in the middle of simulation run, i.e. whendate()
==beginDate()
+time()
*timeUnit()
time()
> 0. In these cases, aIllegalStateException
will be thrown- Parameters:
beginDate
- date that corresponds to zero simulation timetimeUnit
- time unit that corresponds to 1 simulation time unit- See Also:
-
run
public void run()Starts or resumes running the engine asynchronously and returns immediately. Does nothing if this engine is already running -
run
public void run(boolean blockUntilStopped) Starts or resumes running the engine either synchronously or asynchronously, depending on the value of the argument.If blockUntilStopped = false, the behavior of the method is the same as
run()
, i.e. it starts running the engine asynchronously and returns immediatelyIf blockUntilStopped = true, starts running the model synchronously and returns after this engine is stopped first time e.g. by calling
stop()
method from inside some simulation event.- Parameters:
blockUntilStopped
- whether or not to block until the simulation is stopped
-
addStateChangedHandler
Adds the specified handler of this engine state change event. Such events include:- stopping this engine if it was running,
- starting this engine if it was stopped,
- changing the time scale of this engine,
- switching between fast and normal modes.
- Parameters:
handler
- action to be executed in the case of engine state change
-
setFastMode
public void setFastMode(boolean fastMode) Switches between the fast and normal modes of events execution. In fast mode, the events are executed one after another without any real time scale. If this engine is running, the change applies immediately. Otherwise, the change applies only after engine starts running- Parameters:
fastMode
- whether to switch on or off the fast mode
-
isRunningFast
public boolean isRunningFast()Checks if this engine is running in fast mode. If this engine is not running, returnsfalse
- Returns:
true
if this engine is running in fast mode,false
otherwise
-
isRunning
public boolean isRunning()Checks whether this engine is currently running- Returns:
true
if this engine is running,false
otherwise
-
stop
public void stop()Stops running this engine. Does nothing if this engine is currently not running -
visualize
Enqueues the request for a visualization procedure. The actual time of performing this visualization procedure is determined by this engine internally.- Parameters:
target
- some object which the visualization procedure is associated with. Must be specified to prevent calling several visualization procedures for the same target unnecessarilyprocedure
- visualization procedure to be executed
-
fastMode
public boolean fastMode()Checks whether fast mode flag is set- Returns:
true
if fast mode flag is set,false
otherwise
-
setTimeScale
public void setTimeScale(double timeScale) Sets the new simulation time scale.If this engine is running in normal (not fast) mode, the time scale change takes effect immediately. If this engine is running in fast mode, there is no immediate effect of this call, but the change may be applied after fast mode is switched off.
If this engine is not running, the change will take effect after the engine starts running in normal mode.
If zero or negative time scale is passed, throws a
RuntimeException
- Parameters:
timeScale
- new time scale
-
scheduleStop
Schedules a stoppage of this engine at the specified absolute time with the specified priority- Parameters:
absoluteTime
- absolute time, i.e. simulation time passed since start of this enginesequence
- priority among other events scheduled for the same time. The less this number, the more the priority of execution. Integer.MIN_VALUE corresponds to maximum prioritydescription
- description of the stop event- Returns:
- event that was created to execute the engine stoppage
-
scheduleStop
Schedules a stoppage of this engine at the specified absolute time with the priority equal to 0- Parameters:
absoluteTime
- absolute time, i.e. simulation time passed since start of this enginedescription
- description of the stop event- Returns:
- event that was created to execute the engine stoppage
-
scheduleAbsolute
Schedules an event for execution at the specified absolute time with the specified priority- Parameters:
absoluteTime
- absolute time, i.e. simulation time passed since start of this enginesequence
- priority among other events scheduled for the same time. The less this number, the more the priority of execution. Integer.MIN_VALUE corresponds to maximum priorityrunnable
- action to be executed- Returns:
- event that was created to execute the specified action
-
scheduleAbsolute
Schedules an event for execution at the specified absolute time with the priority equal to 0- Parameters:
absoluteTime
- absolute time, i.e. simulation time passed since start of this enginerunnable
- action to be executed- Returns:
- event that was created to execute the specified action
-
scheduleRelative
Schedules an event for execution in the specified timeout from the current simulation time with the specified priority.Zero timeout is allowed, negative timeouts are not allowed and will result in throwing a
RuntimeException
- Parameters:
timeout
- timeout from the current simulation time in which the specified action will be executedsequence
- priority among other events scheduled for the same time. The less this number, the more the priority of execution. Integer.MIN_VALUE corresponds to maximum priorityrunnable
- action to be executed- Returns:
- that was created to execute the specified action
-
scheduleRelative
Schedules an event for execution in the specified timeout from the current simulation time with the priority equal to 0.Zero timeout is allowed, negative timeouts are not allowed and will result in throwing a
RuntimeException
- Parameters:
timeout
- timeout from the current simulation time in which the specified action will be executedrunnable
- action to be executed- Returns:
- that was created to execute the specified action
-
execute
If this engine is running, executes the action at current simulation time with the specified sequence. If the engine is not running, schedules execution of action at current time. The less the sequence number, the more priority of action's execution will be- Parameters:
sequence
- sequence number. Integer.MIN_VALUE corresponds to maximum execution priority among all events and actions scheduled at current simulation timerunnable
- action to be executed- Returns:
- event that was created to execute the specified action
-
execute
If this engine is running, executes the action at current simulation time with maximum priority. If the engine is not running, schedules execution of action at current time- Parameters:
runnable
- action to be executed- Returns:
- event that was created to execute the specified action
-
timeScale
public double timeScale()Returns current simulation time scale, i.e. number of simulation time units that engine attempts to simulate in 1 second of real time- Returns:
- current simulation time scale
- See Also:
-
eventsCount
public int eventsCount()Returns the number of events that are scheduled but not yet executed- Returns:
- number of events and actions that are scheduled but not yet executed
-
executedEventsCount
public long executedEventsCount()Returns the number of events completely executed by the engine since its creation or last reset- Returns:
- number of executed events
-
getEvents
Returns the unmodifiable list of events that are scheduled but not yet executed- Returns:
- unmodifiable list of events that are scheduled but not yet executed
-
time
public double time()Returns the current simulation time- Returns:
- current simulation time
-
getTotalRunTimeMillis
public double getTotalRunTimeMillis()Returns the total duration of run since last engine start, in milliseconds. The duration excludes the time the execution was stopped- Returns:
- total duration of run since last engine start, in milliseconds
-
getTotalEventsExecutionTimeMillis
public double getTotalEventsExecutionTimeMillis()Returns the total duration of execution of all simulation events since last engine start, in milliseconds- Returns:
- total duration of execution of all simulation events since last engine start, in milliseconds
-
getEventsExecutionTimeFraction
public double getEventsExecutionTimeFraction()Returns the ratio of events execution time and total run time, i.e.getTotalEventsExecutionTimeMillis()
/getTotalRunTimeMillis()
. The result is calculated since last engine start- Returns:
- ratio of events execution time and total run time
-
getExceptions
Returns the unmodifiable list of exceptions thrown in events action code during simulation run. The exceptions are being caught and collected inside this engine if exceptions catching flag is set totrue
usingsetCatchExceptions(boolean)
method.- Returns:
- unmodifiable list of exceptions
-
date
Returns the current simulation date. The date is calculated using current simulation time and temporal parameters, i.e. begin date and time unit.- Returns:
- current simulation date
- See Also:
-
setTraceEvents
public void setTraceEvents(boolean traceEvents) Sets the value of event tracing flag. If event tracing flag is set totrue
, the engine creates all events as traceable events. Traceable events remember stack trace of the point in code from where it was scheduledTracing events can be useful for debugging because it is sometimes useful to understand where a problematic event was scheduled from. It is not recommended to use event tracing in productive models because calculating and storing stack trace of each event requires significant computational resources
- Parameters:
traceEvents
- value of event tracing flag- See Also:
-
traceEvents
public boolean traceEvents()Returns the value of event tracing flag. If event tracing flag is set totrue
, the engine creates all events as traceable events. Traceable events remember stack trace of the point in code from where it was scheduledTracing events can be useful for debugging because it is sometimes useful to understand where a problematic event was scheduled from. It is not recommended to use event tracing in productive models because calculating and storing stack trace of each event requires significant computational resources
- Returns:
- value of event tracing flag
- See Also:
-
catchExceptions
public boolean catchExceptions()Returns the value of exceptions catching flag. If the flag is set totrue
, all exceptions thrown inside event actions will be caught and collected inside this engine. They can be retrieved usinggetExceptions()
method- Returns:
- value of exceptions catching flag
-
setCatchExceptions
public void setCatchExceptions(boolean catchExceptions) Sets the value of exceptions catching flag. If the flag is set totrue
, all exceptions thrown inside event actions will be caught and collected inside this engine. They can be retrieved usinggetExceptions()
method -
beginDate
Description copied from interface:Timeable
Returns the begin date, i.e. the simulation date that corresponds to zero simulation time -
timeUnit
Description copied from interface:Timeable
Returns the simulation time unit, i.e. period unit that corresponds to one unit of simulation time
-