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
//! Pattern Matching with Variable Binding (P3 Feature - Advanced)
//!
//! This module implements Drools-style pattern matching with:
//! - Variable binding ($var)
//! - Multi-object patterns
//! - Join conditions between patterns
//! - Field constraints with variables

use super::facts::{FactValue, TypedFacts};
use super::multifield::MultifieldOp;
use super::working_memory::{FactHandle, WorkingMemory};
use std::collections::HashMap;

/// Variable name (e.g., "$name", "$age")
pub type Variable = String;

/// Pattern constraint with optional variable binding
#[derive(Debug, Clone)]
pub enum PatternConstraint {
    /// Simple constraint: field op value
    Simple {
        field: String,
        operator: String,
        value: FactValue,
    },
    /// Binding constraint: field = $var (binds value to variable)
    Binding { field: String, variable: Variable },
    /// Variable constraint: field op $var (compare with bound variable)
    Variable {
        field: String,
        operator: String,
        variable: Variable,
    },
    /// Multi-field constraint: pattern matching on arrays/collections
    ///
    /// Examples:
    /// - `Order.items $?all_items` - Collect all items (Collect)
    /// - `Product.tags contains "electronics"` - Check containment (Contains)
    /// - `Order.items count > 0` - Get array length (Count)
    MultiField {
        field: String,
        variable: Option<Variable>, // $?var for multi-field binding
        operator: MultifieldOp,
        value: Option<FactValue>, // For operations like Contains
    },
}

impl PatternConstraint {
    /// Create simple constraint
    pub fn simple(field: String, operator: String, value: FactValue) -> Self {
        Self::Simple {
            field,
            operator,
            value,
        }
    }

    /// Create binding constraint
    pub fn binding(field: String, variable: Variable) -> Self {
        Self::Binding { field, variable }
    }

    /// Create variable constraint
    pub fn variable(field: String, operator: String, variable: Variable) -> Self {
        Self::Variable {
            field,
            operator,
            variable,
        }
    }

    /// Create multi-field constraint
    pub fn multifield(
        field: String,
        operator: MultifieldOp,
        variable: Option<Variable>,
        value: Option<FactValue>,
    ) -> Self {
        Self::MultiField {
            field,
            operator,
            variable,
            value,
        }
    }

    /// Evaluate constraint against facts and bindings
    pub fn evaluate(
        &self,
        facts: &TypedFacts,
        bindings: &HashMap<Variable, FactValue>,
    ) -> Option<HashMap<Variable, FactValue>> {
        match self {
            PatternConstraint::Simple {
                field,
                operator,
                value,
            } => {
                if facts.evaluate_condition(field, operator, value) {
                    Some(HashMap::new())
                } else {
                    None
                }
            }
            PatternConstraint::Binding { field, variable } => {
                if let Some(fact_value) = facts.get(field) {
                    let mut new_bindings = HashMap::new();
                    new_bindings.insert(variable.clone(), fact_value.clone());
                    Some(new_bindings)
                } else {
                    None
                }
            }
            PatternConstraint::Variable {
                field,
                operator,
                variable,
            } => {
                if let Some(bound_value) = bindings.get(variable) {
                    if facts.evaluate_condition(field, operator, bound_value) {
                        Some(HashMap::new())
                    } else {
                        None
                    }
                } else {
                    None // Variable not bound yet
                }
            }
            PatternConstraint::MultiField {
                field,
                operator,
                variable,
                value,
            } => {
                // Delegate to multifield evaluation helper
                super::multifield::evaluate_multifield_pattern(
                    facts,
                    field,
                    operator,
                    variable.as_deref(),
                    value.as_ref(),
                    bindings,
                )
            }
        }
    }
}

/// A pattern matches facts of a specific type with constraints
#[derive(Debug, Clone)]
pub struct Pattern {
    /// Fact type (e.g., "Person", "Order")
    pub fact_type: String,
    /// List of constraints
    pub constraints: Vec<PatternConstraint>,
    /// Optional pattern name for reference
    pub name: Option<String>,
}

impl Pattern {
    /// Create new pattern
    pub fn new(fact_type: String) -> Self {
        Self {
            fact_type,
            constraints: Vec::new(),
            name: None,
        }
    }

    /// Add constraint
    pub fn with_constraint(mut self, constraint: PatternConstraint) -> Self {
        self.constraints.push(constraint);
        self
    }

    /// Set pattern name
    pub fn with_name(mut self, name: String) -> Self {
        self.name = Some(name);
        self
    }

    /// Match this pattern against a fact
    pub fn matches(
        &self,
        facts: &TypedFacts,
        bindings: &HashMap<Variable, FactValue>,
    ) -> Option<HashMap<Variable, FactValue>> {
        let mut new_bindings = bindings.clone();

        for constraint in &self.constraints {
            match constraint.evaluate(facts, &new_bindings) {
                Some(additional_bindings) => {
                    new_bindings.extend(additional_bindings);
                }
                None => return None,
            }
        }

        Some(new_bindings)
    }

    /// Match pattern against working memory
    pub fn match_in_working_memory(
        &self,
        wm: &WorkingMemory,
        bindings: &HashMap<Variable, FactValue>,
    ) -> Vec<(FactHandle, HashMap<Variable, FactValue>)> {
        let mut results = Vec::new();

        for fact in wm.get_by_type(&self.fact_type) {
            if let Some(new_bindings) = self.matches(&fact.data, bindings) {
                results.push((fact.handle, new_bindings));
            }
        }

        results
    }
}

/// Multi-pattern rule with joins
#[derive(Debug, Clone)]
pub struct MultiPattern {
    /// List of patterns that must all match
    pub patterns: Vec<Pattern>,
    /// Rule name
    pub name: String,
}

impl MultiPattern {
    /// Create new multi-pattern rule
    pub fn new(name: String) -> Self {
        Self {
            patterns: Vec::new(),
            name,
        }
    }

    /// Add pattern
    pub fn with_pattern(mut self, pattern: Pattern) -> Self {
        self.patterns.push(pattern);
        self
    }

    /// Match all patterns (with variable binding across patterns)
    pub fn match_all(
        &self,
        wm: &WorkingMemory,
    ) -> Vec<(Vec<FactHandle>, HashMap<Variable, FactValue>)> {
        if self.patterns.is_empty() {
            return Vec::new();
        }

        // Start with first pattern
        let mut results = Vec::new();
        let first_pattern = &self.patterns[0];
        let empty_bindings = HashMap::new();

        for (handle, bindings) in first_pattern.match_in_working_memory(wm, &empty_bindings) {
            results.push((vec![handle], bindings));
        }

        // Join with remaining patterns
        for pattern in &self.patterns[1..] {
            let mut new_results = Vec::new();

            for (handles, bindings) in results {
                for (handle, new_bindings) in pattern.match_in_working_memory(wm, &bindings) {
                    let mut combined_handles = handles.clone();
                    combined_handles.push(handle);
                    new_results.push((combined_handles, new_bindings));
                }
            }

            results = new_results;

            if results.is_empty() {
                break; // No matches, stop early
            }
        }

        results
    }
}

/// Pattern builder for easier construction (Drools-style DSL)
pub struct PatternBuilder {
    pattern: Pattern,
}

impl PatternBuilder {
    /// Start building pattern for a fact type
    pub fn for_type(fact_type: impl Into<String>) -> Self {
        Self {
            pattern: Pattern::new(fact_type.into()),
        }
    }

    /// Add simple constraint (field op value)
    pub fn where_field(
        mut self,
        field: impl Into<String>,
        operator: impl Into<String>,
        value: FactValue,
    ) -> Self {
        self.pattern.constraints.push(PatternConstraint::Simple {
            field: field.into(),
            operator: operator.into(),
            value,
        });
        self
    }

    /// Bind field to variable ($var)
    pub fn bind(mut self, field: impl Into<String>, variable: impl Into<String>) -> Self {
        self.pattern.constraints.push(PatternConstraint::Binding {
            field: field.into(),
            variable: variable.into(),
        });
        self
    }

    /// Compare field with variable ($var)
    pub fn where_var(
        mut self,
        field: impl Into<String>,
        operator: impl Into<String>,
        variable: impl Into<String>,
    ) -> Self {
        self.pattern.constraints.push(PatternConstraint::Variable {
            field: field.into(),
            operator: operator.into(),
            variable: variable.into(),
        });
        self
    }

    /// Set pattern name
    pub fn named(mut self, name: impl Into<String>) -> Self {
        self.pattern.name = Some(name.into());
        self
    }

    /// Build the pattern
    pub fn build(self) -> Pattern {
        self.pattern
    }
}

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

    #[test]
    fn test_simple_pattern() {
        let pattern = PatternBuilder::for_type("Person")
            .where_field("age", ">", FactValue::Integer(18))
            .where_field("status", "==", FactValue::String("active".to_string()))
            .build();

        let mut facts = TypedFacts::new();
        facts.set("age", 25i64);
        facts.set("status", "active");

        let bindings = HashMap::new();
        let result = pattern.matches(&facts, &bindings);
        assert!(result.is_some());
    }

    #[test]
    fn test_variable_binding() {
        let pattern = PatternBuilder::for_type("Person")
            .bind("name", "$personName")
            .bind("age", "$personAge")
            .build();

        let mut facts = TypedFacts::new();
        facts.set("name", "John");
        facts.set("age", 25i64);

        let bindings = HashMap::new();
        let result = pattern.matches(&facts, &bindings).unwrap();

        assert_eq!(result.get("$personName").unwrap().as_string(), "John");
        assert_eq!(result.get("$personAge").unwrap().as_integer(), Some(25));
    }

    #[test]
    fn test_variable_constraint() {
        // First bind $minAge
        let mut bindings = HashMap::new();
        bindings.insert("$minAge".to_string(), FactValue::Integer(18));

        // Then use $minAge in constraint
        let pattern = PatternBuilder::for_type("Person")
            .where_var("age", ">=", "$minAge")
            .build();

        let mut facts = TypedFacts::new();
        facts.set("age", 25i64);

        let result = pattern.matches(&facts, &bindings);
        assert!(result.is_some());
    }

    #[test]
    fn test_multi_pattern_join() {
        let mut wm = WorkingMemory::new();

        // Insert Person
        let mut person = TypedFacts::new();
        person.set("name", "John");
        person.set("age", 25i64);
        wm.insert("Person".to_string(), person);

        // Insert Order for John
        let mut order = TypedFacts::new();
        order.set("customer", "John");
        order.set("amount", 1000.0);
        wm.insert("Order".to_string(), order);

        // Multi-pattern: Person($name) AND Order(customer == $name)
        let person_pattern = PatternBuilder::for_type("Person")
            .bind("name", "$name")
            .build();

        let order_pattern = PatternBuilder::for_type("Order")
            .where_var("customer", "==", "$name")
            .build();

        let multi = MultiPattern::new("PersonWithOrder".to_string())
            .with_pattern(person_pattern)
            .with_pattern(order_pattern);

        let matches = multi.match_all(&wm);
        assert_eq!(matches.len(), 1);

        let (handles, bindings) = &matches[0];
        assert_eq!(handles.len(), 2);
        assert_eq!(bindings.get("$name").unwrap().as_string(), "John");
    }
}