Event

BPTK API Documentation for the Event class
Keywords

agent-based modeling, abm, events, bptk, bptk-py, python, business prototyping

Event

Event Constructor

Event(name, sender_id, receiver_id, data=None)

Agents in BPTK do not call each other, they send each other events. An event carries a name, who sent it, who it is for, and optionally a payload.

The framework never looks inside data — it routes the event and hands it to the receiving agent’s handle_event, which is where the meaning lives. That is what keeps agents independent of each other: the sender needs to know a name and a receiver id, nothing about how the receiver works.

  • Parameters

    • name – String. The name of the event. The receiving agent dispatches on this, so it is the one part both sides have to agree on.

    • sender_id – Integer. The id of the agent sending the event. Use it to reply.

    • receiver_id – Integer. The id of the agent the event is addressed to.

    • data – Any (Default=None). The payload. Any Python object; in practice usually a dictionary. Not touched by the framework.

Attributes

Each constructor argument is available as an attribute of the same name on the event the receiver is handed:

Attribute
name The event name
sender_id The id of the sending agent
receiver_id The id of the receiving agent
data The payload, or None

Example

Sending and handling:

from BPTK_Py import Agent, Event


class Consumer(Agent):
    def initialize(self):
        self.agent_type = "consumer"
        self.state = "active"

    def act(self, time, round_no, step_no):
        self.model.enqueue_event(
            Event(
                name="order",
                sender_id=self.id,
                receiver_id=self.retailer_id,
                data={"quantity": 4},
            )
        )

    def handle_event(self, event):
        if event.name == "delivery":
            self.stock += event.data["quantity"]

To send an event that arrives some timesteps later rather than in the next round, use DelayedEvent.