negmas.events

Implements Event management

class negmas.events.Event(type: str, data: Any)[source]

Bases: object

An event that can be raised and consumed

data: Any[source]
type: str[source]
class negmas.events.EventLogger(file_name: str | Path, types: list[str] | None = None)[source]

Bases: EventSink

Logs 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]

On event.

Parameters:
  • event – Event.

  • sender – Sender.

  • python_class_identifier – Python class identifier.

reset_timer()[source]

Resets the internal timer used for timestamp calculation.

class negmas.events.EventSink[source]

Bases: object

An 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: object

An 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_event method that receives an event: Event and a sender: EventSource)

class negmas.events.Notifiable[source]

Bases: object

An 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: object

A notification message with a type identifier and associated data.

data: Any[source]
type: str[source]
class negmas.events.Notifier(name: str | None = None, *, id: str | None = None, type_name: str | None = None)[source]

Bases: NamedObject

An object that can notify other objects

notify(notifiable: Notifiable, notification: Notification)[source]

Notify.

Parameters:
  • notifiable – Notifiable.

  • notification – Notification.