engine.rete.network

RETE network construction, node sharing, and matching.

Classes

AlphaNode

First layer of the RETE network.

PredicateNode

Specialized alpha node that wraps read-only predicate callables.

BetaNode

Join node maintaining partial-match memory for conjunctive rule bodies.

TerminalNode

Schedule engine-managed consequents from one completed match.

NodeRegistry

Canonicalizing store for structurally shared network nodes.

NetworkBuilder

Orchestrator for RETE network assembly from compiled rule representations.

MatcherContext

Minimal read-only rule context for builtin predicate evaluation.

NetworkMatcher

Minimal matching pass over alpha, beta, predicate, and terminal nodes.

Module Contents

class engine.rete.network.AlphaNode(/, **data: Any)

Bases: pydantic.BaseModel

First layer of the RETE network.

Filters individual facts based on literal constant constraints before any join work occurs.

model_config

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

key: str = None
condition: engine.rete.compiler.CompiledTripleCondition = None
constraints: tuple[engine.rete.compiler.AlphaConstraint, Ellipsis] = None
class engine.rete.network.PredicateNode(/, **data: Any)

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

model_config

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

key: str = None
condition: engine.rete.compiler.CompiledPredicateCondition = None
required_variables: tuple[str, Ellipsis] = None
class engine.rete.network.BetaNode(/, **data: Any)

Bases: pydantic.BaseModel

Join node maintaining partial-match memory for conjunctive rule bodies.

model_config

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

key: str = None
left_key: str = None
right_key: str = None
shared_variables: tuple[str, Ellipsis] = None
class engine.rete.network.TerminalNode(/, **data: Any)

Bases: pydantic.BaseModel

Schedule engine-managed consequents from one completed match.

Terminal nodes handle logical production, optional observational callbacks, and contradiction diagnostics.

model_config

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

key: str = None
rule: engine.rete.compiler.CompiledRule = None
input_keys: tuple[str, Ellipsis] = None
class engine.rete.network.NodeRegistry(/, **data: Any)

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

model_config

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

alpha_nodes: dict[str, AlphaNode] = None
predicate_nodes: dict[str, PredicateNode] = None
beta_nodes: dict[str, BetaNode] = None
terminal_nodes: dict[str, TerminalNode] = None
alpha_memory: dict[str, dict[str, engine.rete.facts.PartialMatch]] = None
beta_memory: dict[str, dict[str, engine.rete.facts.PartialMatch]] = None
get_or_create_alpha(node: AlphaNode) → AlphaNode

Return a shared alpha node, creating node when needed.

get_or_create_predicate(node: PredicateNode) → PredicateNode

Return a shared predicate node, creating node when needed.

get_or_create_beta(node: BetaNode) → BetaNode

Return a shared beta node, creating node when needed.

add_terminal(node: TerminalNode) → TerminalNode

Register node as a terminal and return it.

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.

class engine.rete.network.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.

registry: NodeRegistry
build_rule(rule: engine.rete.compiler.CompiledRule) → TerminalNode

Build the terminal node and join chain for one compiled rule.

build_rules(rules: tuple[engine.rete.compiler.CompiledRule, Ellipsis]) → tuple[TerminalNode, Ellipsis]

Build terminal nodes for every compiled rule in rules.

class engine.rete.network.MatcherContext(terminal: TerminalNode)

Bases: engine.builtins.RuleContext

Minimal read-only rule context for builtin predicate evaluation.

terminal
class engine.rete.network.NetworkMatcher(registry: NodeRegistry, *, predicates: dict[str, engine.builtins.PredicateHook] | None = None)

Minimal matching pass over alpha, beta, predicate, and terminal nodes.

registry: NodeRegistry
predicates: dict[str, engine.builtins.PredicateHook]
match_terminal(terminal: TerminalNode, facts: collections.abc.Iterable[engine.rete.facts.Fact | axiom.common.Triple]) → tuple[engine.rete.consequents.ActionInstance, Ellipsis]

Match terminal against facts and return scheduled actions.

alpha_memory_size(key: str) → int

Return the number of persisted partial matches for alpha node key.

beta_memory_size(key: str) → int

Return the number of persisted partial matches for beta node key.

match_terminals(terminals: collections.abc.Iterable[TerminalNode], facts: collections.abc.Iterable[engine.rete.facts.Fact | axiom.common.Triple]) → tuple[engine.rete.consequents.ActionInstance, Ellipsis]

Match every terminal in terminals against facts.