oxirs-rule 0.2.4

Forward/backward rule engine for RDFS, OWL, and SWRL reasoning
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
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
//! # Rule Engine Debugging Tools
//!
//! Advanced debugging capabilities for rule engines including execution visualization,
//! derivation path tracing, performance profiling, and conflict detection.

use anyhow::{anyhow, Result};
use serde::{Deserialize, Serialize};
use std::collections::{HashMap, HashSet};
use std::fmt::{self, Display};
use std::time::{Duration, Instant};

use crate::{RuleAtom, RuleEngine};

/// Debug trace entry recording rule execution steps
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct TraceEntry {
    pub timestamp: u64,
    pub rule_name: String,
    pub action: TraceAction,
    pub input_facts: Vec<RuleAtom>,
    pub output_facts: Vec<RuleAtom>,
    pub duration: Duration,
    pub memory_usage: usize,
}

/// Types of trace actions
#[derive(Debug, Clone, Serialize, Deserialize)]
pub enum TraceAction {
    RuleExecution,
    FactAddition,
    Unification,
    BackwardChaining,
    ForwardChaining,
    ReteActivation,
}

/// Derivation path showing how a fact was derived
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct DerivationPath {
    pub target_fact: RuleAtom,
    pub steps: Vec<DerivationStep>,
    pub total_depth: usize,
    pub involved_rules: HashSet<String>,
}

/// Single step in a derivation path
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct DerivationStep {
    pub rule_name: String,
    pub premises: Vec<RuleAtom>,
    pub conclusion: RuleAtom,
    pub unification: HashMap<String, String>,
    pub step_number: usize,
}

/// Performance metrics for rule execution
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct PerformanceMetrics {
    pub total_execution_time: Duration,
    pub rule_execution_times: HashMap<String, Duration>,
    pub rule_execution_counts: HashMap<String, usize>,
    pub facts_processed: usize,
    pub facts_derived: usize,
    pub memory_peak: usize,
    pub cache_hits: usize,
    pub cache_misses: usize,
}

/// Rule conflict detection and analysis
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct RuleConflict {
    pub conflict_type: ConflictType,
    pub involved_rules: Vec<String>,
    pub conflicting_facts: Vec<RuleAtom>,
    pub severity: ConflictSeverity,
    pub resolution_suggestion: String,
}

/// Types of rule conflicts
#[derive(Debug, Clone, Serialize, Deserialize)]
pub enum ConflictType {
    ContradictoryConclusions,
    CircularDependency,
    RedundantRules,
    UnreachableRules,
    PerformanceBottleneck,
}

/// Severity levels for conflicts
#[derive(Debug, Clone, Serialize, Deserialize)]
pub enum ConflictSeverity {
    Critical,
    Warning,
    Info,
}

/// Debugger state
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum DebuggerState {
    /// Running normally
    Running,
    /// Paused at breakpoint
    Paused,
    /// Stepping through execution
    Stepping,
    /// Execution complete
    Finished,
}

/// Debug command for interactive debugging
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum DebugCommand {
    /// Continue execution until next breakpoint
    Continue,
    /// Step to next rule execution
    Step,
    /// Step over current rule (execute without entering)
    Next,
    /// Step out of current rule
    StepOut,
    /// Print current state
    Print,
    /// List breakpoints
    ListBreakpoints,
    /// Show call stack
    Backtrace,
    /// Quit debugging
    Quit,
}

/// Stack frame for call stack tracking
#[derive(Debug, Clone)]
pub struct StackFrame {
    /// Rule name
    pub rule_name: String,
    /// Current substitutions
    pub substitutions: HashMap<String, String>,
    /// Input facts for this frame
    pub input_facts: Vec<RuleAtom>,
    /// Frame depth
    pub depth: usize,
}

/// Watch expression for monitoring values
#[derive(Debug, Clone)]
pub struct WatchExpression {
    /// Expression name/description
    pub name: String,
    /// Variable to watch
    pub variable: String,
    /// Last known value
    pub last_value: Option<String>,
    /// Break when value changes
    pub break_on_change: bool,
}

/// Conditional breakpoint
#[derive(Debug, Clone)]
pub struct ConditionalBreakpoint {
    /// Rule name to break on
    pub rule_name: String,
    /// Condition that must be true (variable = value)
    pub condition: Option<BreakpointCondition>,
    /// Hit count (break after N hits)
    pub hit_count: Option<usize>,
    /// Current hit count
    pub current_hits: usize,
    /// Is breakpoint enabled
    pub enabled: bool,
}

/// Breakpoint condition
#[derive(Debug, Clone)]
pub struct BreakpointCondition {
    /// Variable name
    pub variable: String,
    /// Expected value
    pub value: String,
    /// Comparison operator
    pub operator: ConditionOperator,
}

/// Condition operator
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum ConditionOperator {
    /// Equal
    Equals,
    /// Not equal
    NotEquals,
    /// Contains substring
    Contains,
}

/// Interactive debugging session
#[derive(Debug)]
pub struct DebugSession {
    pub trace: Vec<TraceEntry>,
    pub derivations: HashMap<String, DerivationPath>,
    pub metrics: PerformanceMetrics,
    pub conflicts: Vec<RuleConflict>,
    pub breakpoints: HashSet<String>,
    pub step_mode: bool,
    pub current_step: usize,
    /// Debugger state
    pub state: DebuggerState,
    /// Call stack
    pub call_stack: Vec<StackFrame>,
    /// Conditional breakpoints
    pub conditional_breakpoints: Vec<ConditionalBreakpoint>,
    /// Watch expressions
    pub watch_expressions: Vec<WatchExpression>,
    /// Current substitutions (variable bindings)
    pub current_substitutions: HashMap<String, String>,
    /// Paused events queue (for async notification)
    pub paused_events: Vec<String>,
}

/// Enhanced rule engine with debugging capabilities
#[derive(Debug)]
pub struct DebuggableRuleEngine {
    pub engine: RuleEngine,
    pub debug_session: DebugSession,
    pub debug_enabled: bool,
}

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

impl DebuggableRuleEngine {
    /// Create a new debuggable rule engine
    pub fn new() -> Self {
        Self {
            engine: RuleEngine::new(),
            debug_session: DebugSession::new(),
            debug_enabled: false,
        }
    }

    /// Enable debugging with optional configuration
    pub fn enable_debugging(&mut self, step_mode: bool) {
        self.debug_enabled = true;
        self.debug_session.step_mode = step_mode;
        self.debug_session.clear();
    }

    /// Disable debugging
    pub fn disable_debugging(&mut self) {
        self.debug_enabled = false;
        self.debug_session.clear();
    }

    /// Add a breakpoint on a specific rule
    pub fn add_breakpoint(&mut self, rule_name: &str) {
        self.debug_session.breakpoints.insert(rule_name.to_string());
    }

    /// Remove a breakpoint
    pub fn remove_breakpoint(&mut self, rule_name: &str) {
        self.debug_session.breakpoints.remove(rule_name);
    }

    /// Execute forward chaining with debugging
    pub fn debug_forward_chain(&mut self, facts: &[RuleAtom]) -> Result<Vec<RuleAtom>> {
        if !self.debug_enabled {
            return self.engine.forward_chain(facts);
        }

        let start_time = Instant::now();
        let initial_memory = self.estimate_memory_usage();

        // Record fact addition
        self.record_trace_entry(TraceEntry {
            timestamp: self.current_timestamp(),
            rule_name: "__fact_addition__".to_string(),
            action: TraceAction::FactAddition,
            input_facts: vec![],
            output_facts: facts.to_vec(),
            duration: Duration::from_nanos(0),
            memory_usage: initial_memory,
        });

        // Execute with tracing
        let result = self.traced_forward_chain(facts)?;

        // Update metrics
        self.debug_session.metrics.total_execution_time = start_time.elapsed();
        self.debug_session.metrics.facts_processed = facts.len();
        self.debug_session.metrics.facts_derived = result.len() - facts.len();
        self.debug_session.metrics.memory_peak = self.estimate_memory_usage();

        // Analyze for conflicts
        self.analyze_conflicts();

        Ok(result)
    }

    /// Execute backward chaining with debugging
    pub fn debug_backward_chain(&mut self, goal: &RuleAtom) -> Result<bool> {
        if !self.debug_enabled {
            return self.engine.backward_chain(goal);
        }

        let start_time = Instant::now();

        // Build derivation path
        let derivation = self.build_derivation_path(goal)?;
        if let Some(path) = derivation {
            let goal_key = format!("{goal:?}");
            self.debug_session.derivations.insert(goal_key, path);
        }

        let result = self.traced_backward_chain(goal)?;

        // Record performance
        let duration = start_time.elapsed();
        self.record_trace_entry(TraceEntry {
            timestamp: self.current_timestamp(),
            rule_name: "__backward_chain__".to_string(),
            action: TraceAction::BackwardChaining,
            input_facts: vec![goal.clone()],
            output_facts: if result { vec![goal.clone()] } else { vec![] },
            duration,
            memory_usage: self.estimate_memory_usage(),
        });

        Ok(result)
    }

    /// Get execution trace
    pub fn get_trace(&self) -> &[TraceEntry] {
        &self.debug_session.trace
    }

    /// Get derivation path for a fact
    pub fn get_derivation(&self, fact: &RuleAtom) -> Option<&DerivationPath> {
        let key = format!("{fact:?}");
        self.debug_session.derivations.get(&key)
    }

    /// Get performance metrics
    pub fn get_metrics(&self) -> &PerformanceMetrics {
        &self.debug_session.metrics
    }

    /// Get detected conflicts
    pub fn get_conflicts(&self) -> &[RuleConflict] {
        &self.debug_session.conflicts
    }

    /// Generate debug report
    pub fn generate_debug_report(&self) -> String {
        let mut report = String::new();

        report.push_str("=== RULE ENGINE DEBUG REPORT ===\n\n");

        // Performance metrics
        report.push_str("PERFORMANCE METRICS:\n");
        report.push_str(&format!(
            "Total execution time: {:?}\n",
            self.debug_session.metrics.total_execution_time
        ));
        report.push_str(&format!(
            "Facts processed: {}\n",
            self.debug_session.metrics.facts_processed
        ));
        report.push_str(&format!(
            "Facts derived: {}\n",
            self.debug_session.metrics.facts_derived
        ));
        report.push_str(&format!(
            "Memory peak: {} bytes\n",
            self.debug_session.metrics.memory_peak
        ));
        report.push_str(&format!(
            "Cache hits: {}\n",
            self.debug_session.metrics.cache_hits
        ));
        report.push_str(&format!(
            "Cache misses: {}\n",
            self.debug_session.metrics.cache_misses
        ));
        report.push('\n');

        // Rule execution times
        report.push_str("RULE EXECUTION TIMES:\n");
        let mut rule_times: Vec<_> = self
            .debug_session
            .metrics
            .rule_execution_times
            .iter()
            .collect();
        rule_times.sort_by(|a, b| b.1.cmp(a.1));
        for (rule, time) in rule_times.iter().take(10) {
            let count = self
                .debug_session
                .metrics
                .rule_execution_counts
                .get(*rule)
                .unwrap_or(&0);
            report.push_str(&format!("  {rule}: {time:?} (executed {count} times)\n"));
        }
        report.push('\n');

        // Conflicts
        if !self.debug_session.conflicts.is_empty() {
            report.push_str("DETECTED CONFLICTS:\n");
            for conflict in &self.debug_session.conflicts {
                report.push_str(&format!(
                    "  {:?} - {:?}: {}\n",
                    conflict.severity, conflict.conflict_type, conflict.resolution_suggestion
                ));
            }
            report.push('\n');
        }

        // Derivation paths
        if !self.debug_session.derivations.is_empty() {
            report.push_str("DERIVATION PATHS:\n");
            for (fact, path) in &self.debug_session.derivations {
                report.push_str(&format!("  Fact: {fact}\n"));
                report.push_str(&format!("  Depth: {}\n", path.total_depth));
                report.push_str(&format!(
                    "  Rules involved: {}\n",
                    path.involved_rules
                        .iter()
                        .cloned()
                        .collect::<Vec<_>>()
                        .join(", ")
                ));
                for step in &path.steps {
                    report.push_str(&format!(
                        "    Step {}: {} -> {:?}\n",
                        step.step_number, step.rule_name, step.conclusion
                    ));
                }
                report.push('\n');
            }
        }

        report
    }

    /// Export debug data to JSON
    pub fn export_debug_data(&self) -> Result<String> {
        #[derive(Serialize)]
        struct DebugData {
            trace: Vec<TraceEntry>,
            derivations: HashMap<String, DerivationPath>,
            metrics: PerformanceMetrics,
            conflicts: Vec<RuleConflict>,
        }

        let data = DebugData {
            trace: self.debug_session.trace.clone(),
            derivations: self.debug_session.derivations.clone(),
            metrics: self.debug_session.metrics.clone(),
            conflicts: self.debug_session.conflicts.clone(),
        };

        serde_json::to_string_pretty(&data)
            .map_err(|e| anyhow!("Failed to serialize debug data: {}", e))
    }

    // Private implementation methods

    fn traced_forward_chain(&mut self, facts: &[RuleAtom]) -> Result<Vec<RuleAtom>> {
        // Implementation would integrate with the actual forward chaining
        // For now, delegate to the underlying engine
        self.engine.forward_chain(facts)
    }

    fn traced_backward_chain(&mut self, goal: &RuleAtom) -> Result<bool> {
        // Implementation would integrate with the actual backward chaining
        // For now, delegate to the underlying engine
        self.engine.backward_chain(goal)
    }

    fn build_derivation_path(&self, goal: &RuleAtom) -> Result<Option<DerivationPath>> {
        // Placeholder implementation - would build actual derivation tree
        Ok(Some(DerivationPath {
            target_fact: goal.clone(),
            steps: vec![],
            total_depth: 0,
            involved_rules: HashSet::new(),
        }))
    }

    fn analyze_conflicts(&mut self) {
        // Analyze rule conflicts and populate conflicts vector
        self.detect_circular_dependencies();
        self.detect_contradictory_rules();
        self.detect_redundant_rules();
        self.detect_performance_bottlenecks();
    }

    fn detect_circular_dependencies(&mut self) {
        // Implementation for detecting circular rule dependencies
        // This would analyze the rule dependency graph
    }

    fn detect_contradictory_rules(&mut self) {
        // Implementation for detecting contradictory rule conclusions
    }

    fn detect_redundant_rules(&mut self) {
        // Implementation for detecting redundant rules
    }

    fn detect_performance_bottlenecks(&mut self) {
        // Analyze performance metrics to identify bottlenecks
        for (rule_name, duration) in &self.debug_session.metrics.rule_execution_times {
            if duration.as_millis() > 100 {
                // Threshold for slow rules
                self.debug_session.conflicts.push(RuleConflict {
                    conflict_type: ConflictType::PerformanceBottleneck,
                    involved_rules: vec![rule_name.clone()],
                    conflicting_facts: vec![],
                    severity: ConflictSeverity::Warning,
                    resolution_suggestion: format!(
                        "Rule '{}' is slow ({}ms). Consider optimization.",
                        rule_name,
                        duration.as_millis()
                    ),
                });
            }
        }
    }

    fn record_trace_entry(&mut self, entry: TraceEntry) {
        // Extract values before moving the entry
        let rule_name = entry.rule_name.clone();
        let duration = entry.duration;

        self.debug_session.trace.push(entry);

        // Update metrics
        if let Some(current_count) = self
            .debug_session
            .metrics
            .rule_execution_counts
            .get_mut(&rule_name)
        {
            *current_count += 1;
        } else {
            self.debug_session
                .metrics
                .rule_execution_counts
                .insert(rule_name.clone(), 1);
        }

        // Update timing
        if let Some(current_time) = self
            .debug_session
            .metrics
            .rule_execution_times
            .get_mut(&rule_name)
        {
            *current_time += duration;
        } else {
            self.debug_session
                .metrics
                .rule_execution_times
                .insert(rule_name, duration);
        }
    }

    fn current_timestamp(&self) -> u64 {
        std::time::SystemTime::now()
            .duration_since(std::time::UNIX_EPOCH)
            .unwrap_or_default()
            .as_millis() as u64
    }

    fn estimate_memory_usage(&self) -> usize {
        // Rough estimation of memory usage
        std::mem::size_of::<RuleEngine>()
            + self.debug_session.trace.len() * std::mem::size_of::<TraceEntry>()
            + self.debug_session.derivations.len() * 1024 // Rough estimate
    }
}

impl DebugSession {
    fn new() -> Self {
        Self {
            trace: Vec::new(),
            derivations: HashMap::new(),
            metrics: PerformanceMetrics::default(),
            conflicts: Vec::new(),
            breakpoints: HashSet::new(),
            step_mode: false,
            current_step: 0,
            state: DebuggerState::Running,
            call_stack: Vec::new(),
            conditional_breakpoints: Vec::new(),
            watch_expressions: Vec::new(),
            current_substitutions: HashMap::new(),
            paused_events: Vec::new(),
        }
    }

    fn clear(&mut self) {
        self.trace.clear();
        self.derivations.clear();
        self.conflicts.clear();
        self.metrics = PerformanceMetrics::default();
        self.current_step = 0;
        self.state = DebuggerState::Running;
        self.call_stack.clear();
        self.current_substitutions.clear();
        self.paused_events.clear();
        // Keep breakpoints and watch expressions
    }
}

impl DebuggableRuleEngine {
    /// Add a conditional breakpoint
    pub fn add_conditional_breakpoint(
        &mut self,
        rule_name: &str,
        condition: Option<BreakpointCondition>,
        hit_count: Option<usize>,
    ) {
        self.debug_session
            .conditional_breakpoints
            .push(ConditionalBreakpoint {
                rule_name: rule_name.to_string(),
                condition,
                hit_count,
                current_hits: 0,
                enabled: true,
            });
    }

    /// Add a watch expression
    pub fn add_watch(&mut self, name: &str, variable: &str, break_on_change: bool) {
        self.debug_session.watch_expressions.push(WatchExpression {
            name: name.to_string(),
            variable: variable.to_string(),
            last_value: None,
            break_on_change,
        });
    }

    /// Remove a watch expression
    pub fn remove_watch(&mut self, name: &str) {
        self.debug_session
            .watch_expressions
            .retain(|w| w.name != name);
    }

    /// Get current call stack
    pub fn get_call_stack(&self) -> &[StackFrame] {
        &self.debug_session.call_stack
    }

    /// Get current variable bindings
    pub fn get_substitutions(&self) -> &HashMap<String, String> {
        &self.debug_session.current_substitutions
    }

    /// Get value of a specific variable
    pub fn get_variable(&self, name: &str) -> Option<&String> {
        self.debug_session.current_substitutions.get(name)
    }

    /// Set a variable value (for debugging)
    pub fn set_variable(&mut self, name: &str, value: &str) {
        self.debug_session
            .current_substitutions
            .insert(name.to_string(), value.to_string());
    }

    /// Execute a debug command
    pub fn execute_command(&mut self, command: DebugCommand) -> String {
        match command {
            DebugCommand::Continue => {
                self.debug_session.state = DebuggerState::Running;
                self.debug_session.step_mode = false;
                "Continuing execution...".to_string()
            }
            DebugCommand::Step => {
                self.debug_session.state = DebuggerState::Stepping;
                self.debug_session.step_mode = true;
                "Stepping to next rule...".to_string()
            }
            DebugCommand::Next => {
                // Step over - execute current rule without entering
                self.debug_session.state = DebuggerState::Stepping;
                "Stepping over...".to_string()
            }
            DebugCommand::StepOut => {
                // Step out of current frame
                if !self.debug_session.call_stack.is_empty() {
                    self.debug_session.call_stack.pop();
                    "Stepping out of current frame...".to_string()
                } else {
                    "No frame to step out of".to_string()
                }
            }
            DebugCommand::Print => self.format_current_state(),
            DebugCommand::ListBreakpoints => self.format_breakpoints(),
            DebugCommand::Backtrace => self.format_backtrace(),
            DebugCommand::Quit => {
                self.debug_session.state = DebuggerState::Finished;
                "Quitting debugger...".to_string()
            }
        }
    }

    /// Check if execution should pause
    pub fn should_pause(&self, rule_name: &str) -> bool {
        // Check step mode
        if self.debug_session.step_mode {
            return true;
        }

        // Check simple breakpoints
        if self.debug_session.breakpoints.contains(rule_name) {
            return true;
        }

        // Check conditional breakpoints
        for bp in &self.debug_session.conditional_breakpoints {
            if bp.enabled && bp.rule_name == rule_name {
                // Check hit count
                if let Some(hit_count) = bp.hit_count {
                    if bp.current_hits >= hit_count {
                        continue;
                    }
                }

                // Check condition
                if let Some(ref condition) = bp.condition {
                    if !self.evaluate_condition(condition) {
                        continue;
                    }
                }

                return true;
            }
        }

        false
    }

    /// Evaluate a breakpoint condition
    fn evaluate_condition(&self, condition: &BreakpointCondition) -> bool {
        if let Some(value) = self
            .debug_session
            .current_substitutions
            .get(&condition.variable)
        {
            match condition.operator {
                ConditionOperator::Equals => value == &condition.value,
                ConditionOperator::NotEquals => value != &condition.value,
                ConditionOperator::Contains => value.contains(&condition.value),
            }
        } else {
            false
        }
    }

    /// Push a stack frame
    pub fn push_frame(&mut self, rule_name: &str, input_facts: Vec<RuleAtom>) {
        let depth = self.debug_session.call_stack.len();
        self.debug_session.call_stack.push(StackFrame {
            rule_name: rule_name.to_string(),
            substitutions: self.debug_session.current_substitutions.clone(),
            input_facts,
            depth,
        });
    }

    /// Pop a stack frame
    pub fn pop_frame(&mut self) -> Option<StackFrame> {
        self.debug_session.call_stack.pop()
    }

    /// Update substitutions
    pub fn update_substitutions(&mut self, substitutions: HashMap<String, String>) {
        // Check watch expressions for changes
        for watch in &mut self.debug_session.watch_expressions {
            if let Some(new_value) = substitutions.get(&watch.variable) {
                let changed = watch.last_value.as_ref() != Some(new_value);
                if changed && watch.break_on_change {
                    self.debug_session.paused_events.push(format!(
                        "Watch '{}' changed: {:?} -> {}",
                        watch.name, watch.last_value, new_value
                    ));
                    self.debug_session.state = DebuggerState::Paused;
                }
                watch.last_value = Some(new_value.clone());
            }
        }

        self.debug_session.current_substitutions = substitutions;
    }

    /// Format current debugger state
    fn format_current_state(&self) -> String {
        let mut output = String::new();

        output.push_str("=== Current State ===\n");
        output.push_str(&format!("Debugger: {:?}\n", self.debug_session.state));
        output.push_str(&format!("Step: {}\n", self.debug_session.current_step));

        output.push_str("\nVariables:\n");
        for (name, value) in &self.debug_session.current_substitutions {
            output.push_str(&format!("  {} = {}\n", name, value));
        }

        if !self.debug_session.watch_expressions.is_empty() {
            output.push_str("\nWatches:\n");
            for watch in &self.debug_session.watch_expressions {
                output.push_str(&format!(
                    "  {}: {} = {:?}\n",
                    watch.name, watch.variable, watch.last_value
                ));
            }
        }

        output
    }

    /// Format breakpoints list
    fn format_breakpoints(&self) -> String {
        let mut output = String::new();
        output.push_str("=== Breakpoints ===\n");

        // Simple breakpoints
        for bp in &self.debug_session.breakpoints {
            output.push_str(&format!("  {} (simple)\n", bp));
        }

        // Conditional breakpoints
        for (i, bp) in self
            .debug_session
            .conditional_breakpoints
            .iter()
            .enumerate()
        {
            let status = if bp.enabled { "enabled" } else { "disabled" };
            let condition = bp
                .condition
                .as_ref()
                .map(|c| format!(" when {} {:?} {}", c.variable, c.operator, c.value))
                .unwrap_or_default();
            let hit_info = bp
                .hit_count
                .map(|h| format!(" (hit {}/{})", bp.current_hits, h))
                .unwrap_or_default();

            output.push_str(&format!(
                "  [{}] {}{}{} ({})\n",
                i, bp.rule_name, condition, hit_info, status
            ));
        }

        output
    }

    /// Format call stack backtrace
    fn format_backtrace(&self) -> String {
        let mut output = String::new();
        output.push_str("=== Call Stack ===\n");

        for (i, frame) in self.debug_session.call_stack.iter().rev().enumerate() {
            output.push_str(&format!(
                "  #{} {} (depth: {})\n",
                i, frame.rule_name, frame.depth
            ));

            if !frame.substitutions.is_empty() {
                output.push_str("    Bindings:\n");
                for (var, val) in &frame.substitutions {
                    output.push_str(&format!("      {} = {}\n", var, val));
                }
            }
        }

        if self.debug_session.call_stack.is_empty() {
            output.push_str("  (empty)\n");
        }

        output
    }

    /// Get debugger state
    pub fn get_state(&self) -> DebuggerState {
        self.debug_session.state
    }

    /// Check if debugger is paused
    pub fn is_paused(&self) -> bool {
        self.debug_session.state == DebuggerState::Paused
    }

    /// Get paused events
    pub fn get_paused_events(&mut self) -> Vec<String> {
        std::mem::take(&mut self.debug_session.paused_events)
    }

    /// Increment hit counter for conditional breakpoint
    pub fn increment_hit_count(&mut self, rule_name: &str) {
        for bp in &mut self.debug_session.conditional_breakpoints {
            if bp.rule_name == rule_name {
                bp.current_hits += 1;
            }
        }
    }

    /// Reset hit counts for all breakpoints
    pub fn reset_hit_counts(&mut self) {
        for bp in &mut self.debug_session.conditional_breakpoints {
            bp.current_hits = 0;
        }
    }

    /// Enable/disable a conditional breakpoint
    pub fn set_breakpoint_enabled(&mut self, index: usize, enabled: bool) -> bool {
        if let Some(bp) = self.debug_session.conditional_breakpoints.get_mut(index) {
            bp.enabled = enabled;
            true
        } else {
            false
        }
    }
}

impl Default for PerformanceMetrics {
    fn default() -> Self {
        Self {
            total_execution_time: Duration::from_nanos(0),
            rule_execution_times: HashMap::new(),
            rule_execution_counts: HashMap::new(),
            facts_processed: 0,
            facts_derived: 0,
            memory_peak: 0,
            cache_hits: 0,
            cache_misses: 0,
        }
    }
}

impl Display for ConflictType {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            ConflictType::ContradictoryConclusions => write!(f, "Contradictory Conclusions"),
            ConflictType::CircularDependency => write!(f, "Circular Dependency"),
            ConflictType::RedundantRules => write!(f, "Redundant Rules"),
            ConflictType::UnreachableRules => write!(f, "Unreachable Rules"),
            ConflictType::PerformanceBottleneck => write!(f, "Performance Bottleneck"),
        }
    }
}

impl Display for ConflictSeverity {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            ConflictSeverity::Critical => write!(f, "CRITICAL"),
            ConflictSeverity::Warning => write!(f, "WARNING"),
            ConflictSeverity::Info => write!(f, "INFO"),
        }
    }
}

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

    #[test]
    fn test_debuggable_engine_creation() {
        let engine = DebuggableRuleEngine::new();
        assert!(!engine.debug_enabled);
        assert!(engine.debug_session.trace.is_empty());
    }

    #[test]
    fn test_debugging_enable_disable() {
        let mut engine = DebuggableRuleEngine::new();

        engine.enable_debugging(true);
        assert!(engine.debug_enabled);
        assert!(engine.debug_session.step_mode);

        engine.disable_debugging();
        assert!(!engine.debug_enabled);
    }

    #[test]
    fn test_breakpoint_management() {
        let mut engine = DebuggableRuleEngine::new();

        engine.add_breakpoint("test_rule");
        assert!(engine.debug_session.breakpoints.contains("test_rule"));

        engine.remove_breakpoint("test_rule");
        assert!(!engine.debug_session.breakpoints.contains("test_rule"));
    }

    #[test]
    fn test_trace_entry_recording() {
        let mut engine = DebuggableRuleEngine::new();
        engine.enable_debugging(false);

        let entry = TraceEntry {
            timestamp: engine.current_timestamp(),
            rule_name: "test_rule".to_string(),
            action: TraceAction::RuleExecution,
            input_facts: vec![],
            output_facts: vec![],
            duration: Duration::from_millis(10),
            memory_usage: 1024,
        };

        engine.record_trace_entry(entry);
        assert_eq!(engine.debug_session.trace.len(), 1);
        assert_eq!(
            engine
                .debug_session
                .metrics
                .rule_execution_counts
                .get("test_rule"),
            Some(&1)
        );
    }

    #[test]
    fn test_performance_bottleneck_detection() {
        let mut engine = DebuggableRuleEngine::new();
        engine.enable_debugging(false);

        // Add a slow rule execution
        engine
            .debug_session
            .metrics
            .rule_execution_times
            .insert("slow_rule".to_string(), Duration::from_millis(200));

        engine.analyze_conflicts();

        // Should detect performance bottleneck
        assert!(!engine.debug_session.conflicts.is_empty());
        assert!(engine
            .debug_session
            .conflicts
            .iter()
            .any(|c| { matches!(c.conflict_type, ConflictType::PerformanceBottleneck) }));
    }

    #[test]
    fn test_debug_report_generation() {
        let mut engine = DebuggableRuleEngine::new();
        engine.enable_debugging(false);

        // Add some test data
        engine.debug_session.metrics.facts_processed = 100;
        engine.debug_session.metrics.facts_derived = 50;
        engine.debug_session.metrics.total_execution_time = Duration::from_millis(500);

        let report = engine.generate_debug_report();
        assert!(report.contains("RULE ENGINE DEBUG REPORT"));
        assert!(report.contains("Facts processed: 100"));
        assert!(report.contains("Facts derived: 50"));
    }

    #[test]
    fn test_debug_data_export() -> Result<(), Box<dyn std::error::Error>> {
        let engine = DebuggableRuleEngine::new();
        let json_result = engine.export_debug_data();
        assert!(json_result.is_ok());

        let json_data = json_result?;
        assert!(json_data.contains("trace"));
        assert!(json_data.contains("metrics"));
        assert!(json_data.contains("conflicts"));
        Ok(())
    }

    #[test]
    fn test_conditional_breakpoint() {
        let mut engine = DebuggableRuleEngine::new();
        engine.enable_debugging(false);

        // Add conditional breakpoint
        let condition = BreakpointCondition {
            variable: "X".to_string(),
            value: "test".to_string(),
            operator: ConditionOperator::Equals,
        };
        engine.add_conditional_breakpoint("test_rule", Some(condition), None);

        // Set variable to meet condition
        engine.set_variable("X", "test");

        // Should pause
        assert!(engine.should_pause("test_rule"));

        // Different value should not pause
        engine.set_variable("X", "other");
        assert!(!engine.should_pause("test_rule"));
    }

    #[test]
    fn test_hit_count_breakpoint() {
        let mut engine = DebuggableRuleEngine::new();
        engine.enable_debugging(false);

        // Add breakpoint with hit count
        engine.add_conditional_breakpoint("count_rule", None, Some(3));

        // Should pause first 3 times
        assert!(engine.should_pause("count_rule"));
        engine.increment_hit_count("count_rule");
        assert!(engine.should_pause("count_rule"));
        engine.increment_hit_count("count_rule");
        assert!(engine.should_pause("count_rule"));
        engine.increment_hit_count("count_rule");

        // After 3 hits, should not pause
        assert!(!engine.should_pause("count_rule"));
    }

    #[test]
    fn test_watch_expressions() {
        let mut engine = DebuggableRuleEngine::new();
        engine.enable_debugging(false);

        engine.add_watch("x_watch", "X", true);

        // Initial update
        let mut subs = HashMap::new();
        subs.insert("X".to_string(), "value1".to_string());
        engine.update_substitutions(subs);

        // Check watch has value
        assert!(engine.debug_session.watch_expressions[0]
            .last_value
            .is_some());

        // Update with new value - should trigger pause
        let mut subs2 = HashMap::new();
        subs2.insert("X".to_string(), "value2".to_string());
        engine.update_substitutions(subs2);

        assert!(engine.is_paused());
        let events = engine.get_paused_events();
        assert!(!events.is_empty());
    }

    #[test]
    fn test_call_stack() -> Result<(), Box<dyn std::error::Error>> {
        let mut engine = DebuggableRuleEngine::new();
        engine.enable_debugging(false);

        // Push frames
        engine.push_frame("rule1", vec![]);
        engine.push_frame("rule2", vec![]);
        engine.push_frame("rule3", vec![]);

        // Check stack depth
        assert_eq!(engine.get_call_stack().len(), 3);

        // Pop frame
        let frame = engine.pop_frame();
        assert!(frame.is_some());
        assert_eq!(frame.ok_or("expected Some value")?.rule_name, "rule3");

        // Check stack after pop
        assert_eq!(engine.get_call_stack().len(), 2);
        Ok(())
    }

    #[test]
    fn test_debug_commands() {
        let mut engine = DebuggableRuleEngine::new();
        engine.enable_debugging(true);

        // Test step command
        let result = engine.execute_command(DebugCommand::Step);
        assert!(result.contains("Stepping"));
        assert!(engine.debug_session.step_mode);

        // Test continue command
        let result = engine.execute_command(DebugCommand::Continue);
        assert!(result.contains("Continuing"));
        assert!(!engine.debug_session.step_mode);

        // Test quit command
        let result = engine.execute_command(DebugCommand::Quit);
        assert!(result.contains("Quitting"));
        assert_eq!(engine.get_state(), DebuggerState::Finished);
    }

    #[test]
    fn test_format_backtrace() {
        let mut engine = DebuggableRuleEngine::new();
        engine.enable_debugging(false);

        // Add some frames with substitutions
        engine.set_variable("X", "value1");
        engine.push_frame("rule1", vec![]);
        engine.set_variable("Y", "value2");
        engine.push_frame("rule2", vec![]);

        let backtrace = engine.execute_command(DebugCommand::Backtrace);
        assert!(backtrace.contains("Call Stack"));
        assert!(backtrace.contains("rule1"));
        assert!(backtrace.contains("rule2"));
    }

    #[test]
    fn test_breakpoint_enable_disable() {
        let mut engine = DebuggableRuleEngine::new();
        engine.enable_debugging(false);

        engine.add_conditional_breakpoint("test_rule", None, None);

        // Initially enabled
        assert!(engine.should_pause("test_rule"));

        // Disable
        engine.set_breakpoint_enabled(0, false);
        assert!(!engine.should_pause("test_rule"));

        // Re-enable
        engine.set_breakpoint_enabled(0, true);
        assert!(engine.should_pause("test_rule"));
    }

    #[test]
    fn test_condition_operators() {
        let mut engine = DebuggableRuleEngine::new();
        engine.enable_debugging(false);

        // Contains operator
        let condition = BreakpointCondition {
            variable: "X".to_string(),
            value: "sub".to_string(),
            operator: ConditionOperator::Contains,
        };
        engine.add_conditional_breakpoint("rule1", Some(condition), None);

        engine.set_variable("X", "substring");
        assert!(engine.should_pause("rule1"));

        // Not equals operator
        engine.debug_session.conditional_breakpoints.clear();
        let condition2 = BreakpointCondition {
            variable: "Y".to_string(),
            value: "excluded".to_string(),
            operator: ConditionOperator::NotEquals,
        };
        engine.add_conditional_breakpoint("rule2", Some(condition2), None);

        engine.set_variable("Y", "included");
        assert!(engine.should_pause("rule2"));

        engine.set_variable("Y", "excluded");
        assert!(!engine.should_pause("rule2"));
    }

    #[test]
    fn test_step_out_command() {
        let mut engine = DebuggableRuleEngine::new();
        engine.enable_debugging(false);

        // Push some frames
        engine.push_frame("rule1", vec![]);
        engine.push_frame("rule2", vec![]);

        // Step out should pop frame
        let result = engine.execute_command(DebugCommand::StepOut);
        assert!(result.contains("Stepping out"));
        assert_eq!(engine.get_call_stack().len(), 1);

        // Step out again
        engine.execute_command(DebugCommand::StepOut);
        assert_eq!(engine.get_call_stack().len(), 0);

        // Step out with empty stack
        let result = engine.execute_command(DebugCommand::StepOut);
        assert!(result.contains("No frame"));
    }
}