Skip to content

openg2g.events

openg2g.events

Clock-aligned simulation event primitives.

SimEvent dataclass

Structured simulation event with canonical clock metadata.

Source code in openg2g/events.py
@dataclass(frozen=True)
class SimEvent:
    """Structured simulation event with canonical clock metadata."""

    tick: int
    t_s: float
    source: EventSource
    topic: str
    data: dict[str, Any] = field(default_factory=dict)

EventSink

Bases: Protocol

Receives simulation events from components.

Source code in openg2g/events.py
class EventSink(Protocol):
    """Receives simulation events from components."""

    def emit(self, event: SimEvent) -> None:
        """Consume one event."""

emit(event)

Consume one event.

Source code in openg2g/events.py
def emit(self, event: SimEvent) -> None:
    """Consume one event."""

EventEmitter dataclass

Source-bound event helper that stamps events with clock metadata.

Source code in openg2g/events.py
@dataclass
class EventEmitter:
    """Source-bound event helper that stamps events with clock metadata."""

    clock: SimulationClock
    sink: EventSink
    source: EventSource

    def emit(self, topic: str, data: dict[str, Any] | None = None) -> None:
        """Emit one event with current clock metadata."""
        t_s = float(self.clock.time_s)
        self.sink.emit(
            SimEvent(
                tick=int(self.clock.step),
                t_s=t_s,
                source=self.source,
                topic=str(topic),
                data={} if data is None else dict(data),
            )
        )

emit(topic, data=None)

Emit one event with current clock metadata.

Source code in openg2g/events.py
def emit(self, topic: str, data: dict[str, Any] | None = None) -> None:
    """Emit one event with current clock metadata."""
    t_s = float(self.clock.time_s)
    self.sink.emit(
        SimEvent(
            tick=int(self.clock.step),
            t_s=t_s,
            source=self.source,
            topic=str(topic),
            data={} if data is None else dict(data),
        )
    )