engine.rete.tms

Truth-maintenance support graph, working memory, and retraction control.

Classes

DependencyGraph

Bidirectional map tracking logical support relationships among facts.

Justification

A single reason for a fact's logical support.

SupportSnapshot

Immutable view of a fact's TMS support state at a moment in time.

RetractionOutcome

Immutable record of the facts and justifications affected by a retraction.

WorkingMemory

Reactive store of currently asserted facts flowing through the network.

TMSController

Controller for truth-maintenance bookkeeping and recursive retraction.

Module Contents

class engine.rete.tms.DependencyGraph(/, **data: Any)

Bases: pydantic.BaseModel

Bidirectional map tracking logical support relationships among facts.

model_config

Configuration for the model, should be a dictionary conforming to [ConfigDict][pydantic.config.ConfigDict].

antecedents_by_consequent: dict[str, set[str]] = None
consequents_by_antecedent: dict[str, set[str]] = None
add_support(consequent_id: str, antecedent_ids: tuple[str, Ellipsis]) → None

Record that consequent_id is supported by antecedent_ids.

antecedents_of(consequent_id: str) → tuple[str, Ellipsis]

Return sorted antecedent fact ids supporting consequent_id.

consequents_of(antecedent_id: str) → tuple[str, Ellipsis]

Return sorted consequent fact ids supported by antecedent_id.

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.

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.

clear() → None

Remove all dependency edges from the graph.

class engine.rete.tms.Justification(/, **data: Any)

Bases: 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.

model_config

Configuration for the model, should be a dictionary conforming to [ConfigDict][pydantic.config.ConfigDict].

id: str
rule_id: engine.proof.RuleId
consequent_id: str
antecedent_ids: tuple[str, Ellipsis]
metadata: dict[str, Any]
class engine.rete.tms.SupportSnapshot(/, **data: Any)

Bases: pydantic.BaseModel

Immutable view of a fact’s TMS support state at a moment in time.

model_config

Configuration for the model, should be a dictionary conforming to [ConfigDict][pydantic.config.ConfigDict].

triple: axiom.common.Triple
fact_id: str | None
is_present: bool
is_stated: bool
justification_ids: tuple[str, Ellipsis]
property justification_count: int

Return the number of justifications recorded in this snapshot.

property is_supported: bool

Return whether the fact is present and has stated or derived support.

class engine.rete.tms.RetractionOutcome(/, **data: Any)

Bases: 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.

model_config

Configuration for the model, should be a dictionary conforming to [ConfigDict][pydantic.config.ConfigDict].

removed_fact_ids: tuple[str, Ellipsis] = ()
removed_triples: tuple[axiom.common.Triple, Ellipsis] = ()
removed_justification_ids: tuple[str, Ellipsis] = ()
unstated_fact_ids: tuple[str, Ellipsis] = ()
property is_empty: bool

Return whether the retraction changed no facts or justifications.

class engine.rete.tms.WorkingMemory(/, **data: Any)

Bases: 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.

model_config

Configuration for the model, should be a dictionary conforming to [ConfigDict][pydantic.config.ConfigDict].

facts_by_id: dict[str, engine.rete.facts.Fact] = None
fact_ids_by_triple: dict[axiom.common.Triple, str] = None
add_fact(triple: axiom.common.Triple, *, stated: bool) → engine.rete.facts.Fact

Insert or promote triple in working memory and return its fact.

get_fact(triple: axiom.common.Triple) → engine.rete.facts.Fact | None

Return the working-memory fact for triple, if present.

has_fact(triple: axiom.common.Triple) → bool

Return whether triple is currently present in working memory.

facts() → tuple[engine.rete.facts.Fact, Ellipsis]

Return all facts currently stored in working memory.

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.

clear() → None

Remove every fact from working memory.

class engine.rete.tms.TMSController(/, **data: Any)

Bases: 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.

model_config

Configuration for the model, should be a dictionary conforming to [ConfigDict][pydantic.config.ConfigDict].

working_memory: WorkingMemory = None
dependency_graph: DependencyGraph = None
justifications_by_consequent: dict[str, dict[str, Justification]] = None
register_stated(triples: collections.abc.Iterable[axiom.common.Triple]) → tuple[engine.rete.facts.Fact, Ellipsis]

Add stated triples to working memory.

record_derivation(triple: 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.

fact_for_triple(triple: axiom.common.Triple) → engine.rete.facts.Fact | None

Return the working-memory fact for triple.

justifications_for(triple: axiom.common.Triple) → tuple[Justification, Ellipsis]

Return justifications supporting triple.

justifications_for_fact_id(fact_id: str) → tuple[Justification, Ellipsis]

Return justifications recorded for fact_id.

support_count(triple: axiom.common.Triple) → int

Return the number of justifications supporting triple.

is_supported(triple: axiom.common.Triple) → bool

Return whether triple is stated or has at least one justification.

support_snapshot(triple: axiom.common.Triple) → SupportSnapshot

Return an immutable view of current support state for triple.

would_remain_supported(triple: 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.

transitively_supported(triple: axiom.common.Triple) → bool

Return whether every justification chain for triple is fully grounded.

dependents_of(triple: axiom.common.Triple) → tuple[str, Ellipsis]

Return direct consequent fact ids that depend on triple.

transitive_dependents_of(triple: axiom.common.Triple) → tuple[str, Ellipsis]

Return all transitive consequent fact ids that depend on triple.

retract_triple(triple: 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.

clear() → None

Reset working memory, dependency graph, and justification tables.