grl_graph_flow/
grl_graph_flow.rs

1use rust_logic_graph::{Graph, GraphIO, Executor, RuleNode, DBNode, AINode, RuleEngine};
2use tracing_subscriber;
3use serde_json::json;
4
5#[tokio::main]
6async fn main() -> anyhow::Result<()> {
7    // Initialize tracing
8    tracing_subscriber::fmt()
9        .with_max_level(tracing::Level::INFO)
10        .init();
11
12    println!("=== Rust Logic Graph - GRL Integration Example ===");
13    println!("Scenario: Loan Application with Advanced GRL Rules\n");
14
15    // Load graph definition
16    let def = GraphIO::load_from_file("examples/grl_graph_flow.json")?;
17
18    // Create custom executor with GRL-powered nodes
19    let mut executor = Executor::new();
20
21    // Input validation with GRL
22    executor.register_node(Box::new(RuleNode::new(
23        "input_validation",
24        "loan_amount > 0 && loan_amount <= 1000000"
25    )));
26
27    // Fetch customer data
28    executor.register_node(Box::new(DBNode::new(
29        "fetch_customer",
30        "SELECT * FROM customers WHERE id = ?"
31    )));
32
33    // Risk assessment with complex GRL rules
34    executor.register_node(Box::new(RuleNode::new(
35        "risk_assessment",
36        "credit_score >= 600 && income >= loan_amount * 3"
37    )));
38
39    // Fraud detection AI
40    executor.register_node(Box::new(AINode::new(
41        "fraud_detection",
42        "Analyze transaction patterns for fraud indicators"
43    )));
44
45    // Final approval decision
46    executor.register_node(Box::new(RuleNode::new(
47        "approval_decision",
48        "risk_score < 50 && fraud_score < 30"
49    )));
50
51    // Notification
52    executor.register_node(Box::new(AINode::new(
53        "notification",
54        "Generate approval/rejection notification email"
55    )));
56
57    // Create graph with initial context
58    let mut graph = Graph::new(def);
59
60    // Set application data
61    graph.context.data.insert("loan_amount".to_string(), json!(50000));
62    graph.context.data.insert("credit_score".to_string(), json!(720));
63    graph.context.data.insert("income".to_string(), json!(180000));
64    graph.context.data.insert("customer_id".to_string(), json!(12345));
65
66    println!("Application Data:");
67    println!("  Loan Amount: $50,000");
68    println!("  Credit Score: 720");
69    println!("  Annual Income: $180,000");
70    println!("  Customer ID: 12345\n");
71
72    // Execute the graph
73    println!("Processing loan application through GRL-powered workflow...\n");
74    executor.execute(&mut graph).await?;
75
76    // Display results
77    println!("\n=== Application Results ===\n");
78
79    if let Some(validation) = graph.context.data.get("input_validation_result") {
80        println!("✓ Input Validation: {}", validation);
81    }
82
83    if let Some(customer) = graph.context.data.get("fetch_customer_result") {
84        println!("✓ Customer Data Retrieved");
85    }
86
87    if let Some(risk) = graph.context.data.get("risk_assessment_result") {
88        println!("✓ Risk Assessment: {}", risk);
89    }
90
91    if let Some(fraud) = graph.context.data.get("fraud_detection_result") {
92        println!("✓ Fraud Detection Completed");
93    }
94
95    if let Some(decision) = graph.context.data.get("approval_decision_result") {
96        println!("✓ Approval Decision: {}", decision);
97    }
98
99    println!("\n=== GRL-Powered Workflow Complete ===");
100
101    // Demonstrate standalone GRL engine
102    println!("\n=== Bonus: Advanced GRL Rules ===\n");
103
104    let mut grl_engine = RuleEngine::new();
105
106    let advanced_rules = r#"
107rule "HighValueLoan" salience 100 {
108    when
109        loan_amount > 100000 && credit_score < 750
110    then
111        requires_manual_review = true;
112        approval_tier = "senior";
113}
114
115rule "StandardApproval" salience 50 {
116    when
117        loan_amount <= 100000 && credit_score >= 650
118    then
119        auto_approve = true;
120        approval_tier = "standard";
121}
122
123rule "RiskMitigation" salience 25 {
124    when
125        debt_to_income_ratio > 0.4
126    then
127        requires_collateral = true;
128        interest_rate_adjustment = 1.5;
129}
130"#;
131
132    grl_engine.add_grl_rule(advanced_rules)?;
133
134    println!("Advanced GRL rules loaded:");
135    println!("  - High Value Loan Review");
136    println!("  - Standard Approval Process");
137    println!("  - Risk Mitigation Measures");
138
139    println!("\n✅ All systems operational with rust-rule-engine integration!");
140
141    Ok(())
142}