Expand description
A rule engine compatible with Grule’s rule language (GRL) and execution model, for embedding business rules in Rust.
Rules are written in GRL and loaded into a RuleEngine; facts are named
FactValues, typically converted from your own serde types. Executing runs rules
until none apply: each cycle fires the highest-salience rule whose condition holds.
use rust_runes::{Fact, RuleEngine};
use serde::{Deserialize, Serialize};
use std::collections::HashMap;
#[derive(Serialize, Deserialize)]
#[allow(non_snake_case)]
struct Ride {
Distance: i64,
NetAmount: f64,
Priced: bool,
}
let mut engine = RuleEngine::new();
engine.add_rules_from_str(r#"
rule LongRide "Long rides get a flat rate" salience 10 {
when Ride.Distance > 5000 && !Ride.Priced
then Ride.NetAmount = 143.32; Ride.Priced = true;
}
"#)?;
let ride = Ride { Distance: 6000, NetAmount: 0.0, Priced: false };
let mut facts = HashMap::from([("Ride".to_string(), Fact::from_serialize("Ride", &ride)?)]);
let result = engine.execute(&mut facts)?;
let ride: Ride = facts["Ride"].deserialize()?;
assert_eq!(result.rules_fired, ["LongRide"]);
assert_eq!(ride.NetAmount, 143.32);See the guide for the rule language, built-in functions, execution model, and how this differs from Grule.
Re-exports§
pub use engine::EngineError;pub use engine::EngineListener;pub use engine::ExecutionResult;pub use engine::RuleEngine;pub use engine::RulePhase;pub use facts::serde_time;pub use facts::Fact;pub use facts::FactValue;pub use knowledge_base::KnowledgeBase;pub use rule::Rule;