rust-rule-engine 1.20.3

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
//! Proof Graph for Incremental Caching and TMS Integration
//!
//! This module provides a global cache of proven facts with dependency tracking,
//! enabling reuse across multiple queries and incremental updates when facts change.
//!
//! Architecture:
//! - ProofGraph: maintains mapping from FactKeys to proven facts with justifications
//! - ProofGraphNode: represents a proven fact with its supporting premises and rules
//! - Integration with IncrementalEngine for TMS-aware retraction propagation

use crate::rete::FactHandle;
use crate::types::Value;
use std::collections::{HashMap, HashSet};
use std::sync::Arc;

/// Canonical key for identifying a fact (type + field values)
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct FactKey {
    /// Fact type (e.g., "User", "Order")
    pub fact_type: String,

    /// Field name if specific field query (e.g., "User.Score")
    pub field: Option<String>,

    /// Expected value for field (e.g., "Score >= 80")
    pub pattern: String,
}

impl FactKey {
    /// Create a new fact key from a pattern string
    pub fn from_pattern(pattern: &str) -> Self {
        // Parse pattern like "User.Score >= 80" into components
        if let Some(dot_pos) = pattern.find('.') {
            let fact_type = pattern[..dot_pos].trim().to_string();
            let rest = &pattern[dot_pos + 1..];

            // Extract field name (before operator)
            let field = if let Some(op_pos) = rest.find(|c: char| !c.is_alphanumeric() && c != '_')
            {
                Some(rest[..op_pos].trim().to_string())
            } else {
                Some(rest.trim().to_string())
            };

            Self {
                fact_type,
                field,
                pattern: pattern.to_string(),
            }
        } else {
            // Simple pattern without dot notation
            Self {
                fact_type: pattern.to_string(),
                field: None,
                pattern: pattern.to_string(),
            }
        }
    }

    /// Create from explicit components
    pub fn new(fact_type: String, field: Option<String>, pattern: String) -> Self {
        Self {
            fact_type,
            field,
            pattern,
        }
    }
}

/// A justification for a proven fact (one way it was derived)
#[derive(Debug, Clone)]
pub struct Justification {
    /// Rule that produced this fact
    pub rule_name: String,

    /// Premise fact handles that were used
    pub premises: Vec<FactHandle>,

    /// Premise keys (for human-readable tracing)
    pub premise_keys: Vec<String>,

    /// When this justification was created (generation/timestamp)
    pub generation: u64,
}

/// A node in the proof graph representing a proven fact
#[derive(Debug, Clone)]
pub struct ProofGraphNode {
    /// Unique key for this fact
    pub key: FactKey,

    /// Fact handle from IncrementalEngine (if inserted logically)
    pub handle: Option<FactHandle>,

    /// All justifications (ways this fact was proven)
    pub justifications: Vec<Justification>,

    /// Dependents (facts that depend on this fact as premise)
    pub dependents: HashSet<FactHandle>,

    /// Whether this fact is currently valid
    pub valid: bool,

    /// Generation when last validated
    pub generation: u64,

    /// Variable bindings (if any) associated with this proof
    pub bindings: HashMap<String, Value>,
}

impl ProofGraphNode {
    /// Create a new proof graph node
    pub fn new(key: FactKey) -> Self {
        Self {
            key,
            handle: None,
            justifications: Vec::new(),
            dependents: HashSet::new(),
            valid: true,
            generation: 0,
            bindings: HashMap::new(),
        }
    }

    /// Add a justification
    pub fn add_justification(
        &mut self,
        rule_name: String,
        premises: Vec<FactHandle>,
        premise_keys: Vec<String>,
        generation: u64,
    ) {
        self.justifications.push(Justification {
            rule_name,
            premises,
            premise_keys,
            generation,
        });
        self.valid = true;
        self.generation = generation;
    }

    /// Check if this node has any valid justifications
    pub fn has_valid_justifications(&self) -> bool {
        !self.justifications.is_empty()
    }

    /// Remove a justification involving a retracted premise
    pub fn remove_justifications_with_premise(&mut self, premise_handle: &FactHandle) -> bool {
        let before = self.justifications.len();
        self.justifications
            .retain(|j| !j.premises.contains(premise_handle));
        let after = self.justifications.len();

        // If no justifications left, mark invalid
        if self.justifications.is_empty() {
            self.valid = false;
        }

        before != after
    }
}

/// Global proof graph cache
pub struct ProofGraph {
    /// Nodes indexed by fact handle
    nodes_by_handle: HashMap<FactHandle, ProofGraphNode>,

    /// Index from fact key to handles (for pattern lookup)
    index_by_key: HashMap<FactKey, Vec<FactHandle>>,

    /// Reverse dependency index (premise -> dependents)
    dependencies: HashMap<FactHandle, HashSet<FactHandle>>,

    /// Generation counter for tracking updates
    generation: u64,

    /// Statistics
    pub stats: ProofGraphStats,
}

/// Statistics about proof graph usage
#[derive(Debug, Clone, Default)]
pub struct ProofGraphStats {
    pub total_nodes: usize,
    pub cache_hits: usize,
    pub cache_misses: usize,
    pub invalidations: usize,
    pub justifications_added: usize,
}

impl ProofGraph {
    /// Create a new proof graph
    pub fn new() -> Self {
        Self {
            nodes_by_handle: HashMap::new(),
            index_by_key: HashMap::new(),
            dependencies: HashMap::new(),
            generation: 0,
            stats: ProofGraphStats::default(),
        }
    }

    /// Insert a proof into the graph
    pub fn insert_proof(
        &mut self,
        handle: FactHandle,
        key: FactKey,
        rule_name: String,
        premises: Vec<FactHandle>,
        premise_keys: Vec<String>,
    ) {
        self.generation += 1;

        // Get or create node
        let node = self.nodes_by_handle.entry(handle).or_insert_with(|| {
            let mut node = ProofGraphNode::new(key.clone());
            node.handle = Some(handle);
            self.stats.total_nodes += 1;
            node
        });

        // Add justification
        node.add_justification(rule_name, premises.clone(), premise_keys, self.generation);
        self.stats.justifications_added += 1;

        // Update key index
        self.index_by_key
            .entry(key.clone())
            .or_default()
            .push(handle);

        // Update dependency edges
        for premise in &premises {
            self.dependencies
                .entry(*premise)
                .or_default()
                .insert(handle);

            // Also update the premise node's dependents
            if let Some(premise_node) = self.nodes_by_handle.get_mut(premise) {
                premise_node.dependents.insert(handle);
            }
        }
    }

    /// Lookup proven facts by key pattern
    pub fn lookup_by_key(&mut self, key: &FactKey) -> Option<Vec<&ProofGraphNode>> {
        if let Some(handles) = self.index_by_key.get(key) {
            let nodes: Vec<&ProofGraphNode> = handles
                .iter()
                .filter_map(|h| self.nodes_by_handle.get(h))
                .filter(|n| n.valid)
                .collect();

            if !nodes.is_empty() {
                self.stats.cache_hits += 1;
                Some(nodes)
            } else {
                self.stats.cache_misses += 1;
                None
            }
        } else {
            self.stats.cache_misses += 1;
            None
        }
    }

    /// Check if a fact key has been proven
    pub fn is_proven(&mut self, key: &FactKey) -> bool {
        self.lookup_by_key(key).is_some()
    }

    /// Invalidate a fact handle (e.g., when retracted by TMS)
    pub fn invalidate_handle(&mut self, handle: &FactHandle) {
        self.stats.invalidations += 1;

        // Get dependents before removing
        let dependents = self.dependencies.get(handle).cloned();

        // Mark node invalid
        if let Some(node) = self.nodes_by_handle.get_mut(handle) {
            node.valid = false;
        }

        // Propagate to dependents
        if let Some(deps) = dependents {
            for dep_handle in deps {
                self.propagate_invalidation(&dep_handle, handle);
            }
        }
    }

    /// Propagate invalidation to a dependent fact
    fn propagate_invalidation(
        &mut self,
        dependent_handle: &FactHandle,
        premise_handle: &FactHandle,
    ) {
        if let Some(node) = self.nodes_by_handle.get_mut(dependent_handle) {
            // Remove justifications that depend on this premise
            let changed = node.remove_justifications_with_premise(premise_handle);

            // If node became invalid (no justifications left), propagate further
            if changed && !node.valid {
                self.stats.invalidations += 1;

                // Get dependents and propagate recursively
                let further_deps = node.dependents.clone();
                for further_dep in further_deps {
                    self.propagate_invalidation(&further_dep, dependent_handle);
                }
            }
        }
    }

    /// Get a node by handle
    pub fn get_node(&self, handle: &FactHandle) -> Option<&ProofGraphNode> {
        self.nodes_by_handle.get(handle)
    }

    /// Clear all cached proofs (reset graph)
    pub fn clear(&mut self) {
        self.nodes_by_handle.clear();
        self.index_by_key.clear();
        self.dependencies.clear();
        self.generation = 0;
        self.stats = ProofGraphStats::default();
    }

    /// Get current generation counter
    pub fn generation(&self) -> u64 {
        self.generation
    }

    /// Print statistics
    pub fn print_stats(&self) {
        println!("ProofGraph Statistics:");
        println!("  Total nodes: {}", self.stats.total_nodes);
        println!("  Cache hits: {}", self.stats.cache_hits);
        println!("  Cache misses: {}", self.stats.cache_misses);
        println!("  Invalidations: {}", self.stats.invalidations);
        println!(
            "  Justifications added: {}",
            self.stats.justifications_added
        );

        if self.stats.cache_hits + self.stats.cache_misses > 0 {
            let hit_rate = (self.stats.cache_hits as f64)
                / ((self.stats.cache_hits + self.stats.cache_misses) as f64)
                * 100.0;
            println!("  Cache hit rate: {:.1}%", hit_rate);
        }
    }
}

impl Default for ProofGraph {
    fn default() -> Self {
        Self::new()
    }
}

/// Thread-safe wrapper for ProofGraph
pub type SharedProofGraph = Arc<std::sync::Mutex<ProofGraph>>;

/// Create a new shared proof graph
pub fn new_shared() -> SharedProofGraph {
    Arc::new(std::sync::Mutex::new(ProofGraph::new()))
}

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

    #[test]
    fn test_fact_key_from_pattern() {
        let key = FactKey::from_pattern("User.Score >= 80");
        assert_eq!(key.fact_type, "User");
        assert_eq!(key.field, Some("Score".to_string()));
        assert_eq!(key.pattern, "User.Score >= 80");
    }

    #[test]
    fn test_proof_graph_insert_and_lookup() {
        let mut graph = ProofGraph::new();
        let handle = FactHandle::new(1);
        let key = FactKey::from_pattern("User.Score >= 80");

        graph.insert_proof(handle, key.clone(), "ScoreRule".to_string(), vec![], vec![]);

        assert!(graph.is_proven(&key));
        assert_eq!(graph.stats.total_nodes, 1);
    }

    #[test]
    fn test_dependency_tracking() {
        let mut graph = ProofGraph::new();
        let premise_handle = FactHandle::new(1);
        let conclusion_handle = FactHandle::new(2);

        let premise_key = FactKey::from_pattern("User.Age >= 18");
        let conclusion_key = FactKey::from_pattern("User.CanVote == true");

        // Insert premise
        graph.insert_proof(
            premise_handle,
            premise_key.clone(),
            "AgeRule".to_string(),
            vec![],
            vec![],
        );

        // Insert conclusion depending on premise
        graph.insert_proof(
            conclusion_handle,
            conclusion_key.clone(),
            "VotingRule".to_string(),
            vec![premise_handle],
            vec!["User.Age >= 18".to_string()],
        );

        assert!(graph.is_proven(&premise_key));
        assert!(graph.is_proven(&conclusion_key));

        // Invalidate premise
        graph.invalidate_handle(&premise_handle);

        // Conclusion should now be invalid
        let conclusion_node = graph.get_node(&conclusion_handle).unwrap();
        assert!(!conclusion_node.valid);
        assert_eq!(graph.stats.invalidations, 2); // premise + dependent
    }

    #[test]
    fn test_multiple_justifications() {
        let mut graph = ProofGraph::new();
        let handle = FactHandle::new(1);
        let key = FactKey::from_pattern("User.IsVIP == true");

        // Add first justification
        graph.insert_proof(
            handle,
            key.clone(),
            "HighSpenderRule".to_string(),
            vec![],
            vec![],
        );

        // Add second justification for same fact
        graph.insert_proof(
            handle,
            key.clone(),
            "LoyaltyRule".to_string(),
            vec![],
            vec![],
        );

        let node = graph.get_node(&handle).unwrap();
        assert_eq!(node.justifications.len(), 2);
        assert!(node.valid);
    }

    #[test]
    fn test_cache_statistics() {
        let mut graph = ProofGraph::new();
        let key = FactKey::from_pattern("User.Active == true");

        // Miss
        assert!(!graph.is_proven(&key));
        assert_eq!(graph.stats.cache_misses, 1);

        // Insert
        let handle = FactHandle::new(1);
        graph.insert_proof(
            handle,
            key.clone(),
            "ActiveRule".to_string(),
            vec![],
            vec![],
        );

        // Hit
        assert!(graph.is_proven(&key));
        assert_eq!(graph.stats.cache_hits, 1);
    }

    #[test]
    fn test_clear() {
        let mut graph = ProofGraph::new();
        let handle = FactHandle::new(1);
        let key = FactKey::from_pattern("Test.Value == 42");

        graph.insert_proof(handle, key.clone(), "TestRule".to_string(), vec![], vec![]);
        assert!(graph.is_proven(&key));

        graph.clear();
        assert!(!graph.is_proven(&key));
        assert_eq!(graph.stats.total_nodes, 0);
    }
}