Class EngineEventContainer<T>

java.lang.Object
com.amalgamasimulation.utils.time.EventContainer<T>
com.amalgamasimulation.engine.EngineEventContainer<T>
Type Parameters:
T - type of events

public class EngineEventContainer<T> extends EventContainer<T>
Class that takes one or several instances of 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 Details

    • EngineEventContainer

      public EngineEventContainer(Engine engine)
      Creates an instance of engine event container with no time series and thus no events. Time series can be added later by calling addEvent(Object, TimeSeries) or addEvents(List) methods.
      Parameters:
      engine - Engine that will be used to schedule the events
    • EngineEventContainer

      public EngineEventContainer(Engine engine, int eventsOrder)
      Creates an instance of engine event container with no time series and thus no events. Time series can be added later by calling addEvent(Object, TimeSeries) or addEvents(List) methods.
      Parameters:
      engine - Engine that will be used to schedule the events
      eventsOrder - priority that all events scheduled in the engine will have among other events scheduled for the same time. For explanation of events ordering, see {#link Engine.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 events
      events - specified list of (Event, TimeSeries) pairs
      handlers - 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 events
      eventsOrder - priority that all events scheduled in the engine will have among other events scheduled for the same time. For explanation of events ordering, see {#link Engine.scheduleAbsolute(double, int, Runnable)
      events - specified list of (Event, TimeSeries) pairs
      handlers - specified list of event handlers
  • Method Details

    • addHandler

      public EngineEventContainer<T> addHandler(Consumer<T> handler)
      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 the Engine 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 if initialize() method has already been called for this instance, false otherwise.
      Returns:
      true if initialize() method has already been called for this instance, false otherwise
    • addEvent

      public EventContainer<T> addEvent(T event, TimeSeries timeSeries)
      Overrides:
      addEvent in class EventContainer<T>
    • addEvents

      public EventContainer<T> addEvents(List<Pair<T,TimeSeries>> events)
      Overrides:
      addEvents in class EventContainer<T>