negmas.events¶
Implements Event management
- class negmas.events.Event(type: str, data: Any)[source]¶
Bases:
objectAn event that can be raised and consumed
- class negmas.events.EventLogger(file_name: str | Path, types: list[str] | None = None)[source]¶
Bases:
EventSinkLogs events to a file
- Parameters:
file_name – Name of the file to save events into
types – The types of events to save. If None, all events will be saved
- on_event(event: Event, sender: EventSource, python_class_identifier='__python_class__')[source]¶
Handle an event by logging it to the configured file.
- Parameters:
event – The event to log.
sender – The source object that emitted this event.
python_class_identifier – Key for class type identification during serialization.
- class negmas.events.EventSink[source]¶
Bases:
objectAn object capable of receiving events
- on_event(event: Event, sender: EventSource)[source]¶
Called when an event is received from a source.
- Parameters:
event – The event that was raised.
sender – The object that raised the event.
- class negmas.events.EventSource[source]¶
Bases:
objectAn object capable of raising events
- announce(event: Event)[source]¶
Raises an event and informs all event sinks that are registered for notifications on this event type
- register_listener(event_type: str | None, listener: EventSink)[source]¶
Registers a listener for the given event_type.
- Parameters:
event_type – The type to register. If None, the listener will be registered for all types
listener – The listening agent (must have an
on_eventmethod that receives an event:Eventand a sender:EventSource)
- class negmas.events.Notifiable[source]¶
Bases:
objectAn object that can be notified
- add_handler(notification_type: str, callback: Callable[[Notification, str], bool])[source]¶
Adds a notification handler to the list of handlers of the given type. These handlers will be called in the order in which they are received
- Parameters:
notification_type – Notification type as specificed in the type member of the Notification class
callback – The callback which must receive a Notification object and a string and returns a boolean. If True is returned from one callback, the remaining callbacks will not be called
Returns:
- handlers(notification_type: str) list[Callable[[Notification, str], bool]][source]¶
Gets the list of handlers registered for some notification type. This list can be modified in place to change the order of handlers for example. It is NOT a copy.
- on_notification(notification: Notification, notifier: str) None[source]¶
Called when a notification is received and is not handled by any registered handler
- Parameters:
notification – The notification received
notifier – The notifier ID
Remarks:
override this method to provide a catch-all notification handling method.
- on_notification_(notification: Notification, notifier: str) bool[source]¶
Called when a notification is received. Do NOT directly override this method
- Parameters:
notification
notifier
Returns:
- remove_handler(notification_type: str, callback: Callable[[Notification, str], bool]) bool[source]¶
Removes a notification handler from the list of handlers of the given type.
- Parameters:
notification_type – Notification type as specificed in the type member of the Notification class
callback – The callback which must receive a Notification object and a string and returns a boolean. If True is returned from one callback, the remaining callbacks will not be called
- Returns:
Whether or not the handler was in the list of handlers for this type. In all cases, the handler will not be called after this call (either it was not there or it will be removed).
- class negmas.events.Notification(type: str, data: Any)[source]¶
Bases:
objectA notification message with a type identifier and associated data.
- class negmas.events.Notifier(name: str | None = None, *, id: str | None = None, type_name: str | None = None)[source]¶
Bases:
NamedObjectAn object that can notify other objects
- notify(notifiable: Notifiable, notification: Notification)[source]¶
Send a notification to a notifiable object.
- Parameters:
notifiable – The object to be notified.
notification – The notification data to send.