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
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
//! Rule execution for backward chaining
//!
//! This module provides proper condition evaluation and action execution for backward
//! chaining queries. It integrates with the Truth Maintenance System (TMS) to support
//! logical fact insertion and justification-based retraction.

#![allow(deprecated)]
#![allow(clippy::type_complexity)]
//!
//! # Features
//!
//! - **Condition evaluation** - Evaluate rule conditions against current facts
//! - **Action execution** - Execute rule actions (Set, MethodCall, Log, Retract)
//! - **TMS integration** - Optional logical fact insertion with justifications
//! - **Function support** - Built-in functions (len, isEmpty, exists, etc.)
//! - **Type conversion** - Convert between Facts (string-based) and TypedFacts (RETE)
//!
//! # Architecture
//!
//! ```text
//! ┌──────────────────┐
//! │  RuleExecutor    │
//! │                  │
//! │  ┌────────────┐  │
//! │  │ Condition  │──┼──> ConditionEvaluator
//! │  │ Evaluator  │  │       │
//! │  └────────────┘  │       ├─> Built-in functions
//! │                  │       └─> Field comparison
//! │  ┌────────────┐  │
//! │  │   Action   │──┼──> Set, MethodCall, Log
//! │  │ Executor   │  │       │
//! │  └────────────┘  │       └─> TMS Inserter (optional)
//! └──────────────────┘
//! ```
//!
//! # Example: Basic Rule Execution
//!
//! ```rust
//! use rust_rule_engine::backward::rule_executor::RuleExecutor;
//! use rust_rule_engine::engine::rule::{Rule, Condition, ConditionGroup};
//! use rust_rule_engine::types::{Operator, ActionType, Value};
//! use rust_rule_engine::{KnowledgeBase, Facts};
//!
//! # fn example() -> Result<(), Box<dyn std::error::Error>> {
//! let kb = KnowledgeBase::new("test");
//! let executor = RuleExecutor::new(kb);
//!
//! // Define a rule: If User.Age > 18, then User.IsAdult = true
//! let conditions = ConditionGroup::Single(
//!     Condition::new(
//!         "User.Age".to_string(),
//!         Operator::GreaterThan,
//!         Value::Number(18.0),
//!     )
//! );
//! let actions = vec![ActionType::Set {
//!     field: "User.IsAdult".to_string(),
//!     value: Value::Boolean(true),
//! }];
//! let rule = Rule::new("CheckAdult".to_string(), conditions, actions);
//!
//! // Execute rule
//! let mut facts = Facts::new();
//! facts.set("User.Age", Value::Number(25.0));
//!
//! let executed = executor.try_execute_rule(&rule, &mut facts)?;
//! assert!(executed); // Rule should execute successfully
//! assert_eq!(facts.get("User.IsAdult"), Some(Value::Boolean(true)));
//! # Ok(())
//! # }
//! ```
//!
//! # Example: TMS Integration
//!
//! ```rust
//! use rust_rule_engine::backward::rule_executor::RuleExecutor;
//! use rust_rule_engine::rete::propagation::IncrementalEngine;
//! use rust_rule_engine::{KnowledgeBase, Facts};
//! use std::sync::{Arc, Mutex};
//!
//! # fn example() -> Result<(), Box<dyn std::error::Error>> {
//! let kb = KnowledgeBase::new("test");
//! let rete_engine = Arc::new(Mutex::new(IncrementalEngine::new()));
//!
//! // Create TMS inserter callback
//! let inserter = {
//!     let eng = rete_engine.clone();
//!     Arc::new(move |fact_type: String, data, rule_name: String, premises: Vec<String>| {
//!         if let Ok(mut e) = eng.lock() {
//!             let handles = e.resolve_premise_keys(premises);
//!             let _ = e.insert_logical(fact_type, data, rule_name, handles);
//!         }
//!     })
//! };
//!
//! let executor = RuleExecutor::new_with_inserter(kb, Some(inserter));
//! // Now rule executions will insert facts logically with justifications
//! # Ok(())
//! # }
//! ```
//!
//! # Supported Action Types
//!
//! - **Set** - Set a fact value: `field: value`
//! - **MethodCall** - Call a method on an object: `object.method(args)`
//! - **Log** - Log a message: `log("message")`
//! - **Retract** - Retract a fact: `retract(field)`
//!
//! # Built-in Functions
//!
//! The executor supports these built-in functions for condition evaluation:
//! - `len(field)` - Get string/array length
//! - `isEmpty(field)` - Check if string/array is empty
//! - `exists(field)` - Check if field exists
//! - `count(field)` - Count array elements

use crate::engine::condition_evaluator::ConditionEvaluator;
use crate::engine::rule::{Condition, ConditionGroup, Rule};
use crate::errors::{Result, RuleEngineError};
use crate::types::{ActionType, Value};
use crate::{Facts, KnowledgeBase};

/// Rule executor for backward chaining
pub struct RuleExecutor {
    evaluator: ConditionEvaluator,
    /// Optional TMS inserter callback: (fact_type, typed_data, source_rule, premise_keys)
    /// premise_keys are strings in the format: "Type.field=value" which the inserter
    /// can use to resolve to working-memory FactHandles.
    tms_inserter: Option<
        std::sync::Arc<dyn Fn(String, crate::rete::TypedFacts, String, Vec<String>) + Send + Sync>,
    >,
}

impl RuleExecutor {
    /// Create a new rule executor
    ///
    /// Note: The knowledge_base parameter is kept for API compatibility but is not used.
    /// Rule evaluation is done through the ConditionEvaluator.
    pub fn new(_knowledge_base: KnowledgeBase) -> Self {
        Self::new_with_inserter(_knowledge_base, None)
    }

    /// Create a new executor with optional TMS inserter callback
    ///
    /// Note: The knowledge_base parameter is kept for API compatibility but is not used.
    /// Rule evaluation is done through the ConditionEvaluator.
    pub fn new_with_inserter(
        _knowledge_base: KnowledgeBase,
        inserter: Option<
            std::sync::Arc<
                dyn Fn(String, crate::rete::TypedFacts, String, Vec<String>) + Send + Sync,
            >,
        >,
    ) -> Self {
        Self {
            evaluator: ConditionEvaluator::with_builtin_functions(),
            tms_inserter: inserter,
        }
    }

    /// Check if rule conditions are satisfied and execute if they are
    ///
    /// Returns:
    /// - Ok(true) if rule executed successfully
    /// - Ok(false) if conditions not satisfied
    /// - Err if execution failed
    pub fn try_execute_rule(&self, rule: &Rule, facts: &mut Facts) -> Result<bool> {
        // Check if all conditions are satisfied
        if !self.evaluate_conditions(&rule.conditions, facts)? {
            return Ok(false);
        }

        // Conditions satisfied - execute actions
        self.execute_actions(rule, facts)?;

        Ok(true)
    }

    /// Evaluate condition group
    pub fn evaluate_conditions(&self, group: &ConditionGroup, facts: &Facts) -> Result<bool> {
        // Delegate to shared evaluator
        self.evaluator.evaluate_conditions(group, facts)
    }

    /// Evaluate a single condition
    pub fn evaluate_condition(&self, condition: &Condition, facts: &Facts) -> Result<bool> {
        // Delegate to shared evaluator
        self.evaluator.evaluate_condition(condition, facts)
    }

    /// Execute rule actions
    fn execute_actions(&self, rule: &Rule, facts: &mut Facts) -> Result<()> {
        for action in &rule.actions {
            self.execute_action(Some(rule), action, facts)?;
        }

        Ok(())
    }

    /// Execute a single action (has access to rule for TMS justifications)
    fn execute_action(
        &self,
        rule: Option<&Rule>,
        action: &ActionType,
        facts: &mut Facts,
    ) -> Result<()> {
        match action {
            ActionType::Set { field, value } => {
                // Evaluate value expression if needed
                let evaluated_value = self.evaluate_value_expression(value, facts)?;

                // If we have a TMS inserter and the field looks like "Type.field",
                // attempt to create a TypedFacts wrapper and call the inserter as a logical assertion.
                if let Some(inserter) = &self.tms_inserter {
                    if let Some(dot_pos) = field.find('.') {
                        let fact_type = field[..dot_pos].to_string();
                        let field_name = field[dot_pos + 1..].to_string();

                        // Build TypedFacts with this single field
                        let mut typed = crate::rete::TypedFacts::new();
                        // Map crate::types::Value -> rete::FactValue
                        let fv = match &evaluated_value {
                            crate::types::Value::String(s) => {
                                crate::rete::FactValue::String(s.clone())
                            }
                            crate::types::Value::Integer(i) => crate::rete::FactValue::Integer(*i),
                            crate::types::Value::Number(n) => crate::rete::FactValue::Float(*n),
                            crate::types::Value::Boolean(b) => crate::rete::FactValue::Boolean(*b),
                            _ => crate::rete::FactValue::String(format!("{:?}", evaluated_value)),
                        };

                        typed.set(field_name, fv);

                        // Build premise keys from the rule's conditions (best-effort):
                        // format: "Type.field=value" so the RETE engine can map to handles.
                        let premises = match rule {
                            Some(r) => self.collect_premise_keys_from_rule(r, facts),
                            None => Vec::new(),
                        };

                        // Call inserter with rule name (string-based premises)
                        let source_name = rule
                            .map(|r| r.name.clone())
                            .unwrap_or_else(|| "<unknown>".to_string());
                        (inserter)(fact_type, typed, source_name, premises);
                        // Also apply to local Facts representation so backward search sees it
                        facts.set(field, evaluated_value);
                        return Ok(());
                    }
                }

                // Fallback: just set into Facts
                facts.set(field, evaluated_value);
                Ok(())
            }

            ActionType::MethodCall {
                object,
                method,
                args,
            } => {
                // Execute method call
                if let Some(obj_value) = facts.get(object) {
                    let mut obj_value = obj_value.clone();
                    // Evaluate arguments
                    let mut arg_values = Vec::new();
                    for arg in args {
                        let val = self.evaluate_value_expression(arg, facts)?;
                        arg_values.push(val);
                    }

                    // Call method
                    let result = obj_value
                        .call_method(method, arg_values)
                        .map_err(RuleEngineError::ExecutionError)?;

                    // Update object
                    facts.set(object, obj_value);

                    // Store result if there's a return value
                    if result != Value::Null {
                        facts.set(&format!("{}._return", object), result);
                    }

                    Ok(())
                } else {
                    Err(RuleEngineError::ExecutionError(format!(
                        "Object not found: {}",
                        object
                    )))
                }
            }

            ActionType::Retract { object } => {
                // Retract fact from working memory
                // In backward chaining, we just remove the fact
                facts.remove(object);
                Ok(())
            }

            ActionType::Log { message } => {
                // Just log for now
                println!("[BC Action] {}", message);
                Ok(())
            }

            ActionType::Custom { .. } => {
                // Custom actions not supported in backward chaining yet
                Ok(())
            }

            ActionType::ActivateAgendaGroup { .. } => {
                // Agenda groups not supported in backward chaining
                Ok(())
            }

            ActionType::ScheduleRule { .. } => {
                // Rule scheduling not supported in backward chaining
                Ok(())
            }

            ActionType::CompleteWorkflow { .. } => {
                // Workflows not supported in backward chaining
                Ok(())
            }

            ActionType::SetWorkflowData { .. } => {
                // Workflow data not supported in backward chaining
                Ok(())
            }

            ActionType::Append { field, value } => {
                // Evaluate value expression if needed
                let evaluated_value = self.evaluate_value_expression(value, facts)?;

                // Get current array or create new one
                let current_value = facts.get(field);
                let mut array = match current_value {
                    Some(Value::Array(arr)) => arr.clone(),
                    Some(_) => {
                        // Field exists but is not an array, create new array
                        Vec::new()
                    }
                    None => Vec::new(),
                };

                // Append value
                array.push(evaluated_value);

                // Set the updated array
                facts.set(field, Value::Array(array));

                Ok(())
            }
        }
    }

    /// Collect a best-effort list of premise keys from the rule's conditions.
    /// Each entry has the format: "Type.field=value" when possible. This is
    /// intentionally conservative: only field-based conditions with a dotted
    /// "Type.field" expression are collected.
    fn collect_premise_keys_from_rule(&self, rule: &Rule, facts: &Facts) -> Vec<String> {
        use crate::engine::rule::{ConditionExpression, ConditionGroup};

        let mut keys = Vec::new();

        fn collect_from_group(group: &ConditionGroup, keys: &mut Vec<String>, facts: &Facts) {
            match group {
                ConditionGroup::Single(cond) => {
                    if let ConditionExpression::Field(f) = &cond.expression {
                        if let Some(dot_pos) = f.find('.') {
                            let fact_type = &f[..dot_pos];
                            let field_name = &f[dot_pos + 1..];

                            // Try to get value from facts
                            if let Some(val) = facts.get(f).or_else(|| facts.get_nested(f)) {
                                let value_str = match val {
                                    crate::types::Value::String(s) => s.clone(),
                                    crate::types::Value::Integer(i) => i.to_string(),
                                    crate::types::Value::Number(n) => n.to_string(),
                                    crate::types::Value::Boolean(b) => b.to_string(),
                                    _ => format!("{:?}", val),
                                };

                                keys.push(format!("{}.{}={}", fact_type, field_name, value_str));
                            } else {
                                // If we don't have a value at this time, still record the key without value
                                keys.push(format!("{}.{}=", fact_type, field_name));
                            }
                        }
                    }
                }
                ConditionGroup::Compound { left, right, .. } => {
                    collect_from_group(left, keys, facts);
                    collect_from_group(right, keys, facts);
                }
                // For other complex groups, skip
                _ => {}
            }
        }

        collect_from_group(&rule.conditions, &mut keys, facts);
        keys
    }

    /// Evaluate value expression
    fn evaluate_value_expression(&self, value: &Value, facts: &Facts) -> Result<Value> {
        match value {
            Value::Expression(expr) => {
                // Try simple field lookup first
                if let Some(val) = facts.get(expr).or_else(|| facts.get_nested(expr)) {
                    return Ok(val);
                }

                // Try arithmetic expression evaluation
                if let Some(result) = self.try_evaluate_arithmetic(expr, facts) {
                    return Ok(result);
                }

                // Try to parse as literal
                if expr == "true" {
                    Ok(Value::Boolean(true))
                } else if expr == "false" {
                    Ok(Value::Boolean(false))
                } else if expr == "null" {
                    Ok(Value::Null)
                } else if let Ok(n) = expr.parse::<f64>() {
                    Ok(Value::Number(n))
                } else if let Ok(i) = expr.parse::<i64>() {
                    Ok(Value::Integer(i))
                } else {
                    // Try to parse as literal using simple parsing
                    if expr == "true" {
                        Ok(Value::Boolean(true))
                    } else if expr == "false" {
                        Ok(Value::Boolean(false))
                    } else if expr == "null" {
                        Ok(Value::Null)
                    } else if let Ok(n) = expr.parse::<f64>() {
                        Ok(Value::Number(n))
                    } else if let Ok(i) = expr.parse::<i64>() {
                        Ok(Value::Integer(i))
                    } else {
                        Ok(value.clone())
                    }
                }
            }
            _ => Ok(value.clone()),
        }
    }

    /// Try to evaluate simple arithmetic expressions
    /// Supports: +, -, *, /
    fn try_evaluate_arithmetic(&self, expr: &str, facts: &Facts) -> Option<Value> {
        // Check for division
        if let Some(div_pos) = expr.find(" / ") {
            let left = expr[..div_pos].trim();
            let right = expr[div_pos + 3..].trim();

            let left_val = self.get_numeric_value(left, facts)?;
            let right_val = self.get_numeric_value(right, facts)?;

            if right_val != 0.0 {
                return Some(Value::Number(left_val / right_val));
            }
            return None;
        }

        // Check for multiplication
        if let Some(mul_pos) = expr.find(" * ") {
            let left = expr[..mul_pos].trim();
            let right = expr[mul_pos + 3..].trim();

            let left_val = self.get_numeric_value(left, facts)?;
            let right_val = self.get_numeric_value(right, facts)?;

            return Some(Value::Number(left_val * right_val));
        }

        // Check for addition
        if let Some(add_pos) = expr.find(" + ") {
            let left = expr[..add_pos].trim();
            let right = expr[add_pos + 3..].trim();

            let left_val = self.get_numeric_value(left, facts)?;
            let right_val = self.get_numeric_value(right, facts)?;

            return Some(Value::Number(left_val + right_val));
        }

        // Check for subtraction
        if let Some(sub_pos) = expr.find(" - ") {
            let left = expr[..sub_pos].trim();
            let right = expr[sub_pos + 3..].trim();

            let left_val = self.get_numeric_value(left, facts)?;
            let right_val = self.get_numeric_value(right, facts)?;

            return Some(Value::Number(left_val - right_val));
        }

        None
    }

    /// Get numeric value from field name or literal
    fn get_numeric_value(&self, s: &str, facts: &Facts) -> Option<f64> {
        // Try parsing as number first
        if let Ok(n) = s.parse::<f64>() {
            return Some(n);
        }

        // Try getting from facts
        if let Some(val) = facts.get(s).or_else(|| facts.get_nested(s)) {
            match val {
                Value::Number(n) => Some(n),
                Value::Integer(i) => Some(i as f64),
                _ => None,
            }
        } else {
            None
        }
    }
}

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

    #[test]
    fn test_evaluate_simple_condition() {
        let kb = KnowledgeBase::new("test");
        let executor = RuleExecutor::new(kb);

        let facts = Facts::new();
        facts.set("User.Age", Value::Number(25.0));

        let condition = Condition::new(
            "User.Age".to_string(),
            Operator::GreaterThan,
            Value::Number(18.0),
        );

        let result = executor.evaluate_condition(&condition, &facts).unwrap();
        assert!(result);
    }

    #[test]
    fn test_evaluate_function_call_len() {
        let kb = KnowledgeBase::new("test");
        let executor = RuleExecutor::new(kb);

        let facts = Facts::new();
        facts.set("User.Name", Value::String("John".to_string()));

        let condition = Condition::with_function(
            "len".to_string(),
            vec!["User.Name".to_string()],
            Operator::GreaterThan,
            Value::Number(3.0),
        );

        let result = executor.evaluate_condition(&condition, &facts).unwrap();
        assert!(result); // "John".len() = 4 > 3
    }

    #[test]
    fn test_execute_set_action() {
        let kb = KnowledgeBase::new("test");
        let executor = RuleExecutor::new(kb);

        let mut facts = Facts::new();

        let action = ActionType::Set {
            field: "User.IsVIP".to_string(),
            value: Value::Boolean(true),
        };

        executor.execute_action(None, &action, &mut facts).unwrap();

        assert_eq!(facts.get("User.IsVIP"), Some(Value::Boolean(true)));
    }

    #[test]
    fn test_evaluate_compound_and_condition() {
        let kb = KnowledgeBase::new("test");
        let executor = RuleExecutor::new(kb);

        let facts = Facts::new();
        facts.set("User.Age", Value::Number(25.0));
        facts.set("User.Country", Value::String("US".to_string()));

        let conditions = ConditionGroup::Compound {
            left: Box::new(ConditionGroup::Single(Condition::new(
                "User.Age".to_string(),
                Operator::GreaterThan,
                Value::Number(18.0),
            ))),
            operator: crate::types::LogicalOperator::And,
            right: Box::new(ConditionGroup::Single(Condition::new(
                "User.Country".to_string(),
                Operator::Equal,
                Value::String("US".to_string()),
            ))),
        };

        let result = executor.evaluate_conditions(&conditions, &facts).unwrap();
        assert!(result);
    }

    #[test]
    fn test_evaluate_compound_or_condition() {
        let kb = KnowledgeBase::new("test");
        let executor = RuleExecutor::new(kb);

        let facts = Facts::new();
        facts.set("User.Age", Value::Number(15.0));
        facts.set("User.HasParentalConsent", Value::Boolean(true));

        let conditions = ConditionGroup::Compound {
            left: Box::new(ConditionGroup::Single(Condition::new(
                "User.Age".to_string(),
                Operator::GreaterThan,
                Value::Number(18.0),
            ))),
            operator: crate::types::LogicalOperator::Or,
            right: Box::new(ConditionGroup::Single(Condition::new(
                "User.HasParentalConsent".to_string(),
                Operator::Equal,
                Value::Boolean(true),
            ))),
        };

        let result = executor.evaluate_conditions(&conditions, &facts).unwrap();
        assert!(result); // True because HasParentalConsent is true
    }

    #[test]
    fn test_evaluate_not_condition() {
        let kb = KnowledgeBase::new("test");
        let executor = RuleExecutor::new(kb);

        let facts = Facts::new();
        facts.set("User.IsBanned", Value::Boolean(false));

        let conditions = ConditionGroup::Not(Box::new(ConditionGroup::Single(Condition::new(
            "User.IsBanned".to_string(),
            Operator::Equal,
            Value::Boolean(true),
        ))));

        let result = executor.evaluate_conditions(&conditions, &facts).unwrap();
        assert!(result); // True because NOT banned
    }

    #[test]
    fn test_evaluate_function_isempty() {
        let kb = KnowledgeBase::new("test");
        let executor = RuleExecutor::new(kb);

        let facts = Facts::new();
        facts.set("User.Description", Value::String("".to_string()));

        let condition = Condition::with_function(
            "isEmpty".to_string(),
            vec!["User.Description".to_string()],
            Operator::Equal,
            Value::Boolean(true),
        );

        let result = executor.evaluate_condition(&condition, &facts).unwrap();
        assert!(result); // Empty string
    }

    #[test]
    fn test_evaluate_test_expression_exists() {
        let kb = KnowledgeBase::new("test");
        let executor = RuleExecutor::new(kb);

        let facts = Facts::new();
        facts.set("User.Email", Value::String("user@example.com".to_string()));

        let condition = Condition {
            field: "User.Email".to_string(),
            expression: crate::engine::rule::ConditionExpression::Test {
                name: "exists".to_string(),
                args: vec!["User.Email".to_string()],
            },
            operator: Operator::Equal,
            value: Value::Boolean(true),
        };

        let result = executor.evaluate_condition(&condition, &facts).unwrap();
        assert!(result);
    }

    #[test]
    fn test_execute_log_action() {
        let kb = KnowledgeBase::new("test");
        let executor = RuleExecutor::new(kb);

        let mut facts = Facts::new();

        let action = ActionType::Log {
            message: "Test log message".to_string(),
        };

        // Should not panic
        executor.execute_action(None, &action, &mut facts).unwrap();
    }

    #[test]
    fn test_try_execute_rule_success() {
        let kb = KnowledgeBase::new("test");
        let executor = RuleExecutor::new(kb);

        let mut facts = Facts::new();
        facts.set("User.Age", Value::Number(25.0));

        let conditions = ConditionGroup::Single(Condition::new(
            "User.Age".to_string(),
            Operator::GreaterThan,
            Value::Number(18.0),
        ));

        let actions = vec![ActionType::Set {
            field: "User.IsAdult".to_string(),
            value: Value::Boolean(true),
        }];

        let rule = Rule::new("CheckAdult".to_string(), conditions, actions);

        let executed = executor.try_execute_rule(&rule, &mut facts).unwrap();
        assert!(executed);
        assert_eq!(facts.get("User.IsAdult"), Some(Value::Boolean(true)));
    }

    #[test]
    fn test_try_execute_rule_failure() {
        let kb = KnowledgeBase::new("test");
        let executor = RuleExecutor::new(kb);

        let mut facts = Facts::new();
        facts.set("User.Age", Value::Number(15.0));

        let conditions = ConditionGroup::Single(Condition::new(
            "User.Age".to_string(),
            Operator::GreaterThan,
            Value::Number(18.0),
        ));

        let actions = vec![ActionType::Set {
            field: "User.IsAdult".to_string(),
            value: Value::Boolean(true),
        }];

        let rule = Rule::new("CheckAdult".to_string(), conditions, actions);

        let executed = executor.try_execute_rule(&rule, &mut facts).unwrap();
        assert!(!executed); // Conditions not met
        assert_eq!(facts.get("User.IsAdult"), None); // Action not executed
    }

    #[test]
    fn test_evaluate_string_operators() {
        let kb = KnowledgeBase::new("test");
        let executor = RuleExecutor::new(kb);

        let facts = Facts::new();
        facts.set("User.Email", Value::String("user@example.com".to_string()));

        // Test Contains
        let condition = Condition::new(
            "User.Email".to_string(),
            Operator::Contains,
            Value::String("@example".to_string()),
        );
        assert!(executor.evaluate_condition(&condition, &facts).unwrap());

        // Test StartsWith
        let condition = Condition::new(
            "User.Email".to_string(),
            Operator::StartsWith,
            Value::String("user".to_string()),
        );
        assert!(executor.evaluate_condition(&condition, &facts).unwrap());

        // Test EndsWith
        let condition = Condition::new(
            "User.Email".to_string(),
            Operator::EndsWith,
            Value::String(".com".to_string()),
        );
        assert!(executor.evaluate_condition(&condition, &facts).unwrap());
    }

    #[test]
    fn test_evaluate_numeric_operators() {
        let kb = KnowledgeBase::new("test");
        let executor = RuleExecutor::new(kb);

        let facts = Facts::new();
        facts.set("Order.Amount", Value::Number(1500.0));

        // Test GreaterThanOrEqual
        let condition = Condition::new(
            "Order.Amount".to_string(),
            Operator::GreaterThanOrEqual,
            Value::Number(1500.0),
        );
        assert!(executor.evaluate_condition(&condition, &facts).unwrap());

        // Test LessThan
        let condition = Condition::new(
            "Order.Amount".to_string(),
            Operator::LessThan,
            Value::Number(2000.0),
        );
        assert!(executor.evaluate_condition(&condition, &facts).unwrap());

        // Test NotEqual
        let condition = Condition::new(
            "Order.Amount".to_string(),
            Operator::NotEqual,
            Value::Number(1000.0),
        );
        assert!(executor.evaluate_condition(&condition, &facts).unwrap());
    }

    #[test]
    fn test_evaluate_missing_field() {
        let kb = KnowledgeBase::new("test");
        let executor = RuleExecutor::new(kb);

        let facts = Facts::new(); // Empty facts

        let condition = Condition::new(
            "User.Age".to_string(),
            Operator::GreaterThan,
            Value::Number(18.0),
        );

        let result = executor.evaluate_condition(&condition, &facts).unwrap();
        assert!(!result); // Missing field evaluates to false
    }

    #[test]
    fn test_execute_multiple_actions() {
        let kb = KnowledgeBase::new("test");
        let executor = RuleExecutor::new(kb);

        let mut facts = Facts::new();
        facts.set("User.Points", Value::Number(150.0));

        let conditions = ConditionGroup::Single(Condition::new(
            "User.Points".to_string(),
            Operator::GreaterThan,
            Value::Number(100.0),
        ));

        let actions = vec![
            ActionType::Set {
                field: "User.IsVIP".to_string(),
                value: Value::Boolean(true),
            },
            ActionType::Log {
                message: "User promoted to VIP".to_string(),
            },
            ActionType::Set {
                field: "User.Discount".to_string(),
                value: Value::Number(0.2),
            },
        ];

        let rule = Rule::new("PromoteToVIP".to_string(), conditions, actions);

        let executed = executor.try_execute_rule(&rule, &mut facts).unwrap();
        assert!(executed);
        assert_eq!(facts.get("User.IsVIP"), Some(Value::Boolean(true)));
        assert_eq!(facts.get("User.Discount"), Some(Value::Number(0.2)));
    }

    #[test]
    fn test_evaluate_endswith_operator() {
        let kb = KnowledgeBase::new("test");
        let executor = RuleExecutor::new(kb);

        let facts = Facts::new();
        facts.set("User.Email", Value::String("user@example.com".to_string()));
        facts.set("File.Name", Value::String("document.pdf".to_string()));
        facts.set(
            "Domain.URL",
            Value::String("https://api.example.org".to_string()),
        );

        // Test EndsWith with .com suffix
        let condition = Condition::new(
            "User.Email".to_string(),
            Operator::EndsWith,
            Value::String(".com".to_string()),
        );
        assert!(executor.evaluate_condition(&condition, &facts).unwrap());

        // Test EndsWith with .pdf suffix
        let condition = Condition::new(
            "File.Name".to_string(),
            Operator::EndsWith,
            Value::String(".pdf".to_string()),
        );
        assert!(executor.evaluate_condition(&condition, &facts).unwrap());

        // Test EndsWith with .org suffix
        let condition = Condition::new(
            "Domain.URL".to_string(),
            Operator::EndsWith,
            Value::String(".org".to_string()),
        );
        assert!(executor.evaluate_condition(&condition, &facts).unwrap());

        // Test EndsWith that should fail
        let condition = Condition::new(
            "User.Email".to_string(),
            Operator::EndsWith,
            Value::String(".net".to_string()),
        );
        assert!(!executor.evaluate_condition(&condition, &facts).unwrap());

        // Test EndsWith with full string match
        let condition = Condition::new(
            "File.Name".to_string(),
            Operator::EndsWith,
            Value::String("document.pdf".to_string()),
        );
        assert!(executor.evaluate_condition(&condition, &facts).unwrap());
    }

    #[test]
    fn test_evaluate_endswith_edge_cases() {
        let kb = KnowledgeBase::new("test");
        let executor = RuleExecutor::new(kb);

        let facts = Facts::new();
        facts.set("Empty.String", Value::String("".to_string()));
        facts.set("Single.Char", Value::String("a".to_string()));
        facts.set("Number.Value", Value::Number(123.0));

        // Test EndsWith with empty string (should match everything)
        let condition = Condition::new(
            "Empty.String".to_string(),
            Operator::EndsWith,
            Value::String("".to_string()),
        );
        assert!(executor.evaluate_condition(&condition, &facts).unwrap());

        // Test EndsWith on single character
        let condition = Condition::new(
            "Single.Char".to_string(),
            Operator::EndsWith,
            Value::String("a".to_string()),
        );
        assert!(executor.evaluate_condition(&condition, &facts).unwrap());

        // Test EndsWith with non-string value (should fail gracefully)
        let condition = Condition::new(
            "Number.Value".to_string(),
            Operator::EndsWith,
            Value::String(".0".to_string()),
        );
        assert!(!executor.evaluate_condition(&condition, &facts).unwrap());

        // Test EndsWith on missing field (should fail gracefully)
        let condition = Condition::new(
            "Missing.Field".to_string(),
            Operator::EndsWith,
            Value::String("test".to_string()),
        );
        assert!(!executor.evaluate_condition(&condition, &facts).unwrap());

        // Test case sensitivity
        let facts2 = Facts::new();
        facts2.set("Text.Value", Value::String("HelloWorld".to_string()));

        let condition = Condition::new(
            "Text.Value".to_string(),
            Operator::EndsWith,
            Value::String("world".to_string()),
        );
        assert!(!executor.evaluate_condition(&condition, &facts2).unwrap()); // Should fail due to case mismatch

        let condition = Condition::new(
            "Text.Value".to_string(),
            Operator::EndsWith,
            Value::String("World".to_string()),
        );
        assert!(executor.evaluate_condition(&condition, &facts2).unwrap()); // Should pass with correct case
    }

    #[test]
    fn test_evaluate_matches_operator() {
        let kb = KnowledgeBase::new("test");
        let executor = RuleExecutor::new(kb);

        let facts = Facts::new();
        facts.set("User.Email", Value::String("user@example.com".to_string()));
        facts.set(
            "Product.Name",
            Value::String("Premium Laptop Model X".to_string()),
        );
        facts.set(
            "Log.Message",
            Value::String("Error: Connection timeout".to_string()),
        );

        // Test Matches with pattern "example"
        let condition = Condition::new(
            "User.Email".to_string(),
            Operator::Matches,
            Value::String("example".to_string()),
        );
        assert!(executor.evaluate_condition(&condition, &facts).unwrap());

        // Test Matches with pattern "Premium"
        let condition = Condition::new(
            "Product.Name".to_string(),
            Operator::Matches,
            Value::String("Premium".to_string()),
        );
        assert!(executor.evaluate_condition(&condition, &facts).unwrap());

        // Test Matches with pattern "Error"
        let condition = Condition::new(
            "Log.Message".to_string(),
            Operator::Matches,
            Value::String("Error".to_string()),
        );
        assert!(executor.evaluate_condition(&condition, &facts).unwrap());

        // Test Matches that should fail
        let condition = Condition::new(
            "User.Email".to_string(),
            Operator::Matches,
            Value::String("notfound".to_string()),
        );
        assert!(!executor.evaluate_condition(&condition, &facts).unwrap());

        // Test Matches with partial pattern
        let condition = Condition::new(
            "Product.Name".to_string(),
            Operator::Matches,
            Value::String("Laptop".to_string()),
        );
        assert!(executor.evaluate_condition(&condition, &facts).unwrap());

        // Test Matches with full string
        let condition = Condition::new(
            "Log.Message".to_string(),
            Operator::Matches,
            Value::String("Error: Connection timeout".to_string()),
        );
        assert!(executor.evaluate_condition(&condition, &facts).unwrap());
    }

    #[test]
    fn test_evaluate_matches_edge_cases() {
        let kb = KnowledgeBase::new("test");
        let executor = RuleExecutor::new(kb);

        let facts = Facts::new();
        facts.set("Empty.String", Value::String("".to_string()));
        facts.set("Single.Char", Value::String("x".to_string()));
        facts.set("Number.Value", Value::Number(456.0));
        facts.set("Special.Chars", Value::String("test@#$%^&*()".to_string()));

        // Test Matches with empty pattern (should match empty string)
        let condition = Condition::new(
            "Empty.String".to_string(),
            Operator::Matches,
            Value::String("".to_string()),
        );
        assert!(executor.evaluate_condition(&condition, &facts).unwrap());

        // Test Matches on single character
        let condition = Condition::new(
            "Single.Char".to_string(),
            Operator::Matches,
            Value::String("x".to_string()),
        );
        assert!(executor.evaluate_condition(&condition, &facts).unwrap());

        // Test Matches with non-string value (should fail gracefully)
        let condition = Condition::new(
            "Number.Value".to_string(),
            Operator::Matches,
            Value::String("456".to_string()),
        );
        assert!(!executor.evaluate_condition(&condition, &facts).unwrap());

        // Test Matches on missing field (should fail gracefully)
        let condition = Condition::new(
            "Missing.Field".to_string(),
            Operator::Matches,
            Value::String("pattern".to_string()),
        );
        assert!(!executor.evaluate_condition(&condition, &facts).unwrap());

        // Test Matches with special characters
        let condition = Condition::new(
            "Special.Chars".to_string(),
            Operator::Matches,
            Value::String("@#$".to_string()),
        );
        assert!(executor.evaluate_condition(&condition, &facts).unwrap());

        // Test case sensitivity
        let facts2 = Facts::new();
        facts2.set("Text.Value", Value::String("HelloWorld".to_string()));

        let condition = Condition::new(
            "Text.Value".to_string(),
            Operator::Matches,
            Value::String("hello".to_string()),
        );
        assert!(!executor.evaluate_condition(&condition, &facts2).unwrap()); // Should fail due to case mismatch

        let condition = Condition::new(
            "Text.Value".to_string(),
            Operator::Matches,
            Value::String("Hello".to_string()),
        );
        assert!(executor.evaluate_condition(&condition, &facts2).unwrap()); // Should pass with correct case
    }

    #[test]
    fn test_endswith_matches_in_rules() {
        // Integration test: EndsWith and Matches in actual rules
        let kb = KnowledgeBase::new("test");

        // Rule 1: If email ends with .edu, set IsStudent = true
        let condition1 = Condition::new(
            "User.Email".to_string(),
            Operator::EndsWith,
            Value::String(".edu".to_string()),
        );
        let actions1 = vec![ActionType::Set {
            field: "User.IsStudent".to_string(),
            value: Value::Boolean(true),
        }];
        let rule1 = Rule::new(
            "StudentEmailRule".to_string(),
            ConditionGroup::Single(condition1),
            actions1,
        );

        // Rule 2: If product name matches "Premium", set IsPremium = true
        let condition2 = Condition::new(
            "Product.Name".to_string(),
            Operator::Matches,
            Value::String("Premium".to_string()),
        );
        let actions2 = vec![ActionType::Set {
            field: "Product.IsPremium".to_string(),
            value: Value::Boolean(true),
        }];
        let rule2 = Rule::new(
            "PremiumProductRule".to_string(),
            ConditionGroup::Single(condition2),
            actions2,
        );

        let _ = kb.add_rule(rule1.clone());
        let _ = kb.add_rule(rule2.clone());

        let executor = RuleExecutor::new(kb);

        // Test scenario 1: Student email
        let mut facts1 = Facts::new();
        facts1.set(
            "User.Email",
            Value::String("student@university.edu".to_string()),
        );

        let executed = executor.try_execute_rule(&rule1, &mut facts1).unwrap();
        assert!(executed);
        assert_eq!(facts1.get("User.IsStudent"), Some(Value::Boolean(true)));

        // Test scenario 2: Premium product
        let mut facts2 = Facts::new();
        facts2.set(
            "Product.Name",
            Value::String("Premium Laptop X1".to_string()),
        );

        let executed = executor.try_execute_rule(&rule2, &mut facts2).unwrap();
        assert!(executed);
        assert_eq!(facts2.get("Product.IsPremium"), Some(Value::Boolean(true)));

        // Test scenario 3: Non-matching cases
        let mut facts3 = Facts::new();
        facts3.set("User.Email", Value::String("user@company.com".to_string()));

        let executed = executor.try_execute_rule(&rule1, &mut facts3).unwrap();
        assert!(!executed); // Should not execute because email doesn't end with .edu
    }
}