pub struct GrlRule {
pub id: String,
pub grl_content: String,
}Expand description
Advanced rule with GRL support
Fields§
§id: String§grl_content: StringImplementations§
Source§impl GrlRule
impl GrlRule
Sourcepub fn new(id: impl Into<String>, grl_content: impl Into<String>) -> Self
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}Sourcepub fn evaluate(&self, context: &HashMap<String, Value>) -> RuleResult
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}Sourcepub fn from_simple(id: impl Into<String>, condition: &str, action: &str) -> Self
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§
Auto Trait Implementations§
impl Freeze for GrlRule
impl RefUnwindSafe for GrlRule
impl Send for GrlRule
impl Sync for GrlRule
impl Unpin for GrlRule
impl UnwindSafe for GrlRule
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