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
//! Incremental Propagation Engine (P3 Feature - Advanced)
//!
//! This module implements incremental updates similar to Drools:
//! - Only propagate changed facts through the network
//! - Track affected rules and activations
//! - Efficient re-evaluation after updates

use super::agenda::{Activation, AdvancedAgenda};
use super::deffacts::DeffactsRegistry;
use super::facts::{FactValue, TypedFacts};
use super::globals::GlobalsRegistry;
use super::network::TypedReteUlRule;
use super::template::TemplateRegistry;
use super::tms::TruthMaintenanceSystem;
use super::working_memory::{FactHandle, WorkingMemory};
use crate::errors::{Result, RuleEngineError};
use std::collections::{HashMap, HashSet};
use std::sync::Arc;

/// Track which rules are affected by which fact types
#[derive(Debug)]
pub struct RuleDependencyGraph {
    /// Map: fact_type -> set of rule indices that depend on it
    fact_type_to_rules: HashMap<String, HashSet<usize>>,
    /// Map: rule index -> set of fact types it depends on
    rule_to_fact_types: HashMap<usize, HashSet<String>>,
}

impl RuleDependencyGraph {
    /// Create new dependency graph
    pub fn new() -> Self {
        Self {
            fact_type_to_rules: HashMap::new(),
            rule_to_fact_types: HashMap::new(),
        }
    }

    /// Add dependency: rule depends on fact type
    pub fn add_dependency(&mut self, rule_idx: usize, fact_type: String) {
        self.fact_type_to_rules
            .entry(fact_type.clone())
            .or_default()
            .insert(rule_idx);

        self.rule_to_fact_types
            .entry(rule_idx)
            .or_default()
            .insert(fact_type);
    }

    /// Get rules affected by a fact type change
    pub fn get_affected_rules(&self, fact_type: &str) -> HashSet<usize> {
        self.fact_type_to_rules
            .get(fact_type)
            .cloned()
            .unwrap_or_else(HashSet::new)
    }

    /// Get fact types that a rule depends on
    pub fn get_rule_dependencies(&self, rule_idx: usize) -> HashSet<String> {
        self.rule_to_fact_types
            .get(&rule_idx)
            .cloned()
            .unwrap_or_else(HashSet::new)
    }
}

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

/// Type alias for custom test functions in RETE engine
/// Functions take a slice of FactValues and return a FactValue (typically Boolean)
pub type ReteCustomFunction =
    Arc<dyn Fn(&[FactValue], &TypedFacts) -> Result<FactValue> + Send + Sync>;

/// Incremental Propagation Engine
/// Only re-evaluates rules affected by changed facts
pub struct IncrementalEngine {
    /// Working memory
    working_memory: WorkingMemory,
    /// Rules
    rules: Vec<TypedReteUlRule>,
    /// Dependency graph
    dependencies: RuleDependencyGraph,
    /// Advanced agenda
    agenda: AdvancedAgenda,
    /// Track which facts each rule last matched
    rule_matched_facts: HashMap<usize, HashSet<FactHandle>>,
    /// Template registry for type-safe facts
    templates: TemplateRegistry,
    /// Global variables registry
    globals: GlobalsRegistry,
    /// Deffacts registry for initial facts
    deffacts: DeffactsRegistry,
    /// Custom functions for Test CE support
    custom_functions: HashMap<String, ReteCustomFunction>,
    /// Truth Maintenance System
    tms: TruthMaintenanceSystem,
}

impl IncrementalEngine {
    /// Create new incremental engine
    pub fn new() -> Self {
        Self {
            working_memory: WorkingMemory::new(),
            rules: Vec::new(),
            dependencies: RuleDependencyGraph::new(),
            agenda: AdvancedAgenda::new(),
            rule_matched_facts: HashMap::new(),
            custom_functions: HashMap::new(),
            templates: TemplateRegistry::new(),
            globals: GlobalsRegistry::new(),
            deffacts: DeffactsRegistry::new(),
            tms: TruthMaintenanceSystem::new(),
        }
    }

    /// Add rule and register its dependencies
    pub fn add_rule(&mut self, rule: TypedReteUlRule, depends_on: Vec<String>) {
        let rule_idx = self.rules.len();

        // Register dependencies
        for fact_type in depends_on {
            self.dependencies.add_dependency(rule_idx, fact_type);
        }

        self.rules.push(rule);
    }

    /// Insert fact into working memory
    pub fn insert(&mut self, fact_type: String, data: TypedFacts) -> FactHandle {
        let handle = self.working_memory.insert(fact_type.clone(), data);

        // Default: Treat as explicit assertion (backward compatible)
        self.tms.add_explicit_justification(handle);

        // Trigger incremental propagation for this fact type
        self.propagate_changes_for_type(&fact_type);

        handle
    }

    /// Update fact in working memory
    pub fn update(&mut self, handle: FactHandle, data: TypedFacts) -> Result<()> {
        // Get fact type before update
        let fact_type = self
            .working_memory
            .get(&handle)
            .map(|f| f.fact_type.clone())
            .ok_or_else(|| RuleEngineError::FieldNotFound {
                field: format!("FactHandle {} not found", handle),
            })?;

        self.working_memory
            .update(handle, data)
            .map_err(|e| RuleEngineError::EvaluationError { message: e })?;

        // Trigger incremental propagation for this fact type
        self.propagate_changes_for_type(&fact_type);

        Ok(())
    }

    /// Retract fact from working memory
    pub fn retract(&mut self, handle: FactHandle) -> Result<()> {
        // Get fact type before retract
        let fact_type = self
            .working_memory
            .get(&handle)
            .map(|f| f.fact_type.clone())
            .ok_or_else(|| RuleEngineError::FieldNotFound {
                field: format!("FactHandle {} not found", handle),
            })?;

        self.working_memory
            .retract(handle)
            .map_err(|e| RuleEngineError::EvaluationError { message: e })?;

        // TMS: Handle cascade retraction
        let cascaded_facts = self.tms.retract_with_cascade(handle);

        // Actually retract cascaded facts from working memory
        for cascaded_handle in cascaded_facts {
            if let Ok(fact_type) = self
                .working_memory
                .get(&cascaded_handle)
                .map(|f| f.fact_type.clone())
                .ok_or_else(|| RuleEngineError::FieldNotFound {
                    field: format!("FactHandle {} not found", cascaded_handle),
                })
            {
                let _ = self.working_memory.retract(cascaded_handle);
                // Propagate for each cascaded fact
                self.propagate_changes_for_type(&fact_type);
            }
        }

        // Trigger incremental propagation for this fact type
        self.propagate_changes_for_type(&fact_type);

        Ok(())
    }

    /// Insert a fact with explicit assertion (user provided)
    /// This fact will NOT be auto-retracted by TMS
    pub fn insert_explicit(&mut self, fact_type: String, data: TypedFacts) -> FactHandle {
        let handle = self.working_memory.insert(fact_type.clone(), data);

        // Add explicit justification in TMS
        self.tms.add_explicit_justification(handle);

        // Trigger incremental propagation for this fact type
        self.propagate_changes_for_type(&fact_type);

        handle
    }

    /// Insert a fact with logical assertion (derived by a rule)
    /// This fact WILL be auto-retracted if its premises become invalid
    ///
    /// # Arguments
    /// * `fact_type` - Type of the fact (e.g., "Customer")
    /// * `data` - The fact data
    /// * `source_rule` - Name of the rule deriving this fact
    /// * `premise_handles` - Handles of facts matched in the rule's WHEN clause
    pub fn insert_logical(
        &mut self,
        fact_type: String,
        data: TypedFacts,
        source_rule: String,
        premise_handles: Vec<FactHandle>,
    ) -> FactHandle {
        let handle = self.working_memory.insert(fact_type.clone(), data);

        // Add logical justification in TMS
        self.tms
            .add_logical_justification(handle, source_rule, premise_handles);

        // Trigger incremental propagation for this fact type
        self.propagate_changes_for_type(&fact_type);

        handle
    }

    /// Resolve premise keys (format: "Type.field=value" or "Type.field=")
    /// to a Vec<FactHandle> by looking up facts of the given type and matching
    /// the field value when provided. If value is empty, return the most recent
    /// handle for that type (if any).
    pub fn resolve_premise_keys(&self, premise_keys: Vec<String>) -> Vec<FactHandle> {
        let mut handles = Vec::new();

        for key in premise_keys {
            // Split into type.field=value
            if let Some(eq_pos) = key.find('=') {
                let left = &key[..eq_pos];
                let value_part = &key[eq_pos + 1..];

                if let Some(dot_pos) = left.find('.') {
                    let fact_type = &left[..dot_pos];
                    let field = &left[dot_pos + 1..];

                    // Search facts of this type
                    let facts = self.working_memory.get_by_type(fact_type);
                    // If value_part is empty, pick last handle if any
                    if value_part.is_empty() {
                        // Prefer the most recent non-retracted fact for this type
                        if let Some(fact) = facts.iter().rev().find(|f| !f.metadata.retracted) {
                            handles.push(fact.handle);
                            continue;
                        }
                    } else {
                        // Try to match provided value text against the field in TypedFacts
                        // Parse the provided value into a FactValue-like expectation so we
                        // can compare numbers/booleans properly instead of relying on string equality.
                        fn parse_literal(s: &str) -> super::facts::FactValue {
                            let s = s.trim();
                            if s == "true" {
                                return super::facts::FactValue::Boolean(true);
                            }
                            if s == "false" {
                                return super::facts::FactValue::Boolean(false);
                            }
                            // Quoted string
                            if (s.starts_with('"') && s.ends_with('"'))
                                || (s.starts_with('\'') && s.ends_with('\''))
                            {
                                return super::facts::FactValue::String(
                                    s[1..s.len() - 1].to_string(),
                                );
                            }
                            // Integer
                            if let Ok(i) = s.parse::<i64>() {
                                return super::facts::FactValue::Integer(i);
                            }
                            // Float
                            if let Ok(f) = s.parse::<f64>() {
                                return super::facts::FactValue::Float(f);
                            }

                            // Fallback to string
                            super::facts::FactValue::String(s.to_string())
                        }

                        fn fact_value_equal(
                            a: &super::facts::FactValue,
                            b: &super::facts::FactValue,
                        ) -> bool {
                            use super::facts::FactValue;
                            match (a, b) {
                                (FactValue::Boolean(x), FactValue::Boolean(y)) => x == y,
                                (FactValue::Integer(x), FactValue::Integer(y)) => x == y,
                                (FactValue::Float(x), FactValue::Float(y)) => (x - y).abs() < 1e-9,
                                // Number cross-type comparisons
                                (FactValue::Integer(x), FactValue::Float(y)) => {
                                    ((*x as f64) - *y).abs() < 1e-9
                                }
                                (FactValue::Float(x), FactValue::Integer(y)) => {
                                    (*x - (*y as f64)).abs() < 1e-9
                                }
                                (FactValue::String(x), FactValue::String(y)) => x == y,
                                // Mixed string vs other: compare stringified forms
                                _ => a.as_str() == b.as_str(),
                            }
                        }

                        let expected = parse_literal(value_part);

                        // Prefer the most recent non-retracted matching fact for determinism
                        if let Some(fact) = facts.iter().rev().find(|fact| {
                            if fact.metadata.retracted {
                                return false;
                            }
                            if let Some(fv) = fact.data.get(field) {
                                fact_value_equal(fv, &expected) || fv.as_str() == value_part
                            } else {
                                false
                            }
                        }) {
                            handles.push(fact.handle);
                        }
                    }
                }
            }
        }

        handles
    }

    /// Get TMS reference
    pub fn tms(&self) -> &TruthMaintenanceSystem {
        &self.tms
    }

    /// Get mutable TMS reference
    pub fn tms_mut(&mut self) -> &mut TruthMaintenanceSystem {
        &mut self.tms
    }

    /// Propagate changes for a specific fact type (incremental!)
    fn propagate_changes_for_type(&mut self, fact_type: &str) {
        // Get affected rules
        let affected_rules = self.dependencies.get_affected_rules(fact_type);

        if affected_rules.is_empty() {
            return; // No rules depend on this fact type
        }

        // Get facts of this type
        let facts_of_type = self.working_memory.get_by_type(fact_type);

        // Re-evaluate only affected rules, checking each fact individually
        for &rule_idx in &affected_rules {
            let rule = &self.rules[rule_idx];

            // Check each fact of this type against the rule
            for fact in &facts_of_type {
                // Create TypedFacts for just this fact
                let mut single_fact_data = TypedFacts::new();
                for (key, value) in fact.data.get_all() {
                    single_fact_data.set(format!("{}.{}", fact_type, key), value.clone());
                }
                // Store handle for Retract action
                single_fact_data.set_fact_handle(fact_type.to_string(), fact.handle);

                // Evaluate rule condition with this single fact
                let matches =
                    super::network::evaluate_rete_ul_node_typed(&rule.node, &single_fact_data);

                if matches {
                    // Create activation for this specific fact match
                    let activation = Activation::new(rule.name.clone(), rule.priority)
                        .with_no_loop(rule.no_loop)
                        .with_matched_fact(fact.handle);

                    self.agenda.add_activation(activation);
                }
            }
        }
    }

    /// Propagate changes for all fact types (re-evaluate all rules)
    fn propagate_changes(&mut self) {
        // Get all fact types
        let fact_types: Vec<String> = self
            .working_memory
            .get_all_facts()
            .iter()
            .map(|f| f.fact_type.clone())
            .collect::<std::collections::HashSet<_>>()
            .into_iter()
            .collect();

        // Evaluate each fact type using per-fact evaluation
        for fact_type in fact_types {
            let facts_of_type = self.working_memory.get_by_type(&fact_type);

            for rule in self.rules.iter() {
                // Skip if rule has no-loop and already fired
                if rule.no_loop && self.agenda.has_fired(&rule.name) {
                    continue;
                }

                // Check each fact against the rule
                for fact in &facts_of_type {
                    let mut single_fact_data = TypedFacts::new();
                    for (key, value) in fact.data.get_all() {
                        single_fact_data.set(format!("{}.{}", fact_type, key), value.clone());
                    }

                    let matches =
                        super::network::evaluate_rete_ul_node_typed(&rule.node, &single_fact_data);

                    if matches {
                        let activation = Activation::new(rule.name.clone(), rule.priority)
                            .with_no_loop(rule.no_loop)
                            .with_matched_fact(fact.handle);

                        self.agenda.add_activation(activation);
                    }
                }
            }
        }
    }

    /// Fire all pending activations
    pub fn fire_all(&mut self) -> Vec<String> {
        let mut fired_rules = Vec::new();
        let max_iterations = 1000; // Prevent infinite loops
        let mut iteration_count = 0;

        while let Some(activation) = self.agenda.get_next_activation() {
            iteration_count += 1;
            if iteration_count > max_iterations {
                eprintln!("WARNING: Maximum iterations ({}) reached in fire_all(). Possible infinite loop!", max_iterations);
                break;
            }

            // Find rule
            if let Some((_idx, rule)) = self
                .rules
                .iter_mut()
                .enumerate()
                .find(|(_, r)| r.name == activation.rule_name)
            {
                // Validate that matched fact still exists (hasn't been retracted)
                if let Some(matched_handle) = activation.matched_fact_handle {
                    if self.working_memory.get(&matched_handle).is_none() {
                        // Fact was retracted, skip this activation
                        continue;
                    }
                }

                // Execute action on a copy of all facts
                let original_facts = self.working_memory.to_typed_facts();
                let mut modified_facts = original_facts.clone();

                // Inject matched fact handle if available
                if let Some(matched_handle) = activation.matched_fact_handle {
                    // Find the fact type from working memory
                    if let Some(fact) = self.working_memory.get(&matched_handle) {
                        modified_facts.set_fact_handle(fact.fact_type.clone(), matched_handle);
                    }
                }

                let mut action_results = super::ActionResults::new();
                (rule.action)(&mut modified_facts, &mut action_results);

                // Update working memory: detect changes and apply them
                // Build a map of fact_type -> updated fields
                let mut updates_by_type: HashMap<String, Vec<(String, FactValue)>> = HashMap::new();

                for (key, value) in modified_facts.get_all() {
                    // Keys are in format "FactType.field" or "FactType.handle.field"
                    // We want to extract the FactType and field name
                    if let Some(original_value) = original_facts.get(key) {
                        if original_value != value {
                            // Value changed! Extract fact type and field
                            let parts: Vec<&str> = key.split('.').collect();
                            if parts.len() >= 2 {
                                let fact_type = parts[0].to_string();
                                // Field is the last part (skip handle if present)
                                let field = if parts.len() == 2 {
                                    parts[1].to_string()
                                } else {
                                    parts[parts.len() - 1].to_string()
                                };

                                updates_by_type
                                    .entry(fact_type)
                                    .or_default()
                                    .push((field, value.clone()));
                            }
                        }
                    } else {
                        // New field added
                        let parts: Vec<&str> = key.split('.').collect();
                        if parts.len() >= 2 {
                            let fact_type = parts[0].to_string();
                            let field = if parts.len() == 2 {
                                parts[1].to_string()
                            } else {
                                parts[parts.len() - 1].to_string()
                            };

                            updates_by_type
                                .entry(fact_type)
                                .or_default()
                                .push((field, value.clone()));
                        }
                    }
                }

                // Apply updates to working memory facts
                for (fact_type, field_updates) in updates_by_type {
                    // Get handles of all facts of this type (collect to avoid borrow issues)
                    let fact_handles: Vec<FactHandle> = self
                        .working_memory
                        .get_by_type(&fact_type)
                        .iter()
                        .map(|f| f.handle)
                        .collect();

                    for handle in fact_handles {
                        if let Some(fact) = self.working_memory.get(&handle) {
                            let mut updated_data = fact.data.clone();

                            // Apply all field updates
                            for (field, value) in &field_updates {
                                updated_data.set(field, value.clone());
                            }

                            let _ = self.working_memory.update(handle, updated_data);
                        }
                    }
                }

                // Re-evaluate matches after working memory update
                // This allows subsequent rules to see the updated values
                self.propagate_changes();

                // Process action results (retractions, agenda activations, etc.)
                self.process_action_results(action_results);

                // Track fired rule
                fired_rules.push(activation.rule_name.clone());
                self.agenda.mark_rule_fired(&activation);
            }
        }

        fired_rules
    }

    /// Process action results from rule execution
    fn process_action_results(&mut self, results: super::ActionResults) {
        for result in results.results {
            match result {
                super::ActionResult::Retract(handle) => {
                    // Retract fact by handle
                    if let Err(e) = self.retract(handle) {
                        eprintln!("❌ Failed to retract fact {:?}: {}", handle, e);
                    }
                }
                super::ActionResult::RetractByType(fact_type) => {
                    // Retract first fact of this type
                    let facts_of_type = self.working_memory.get_by_type(&fact_type);
                    if let Some(fact) = facts_of_type.first() {
                        let handle = fact.handle;
                        if let Err(e) = self.retract(handle) {
                            eprintln!("❌ Failed to retract fact {:?}: {}", handle, e);
                        }
                    }
                }
                super::ActionResult::Update(handle) => {
                    // Re-evaluate rules that depend on this fact type
                    if let Some(fact) = self.working_memory.get(&handle) {
                        let fact_type = fact.fact_type.clone();
                        self.propagate_changes_for_type(&fact_type);
                    }
                }
                super::ActionResult::ActivateAgendaGroup(group) => {
                    // Activate agenda group
                    self.agenda.set_focus(group);
                }
                super::ActionResult::InsertFact { fact_type, data } => {
                    // Insert new explicit fact
                    self.insert_explicit(fact_type, data);
                }
                super::ActionResult::InsertLogicalFact {
                    fact_type,
                    data,
                    rule_name,
                    premises,
                } => {
                    // Insert new logical fact
                    let _handle = self.insert_logical(fact_type, data, rule_name, premises);
                }
                super::ActionResult::CallFunction {
                    function_name,
                    args,
                } => {
                    // Try to execute function if registered
                    if let Some(func) = self.custom_functions.get(&function_name) {
                        // Convert string args to FactValues
                        let fact_values: Vec<FactValue> =
                            args.iter().map(|s| FactValue::String(s.clone())).collect();

                        // Execute function (ignore return value for actions)
                        let all_facts = self.working_memory.to_typed_facts();
                        match func(&fact_values, &all_facts) {
                            Ok(_) => println!("✅ Called function: {}", function_name),
                            Err(e) => eprintln!("❌ Function {} failed: {}", function_name, e),
                        }
                    } else {
                        // Function not registered, just log
                        println!("🔧 Function call queued: {}({:?})", function_name, args);
                    }
                }
                super::ActionResult::ScheduleRule {
                    rule_name,
                    delay_ms,
                } => {
                    // Log scheduled rules (requires scheduler to actually execute)
                    println!("⏰ Rule scheduled: {} after {}ms", rule_name, delay_ms);
                    // TODO: Implement rule scheduler
                }
                super::ActionResult::None => {
                    // No action needed
                }
            }
        }
    }

    /// Get working memory
    pub fn working_memory(&self) -> &WorkingMemory {
        &self.working_memory
    }

    /// Get mutable working memory
    pub fn working_memory_mut(&mut self) -> &mut WorkingMemory {
        &mut self.working_memory
    }

    /// Get agenda
    pub fn agenda(&self) -> &AdvancedAgenda {
        &self.agenda
    }

    /// Get mutable agenda
    pub fn agenda_mut(&mut self) -> &mut AdvancedAgenda {
        &mut self.agenda
    }

    /// Set conflict resolution strategy
    ///
    /// Controls how conflicting rules in the agenda are ordered.
    /// Available strategies: Salience (default), LEX, MEA, Depth, Breadth, Simplicity, Complexity, Random
    pub fn set_conflict_resolution_strategy(
        &mut self,
        strategy: super::agenda::ConflictResolutionStrategy,
    ) {
        self.agenda.set_strategy(strategy);
    }

    /// Get current conflict resolution strategy
    pub fn conflict_resolution_strategy(&self) -> super::agenda::ConflictResolutionStrategy {
        self.agenda.strategy()
    }

    /// Get statistics
    pub fn stats(&self) -> IncrementalEngineStats {
        IncrementalEngineStats {
            rules: self.rules.len(),
            working_memory: self.working_memory.stats(),
            agenda: self.agenda.stats(),
            dependencies: self.dependencies.fact_type_to_rules.len(),
        }
    }

    /// Clear fired flags and reset agenda
    pub fn reset(&mut self) {
        self.agenda.reset_fired_flags();
    }

    /// Get template registry
    pub fn templates(&self) -> &TemplateRegistry {
        &self.templates
    }

    /// Get mutable template registry
    pub fn templates_mut(&mut self) -> &mut TemplateRegistry {
        &mut self.templates
    }

    /// Register a custom function for Test CE support
    ///
    /// # Example
    /// ```
    /// use rust_rule_engine::rete::{IncrementalEngine, FactValue};
    ///
    /// let mut engine = IncrementalEngine::new();
    /// engine.register_function(
    ///     "is_valid_email",
    ///     |args, _facts| {
    ///         if let Some(FactValue::String(email)) = args.first() {
    ///             Ok(FactValue::Boolean(email.contains('@')))
    ///         } else {
    ///             Ok(FactValue::Boolean(false))
    ///         }
    ///     }
    /// );
    /// ```
    pub fn register_function<F>(&mut self, name: &str, func: F)
    where
        F: Fn(&[FactValue], &TypedFacts) -> Result<FactValue> + Send + Sync + 'static,
    {
        self.custom_functions
            .insert(name.to_string(), Arc::new(func));
    }

    /// Get a custom function by name (for Test CE evaluation)
    pub fn get_function(&self, name: &str) -> Option<&ReteCustomFunction> {
        self.custom_functions.get(name)
    }

    /// Get global variables registry
    pub fn globals(&self) -> &GlobalsRegistry {
        &self.globals
    }

    /// Get mutable global variables registry
    pub fn globals_mut(&mut self) -> &mut GlobalsRegistry {
        &mut self.globals
    }

    /// Get deffacts registry
    pub fn deffacts(&self) -> &DeffactsRegistry {
        &self.deffacts
    }

    /// Get mutable deffacts registry
    pub fn deffacts_mut(&mut self) -> &mut DeffactsRegistry {
        &mut self.deffacts
    }

    /// Load all registered deffacts into working memory
    /// Returns handles of all inserted facts
    pub fn load_deffacts(&mut self) -> Vec<FactHandle> {
        let mut handles = Vec::new();

        // Get all facts from all registered deffacts
        let all_facts = self.deffacts.get_all_facts();

        for (_deffacts_name, fact_instance) in all_facts {
            // Check if template exists for this fact type
            let handle = if self.templates.get(&fact_instance.fact_type).is_some() {
                // Use template validation
                match self.insert_with_template(&fact_instance.fact_type, fact_instance.data) {
                    Ok(h) => h,
                    Err(_) => continue, // Skip invalid facts
                }
            } else {
                // Insert without template validation
                self.insert(fact_instance.fact_type, fact_instance.data)
            };

            handles.push(handle);
        }

        handles
    }

    /// Load a specific deffacts set by name
    /// Returns handles of inserted facts or error if deffacts not found
    pub fn load_deffacts_by_name(&mut self, name: &str) -> crate::errors::Result<Vec<FactHandle>> {
        // Clone the facts to avoid borrow checker issues
        let facts_to_insert = {
            let deffacts = self.deffacts.get(name).ok_or_else(|| {
                crate::errors::RuleEngineError::EvaluationError {
                    message: format!("Deffacts '{}' not found", name),
                }
            })?;
            deffacts.facts.clone()
        };

        let mut handles = Vec::new();

        for fact_instance in facts_to_insert {
            // Check if template exists for this fact type
            let handle = if self.templates.get(&fact_instance.fact_type).is_some() {
                // Use template validation
                self.insert_with_template(&fact_instance.fact_type, fact_instance.data)?
            } else {
                // Insert without template validation
                self.insert(fact_instance.fact_type, fact_instance.data)
            };

            handles.push(handle);
        }

        Ok(handles)
    }

    /// Reset engine and reload all deffacts (similar to CLIPS reset)
    /// Clears working memory and agenda, then loads all deffacts
    pub fn reset_with_deffacts(&mut self) -> Vec<FactHandle> {
        // Clear working memory and agenda
        self.working_memory = WorkingMemory::new();
        self.agenda.clear();
        self.rule_matched_facts.clear();

        // Reload all deffacts
        self.load_deffacts()
    }

    /// Insert a typed fact with template validation
    pub fn insert_with_template(
        &mut self,
        template_name: &str,
        data: TypedFacts,
    ) -> crate::errors::Result<FactHandle> {
        // Validate against template
        self.templates.validate(template_name, &data)?;

        // Insert into working memory
        Ok(self.insert(template_name.to_string(), data))
    }
}

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

/// Engine statistics
#[derive(Debug)]
pub struct IncrementalEngineStats {
    pub rules: usize,
    pub working_memory: super::working_memory::WorkingMemoryStats,
    pub agenda: super::agenda::AgendaStats,
    pub dependencies: usize,
}

impl std::fmt::Display for IncrementalEngineStats {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(
            f,
            "Engine Stats: {} rules, {} fact types tracked\nWM: {}\nAgenda: {}",
            self.rules, self.dependencies, self.working_memory, self.agenda
        )
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::rete::alpha::AlphaNode;
    use crate::rete::network::ReteUlNode;

    #[test]
    fn test_dependency_graph() {
        let mut graph = RuleDependencyGraph::new();

        graph.add_dependency(0, "Person".to_string());
        graph.add_dependency(1, "Person".to_string());
        graph.add_dependency(1, "Order".to_string());

        let affected = graph.get_affected_rules("Person");
        assert_eq!(affected.len(), 2);
        assert!(affected.contains(&0));
        assert!(affected.contains(&1));

        let deps = graph.get_rule_dependencies(1);
        assert_eq!(deps.len(), 2);
        assert!(deps.contains("Person"));
        assert!(deps.contains("Order"));
    }

    #[test]
    fn test_incremental_propagation() {
        let mut engine = IncrementalEngine::new();

        // Add rule that depends on "Person" type
        let node = ReteUlNode::UlAlpha(AlphaNode {
            field: "Person.age".to_string(),
            operator: ">".to_string(),
            value: "18".to_string(),
        });

        let rule = TypedReteUlRule {
            name: "IsAdult".to_string(),
            node,
            priority: 0,
            no_loop: true,
            action: std::sync::Arc::new(|_, _| {}),
        };

        engine.add_rule(rule, vec!["Person".to_string()]);

        // Insert Person fact
        let mut person = TypedFacts::new();
        person.set("age", 25i64);
        let handle = engine.insert("Person".to_string(), person);

        // Check that rule was activated
        let stats = engine.stats();
        assert!(stats.agenda.total_activations > 0);

        // Update person
        let mut updated = TypedFacts::new();
        updated.set("age", 15i64); // Now under 18
        engine.update(handle, updated).unwrap();

        // Rule should be re-evaluated (incrementally)
    }
}