GrlRule

Struct GrlRule 

Source
pub struct GrlRule {
    pub id: String,
    pub grl_content: String,
}
Expand description

Advanced rule with GRL support

Fields§

§id: String§grl_content: String

Implementations§

Source§

impl GrlRule

Source

pub fn new(id: impl Into<String>, grl_content: impl Into<String>) -> Self

Examples found in repository?
examples/grl_rules.rs (line 25)
5fn main() -> anyhow::Result<()> {
6    println!("=== Rust Logic Graph - GRL Rules Example ===\n");
7
8    // Example 1: Simple GRL Rule
9    println!("Example 1: Simple Age Verification");
10    println!("-----------------------------------");
11
12    let mut context1 = HashMap::new();
13    context1.insert("age".to_string(), json!(25));
14    context1.insert("verified".to_string(), json!(false));
15
16    let grl1 = r#"
17rule "AgeVerification" {
18    when
19        age >= 18
20    then
21        verified = true;
22}
23"#;
24
25    let rule1 = GrlRule::new("age_verify", grl1);
26    match rule1.evaluate(&context1) {
27        Ok(result) => println!("✓ Rule executed: {:?}", result),
28        Err(e) => println!("✗ Rule failed: {}", e),
29    }
30
31    // Example 2: Complex Business Rules
32    println!("\nExample 2: E-commerce Discount Rules");
33    println!("-------------------------------------");
34
35    let mut context2 = HashMap::new();
36    context2.insert("cart_total".to_string(), json!(150.0));
37    context2.insert("is_member".to_string(), json!(true));
38    context2.insert("discount".to_string(), json!(0.0));
39
40    let grl2 = r#"
41rule "MemberDiscount" salience 10 {
42    when
43        is_member == true && cart_total >= 100.0
44    then
45        discount = 0.15;
46}
47
48rule "RegularDiscount" salience 5 {
49    when
50        cart_total >= 100.0 && discount == 0.0
51    then
52        discount = 0.10;
53}
54"#;
55
56    let mut engine2 = RuleEngine::new();
57    engine2.add_grl_rule(grl2)?;
58
59    match engine2.evaluate(&context2) {
60        Ok(result) => println!("✓ Discount rules executed: {:?}", result),
61        Err(e) => println!("✗ Rules failed: {}", e),
62    }
63
64    // Example 3: Using from_simple helper
65    println!("\nExample 3: Simple Rule Helper");
66    println!("------------------------------");
67
68    let mut context3 = HashMap::new();
69    context3.insert("temperature".to_string(), json!(35.0));
70    context3.insert("alert".to_string(), json!(false));
71
72    let rule3 = GrlRule::from_simple(
73        "temperature_alert",
74        "temperature > 30.0",
75        "alert = true"
76    );
77
78    println!("Generated GRL:");
79    println!("{}", rule3.grl_content);
80
81    match rule3.evaluate(&context3) {
82        Ok(result) => println!("✓ Temperature alert: {:?}", result),
83        Err(e) => println!("✗ Rule failed: {}", e),
84    }
85
86    // Example 4: Multiple Conditions
87    println!("\nExample 4: Loan Approval Rules");
88    println!("-------------------------------");
89
90    let mut context4 = HashMap::new();
91    context4.insert("credit_score".to_string(), json!(720));
92    context4.insert("income".to_string(), json!(75000));
93    context4.insert("debt_ratio".to_string(), json!(0.3));
94    context4.insert("approved".to_string(), json!(false));
95
96    let grl4 = r#"
97rule "LoanApproval" {
98    when
99        credit_score >= 700 &&
100        income >= 50000 &&
101        debt_ratio < 0.4
102    then
103        approved = true;
104}
105"#;
106
107    let mut engine4 = RuleEngine::new();
108    engine4.add_grl_rule(grl4)?;
109
110    match engine4.evaluate(&context4) {
111        Ok(result) => println!("✓ Loan approval processed: {:?}", result),
112        Err(e) => println!("✗ Approval failed: {}", e),
113    }
114
115    println!("\n=== All GRL Rules Executed Successfully! ===");
116    Ok(())
117}
Source

pub fn evaluate(&self, context: &HashMap<String, Value>) -> RuleResult

Evaluate the GRL rule

Examples found in repository?
examples/grl_rules.rs (line 26)
5fn main() -> anyhow::Result<()> {
6    println!("=== Rust Logic Graph - GRL Rules Example ===\n");
7
8    // Example 1: Simple GRL Rule
9    println!("Example 1: Simple Age Verification");
10    println!("-----------------------------------");
11
12    let mut context1 = HashMap::new();
13    context1.insert("age".to_string(), json!(25));
14    context1.insert("verified".to_string(), json!(false));
15
16    let grl1 = r#"
17rule "AgeVerification" {
18    when
19        age >= 18
20    then
21        verified = true;
22}
23"#;
24
25    let rule1 = GrlRule::new("age_verify", grl1);
26    match rule1.evaluate(&context1) {
27        Ok(result) => println!("✓ Rule executed: {:?}", result),
28        Err(e) => println!("✗ Rule failed: {}", e),
29    }
30
31    // Example 2: Complex Business Rules
32    println!("\nExample 2: E-commerce Discount Rules");
33    println!("-------------------------------------");
34
35    let mut context2 = HashMap::new();
36    context2.insert("cart_total".to_string(), json!(150.0));
37    context2.insert("is_member".to_string(), json!(true));
38    context2.insert("discount".to_string(), json!(0.0));
39
40    let grl2 = r#"
41rule "MemberDiscount" salience 10 {
42    when
43        is_member == true && cart_total >= 100.0
44    then
45        discount = 0.15;
46}
47
48rule "RegularDiscount" salience 5 {
49    when
50        cart_total >= 100.0 && discount == 0.0
51    then
52        discount = 0.10;
53}
54"#;
55
56    let mut engine2 = RuleEngine::new();
57    engine2.add_grl_rule(grl2)?;
58
59    match engine2.evaluate(&context2) {
60        Ok(result) => println!("✓ Discount rules executed: {:?}", result),
61        Err(e) => println!("✗ Rules failed: {}", e),
62    }
63
64    // Example 3: Using from_simple helper
65    println!("\nExample 3: Simple Rule Helper");
66    println!("------------------------------");
67
68    let mut context3 = HashMap::new();
69    context3.insert("temperature".to_string(), json!(35.0));
70    context3.insert("alert".to_string(), json!(false));
71
72    let rule3 = GrlRule::from_simple(
73        "temperature_alert",
74        "temperature > 30.0",
75        "alert = true"
76    );
77
78    println!("Generated GRL:");
79    println!("{}", rule3.grl_content);
80
81    match rule3.evaluate(&context3) {
82        Ok(result) => println!("✓ Temperature alert: {:?}", result),
83        Err(e) => println!("✗ Rule failed: {}", e),
84    }
85
86    // Example 4: Multiple Conditions
87    println!("\nExample 4: Loan Approval Rules");
88    println!("-------------------------------");
89
90    let mut context4 = HashMap::new();
91    context4.insert("credit_score".to_string(), json!(720));
92    context4.insert("income".to_string(), json!(75000));
93    context4.insert("debt_ratio".to_string(), json!(0.3));
94    context4.insert("approved".to_string(), json!(false));
95
96    let grl4 = r#"
97rule "LoanApproval" {
98    when
99        credit_score >= 700 &&
100        income >= 50000 &&
101        debt_ratio < 0.4
102    then
103        approved = true;
104}
105"#;
106
107    let mut engine4 = RuleEngine::new();
108    engine4.add_grl_rule(grl4)?;
109
110    match engine4.evaluate(&context4) {
111        Ok(result) => println!("✓ Loan approval processed: {:?}", result),
112        Err(e) => println!("✗ Approval failed: {}", e),
113    }
114
115    println!("\n=== All GRL Rules Executed Successfully! ===");
116    Ok(())
117}
Source

pub fn from_simple(id: impl Into<String>, condition: &str, action: &str) -> Self

Create GRL rule from simple condition Example: GrlRule::from_simple(“age_check”, “age >= 18”, “eligible = true”)

Examples found in repository?
examples/grl_rules.rs (lines 72-76)
5fn main() -> anyhow::Result<()> {
6    println!("=== Rust Logic Graph - GRL Rules Example ===\n");
7
8    // Example 1: Simple GRL Rule
9    println!("Example 1: Simple Age Verification");
10    println!("-----------------------------------");
11
12    let mut context1 = HashMap::new();
13    context1.insert("age".to_string(), json!(25));
14    context1.insert("verified".to_string(), json!(false));
15
16    let grl1 = r#"
17rule "AgeVerification" {
18    when
19        age >= 18
20    then
21        verified = true;
22}
23"#;
24
25    let rule1 = GrlRule::new("age_verify", grl1);
26    match rule1.evaluate(&context1) {
27        Ok(result) => println!("✓ Rule executed: {:?}", result),
28        Err(e) => println!("✗ Rule failed: {}", e),
29    }
30
31    // Example 2: Complex Business Rules
32    println!("\nExample 2: E-commerce Discount Rules");
33    println!("-------------------------------------");
34
35    let mut context2 = HashMap::new();
36    context2.insert("cart_total".to_string(), json!(150.0));
37    context2.insert("is_member".to_string(), json!(true));
38    context2.insert("discount".to_string(), json!(0.0));
39
40    let grl2 = r#"
41rule "MemberDiscount" salience 10 {
42    when
43        is_member == true && cart_total >= 100.0
44    then
45        discount = 0.15;
46}
47
48rule "RegularDiscount" salience 5 {
49    when
50        cart_total >= 100.0 && discount == 0.0
51    then
52        discount = 0.10;
53}
54"#;
55
56    let mut engine2 = RuleEngine::new();
57    engine2.add_grl_rule(grl2)?;
58
59    match engine2.evaluate(&context2) {
60        Ok(result) => println!("✓ Discount rules executed: {:?}", result),
61        Err(e) => println!("✗ Rules failed: {}", e),
62    }
63
64    // Example 3: Using from_simple helper
65    println!("\nExample 3: Simple Rule Helper");
66    println!("------------------------------");
67
68    let mut context3 = HashMap::new();
69    context3.insert("temperature".to_string(), json!(35.0));
70    context3.insert("alert".to_string(), json!(false));
71
72    let rule3 = GrlRule::from_simple(
73        "temperature_alert",
74        "temperature > 30.0",
75        "alert = true"
76    );
77
78    println!("Generated GRL:");
79    println!("{}", rule3.grl_content);
80
81    match rule3.evaluate(&context3) {
82        Ok(result) => println!("✓ Temperature alert: {:?}", result),
83        Err(e) => println!("✗ Rule failed: {}", e),
84    }
85
86    // Example 4: Multiple Conditions
87    println!("\nExample 4: Loan Approval Rules");
88    println!("-------------------------------");
89
90    let mut context4 = HashMap::new();
91    context4.insert("credit_score".to_string(), json!(720));
92    context4.insert("income".to_string(), json!(75000));
93    context4.insert("debt_ratio".to_string(), json!(0.3));
94    context4.insert("approved".to_string(), json!(false));
95
96    let grl4 = r#"
97rule "LoanApproval" {
98    when
99        credit_score >= 700 &&
100        income >= 50000 &&
101        debt_ratio < 0.4
102    then
103        approved = true;
104}
105"#;
106
107    let mut engine4 = RuleEngine::new();
108    engine4.add_grl_rule(grl4)?;
109
110    match engine4.evaluate(&context4) {
111        Ok(result) => println!("✓ Loan approval processed: {:?}", result),
112        Err(e) => println!("✗ Approval failed: {}", e),
113    }
114
115    println!("\n=== All GRL Rules Executed Successfully! ===");
116    Ok(())
117}

Trait Implementations§

Source§

impl Clone for GrlRule

Source§

fn clone(&self) -> GrlRule

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for GrlRule

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more