Skip to content

Engine

Classes:

  • InternalStateError

    Raised when the internal state of the RuleEngine is invalid.

  • RecursionLimitError

    Raised when the recursion limit is reached during rule evaluation.

  • Rule

    Represents a rule with a condition and corresponding actions.

  • RuleEngine

    RuleEngine is a class that manages the evaluation of rules based on a set of facts. It allows for the addition of rules,

InternalStateError

Bases: RuntimeError

Raised when the internal state of the RuleEngine is invalid.

RecursionLimitError

Bases: RuntimeError

Raised when the recursion limit is reached during rule evaluation.

Rule dataclass

Represents a rule with a condition and corresponding actions.

Attributes:

  • - (id (UUID) –

    A unique identifier for the rule, automatically generated.

  • - (name (Optional[str]) –

    The name of the rule.

  • - (when (Expression) –

    The condition that triggers the rule.

  • - (then (Action) –

    The action to be executed when the condition is met.

  • - (inverse (Optional[Action]) –

    An optional action to be executed when the condition is not met.

RuleEngine dataclass

RuleEngine is a class that manages the evaluation of rules based on a set of facts. It allows for the addition of rules, updating of facts, and cascading evaluation of rules.

Attributes:

  • enabled (bool) –

    Indicates whether the rule engine is enabled.

  • recusion_limit (int) –

    The maximum number of recursive evaluations allowed.

  • facts (dict[type[Fact], Fact]) –

    A dictionary to store facts with their types as keys.

  • rules (dict[str, list[Rule]]) –

    A dictionary to store rules associated with fact strings.

Methods:

  • rule

    str | None = None, when: LogicEvaluator, then: BaseAction, inverse: BaseAction | None = None): Adds a rule to the rule engine.

  • update_facts

    tuple[Fact | partial[Fact], ...] | partial[Fact] | Fact) -> Iterator[str]: Updates the facts in the working memory.

  • evaluate

    bool = False): Evaluates the rules based on the current facts in working memory.

  • yaml_report

    Returns the YAML report of the last evaluation (if tracing was enabled).

__getitem__(key: type[T]) -> T

Retrieves a fact from the working memory.

Parameters:

  • key (type[Fact]) –

    The type of the fact to retrieve.

Returns:

  • T ( T ) –

    The fact instance of the specified type.

fact(fact: Fact | partial[Fact])

Updates the working memory with a new fact or merges a partial fact.

Parameters:

  • fact (Union[Fact, partial[Fact]]) –

    The fact instance or partial fact to update the working memory with.

Raises:

  • InternalStateError

    If a partial fact cannot be instantiated due to missing required fields

rule(*, name: str | None = None, when: Expression, then: Action, inverse: Action | None = None) -> None

Convenience method for adding a rule to the rule engine.

Parameters:

  • name (Optional[str], default: None ) –

    The name of the rule. Defaults to None.

  • when (Expression) –

    The condition that triggers the rule.

  • then (Action) –

    The action to be executed when the condition is met.

  • inverse (Optional[Action], default: None ) –

    The action to be executed when the condition is not met. Defaults to None.

Returns:

  • None

    None

evaluate(fact: Fact | partial[Fact] | None = None, *, audit: bool = False)

Cascading evaluation of rules based on the facts in working memory.

If provided a fact, will update and evaluate immediately. Otherwise all rules will be evaluated.

Parameters:

  • fact (Fact | partial[Fact] | None, default: None ) –

    Optional fact to update and evaluate immediately

  • audit (bool, default: False ) –

    Enables tracing for explanbility report generation