rust-rule-engine 1.20.1

A blazing-fast Rust rule engine with RETE algorithm, backward chaining inference, and GRL (Grule Rule Language) syntax. Features: forward/backward chaining, pattern matching, unification, O(1) rule indexing, TMS, expression evaluation, method calls, streaming with Redis state backend, watermarking, and custom functions. Production-ready for business rules, expert systems, real-time stream processing, and decision automation.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
// Proof Tree for Backward Chaining Explanations
//
// This module provides data structures for capturing and visualizing
// the reasoning process in backward chaining queries.
//
// Version: 1.9.0

use super::unification::Bindings;
use serde::{Deserialize, Serialize};
use std::collections::HashMap;

/// Represents a single node in the proof tree
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ProofNode {
    /// The goal that was proven at this node
    pub goal: String,

    /// Name of the rule that was used (if any)
    pub rule_name: Option<String>,

    /// Variable bindings at this node
    #[serde(skip_serializing_if = "HashMap::is_empty")]
    pub bindings: HashMap<String, String>,

    /// Child nodes (sub-goals that were proven)
    #[serde(skip_serializing_if = "Vec::is_empty")]
    pub children: Vec<ProofNode>,

    /// Depth in the proof tree
    pub depth: usize,

    /// Whether this goal was proven successfully
    pub proven: bool,

    /// Type of proof node
    pub node_type: ProofNodeType,
}

/// Type of proof node
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub enum ProofNodeType {
    /// Goal proven by a fact
    Fact,

    /// Goal proven by a rule
    Rule,

    /// Negated goal (NOT)
    Negation,

    /// Goal failed to prove
    Failed,
}

impl ProofNode {
    /// Create a new proof node
    pub fn new(goal: String, depth: usize) -> Self {
        ProofNode {
            goal,
            rule_name: None,
            bindings: HashMap::new(),
            children: Vec::new(),
            depth,
            proven: false,
            node_type: ProofNodeType::Failed,
        }
    }

    /// Create a fact node
    pub fn fact(goal: String, depth: usize) -> Self {
        ProofNode {
            goal,
            rule_name: None,
            bindings: HashMap::new(),
            children: Vec::new(),
            depth,
            proven: true,
            node_type: ProofNodeType::Fact,
        }
    }

    /// Create a rule node
    pub fn rule(goal: String, rule_name: String, depth: usize) -> Self {
        ProofNode {
            goal,
            rule_name: Some(rule_name),
            bindings: HashMap::new(),
            children: Vec::new(),
            depth,
            proven: true,
            node_type: ProofNodeType::Rule,
        }
    }

    /// Create a negation node
    pub fn negation(goal: String, depth: usize, proven: bool) -> Self {
        ProofNode {
            goal,
            rule_name: None,
            bindings: HashMap::new(),
            children: Vec::new(),
            depth,
            proven,
            node_type: ProofNodeType::Negation,
        }
    }

    /// Add a child node
    pub fn add_child(&mut self, child: ProofNode) {
        self.children.push(child);
    }

    /// Set bindings from Bindings object
    pub fn set_bindings(&mut self, bindings: &Bindings) {
        // Convert Bindings to HashMap using to_map() method
        let binding_map = bindings.to_map();
        self.bindings = binding_map
            .iter()
            .map(|(k, v)| (k.clone(), format!("{:?}", v)))
            .collect();
    }

    /// Set bindings from HashMap
    pub fn set_bindings_map(&mut self, bindings: HashMap<String, String>) {
        self.bindings = bindings;
    }

    /// Check if this is a leaf node
    pub fn is_leaf(&self) -> bool {
        self.children.is_empty()
    }

    /// Print the proof tree
    pub fn print_tree(&self, indent: usize) {
        let prefix = "  ".repeat(indent);
        let status = if self.proven { "" } else { "" };

        println!("{}{} {}", prefix, status, self.goal);

        if let Some(rule) = &self.rule_name {
            println!("{}  [Rule: {}]", prefix, rule);
        }

        match self.node_type {
            ProofNodeType::Fact => println!("{}  [FACT]", prefix),
            ProofNodeType::Negation => println!("{}  [NEGATION]", prefix),
            _ => {}
        }

        if !self.bindings.is_empty() {
            println!("{}  Bindings: {:?}", prefix, self.bindings);
        }

        for child in &self.children {
            child.print_tree(indent + 1);
        }
    }

    /// Get tree height
    pub fn height(&self) -> usize {
        if self.children.is_empty() {
            1
        } else {
            1 + self.children.iter().map(|c| c.height()).max().unwrap_or(0)
        }
    }

    /// Count total nodes
    pub fn node_count(&self) -> usize {
        1 + self.children.iter().map(|c| c.node_count()).sum::<usize>()
    }
}

/// Complete proof tree with metadata
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ProofTree {
    /// Root node of the proof tree
    pub root: ProofNode,

    /// Whether the query was proven
    pub success: bool,

    /// Original query string
    pub query: String,

    /// Statistics
    pub stats: ProofStats,
}

/// Statistics about the proof
#[derive(Debug, Clone, Default, Serialize, Deserialize)]
pub struct ProofStats {
    /// Total number of goals explored
    pub goals_explored: usize,

    /// Total number of rules evaluated
    pub rules_evaluated: usize,

    /// Total number of facts checked
    pub facts_checked: usize,

    /// Maximum depth reached
    pub max_depth: usize,

    /// Total nodes in proof tree
    pub total_nodes: usize,
}

impl ProofTree {
    /// Create a new proof tree
    pub fn new(root: ProofNode, query: String) -> Self {
        let success = root.proven;
        let total_nodes = root.node_count();
        let max_depth = root.height();

        ProofTree {
            root,
            success,
            query,
            stats: ProofStats {
                goals_explored: 0,
                rules_evaluated: 0,
                facts_checked: 0,
                max_depth,
                total_nodes,
            },
        }
    }

    /// Set statistics
    pub fn set_stats(&mut self, stats: ProofStats) {
        self.stats = stats;
    }

    /// Print the entire tree
    pub fn print(&self) {
        println!("Query: {}", self.query);
        println!(
            "Result: {}",
            if self.success {
                "✓ Proven"
            } else {
                "✗ Unprovable"
            }
        );
        println!("\nProof Tree:");
        println!("{}", "=".repeat(80));
        self.root.print_tree(0);
        println!("{}", "=".repeat(80));
        self.print_stats();
    }

    /// Print statistics
    pub fn print_stats(&self) {
        println!("\nStatistics:");
        println!("  Goals explored: {}", self.stats.goals_explored);
        println!("  Rules evaluated: {}", self.stats.rules_evaluated);
        println!("  Facts checked: {}", self.stats.facts_checked);
        println!("  Max depth: {}", self.stats.max_depth);
        println!("  Total nodes: {}", self.stats.total_nodes);
    }

    /// Convert to JSON string
    pub fn to_json(&self) -> Result<String, serde_json::Error> {
        serde_json::to_string_pretty(self)
    }

    /// Convert to Markdown
    pub fn to_markdown(&self) -> String {
        let mut md = String::new();

        md.push_str("# Proof Explanation\n\n");
        md.push_str(&format!("**Query:** `{}`\n\n", self.query));
        md.push_str(&format!(
            "**Result:** {}\n\n",
            if self.success {
                "✓ Proven"
            } else {
                "✗ Unprovable"
            }
        ));

        md.push_str("## Proof Tree\n\n");
        Self::node_to_markdown(&self.root, &mut md, 0);

        md.push_str("\n## Statistics\n\n");
        md.push_str(&format!(
            "- **Goals explored:** {}\n",
            self.stats.goals_explored
        ));
        md.push_str(&format!(
            "- **Rules evaluated:** {}\n",
            self.stats.rules_evaluated
        ));
        md.push_str(&format!(
            "- **Facts checked:** {}\n",
            self.stats.facts_checked
        ));
        md.push_str(&format!("- **Max depth:** {}\n", self.stats.max_depth));
        md.push_str(&format!("- **Total nodes:** {}\n", self.stats.total_nodes));

        md
    }

    /// Convert node to markdown recursively
    fn node_to_markdown(node: &ProofNode, md: &mut String, depth: usize) {
        let prefix = "  ".repeat(depth);
        let status = if node.proven { "" } else { "" };

        md.push_str(&format!("{}* {} `{}`", prefix, status, node.goal));

        if let Some(rule) = &node.rule_name {
            md.push_str(&format!(" **[Rule: {}]**", rule));
        }

        match node.node_type {
            ProofNodeType::Fact => md.push_str(" *[FACT]*"),
            ProofNodeType::Negation => md.push_str(" *[NEGATION]*"),
            _ => {}
        }

        md.push('\n');

        if !node.bindings.is_empty() {
            md.push_str(&format!("{}  * Bindings: `{:?}`\n", prefix, node.bindings));
        }

        for child in &node.children {
            Self::node_to_markdown(child, md, depth + 1);
        }
    }

    /// Convert to HTML
    pub fn to_html(&self) -> String {
        let mut html = String::new();

        html.push_str("<!DOCTYPE html>\n<html>\n<head>\n");
        html.push_str("  <title>Proof Explanation</title>\n");
        html.push_str("  <style>\n");
        html.push_str("    body { font-family: 'Courier New', monospace; margin: 20px; }\n");
        html.push_str("    .proven { color: green; }\n");
        html.push_str("    .failed { color: red; }\n");
        html.push_str("    .node { margin-left: 20px; }\n");
        html.push_str("    .rule { color: blue; font-style: italic; }\n");
        html.push_str("    .bindings { color: gray; font-size: 0.9em; }\n");
        html.push_str("    .stats { margin-top: 20px; padding: 10px; background: #f0f0f0; }\n");
        html.push_str("  </style>\n");
        html.push_str("</head>\n<body>\n");

        html.push_str("<h1>Proof Explanation</h1>\n");
        html.push_str(&format!(
            "<p><strong>Query:</strong> <code>{}</code></p>\n",
            self.query
        ));
        html.push_str(&format!(
            "<p><strong>Result:</strong> <span class=\"{}\">{}</span></p>\n",
            if self.success { "proven" } else { "failed" },
            if self.success {
                "✓ Proven"
            } else {
                "✗ Unprovable"
            }
        ));

        html.push_str("<h2>Proof Tree</h2>\n");
        Self::node_to_html(&self.root, &mut html);

        html.push_str("<div class=\"stats\">\n");
        html.push_str("<h2>Statistics</h2>\n");
        html.push_str(&format!(
            "<p>Goals explored: {}</p>\n",
            self.stats.goals_explored
        ));
        html.push_str(&format!(
            "<p>Rules evaluated: {}</p>\n",
            self.stats.rules_evaluated
        ));
        html.push_str(&format!(
            "<p>Facts checked: {}</p>\n",
            self.stats.facts_checked
        ));
        html.push_str(&format!("<p>Max depth: {}</p>\n", self.stats.max_depth));
        html.push_str(&format!("<p>Total nodes: {}</p>\n", self.stats.total_nodes));
        html.push_str("</div>\n");

        html.push_str("</body>\n</html>");
        html
    }

    /// Convert node to HTML recursively
    fn node_to_html(node: &ProofNode, html: &mut String) {
        let status = if node.proven { "" } else { "" };
        let class = if node.proven { "proven" } else { "failed" };

        html.push_str("<div class=\"node\">\n");
        html.push_str(&format!(
            "  <span class=\"{}\">{} {}</span>",
            class, status, node.goal
        ));

        if let Some(rule) = &node.rule_name {
            html.push_str(&format!(" <span class=\"rule\">[Rule: {}]</span>", rule));
        }

        match node.node_type {
            ProofNodeType::Fact => html.push_str(" <em>[FACT]</em>"),
            ProofNodeType::Negation => html.push_str(" <em>[NEGATION]</em>"),
            _ => {}
        }

        if !node.bindings.is_empty() {
            html.push_str(&format!(
                "<br><span class=\"bindings\">Bindings: {:?}</span>",
                node.bindings
            ));
        }

        html.push('\n');

        for child in &node.children {
            Self::node_to_html(child, html);
        }

        html.push_str("</div>\n");
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_proof_node_creation() {
        let node = ProofNode::new("test_goal".to_string(), 0);
        assert_eq!(node.goal, "test_goal");
        assert_eq!(node.depth, 0);
        assert!(!node.proven);
        assert_eq!(node.node_type, ProofNodeType::Failed);
    }

    #[test]
    fn test_fact_node() {
        let node = ProofNode::fact("fact_goal".to_string(), 1);
        assert!(node.proven);
        assert_eq!(node.node_type, ProofNodeType::Fact);
        assert!(node.is_leaf());
    }

    #[test]
    fn test_rule_node() {
        let node = ProofNode::rule("rule_goal".to_string(), "test_rule".to_string(), 2);
        assert!(node.proven);
        assert_eq!(node.node_type, ProofNodeType::Rule);
        assert_eq!(node.rule_name, Some("test_rule".to_string()));
    }

    #[test]
    fn test_add_child() {
        let mut parent = ProofNode::rule("parent".to_string(), "rule1".to_string(), 0);
        let child = ProofNode::fact("child".to_string(), 1);

        parent.add_child(child);
        assert_eq!(parent.children.len(), 1);
        assert!(!parent.is_leaf());
    }

    #[test]
    fn test_tree_height() {
        let mut root = ProofNode::rule("root".to_string(), "rule1".to_string(), 0);
        let mut child1 = ProofNode::rule("child1".to_string(), "rule2".to_string(), 1);
        let child2 = ProofNode::fact("child2".to_string(), 2);

        child1.add_child(child2);
        root.add_child(child1);

        assert_eq!(root.height(), 3);
    }

    #[test]
    fn test_node_count() {
        let mut root = ProofNode::rule("root".to_string(), "rule1".to_string(), 0);
        let child1 = ProofNode::fact("child1".to_string(), 1);
        let child2 = ProofNode::fact("child2".to_string(), 1);

        root.add_child(child1);
        root.add_child(child2);

        assert_eq!(root.node_count(), 3);
    }

    #[test]
    fn test_proof_tree_creation() {
        let root = ProofNode::fact("test".to_string(), 0);
        let tree = ProofTree::new(root, "test query".to_string());

        assert!(tree.success);
        assert_eq!(tree.query, "test query");
        assert_eq!(tree.stats.total_nodes, 1);
    }

    #[test]
    fn test_json_serialization() {
        let root = ProofNode::fact("test".to_string(), 0);
        let tree = ProofTree::new(root, "test query".to_string());

        let json = tree.to_json().unwrap();
        assert!(json.contains("test query"));
        assert!(json.contains("Fact"));
    }

    #[test]
    fn test_markdown_generation() {
        let root = ProofNode::fact("test".to_string(), 0);
        let tree = ProofTree::new(root, "test query".to_string());

        let md = tree.to_markdown();
        assert!(md.contains("# Proof Explanation"));
        assert!(md.contains("test query"));
        assert!(md.contains(""));
    }

    #[test]
    fn test_html_generation() {
        let root = ProofNode::fact("test".to_string(), 0);
        let tree = ProofTree::new(root, "test query".to_string());

        let html = tree.to_html();
        assert!(html.contains("<!DOCTYPE html>"));
        assert!(html.contains("test query"));
        assert!(html.contains(""));
    }
}