engine.rete.tms =============== .. py:module:: engine.rete.tms .. autoapi-nested-parse:: Truth-maintenance support graph, working memory, and retraction control. Classes ------- .. autoapisummary:: engine.rete.tms.DependencyGraph engine.rete.tms.Justification engine.rete.tms.SupportSnapshot engine.rete.tms.RetractionOutcome engine.rete.tms.WorkingMemory engine.rete.tms.TMSController Module Contents --------------- .. py:class:: DependencyGraph(/, **data: Any) Bases: :py:obj:`pydantic.BaseModel` Bidirectional map tracking logical support relationships among facts. .. py:attribute:: model_config Configuration for the model, should be a dictionary conforming to [`ConfigDict`][pydantic.config.ConfigDict]. .. py:attribute:: antecedents_by_consequent :type: dict[str, set[str]] :value: None .. py:attribute:: consequents_by_antecedent :type: dict[str, set[str]] :value: None .. py:method:: add_support(consequent_id: str, antecedent_ids: tuple[str, Ellipsis]) -> None Record that ``consequent_id`` is supported by ``antecedent_ids``. .. py:method:: antecedents_of(consequent_id: str) -> tuple[str, Ellipsis] Return sorted antecedent fact ids supporting ``consequent_id``. .. py:method:: consequents_of(antecedent_id: str) -> tuple[str, Ellipsis] Return sorted consequent fact ids supported by ``antecedent_id``. .. py:method:: discard_consequent(consequent_id: str) -> None Remove every edge that involves this fact. Used when the fact is being swept from working memory: both the consequent-to-antecedent and antecedent-to-consequent directions of the dependency graph are cleared. .. py:method:: update_consequent_edges(consequent_id: str, antecedent_ids: collections.abc.Iterable[str]) -> None Rewrite a consequent's antecedent edges to the given identifiers. Used when a fact is kept after a sweep but its justification set has been trimmed: the consequent's antecedent edges are set to exactly the union of antecedents of its surviving justifications, and reverse edges that no longer correspond to any surviving justification are discarded. .. py:method:: clear() -> None Remove all dependency edges from the graph. .. py:class:: Justification(/, **data: Any) Bases: :py:obj:`pydantic.BaseModel` A single reason for a fact's logical support. Justifications are logical support records used for future truth maintenance; they are distinct from user-facing proof nodes. .. py:attribute:: model_config Configuration for the model, should be a dictionary conforming to [`ConfigDict`][pydantic.config.ConfigDict]. .. py:attribute:: id :type: str .. py:attribute:: rule_id :type: engine.proof.RuleId .. py:attribute:: consequent_id :type: str .. py:attribute:: antecedent_ids :type: tuple[str, Ellipsis] .. py:attribute:: metadata :type: dict[str, Any] .. py:class:: SupportSnapshot(/, **data: Any) Bases: :py:obj:`pydantic.BaseModel` Immutable view of a fact's TMS support state at a moment in time. .. py:attribute:: model_config Configuration for the model, should be a dictionary conforming to [`ConfigDict`][pydantic.config.ConfigDict]. .. py:attribute:: triple :type: rdflib_reasoning.axiom.common.Triple .. py:attribute:: fact_id :type: str | None .. py:attribute:: is_present :type: bool .. py:attribute:: is_stated :type: bool .. py:attribute:: justification_ids :type: tuple[str, Ellipsis] .. py:property:: justification_count :type: int Return the number of justifications recorded in this snapshot. .. py:property:: is_supported :type: bool Return whether the fact is present and has stated or derived support. .. py:class:: RetractionOutcome(/, **data: Any) Bases: :py:obj:`pydantic.BaseModel` Immutable record of the facts and justifications affected by a retraction. Mark-Verify-Sweep retraction may remove facts from working memory, drop justification records, and clear the ``stated`` flag from a fact that survives because of independent derived support. This outcome surfaces all three categories so that engine-side wiring can mirror the changes through the supported RDFLib integration path. .. py:attribute:: model_config Configuration for the model, should be a dictionary conforming to [`ConfigDict`][pydantic.config.ConfigDict]. .. py:attribute:: removed_fact_ids :type: tuple[str, Ellipsis] :value: () .. py:attribute:: removed_triples :type: tuple[rdflib_reasoning.axiom.common.Triple, Ellipsis] :value: () .. py:attribute:: removed_justification_ids :type: tuple[str, Ellipsis] :value: () .. py:attribute:: unstated_fact_ids :type: tuple[str, Ellipsis] :value: () .. py:property:: is_empty :type: bool Return whether the retraction changed no facts or justifications. .. py:class:: WorkingMemory(/, **data: Any) Bases: :py:obj:`pydantic.BaseModel` Reactive store of currently asserted facts flowing through the network. Working memory is an execution-time structure, not a persisted proof or derivation artifact. .. py:attribute:: model_config Configuration for the model, should be a dictionary conforming to [`ConfigDict`][pydantic.config.ConfigDict]. .. py:attribute:: facts_by_id :type: dict[str, engine.rete.facts.Fact] :value: None .. py:attribute:: fact_ids_by_triple :type: dict[rdflib_reasoning.axiom.common.Triple, str] :value: None .. py:method:: add_fact(triple: rdflib_reasoning.axiom.common.Triple, *, stated: bool) -> engine.rete.facts.Fact Insert or promote ``triple`` in working memory and return its fact. .. py:method:: get_fact(triple: rdflib_reasoning.axiom.common.Triple) -> engine.rete.facts.Fact | None Return the working-memory fact for ``triple``, if present. .. py:method:: has_fact(triple: rdflib_reasoning.axiom.common.Triple) -> bool Return whether ``triple`` is currently present in working memory. .. py:method:: facts() -> tuple[engine.rete.facts.Fact, Ellipsis] Return all facts currently stored in working memory. .. py:method:: remove_fact(fact_id: str) -> engine.rete.facts.Fact Remove the working-memory entry for a fact id and return it. Raises ``KeyError`` if the fact id is not present. .. py:method:: clear() -> None Remove every fact from working memory. .. py:class:: TMSController(/, **data: Any) Bases: :py:obj:`pydantic.BaseModel` Controller for truth-maintenance bookkeeping and recursive retraction. The controller owns fact support state, dependency graph maintenance, and Mark-Verify-Sweep retraction. It deliberately does not mutate RETE matcher memory or backing RDFLib stores; callers such as ``RETEEngine`` and ``RETEStore`` compose those integration responsibilities around the TMS primitive. .. py:attribute:: model_config Configuration for the model, should be a dictionary conforming to [`ConfigDict`][pydantic.config.ConfigDict]. .. py:attribute:: working_memory :type: WorkingMemory :value: None .. py:attribute:: dependency_graph :type: DependencyGraph :value: None .. py:attribute:: justifications_by_consequent :type: dict[str, dict[str, Justification]] :value: None .. py:method:: register_stated(triples: collections.abc.Iterable[rdflib_reasoning.axiom.common.Triple]) -> tuple[engine.rete.facts.Fact, Ellipsis] Add stated ``triples`` to working memory. .. py:method:: record_derivation(triple: rdflib_reasoning.axiom.common.Triple, *, rule_id: engine.proof.RuleId, premises: tuple[engine.rete.facts.Fact, Ellipsis], bindings: collections.abc.Mapping[str, rdflib.term.Node], depth: int) -> engine.rete.facts.Fact Record one derived ``triple`` and attach a justification from ``premises``. .. py:method:: fact_for_triple(triple: rdflib_reasoning.axiom.common.Triple) -> engine.rete.facts.Fact | None Return the working-memory fact for ``triple``. .. py:method:: justifications_for(triple: rdflib_reasoning.axiom.common.Triple) -> tuple[Justification, Ellipsis] Return justifications supporting ``triple``. .. py:method:: justifications_for_fact_id(fact_id: str) -> tuple[Justification, Ellipsis] Return justifications recorded for ``fact_id``. .. py:method:: support_count(triple: rdflib_reasoning.axiom.common.Triple) -> int Return the number of justifications supporting ``triple``. .. py:method:: is_supported(triple: rdflib_reasoning.axiom.common.Triple) -> bool Return whether ``triple`` is stated or has at least one justification. .. py:method:: support_snapshot(triple: rdflib_reasoning.axiom.common.Triple) -> SupportSnapshot Return an immutable view of current support state for ``triple``. .. py:method:: would_remain_supported(triple: rdflib_reasoning.axiom.common.Triple, *, without_justification_id: str | None = None, without_antecedent_id: str | None = None) -> bool Return whether ``triple`` would remain supported after removing one support edge. .. py:method:: transitively_supported(triple: rdflib_reasoning.axiom.common.Triple) -> bool Return whether every justification chain for ``triple`` is fully grounded. .. py:method:: dependents_of(triple: rdflib_reasoning.axiom.common.Triple) -> tuple[str, Ellipsis] Return direct consequent fact ids that depend on ``triple``. .. py:method:: transitive_dependents_of(triple: rdflib_reasoning.axiom.common.Triple) -> tuple[str, Ellipsis] Return all transitive consequent fact ids that depend on ``triple``. .. py:method:: retract_triple(triple: rdflib_reasoning.axiom.common.Triple) -> RetractionOutcome Retract a stated triple using Mark-Verify-Sweep over support paths. Behavior follows the usual JTMS support rules for this controller: stated facts remain supported regardless of whether they also have derived justifications; derived facts are removed only when they have no remaining valid justifications; silent justifications count the same as visible ones for support. - If the triple is unknown, the call is a no-op and an empty outcome is returned. - If the matching fact is non-stated (derived only), ``ValueError`` is raised. Direct retraction of derived facts violates the invariant that derived facts are removed iff their justification set becomes empty; only stated retraction is exposed at this layer. - If the fact is stated and has at least one justification, the ``stated`` flag is cleared. The fact remains present, supported by its derived justifications, and no cascade is performed. - If the fact is stated and has no justifications, the fact is removed and Mark-Verify-Sweep removes any transitive dependents whose remaining justifications cannot be grounded outside the swept set. The method is atomic: the verify fixed point completes before any mutation occurs, so the call either commits the full sweep or makes no mutations at all. This method updates only working memory, the justification table, and the dependency graph. It does not update the RETE alpha/beta network, the agenda, or any backing RDF store; callers that embed this controller in a full engine must apply matching removals there separately. .. py:method:: clear() -> None Reset working memory, dependency graph, and justification tables.