engine.batch_dispatcher ======================= .. py:module:: engine.batch_dispatcher .. autoapi-nested-parse:: Batching RDFLib store events for fixed-point reasoner execution. Attributes ---------- .. autoapisummary:: engine.batch_dispatcher.logger Classes ------- .. autoapisummary:: engine.batch_dispatcher.BatchEvent engine.batch_dispatcher.TripleAddedBatchEvent engine.batch_dispatcher.TripleRemovedBatchEvent engine.batch_dispatcher.BatchDispatcher Module Contents --------------- .. py:data:: logger .. py:class:: BatchEvent(events: set[rdflib_reasoning.axiom.common.Triple], context_id: rdflib_reasoning.axiom.common.ContextIdentifier) Bases: :py:obj:`rdflib.events.Event` Base batch event carrying deduplicated triples for one graph context. .. py:attribute:: events :type: set[rdflib_reasoning.axiom.common.Triple] .. py:attribute:: context_id :type: rdflib_reasoning.axiom.common.ContextIdentifier .. py:class:: TripleAddedBatchEvent(events: set[rdflib_reasoning.axiom.common.Triple], context_id: rdflib_reasoning.axiom.common.ContextIdentifier) Bases: :py:obj:`BatchEvent` Batch of triple additions delivered after fixed-point coalescing. .. py:class:: TripleRemovedBatchEvent(events: set[rdflib_reasoning.axiom.common.Triple], context_id: rdflib_reasoning.axiom.common.ContextIdentifier) Bases: :py:obj:`BatchEvent` Batch of triple removals delivered after fixed-point coalescing. .. py:class:: BatchDispatcher(*, source_dispatcher: rdflib.events.Dispatcher, backing_store: rdflib.store.Store) Bases: :py:obj:`rdflib.events.Dispatcher` A dispatcher that batches store events and drives fixed-point iteration. If a listener to a ``rdflib.Store`` is sensitive to reentrant event handlers, this dispatcher allows for batches of events to be handled without re-entrancy. Additionally, this dispatcher forwards ``TripleAddedEvent`` and ``TripleRemovedEvent`` events after deduplication; subscribers can assume that each event will be unique. **Source-dispatcher contract (per DR-026)** The batch dispatcher subscribes its ``_on_triple_added`` and ``_on_triple_removed`` handlers to a caller-supplied ``source_dispatcher`` rather than to the backing store's dispatcher. The owner of the source dispatcher (in this codebase, ``RETEStore`` via its private ``_raw_dispatcher``) MUST emit one ``TripleAddedEvent`` (respectively ``TripleRemovedEvent``) per mutation, BEFORE performing the mutation, even when the triple is already present or absent. Pre-mutation semantics are required so that the dedup check ``_exists_in_store`` correctly reports "already present" for adds and "still present" for removes. The backing store reference is retained solely for the read-only ``_exists_in_store`` dedup check, which uses the standard ``Store.contexts(triple)`` API. The backing store's own dispatcher is not consumed; events emitted there (for example by ``Memory.add`` via ``Store.add(self, ...)``) are ignored by the engine. **Fixed-point procedure** 1. If a ``TripleAddedEvent`` or ``TripleRemovedEvent`` is received, it is added to the current batch for its context. 2. If there are a nonzero number of batches for the context, the batch is popped and dispatched. 3. During dispatch, any additional events received for that context are added to the next batch for that context. 4. When the dispatch ends, we return to step 2. **Inference engine entrypoint** The entrypoint for the inference engine (e.g. RETE) is subscription to ``TripleAddedBatchEvent`` (and optionally ``TripleRemovedBatchEvent``). Subscribers receive per-context batches and fixed point is achieved by the dispatcher's loop. Reasoners SHOULD subscribe to these batch events and handle the batches accordingly (e.g. materialize derived triples). .. py:attribute:: backing_store :type: rdflib.store.Store