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
//! Multi-field (Multislot) Variables - CLIPS-inspired Feature
//!
//! This module implements CLIPS-style multi-field variables for pattern matching
//! on arrays and collections. Multi-field variables allow:
//!
//! - Collecting all values: `Order.items $?all_items`
//! - Checking containment: `Product.tags contains "electronics"`
//! - Getting array length: `Order.items count > 0`
//! - Accessing elements: `Order.items first`, `Order.items last`
//!
//! ## CLIPS Reference
//!
//! ```clips
//! (deftemplate order
//!   (slot order-id)
//!   (multislot items))
//!
//! (defrule process-order
//!   (order (order-id ?id) (items $?all-items))
//!   =>
//!   (foreach ?item $?all-items
//!     (process ?item)))
//! ```
//!
//! ## Rust API
//!
//! ```rust,ignore
//! use rust_rule_engine::rete::{MultifieldOp, PatternConstraint};
//!
//! // Pattern: Order.items $?all_items
//! let constraint = PatternConstraint::MultiField {
//!     field: "items".to_string(),
//!     variable: Some("$?all_items".to_string()),
//!     operator: MultifieldOp::Collect,
//!     value: None,
//! };
//!
//! // Pattern: Product.tags contains "electronics"
//! let constraint = PatternConstraint::MultiField {
//!     field: "tags".to_string(),
//!     variable: None,
//!     operator: MultifieldOp::Contains,
//!     value: Some(FactValue::String("electronics".to_string())),
//! };
//! ```

use crate::rete::facts::{FactValue, TypedFacts};
use std::collections::HashMap;

/// Multi-field operations for pattern matching
///
/// These operations enable CLIPS-style multi-field variable matching and manipulation.
#[derive(Debug, Clone, PartialEq)]
pub enum MultifieldOp {
    /// Collect all values into a variable: `$?var`
    ///
    /// Example: `Order.items $?all_items` binds all items to `$?all_items`
    Collect,

    /// Check if array contains a specific value
    ///
    /// Example: `Product.tags contains "electronics"`
    Contains,

    /// Get the count/length of the array
    ///
    /// Example: `Order.items count` returns the number of items
    Count,

    /// Get the first element of the array
    ///
    /// Example: `Order.items first` returns the first item
    First,

    /// Get the last element of the array
    ///
    /// Example: `Order.items last` returns the last item
    Last,

    /// Get a specific element by index (0-based)
    ///
    /// Example: `Order.items[0]` returns the first item
    Index(usize),

    /// Get a slice of the array [start:end]
    ///
    /// Example: `Order.items[1:3]` returns items at index 1 and 2
    Slice(usize, usize),

    /// Check if array is empty
    ///
    /// Example: `Order.items empty`
    IsEmpty,

    /// Check if array is not empty
    ///
    /// Example: `Order.items not_empty`
    NotEmpty,
}

impl MultifieldOp {
    /// Evaluate a multi-field operation on facts
    ///
    /// Returns:
    /// - `Some(Vec<FactValue>)` - Collection of values (for Collect, Slice)
    /// - `Some(vec![FactValue::Integer(n)])` - Numeric result (for Count, Index)
    /// - `Some(vec![FactValue::Boolean(b)])` - Boolean result (for Contains, IsEmpty, etc.)
    /// - `None` - Operation failed (field not found, invalid type, etc.)
    pub fn evaluate(
        &self,
        facts: &TypedFacts,
        field: &str,
        value: Option<&FactValue>,
    ) -> Option<Vec<FactValue>> {
        // Get the field value
        let field_value = facts.get(field)?;

        // Ensure it's an array
        let array = match field_value {
            FactValue::Array(arr) => arr,
            _ => return None, // Not an array
        };

        match self {
            MultifieldOp::Collect => {
                // Return all values
                Some(array.clone())
            }

            MultifieldOp::Contains => {
                // Check if array contains the specified value
                let search_value = value?;
                let contains = array.contains(search_value);
                Some(vec![FactValue::Boolean(contains)])
            }

            MultifieldOp::Count => {
                // Return array length
                Some(vec![FactValue::Integer(array.len() as i64)])
            }

            MultifieldOp::First => {
                // Return first element
                array.first().cloned().map(|v| vec![v])
            }

            MultifieldOp::Last => {
                // Return last element
                array.last().cloned().map(|v| vec![v])
            }

            MultifieldOp::Index(idx) => {
                // Return element at index
                array.get(*idx).cloned().map(|v| vec![v])
            }

            MultifieldOp::Slice(start, end) => {
                // Return slice of array
                let end = (*end).min(array.len());
                if *start >= end {
                    return Some(Vec::new());
                }
                Some(array[*start..end].to_vec())
            }

            MultifieldOp::IsEmpty => {
                // Check if array is empty
                Some(vec![FactValue::Boolean(array.is_empty())])
            }

            MultifieldOp::NotEmpty => {
                // Check if array is not empty
                Some(vec![FactValue::Boolean(!array.is_empty())])
            }
        }
    }

    /// Get a string representation of the operation
    #[allow(clippy::inherent_to_string)]
    pub fn to_string(&self) -> String {
        match self {
            MultifieldOp::Collect => "collect".to_string(),
            MultifieldOp::Contains => "contains".to_string(),
            MultifieldOp::Count => "count".to_string(),
            MultifieldOp::First => "first".to_string(),
            MultifieldOp::Last => "last".to_string(),
            MultifieldOp::Index(idx) => format!("[{}]", idx),
            MultifieldOp::Slice(start, end) => format!("[{}:{}]", start, end),
            MultifieldOp::IsEmpty => "empty".to_string(),
            MultifieldOp::NotEmpty => "not_empty".to_string(),
        }
    }
}

/// Helper function to evaluate multi-field patterns in rules
///
/// This function combines the multi-field operation with variable binding.
/// It returns both the result values and optional variable bindings.
///
/// # Arguments
///
/// * `facts` - The facts to evaluate against
/// * `field` - The field name (e.g., "items")
/// * `operator` - The multi-field operation
/// * `variable` - Optional variable name for binding (e.g., "$?all_items")
/// * `value` - Optional value for operations like Contains
/// * `bindings` - Existing variable bindings
///
/// # Returns
///
/// `Some(HashMap<Variable, FactValue>)` - New bindings including multi-field results
/// `None` - Pattern doesn't match
pub fn evaluate_multifield_pattern(
    facts: &TypedFacts,
    field: &str,
    operator: &MultifieldOp,
    variable: Option<&str>,
    value: Option<&FactValue>,
    bindings: &HashMap<String, FactValue>,
) -> Option<HashMap<String, FactValue>> {
    // Evaluate the multi-field operation
    let result = operator.evaluate(facts, field, value)?;

    // Create new bindings
    let mut new_bindings = bindings.clone();

    // If there's a variable, bind the result
    if let Some(var_name) = variable {
        // For Collect operation, bind as array
        if matches!(operator, MultifieldOp::Collect) {
            new_bindings.insert(var_name.to_string(), FactValue::Array(result));
        } else {
            // For single-value results, unwrap
            if result.len() == 1 {
                new_bindings.insert(var_name.to_string(), result[0].clone());
            } else {
                // Multiple values, bind as array
                new_bindings.insert(var_name.to_string(), FactValue::Array(result));
            }
        }
    } else {
        // No variable binding, just check if operation succeeded
        // For boolean operations, check the result
        if result.len() == 1 {
            if let FactValue::Boolean(b) = result[0] {
                if !b {
                    return None; // Pattern doesn't match
                }
            }
        }
    }

    Some(new_bindings)
}

/// Parse multi-field variable syntax
///
/// Recognizes patterns like:
/// - `$?var` - Multi-field variable (collects all values)
/// - `$var` - Single-value binding (not multi-field)
///
/// Returns `Some(variable_name)` if it's a multi-field variable, `None` otherwise
pub fn parse_multifield_variable(input: &str) -> Option<String> {
    let trimmed = input.trim();
    trimmed.strip_prefix("$?").map(|s| s.to_string())
}

/// Check if a string is a multi-field variable
pub fn is_multifield_variable(input: &str) -> bool {
    input.trim().starts_with("$?")
}

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

    fn create_test_facts_with_array() -> TypedFacts {
        let mut facts = TypedFacts::new();
        facts.set(
            "items",
            FactValue::Array(vec![
                FactValue::String("item1".to_string()),
                FactValue::String("item2".to_string()),
                FactValue::String("item3".to_string()),
            ]),
        );
        facts.set(
            "tags",
            FactValue::Array(vec![
                FactValue::String("electronics".to_string()),
                FactValue::String("gadgets".to_string()),
            ]),
        );
        facts
    }

    #[test]
    fn test_collect_operation() {
        let facts = create_test_facts_with_array();
        let op = MultifieldOp::Collect;

        let result = op.evaluate(&facts, "items", None);
        assert!(result.is_some());

        let values = result.unwrap();
        assert_eq!(values.len(), 3);
        assert_eq!(values[0], FactValue::String("item1".to_string()));
    }

    #[test]
    fn test_contains_operation() {
        let facts = create_test_facts_with_array();
        let op = MultifieldOp::Contains;
        let search = FactValue::String("electronics".to_string());

        let result = op.evaluate(&facts, "tags", Some(&search));
        assert!(result.is_some());

        let values = result.unwrap();
        assert_eq!(values.len(), 1);
        assert_eq!(values[0], FactValue::Boolean(true));
    }

    #[test]
    fn test_count_operation() {
        let facts = create_test_facts_with_array();
        let op = MultifieldOp::Count;

        let result = op.evaluate(&facts, "items", None);
        assert!(result.is_some());

        let values = result.unwrap();
        assert_eq!(values[0], FactValue::Integer(3));
    }

    #[test]
    fn test_first_last_operations() {
        let facts = create_test_facts_with_array();

        let first = MultifieldOp::First.evaluate(&facts, "items", None).unwrap();
        assert_eq!(first[0], FactValue::String("item1".to_string()));

        let last = MultifieldOp::Last.evaluate(&facts, "items", None).unwrap();
        assert_eq!(last[0], FactValue::String("item3".to_string()));
    }

    #[test]
    fn test_index_operation() {
        let facts = create_test_facts_with_array();
        let op = MultifieldOp::Index(1);

        let result = op.evaluate(&facts, "items", None).unwrap();
        assert_eq!(result[0], FactValue::String("item2".to_string()));
    }

    #[test]
    fn test_slice_operation() {
        let facts = create_test_facts_with_array();
        let op = MultifieldOp::Slice(0, 2);

        let result = op.evaluate(&facts, "items", None).unwrap();
        assert_eq!(result.len(), 2);
        assert_eq!(result[0], FactValue::String("item1".to_string()));
        assert_eq!(result[1], FactValue::String("item2".to_string()));
    }

    #[test]
    fn test_is_empty_operation() {
        let mut facts = TypedFacts::new();
        facts.set("empty_array", FactValue::Array(Vec::new()));

        let op = MultifieldOp::IsEmpty;
        let result = op.evaluate(&facts, "empty_array", None).unwrap();
        assert_eq!(result[0], FactValue::Boolean(true));
    }

    #[test]
    fn test_parse_multifield_variable() {
        assert_eq!(
            parse_multifield_variable("$?items"),
            Some("items".to_string())
        );
        assert_eq!(parse_multifield_variable("$?all"), Some("all".to_string()));
        assert_eq!(parse_multifield_variable("$single"), None);
        assert_eq!(parse_multifield_variable("items"), None);
    }

    #[test]
    fn test_is_multifield_variable() {
        assert!(is_multifield_variable("$?items"));
        assert!(is_multifield_variable("$?all"));
        assert!(!is_multifield_variable("$single"));
        assert!(!is_multifield_variable("items"));
    }

    #[test]
    fn test_evaluate_multifield_pattern_with_binding() {
        let facts = create_test_facts_with_array();
        let bindings = HashMap::new();

        let result = evaluate_multifield_pattern(
            &facts,
            "items",
            &MultifieldOp::Collect,
            Some("$?all_items"),
            None,
            &bindings,
        );

        assert!(result.is_some());
        let new_bindings = result.unwrap();

        assert!(new_bindings.contains_key("$?all_items"));
        if let FactValue::Array(arr) = &new_bindings["$?all_items"] {
            assert_eq!(arr.len(), 3);
        } else {
            panic!("Expected array binding");
        }
    }

    #[test]
    fn test_evaluate_multifield_pattern_contains() {
        let facts = create_test_facts_with_array();
        let bindings = HashMap::new();
        let search = FactValue::String("electronics".to_string());

        let result = evaluate_multifield_pattern(
            &facts,
            "tags",
            &MultifieldOp::Contains,
            None,
            Some(&search),
            &bindings,
        );

        assert!(result.is_some()); // Should match

        // Test with non-existent value
        let search2 = FactValue::String("nonexistent".to_string());
        let result2 = evaluate_multifield_pattern(
            &facts,
            "tags",
            &MultifieldOp::Contains,
            None,
            Some(&search2),
            &bindings,
        );

        assert!(result2.is_none()); // Should not match
    }
}