Package microsim.event
Class EventQueue
java.lang.Object
microsim.event.EventQueue
The
EventQueue
class manages a time ordered queue of events. It is based on a priority queue. At every
simulation step the head of the queue is taken and fired. This class extends a thread, because it runs independently
of other processes. When activated it runs the simulation.-
Field Summary
Modifier and TypeFieldDescriptionstatic final int
The action type passed to step listeners. -
Constructor Summary
ConstructorDescriptionBuilds a new event queue of size 10.EventQueue
(@NonNull EventQueue previousEventQueue) Builds a new event queue inheriting parameters from another EventQueue. -
Method Summary
Modifier and TypeMethodDescriptionvoid
clear()
Empties this object and sets time to0
for a new simulation.@NonNull Event[]
Converts the event queue to an array.protected void
scheduleEvent
(@NonNull Event event) Adds an event to the queue.scheduleOnce
(@NonNull Event event, double atTime, int withOrdering) Schedules a generic event to occur at a given time.scheduleRepeat
(@NonNull Event event, double atTime, int withOrdering, double timeBetweenEvents) Schedules a generic looped event at a given time and ordering.scheduleSystem
(double atTime, int withOrdering, double withLoop, @NonNull SimulationEngine engine, @NonNull SystemEventType type) Schedules a looped system event.void
simulate()
Runs an entire simulation.void
step()
Makes one simulation step forward.void
unschedule
(@NonNull Event event) Removes a certain event from the queue.
-
Field Details
-
EVENT_LIST_STEP
public static final int EVENT_LIST_STEPThe action type passed to step listeners. Theint
value is 10000.- See Also:
-
eventQueue
-
-
Constructor Details
-
EventQueue
public EventQueue()Builds a new event queue of size 10. -
EventQueue
Builds a new event queue inheriting parameters from another EventQueue.- Parameters:
previousEventQueue
- AnEventQueue
to copy.- Throws:
NullPointerException
- whenpreviousEventQueue
isnull
.
-
-
Method Details
-
clear
public void clear()Empties this object and sets time to0
for a new simulation. -
step
Makes one simulation step forward. Does nothing when the queue is empty.- Throws:
SimulationException
- when fails to invoke an event.
-
simulate
Runs an entire simulation. If model does not stop itself during the process of simulation, it will be stopped automatically at timeout time.- Throws:
SimulationException
- whenstep()
fails to progress.
-
scheduleEvent
Adds an event to the queue.- Parameters:
event
- AnEvent
object.- Throws:
NullPointerException
- whenevent
isnull
.
-
scheduleOnce
Schedules a generic event to occur at a given time.- Parameters:
atTime
- The time when event will be fired.withOrdering
- The order that the event will be fired: for two eventse1
ande2
scheduled to occur at the same timee1.time == e2.time
, ife1.ordering < e2.ordering
, thene1
will be fired first. Ife1.time == e2.time AND e1.ordering == e2.ordering
, the first event that was scheduled (added to theEventQueue
) will be fired first.- Throws:
NullPointerException
- whenevent
isnull
.
-
scheduleRepeat
public EventQueue scheduleRepeat(@NonNull @NonNull Event event, double atTime, int withOrdering, double timeBetweenEvents) Schedules a generic looped event at a given time and ordering.- Parameters:
atTime
- The time when event will be fired for the first time.withOrdering
- The order that the event will be fired: for two eventse1
ande2
scheduled to occur at the same timee1.time == e2.time
, ife1.ordering < e2.ordering
, thene1
will be fired first. Ife1.time == e2.time AND e1.ordering == e2.ordering
, the first event that was scheduled (added to theEventQueue
) will be fired first.timeBetweenEvents
- The time period between repeated firing of the event. If this parameter is set to 0, this event will not be fired more than once.- Throws:
NullPointerException
- whenevent
isnull
.
-
unschedule
Removes a certain event from the queue.- Parameters:
event
- An event to remove.- Throws:
NullPointerException
- whenevent
isnull
.
-
scheduleSystem
public SystemEvent scheduleSystem(double atTime, int withOrdering, double withLoop, @NonNull @NonNull SimulationEngine engine, @NonNull @NonNull SystemEventType type) Schedules a looped system event.- Parameters:
atTime
- The time when event will be fired for the first time.withOrdering
- The order that the event will be fired: for two eventse1
ande2
scheduled to occur at the same timee1.time == e2.time
, ife1.ordering < e2.ordering
, thene1
will be fired first. Ife1.time == e2.time AND e1.ordering == e2.ordering
, the first event that was scheduled (added to theEventQueue
) will be fired first.withLoop
- The time period between repeated firing of the event. If this parameter is set to 0, this event will not be fired more than once.engine
- ASimulationEngine
object to use.type
- ASystemEventType
to use.- Returns:
- a new scheduled
SystemEvent
. - Throws:
NullPointerException
- when any of the input parameters isnull
.
-
getEventArray
Converts the event queue to an array.- Returns:
- an
Event
array.
-