engine.rete.network =================== .. py:module:: engine.rete.network .. autoapi-nested-parse:: RETE network construction, node sharing, and matching. Classes ------- .. autoapisummary:: engine.rete.network.AlphaNode engine.rete.network.PredicateNode engine.rete.network.BetaNode engine.rete.network.TerminalNode engine.rete.network.NodeRegistry engine.rete.network.NetworkBuilder engine.rete.network.MatcherContext engine.rete.network.NetworkMatcher Module Contents --------------- .. py:class:: AlphaNode(/, **data: Any) Bases: :py:obj:`pydantic.BaseModel` First layer of the RETE network. Filters individual facts based on literal constant constraints before any join work occurs. .. py:attribute:: model_config Configuration for the model, should be a dictionary conforming to [`ConfigDict`][pydantic.config.ConfigDict]. .. py:attribute:: key :type: str :value: None .. py:attribute:: condition :type: engine.rete.compiler.CompiledTripleCondition :value: None .. py:attribute:: constraints :type: tuple[engine.rete.compiler.AlphaConstraint, Ellipsis] :value: None .. py:class:: PredicateNode(/, **data: Any) Bases: :py:obj:`pydantic.BaseModel` Specialized alpha node that wraps read-only predicate callables. Predicate nodes represent body-side evaluation only; they are not callback hooks and they do not emit logical consequences directly. .. py:attribute:: model_config Configuration for the model, should be a dictionary conforming to [`ConfigDict`][pydantic.config.ConfigDict]. .. py:attribute:: key :type: str :value: None .. py:attribute:: condition :type: engine.rete.compiler.CompiledPredicateCondition :value: None .. py:attribute:: required_variables :type: tuple[str, Ellipsis] :value: None .. py:class:: BetaNode(/, **data: Any) Bases: :py:obj:`pydantic.BaseModel` Join node maintaining partial-match memory for conjunctive rule bodies. .. py:attribute:: model_config Configuration for the model, should be a dictionary conforming to [`ConfigDict`][pydantic.config.ConfigDict]. .. py:attribute:: key :type: str :value: None .. py:attribute:: left_key :type: str :value: None .. py:attribute:: right_key :type: str :value: None .. py:attribute:: shared_variables :type: tuple[str, Ellipsis] :value: None .. py:class:: TerminalNode(/, **data: Any) Bases: :py:obj:`pydantic.BaseModel` Schedule engine-managed consequents from one completed match. Terminal nodes handle logical production, optional observational callbacks, and contradiction diagnostics. .. py:attribute:: model_config Configuration for the model, should be a dictionary conforming to [`ConfigDict`][pydantic.config.ConfigDict]. .. py:attribute:: key :type: str :value: None .. py:attribute:: rule :type: engine.rete.compiler.CompiledRule :value: None .. py:attribute:: input_keys :type: tuple[str, Ellipsis] :value: None .. py:class:: NodeRegistry(/, **data: Any) Bases: :py:obj:`pydantic.BaseModel` Canonicalizing store for structurally shared network nodes. Node sharing is part of the intended RETE optimization strategy and should remain independent from proof reconstruction concerns. .. py:attribute:: model_config Configuration for the model, should be a dictionary conforming to [`ConfigDict`][pydantic.config.ConfigDict]. .. py:attribute:: alpha_nodes :type: dict[str, AlphaNode] :value: None .. py:attribute:: predicate_nodes :type: dict[str, PredicateNode] :value: None .. py:attribute:: beta_nodes :type: dict[str, BetaNode] :value: None .. py:attribute:: terminal_nodes :type: dict[str, TerminalNode] :value: None .. py:attribute:: alpha_memory :type: dict[str, dict[str, engine.rete.facts.PartialMatch]] :value: None .. py:attribute:: beta_memory :type: dict[str, dict[str, engine.rete.facts.PartialMatch]] :value: None .. py:method:: get_or_create_alpha(node: AlphaNode) -> AlphaNode Return a shared alpha node, creating ``node`` when needed. .. py:method:: get_or_create_predicate(node: PredicateNode) -> PredicateNode Return a shared predicate node, creating ``node`` when needed. .. py:method:: get_or_create_beta(node: BetaNode) -> BetaNode Return a shared beta node, creating ``node`` when needed. .. py:method:: add_terminal(node: TerminalNode) -> TerminalNode Register ``node`` as a terminal and return it. .. py:method:: evict_partial_matches_referencing(fact_ids: frozenset[str]) -> None Drop persisted partial matches that reference any of these fact ids. Network alpha and beta memories accumulate ``PartialMatch`` records across calls to ``NetworkMatcher.match_terminals``. When the engine retracts triples, the corresponding ``Fact`` objects leave working memory; the persisted partial matches must also be evicted so that subsequent join passes do not produce activations grounded in removed facts. .. py:class:: NetworkBuilder(registry: NodeRegistry | None = None) Orchestrator for RETE network assembly from compiled rule representations. The builder turns compiled rules into canonical alpha, beta, and predicate nodes plus per-rule terminal nodes using a left-deep join assembly. .. py:attribute:: registry :type: NodeRegistry .. py:method:: build_rule(rule: engine.rete.compiler.CompiledRule) -> TerminalNode Build the terminal node and join chain for one compiled rule. .. py:method:: build_rules(rules: tuple[engine.rete.compiler.CompiledRule, Ellipsis]) -> tuple[TerminalNode, Ellipsis] Build terminal nodes for every compiled rule in ``rules``. .. py:class:: MatcherContext(terminal: TerminalNode) Bases: :py:obj:`engine.builtins.RuleContext` Minimal read-only rule context for builtin predicate evaluation. .. py:attribute:: terminal .. py:class:: NetworkMatcher(registry: NodeRegistry, *, predicates: dict[str, engine.builtins.PredicateHook] | None = None) Minimal matching pass over alpha, beta, predicate, and terminal nodes. .. py:attribute:: registry :type: NodeRegistry .. py:attribute:: predicates :type: dict[str, engine.builtins.PredicateHook] .. py:method:: match_terminal(terminal: TerminalNode, facts: collections.abc.Iterable[engine.rete.facts.Fact | rdflib_reasoning.axiom.common.Triple]) -> tuple[engine.rete.consequents.ActionInstance, Ellipsis] Match ``terminal`` against ``facts`` and return scheduled actions. .. py:method:: alpha_memory_size(key: str) -> int Return the number of persisted partial matches for alpha node ``key``. .. py:method:: beta_memory_size(key: str) -> int Return the number of persisted partial matches for beta node ``key``. .. py:method:: match_terminals(terminals: collections.abc.Iterable[TerminalNode], facts: collections.abc.Iterable[engine.rete.facts.Fact | rdflib_reasoning.axiom.common.Triple]) -> tuple[engine.rete.consequents.ActionInstance, Ellipsis] Match every terminal in ``terminals`` against ``facts``.