Event Buffer¶
maro.event_buffer.event_buffer¶
-
class
maro.event_buffer.event_buffer.EventBuffer(disable_finished_events: bool = False)[source]¶ Bases:
objectEventBuffer used to hold events, and dispatch them at specified tick.
Note
Different with normal event dispatcher, EventBuffer will stop executing and return following cascade events when it meet first pending cascade event, this may cause each tick is separated into several parts, users should check the return result before step to next tick.
And insert order will affect the processing order, so ensure the order when you need something strange.
- Parameters
disable_finished_events (bool) – Is disable the method to get finished event list, EventBuffer will recycle the finished events for furthure using, not push them into finished events list, so it will cause method “get_finished_events” return empty list.
-
execute(tick: int) → List[Union[maro.event_buffer.atom_event.AtomEvent, maro.event_buffer.cascade_event.CascadeEvent]][source]¶ Process and dispatch event by tick.
Note
The executing process will be stopped if there is any cascade event, and all following cascade events will be returned, so should check if the return list is empty before step to next tick.
- Parameters
tick (int) – Tick used to process events.
- Returns
A list of events that are pending decisions at the current tick.
- Return type
EventList
-
gen_action_event(tick: int, payload: object) → maro.event_buffer.cascade_event.CascadeEvent[source]¶ Generate an event that used to dispatch action to business engine.
- Parameters
tick (int) – Tick that the event will be processed.
payload (object) – Payload of event, used to pass data to handlers.
- Returns
Event object
- Return type
CascadeEvent
-
gen_atom_event(tick: int, event_type: object, payload: object = None) → maro.event_buffer.atom_event.AtomEvent[source]¶ Generate an atom event, an atom event is for normal usages, they will not stop current event dispatching process.
- Parameters
tick (int) – Tick that the event will be processed.
event_type (object) – Type of this event.
payload (object) – Payload of event, used to pass data to handlers.
- Returns
Atom event object
- Return type
AtomEvent
-
gen_cascade_event(tick: int, event_type: object, payload: object) → maro.event_buffer.cascade_event.CascadeEvent[source]¶ Generate an cascade event that used to hold immediate events that run right after current event.
- Parameters
tick (int) – Tick that the event will be processed.
event_type (object) – Type of this event.
payload (object) – Payload of event, used to pass data to handlers.
- Returns
Cascade event object.
- Return type
CascadeEvent
-
gen_decision_event(tick: int, payload: object) → maro.event_buffer.cascade_event.CascadeEvent[source]¶ Generate a decision event that will stop current simulation, and ask agent for action.
- Parameters
tick (int) – Tick that the event will be processed.
payload (object) – Payload of event, used to pass data to handlers.
- Returns
Event object
- Return type
CascadeEvent
-
get_finished_events() → List[Union[maro.event_buffer.atom_event.AtomEvent, maro.event_buffer.cascade_event.CascadeEvent]][source]¶ Get all the processed events, call this function before reset method.
- Returns
List of event object.
- Return type
EventList
-
get_pending_events(tick: int) → List[Union[maro.event_buffer.atom_event.AtomEvent, maro.event_buffer.cascade_event.CascadeEvent]][source]¶ Get pending event at specified tick.
- Parameters
Tick (int) – tick of events to get.
- Returns
List of event object.
- Return type
EventList
-
insert_event(event: Union[maro.event_buffer.atom_event.AtomEvent, maro.event_buffer.cascade_event.CascadeEvent])[source]¶ Insert an event to the pending queue.
- Parameters
event (Event) – Event to insert, usually get event object from get_atom_event or get_cascade_event.
-
register_event_handler(event_type: object, handler: Callable)[source]¶ Register an event with handler, when there is an event need to be processed, EventBuffer will invoke the handler if there are any event’s type match specified at each tick.
Note
Callback function should only hold one parameter that is the event object.
- Parameters
event_type (object) – Type of event that the handler want to process.
handler (Callable) – Handler that will process the event.