Class EngineEventContainer<T>
- Type Parameters:
T
- type of events
TimeSeries
with
associated events of the specified arbitrary type and schedules the
simulation events in the specified Engine
at instants defined by
these time series.
This class also provides API to handle these simulation events with reference to instances of event type.
Example of use case for this class. Suppose some simulation model needs to
simulate shifts for employees. Shift itself is a class, and there are 2
instances of this class - one for day shift and one for night shift. Shift
schedule is represented with TimeSeries
, for example day shift begins
every day at 8am, and night shift begins every day at 8pm.
In the model, an instance of EngineEventContainer
can be created with
2 pairs: new Pair<>(dayShift, dayShiftTimeSeries)
, and
new Pair<>(nightShift, nightShiftTimeSeries)
. The Engine
used to run the model must be passed to this event container.
As the result, the handlers will be called every time a shift begins, and the shift object will be passed as an argument to these handlers.
The full code of the example above can look like the following:
Engine engine = ...;
Shift dayShift = new Shift("Day shift", 11 * hour());
Shift nightShift = new Shift("Night shift", 11 * hour());
TimeSeries dayShiftTimeSeries = TimeSeries.everyNDaysTimeSeries(LocalTime.of(8, 0), 1, engine);
TimeSeries nightShiftTimeSeries = TimeSeries.everyNDaysTimeSeries(LocalTime.of(20, 0), 1, engine);
EngineEventContainer container = new EngineEventContainer<>(engine, List.of(new Pair<>(dayShift, dayShiftTimeSeries), new Pair<>(nightShift, nightShiftTimeSeries)), Collections.emptyList());
container.addHandler(shift -> System.out.println("Shift has started: " + shift));
...
container.initialize();
- Author:
- Andrey Malykhanov
-
Constructor Summary
ConstructorsConstructorDescriptionEngineEventContainer
(Engine engine) Creates an instance of engine event container with no time series and thus no events.EngineEventContainer
(Engine engine, int eventsOrder) Creates an instance of engine event container with no time series and thus no events.EngineEventContainer
(Engine engine, int eventsOrder, List<Pair<T, TimeSeries>> events, List<Consumer<T>> handlers) Creates an instance of engine event container with the specified list of (Event, TimeSeries) pairs and the specified list of event handlers.EngineEventContainer
(Engine engine, List<Pair<T, TimeSeries>> events, List<Consumer<T>> handlers) Creates an instance of engine event container with the specified list of (Event, TimeSeries) pairs and the specified list of event handlers. -
Method Summary
Modifier and TypeMethodDescriptionaddEvent
(T event, TimeSeries timeSeries) addEvents
(List<Pair<T, TimeSeries>> events) addHandler
(Consumer<T> handler) Adds a handler of events.void
Starts scheduling events in theEngine
associated with this event container.boolean
Methods inherited from class com.amalgamasimulation.utils.time.EventContainer
getEvents, getEvents, getEvents, getNextEvents, getPreviousEvents
-
Constructor Details
-
EngineEventContainer
Creates an instance of engine event container with no time series and thus no events. Time series can be added later by callingaddEvent(Object, TimeSeries)
oraddEvents(List)
methods.- Parameters:
engine
-Engine
that will be used to schedule the events
-
EngineEventContainer
Creates an instance of engine event container with no time series and thus no events. Time series can be added later by callingaddEvent(Object, TimeSeries)
oraddEvents(List)
methods.- Parameters:
engine
-Engine
that will be used to schedule the eventseventsOrder
- priority that all events scheduled in the engine will have among other events scheduled for the same time. For explanation of events ordering, see {#linkEngine.scheduleAbsolute(double, int, Runnable)
-
EngineEventContainer
public EngineEventContainer(Engine engine, List<Pair<T, TimeSeries>> events, List<Consumer<T>> handlers) Creates an instance of engine event container with the specified list of (Event, TimeSeries) pairs and the specified list of event handlers.- Parameters:
engine
-Engine
that will be used to schedule the eventsevents
- specified list of (Event, TimeSeries) pairshandlers
- specified list of event handlers
-
EngineEventContainer
public EngineEventContainer(Engine engine, int eventsOrder, List<Pair<T, TimeSeries>> events, List<Consumer<T>> handlers) Creates an instance of engine event container with the specified list of (Event, TimeSeries) pairs and the specified list of event handlers.- Parameters:
engine
-Engine
that will be used to schedule the eventseventsOrder
- priority that all events scheduled in the engine will have among other events scheduled for the same time. For explanation of events ordering, see {#linkEngine.scheduleAbsolute(double, int, Runnable)
events
- specified list of (Event, TimeSeries) pairshandlers
- specified list of event handlers
-
-
Method Details
-
addHandler
Adds a handler of events.- Parameters:
handler
- handler that is called when event occurs. The event instance is passed as an argument of the handler- Returns:
- reference to instance of this event container, for support of builder pattern
-
initialize
public void initialize()Starts scheduling events in theEngine
associated with this event container. Should be called after adding of all events and handlers is complete and before start of running the model. -
isInitialized
public boolean isInitialized()- Returns:
true
ifinitialize()
method has already been called for this instance,false
otherwise
-
addEvent
- Overrides:
addEvent
in classEventContainer<T>
-
addEvents
- Overrides:
addEvents
in classEventContainer<T>
-