EventBasedCounter
- class pydsol.core.statistics.EventBasedCounter(name: str)[source]
Bases:
EventProducer
,EventListener
,Counter
The EventBasedCounter can receive its observations by subscribing (listening) to one or more EventProducers that provides the values for the statistic using the EventProducer’s fire(…) method. This way, the statistic gathering and processing is decoupled from the process in the simulation that generates the data: there can be zero, one, or many statistics listeners for each data producing object in the simulation.
This event-based statistic object also fire events with the values of the calculated statistics values, so a GUI-element such as a graph or table can subscribe to this event-based statistics object and be automatically updated when values of the statistic change. Again, this provides decoupling and flexibility where on beforehand it is not known whether zero, one, or many (graphics or simulation) objects are interested in the values that this statistics object calculates.
The EventBasedCounter is a simple statistics object that can count events or occurrences. Note that the number of observations is not necessarily equal to the value of the counter, since the counter allows any integer as the increment (or decrement) during an observation.
The initialize() method resets the statistics object. The initialize method can, for instance, be called when the warmup period of the simulation experiment has completed.
Example
In simulation, a counter can be used to count arrivals, the number of processed entities in servers, the number of entities in the system, etc.
- Attributes:
_name (str) – the name by which the statistics object can be identified
_n (int) – the number of observations
_count (int) – the current value of the counter
- __init__(name: str)[source]
Construct a new EventBasedCounter statistics object. The EventBasedCounter can receive its observations by subscribing (listening) to one or more EventProducers that provides the values for the statistic using the EventProducer’s fire(…) method. This way, the statistic gathering and processing is decoupled from the process in the simulation that generates the data: there can be zero, one, or many statistics listeners for each data producing object in the simulation.
This event-based statistic object also fire events with the values of the calculated statistics values, so a GUI-element such as a graph or table can subscribe to this event-based statistics object and be automatically updated when values of the statistic change. Again, this provides decoupling and flexibility where on beforehand it is not known whether zero, one, or many (graphics or simulation) objects are interested in the values that this statistics object calculates.
The EventBasedCounter is a simple statistics object that can count events or occurrences. Note that the number of observations is not necessarily equal to the value of the counter, since the counter allows any integer as the increment (or decrement) during an observation.
- Parameters:
name (str) – The name by which the statistics object can be identified.
- Raises:
TypeError – when name is not a string
- initialize()[source]
Initialize the statistics object, resetting all values to the state where no observations have been made. This method can, for instance, be called when the warmup period of the simulation experiment has completed.
- notify(event: Event)[source]
The notify method is the method that is called by the EventProducer to register an observation. The EventType for the observation should be the StatEvents.DATA_EVENT and the payload should be a single integer. This value will be registered by the counter.
- Parameters:
event (Event) – The event fired by the EventProducer to provide data to the statistic. The event’s content should be a single int with the value.
- Raises:
TypeError – when event is not of the type Event
ValueError – when the event’s event_type is not a DATA_EVENT
ValueError – when the event’s payload is not an int
- register(value: int)[source]
The event-based classes still have a register method. This method is called by the notify method, but can also be called separately. The method processes one observation. The value indicates the increment or decrement of the counter (often 1). After processing, the method will fire updates to all listeners with the new values of the statistics.
- Parameters:
value (int) – The increment or decrement of the Counter.
- Raises:
TypeError – when value is not an int
- add_listener(event_type: EventType, listener: EventListener)
Add an EventListener to this EventProducer for a given EventType. If the listener already is registered for this EventType, this will be ignored.
- Parameters:
event_type (EventType) – the EventType for which this listener subscribes
listener (EventListener) – the subscriber to register for the provided Eventtype
- Raises:
EventError – if any of the arguments is of the wrong type
- count()
Return the current value of the counter statistic.
- Returns:
The current value of the counter statistic.
- Return type:
int
- has_listeners() bool
indicate whether this producer has any listeners or not
- n()
Return the number of observations.
- Returns:
The number of observations.
- Return type:
int
- property name
Return the name of this statistics object.
- Returns:
The name of this statistics object.
- Return type:
str
- remove_all_listeners(event_type: EventType | None = None, listener: EventListener | None = None)
Remove an EventListener (if given) for a provided EventType (if given) for this EventProducer. It is no problem if there are no matches. There are four situations:
- event_type == None and listener == None
all listeners for all event types are removed
- event_type == None and listener is specified
the listener is removed for any event for which it was registered
- event_type is specified and listener == None
all listeners are removed for the given event_type
- event_type and listener are both specified
the listener for the given event type is removed, if it was registered; in essence this is the same as remove_listener
- Parameters:
event_type (EventType, optional) – the EventType for which this listener unsubscribes
listener (EventListener, optional) – the subscriber to remove for the provided EventType
- Raises:
EventError – if any of the arguments is of the wrong type
- remove_listener(event_type: EventType, listener: EventListener)
Remove an EventListener of this EventProducer for a given EventType. If the listener is not registered for this EventType, this will be ignored.
- Parameters:
event_type (EventType) – the EventType for which this listener unsubscribes
listener (EventListener) – the subscriber to remove for the provided Eventtype
- Raises:
EventError – if any of the arguments is of the wrong type
Return a string representing a footer for a textual table with a monospaced font that can contain multiple counters.
- classmethod report_header() str
Return a string representing a header for a textual table with a monospaced font that can contain multiple counters.
- report_line() str
Return a string representing a line with important statistics values for this counter, for a textual table with a monospaced font that can contain multiple counters.