engine.rete_engine ================== .. py:module:: engine.rete_engine .. autoapi-nested-parse:: RETE engine facade for per-context triple entailment and truth maintenance. Attributes ---------- .. autoapisummary:: engine.rete_engine.LiteralSubjectPolicy engine.rete_engine.PredicateTermPolicy Classes ------- .. autoapisummary:: engine.rete_engine.EngineCallbackContext engine.rete_engine.RETEEngineConfig engine.rete_engine.RETEEngine Module Contents --------------- .. py:data:: LiteralSubjectPolicy .. py:data:: PredicateTermPolicy .. py:class:: EngineCallbackContext Bases: :py:obj:`engine.builtins.RuleContext` Read-only callback context for one fired rule activation. .. py:attribute:: context :type: rdflib_reasoning.axiom.common.ContextIdentifier | None .. py:attribute:: rule_id :type: Any .. py:attribute:: bindings :type: collections.abc.Mapping[str, rdflib.term.Node] .. py:attribute:: premises :type: tuple[rdflib_reasoning.axiom.common.Triple, Ellipsis] .. py:attribute:: depth :type: int .. py:method:: record(event: Any) -> None Append one observational callback event to this activation context. .. py:class:: RETEEngineConfig Explicit configuration for one RETE engine context. .. py:attribute:: context :type: rdflib_reasoning.axiom.common.ContextIdentifier .. py:attribute:: builtins :type: engine.builtins.Builtins .. py:attribute:: derivation_logger :type: engine.derivation.DerivationLogger .. py:attribute:: contradiction_recorder :type: engine.derivation.ContradictionRecorder .. py:attribute:: literal_subject_policy :type: LiteralSubjectPolicy .. py:attribute:: predicate_term_policy :type: PredicateTermPolicy .. py:class:: RETEEngine(config: RETEEngineConfig, rules: collections.abc.Iterable[engine.rules.Rule]) Public engine facade for fixed-point triple entailment within one context. The facade represents the direct implementation boundary: triple-oriented rule execution, dependency-aware retraction, optional derivation logging, and warm-start over an existing context. User-facing proof reconstruction is layered on top of derivation records rather than being the engine's execution format. The engine also enforces RDF 1.1 triple well-formedness for both stated and inferred triples. Literal subjects and non-IRI predicates never enter working memory or derived outputs. Callers can choose fail-fast or warning-and-skip behavior via ``literal_subject_policy`` and ``predicate_term_policy`` in the engine config. .. py:attribute:: config :type: RETEEngineConfig .. py:attribute:: rules :type: collections.abc.Sequence[engine.rules.Rule] .. py:attribute:: terminals :type: tuple[engine.rete.TerminalNode, Ellipsis] .. py:attribute:: known_triples :type: set[rdflib_reasoning.axiom.common.Triple] .. py:attribute:: materialized_triples :type: set[rdflib_reasoning.axiom.common.Triple] .. py:attribute:: matcher :type: engine.rete.NetworkMatcher .. py:attribute:: derivation_logger :type: engine.derivation.DerivationLogger .. py:attribute:: contradiction_recorder :type: engine.derivation.ContradictionRecorder .. py:attribute:: callbacks :type: collections.abc.Mapping[str, engine.builtins.CallbackHook] .. py:attribute:: tms :type: engine.rete.TMSController .. py:attribute:: working_memory :type: engine.rete.WorkingMemory .. py:attribute:: literal_subject_policy :type: LiteralSubjectPolicy .. py:attribute:: predicate_term_policy :type: PredicateTermPolicy .. py:method:: close() -> None Clear working memory, tracked triples, and attached recorders for this context. .. py:method:: next_contradiction_sequence() -> int Return the next monotonic contradiction sequence id. .. py:method:: contradiction_records(*, context: rdflib_reasoning.axiom.common.ContextIdentifier | None = None) -> tuple[engine.proof.ContradictionRecord, Ellipsis] Return contradiction records, optionally filtered by context. .. py:method:: clear_contradiction_records() -> None Clear contradiction records and reset sequence numbering. .. py:method:: add_triples(triples: collections.abc.Iterable[rdflib_reasoning.axiom.common.Triple]) -> set[rdflib_reasoning.axiom.common.Triple] Add stated triples, saturate inference, and return newly derived triples. Incoming triples are first checked against the engine's RDF 1.1 triple well-formedness policies. Triples rejected under those policies are either raised as errors or warned-and-skipped before they can enter working memory or derivation outputs. Per DR-004, this method is idempotent for triples the engine already knows. Triples already present in ``known_triples`` are skipped before ``register_stated`` runs so that materialization round-trips through the store dispatcher do not retroactively promote derived facts to stated facts. .. py:method:: retract_triples(triples: collections.abc.Iterable[rdflib_reasoning.axiom.common.Triple]) -> set[rdflib_reasoning.axiom.common.Triple] Retract triples; return all triples that left the logical closure. For each input triple the engine looks up the working-memory fact: - If the fact is absent, the input is skipped (idempotent for already-absent triples; a second call with the same input is a no-op). - If the fact is stated, the engine delegates to ``TMSController.retract_triple`` and consumes the resulting ``RetractionOutcome``. Stated facts that also have derived justifications have only the ``stated`` flag cleared; they remain in working memory and are NOT included in the returned set. - If the fact is derived only, the engine leaves working memory unchanged and does NOT include the triple in the returned set. Callers that received this triple from a store-side removal event are expected to re-add it to the backing store under the policy documented in DR-025. After all TMS calls finish, the engine evicts persisted partial matches that referenced the removed facts so that subsequent join passes do not produce activations grounded in retracted facts. ``known_triples`` and ``materialized_triples`` are updated so they no longer include triples that left the logical closure; triples whose working-memory fact survived (stated-and-derived clearings, derived-only) remain in both sets. Returns the union of (a) input triples whose working-memory fact was actually removed and (b) cascade consequents removed by Mark-Verify-Sweep. Idempotence is the symmetric counterpart of the DR-004 requirement that ``add_triples`` is idempotent for already-known triples; ``BatchDispatcher`` relies on this symmetry to converge cascade events without an explicit reentrancy guard. .. py:method:: warmup(existing_triples: collections.abc.Iterable[rdflib_reasoning.axiom.common.Triple]) -> set[rdflib_reasoning.axiom.common.Triple] Warm the engine from existing triples and return engine-managed deductions.