StatefulRuleEngine (excanon v0.1.0)
Copy MarkdownA stateful rule engine implementation using Elixir Agents.
This engine maintains rules in memory using an Agent process, allowing rules to persist across multiple evaluations. Rules are loaded from JSON strings and evaluated against input facts. When rule conditions are met, their actions are executed to modify the facts. Each execution is stateless in terms of input facts, but the rules themselves are stateful and can be updated dynamically.
Features
- Stateful: Rules persist in an Agent process across evaluations
- JSON-based: Rules defined in JSON format for easy configuration
- Sequential execution: Rules are evaluated in order, with actions modifying facts
- Error handling: Invalid rules or JSON are rejected with error messages
Usage
# Start an engine
{:ok, _pid} = StatefulRuleEngine.start_link(:my_engine, [])
# Load rules
rules_json = ~s([
{
"name": "apply_discount",
"description": "10% discount over $100",
"conditions": {"gt": [{"obj": "total"}, 100]},
"actions": [{"set": ["discount", {"mult": [{"obj": "total"}, 0.1]}]}]
}
])
:ok = StatefulRuleEngine.load_rules(:my_engine, rules_json)
# Evaluate facts
facts = %{"total" => 150}
{:ok, result} = StatefulRuleEngine.evaluate(:my_engine, facts)
# result["discount"] == 15.0
Summary
Functions
Returns a specification to start this module under a supervisor.
Evaluates facts against the loaded rules and executes actions for matching conditions.
Loads rules from a JSON string into the rule engine. This function call is idempotent, so each call will replace the existing rules with the new set of rules provided in the JSON string.
Functions
Returns a specification to start this module under a supervisor.
See Supervisor.
Evaluates facts against the loaded rules and executes actions for matching conditions.
Parameters
id: The Agent process identifierfacts: Map of input facts to evaluate
Returns
{:ok, result_facts}where result_facts contains the original facts modified by rule actions- Raises
ArgumentErrorfor invalid arguments
Examples
facts = %{"age" => 25, "premium" => false}
{:ok, result} = StatefulRuleEngine.evaluate(:engine, facts)
# result may have additional keys set by rule actions
Loads rules from a JSON string into the rule engine. This function call is idempotent, so each call will replace the existing rules with the new set of rules provided in the JSON string.
Parameters
id: The Agent process identifierrules: JSON string containing an array of rule objects
Returns
:okon successful loading{:error, reason}on failure (invalid JSON or rule structure)
Examples
rules_json = ~s([
{
"name": "test",
"description": "Test rule",
"conditions": {"eq": [1, 1]},
"actions": [{"set": ["result", true]}]
}
])
StatefulRuleEngine.load_rules(:engine, rules_json)
# => :ok