pub struct RuleEngine { /* private fields */ }Expand description
Convenience wrapper around RustRuleEngine with JSON integration
This provides a simplified API for common use cases while maintaining full access to the underlying rust-rule-engine capabilities.
§Thread Safety
RustRuleEngine is thread-safe (Send + Sync), making it suitable for use in multi-threaded web services like Axum.
§Example
use rust_logic_graph::RuleEngine;
use std::collections::HashMap;
use serde_json::json;
let mut engine = RuleEngine::new();
let grl = r#"
rule "discount_rule" {
salience 100
when
total > 100
then
discount = total * 0.1;
}
"#;
engine.add_grl_rule(grl).unwrap();
let mut context = HashMap::new();
context.insert("total".to_string(), json!(150.0));
let result = engine.evaluate(&context).unwrap();Implementations§
Source§impl RuleEngine
impl RuleEngine
Sourcepub fn with_config(config: EngineConfig) -> Self
pub fn with_config(config: EngineConfig) -> Self
Create a new rule engine with custom configuration
Sourcepub fn add_grl_rule(&mut self, grl_content: &str) -> Result<(), RuleError>
pub fn add_grl_rule(&mut self, grl_content: &str) -> Result<(), RuleError>
Add rules from GRL syntax
§Example
use rust_logic_graph::RuleEngine;
let mut engine = RuleEngine::new();
let grl = r#"
rule "high_value_order" {
salience 100
when
order_amount > 1000
then
priority = "high";
requires_approval = true;
}
"#;
engine.add_grl_rule(grl).unwrap();Sourcepub fn evaluate(&mut self, context: &HashMap<String, JsonValue>) -> RuleResult
pub fn evaluate(&mut self, context: &HashMap<String, JsonValue>) -> RuleResult
Evaluate rules with JSON context (convenience method)
For more control, use inner() or inner_mut() to access the
underlying RustRuleEngine directly.
§Example
use rust_logic_graph::RuleEngine;
use std::collections::HashMap;
use serde_json::json;
let mut engine = RuleEngine::new();
// ... add rules ...
let mut context = HashMap::new();
context.insert("total".to_string(), json!(150.0));
let result = engine.evaluate(&context).unwrap();Sourcepub fn from_grl(grl_script: &str) -> Result<Self, RuleError>
pub fn from_grl(grl_script: &str) -> Result<Self, RuleError>
Create a rule engine from GRL script
§Example
use rust_logic_graph::RuleEngine;
let grl = r#"
rule "example" {
salience 100
when
x > 0
then
y = x * 2;
}
"#;
let mut engine = RuleEngine::from_grl(grl).unwrap();Sourcepub fn inner(&self) -> &RustRuleEngine
pub fn inner(&self) -> &RustRuleEngine
Get reference to underlying RustRuleEngine for advanced usage
This provides full access to rust-rule-engine features:
- Custom functions
- Templates
- Globals
- Deffacts
- Fine-grained control
Sourcepub fn inner_mut(&mut self) -> &mut RustRuleEngine
pub fn inner_mut(&mut self) -> &mut RustRuleEngine
Get mutable reference to underlying RustRuleEngine
Trait Implementations§
Auto Trait Implementations§
impl Freeze for RuleEngine
impl !RefUnwindSafe for RuleEngine
impl Send for RuleEngine
impl Sync for RuleEngine
impl Unpin for RuleEngine
impl !UnwindSafe for RuleEngine
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
Source§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
Converts
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
Converts
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more