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
//! Enhanced RETE Pattern Matching Network
//!
//! This module provides a full-featured RETE implementation with:
//! - Complete beta join support with complex variable bindings
//! - Advanced memory management strategies
//! - Conflict resolution mechanisms
//! - Truth maintenance and fact retraction
//! - Builtin predicate evaluation

use crate::forward::Substitution;
use crate::{RuleAtom, Term};
use anyhow::{anyhow, Result};
use std::collections::{HashMap, HashSet, VecDeque};
use std::time::{Duration, Instant};
use tracing::warn;

/// Enhanced token with timestamp and priority
#[derive(Debug, Clone)]
pub struct EnhancedToken {
    /// Current variable bindings
    pub bindings: Substitution,
    /// Facts that contributed to this token
    pub facts: Vec<RuleAtom>,
    /// Creation timestamp
    pub timestamp: Instant,
    /// Priority for conflict resolution
    pub priority: i32,
    /// Rule specificity (number of conditions)
    pub specificity: usize,
    /// Justification (which rules/facts led to this token)
    pub justification: Vec<String>,
}

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

impl EnhancedToken {
    pub fn new() -> Self {
        Self {
            bindings: HashMap::new(),
            facts: Vec::new(),
            timestamp: Instant::now(),
            priority: 0,
            specificity: 0,
            justification: Vec::new(),
        }
    }

    pub fn with_fact(fact: RuleAtom) -> Self {
        let mut token = Self::new();
        token.facts.push(fact);
        token
    }

    /// Merge two tokens with proper conflict resolution
    pub fn merge(left: &Self, right: &Self) -> Result<Self> {
        let mut merged = Self::new();

        // Merge bindings with conflict detection
        merged.bindings.extend(left.bindings.clone());
        for (var, value) in &right.bindings {
            if let Some(existing) = merged.bindings.get(var) {
                if !terms_compatible(existing, value) {
                    return Err(anyhow!(
                        "Binding conflict for variable {}: {:?} vs {:?}",
                        var,
                        existing,
                        value
                    ));
                }
            } else {
                merged.bindings.insert(var.clone(), value.clone());
            }
        }

        // Combine facts
        merged.facts.extend(left.facts.clone());
        merged.facts.extend(right.facts.clone());

        // Use earliest timestamp
        merged.timestamp = left.timestamp.min(right.timestamp);

        // Combine priorities and specificity
        merged.priority = left.priority.max(right.priority);
        merged.specificity = left.specificity + right.specificity;

        // Merge justifications
        merged.justification.extend(left.justification.clone());
        merged.justification.extend(right.justification.clone());

        Ok(merged)
    }
}

/// Memory management strategies
#[derive(Debug, Clone, Copy)]
pub enum MemoryStrategy {
    /// Keep all tokens
    Unlimited,
    /// Limit by count
    LimitCount(usize),
    /// Limit by age
    LimitAge(Duration),
    /// Least Recently Used
    LRU(usize),
    /// Adaptive based on memory pressure
    Adaptive,
}

/// Conflict resolution strategies
#[derive(Debug, Clone, Copy)]
pub enum ConflictResolution {
    /// First match wins
    First,
    /// Most recent wins
    Recency,
    /// Most specific rule wins
    Specificity,
    /// Highest priority wins
    Priority,
    /// Combined scoring
    Combined,
}

/// Enhanced beta memory with advanced features
#[derive(Debug)]
pub struct BetaMemory {
    /// Left side tokens
    left_tokens: VecDeque<EnhancedToken>,
    /// Right side tokens
    right_tokens: VecDeque<EnhancedToken>,
    /// Index for fast lookup by variable values
    left_index: HashMap<String, HashMap<Term, Vec<usize>>>,
    right_index: HashMap<String, HashMap<Term, Vec<usize>>>,
    /// Memory strategy
    memory_strategy: MemoryStrategy,
    /// Access timestamps for LRU
    access_times: HashMap<usize, Instant>,
    /// Memory statistics
    stats: MemoryStats,
}

#[derive(Debug, Default)]
pub struct MemoryStats {
    pub total_joins: usize,
    pub successful_joins: usize,
    pub evictions: usize,
    pub peak_size: usize,
}

impl BetaMemory {
    pub fn new(strategy: MemoryStrategy) -> Self {
        Self {
            left_tokens: VecDeque::new(),
            right_tokens: VecDeque::new(),
            left_index: HashMap::new(),
            right_index: HashMap::new(),
            memory_strategy: strategy,
            access_times: HashMap::new(),
            stats: MemoryStats::default(),
        }
    }

    /// Add a token to the left side
    pub fn add_left(&mut self, token: EnhancedToken, join_vars: &[String]) -> usize {
        let idx = self.left_tokens.len();
        self.left_tokens.push_back(token.clone());

        // Update indices for join variables
        for var in join_vars {
            if let Some(value) = token.bindings.get(var) {
                self.left_index
                    .entry(var.clone())
                    .or_default()
                    .entry(value.clone())
                    .or_default()
                    .push(idx);
            }
        }

        self.access_times.insert(idx, Instant::now());
        self.apply_memory_management();
        self.stats.peak_size = self.stats.peak_size.max(self.left_tokens.len());

        idx
    }

    /// Add a token to the right side
    pub fn add_right(&mut self, token: EnhancedToken, join_vars: &[String]) -> usize {
        let idx = self.right_tokens.len();
        self.right_tokens.push_back(token.clone());

        // Update indices for join variables
        for var in join_vars {
            if let Some(value) = token.bindings.get(var) {
                self.right_index
                    .entry(var.clone())
                    .or_default()
                    .entry(value.clone())
                    .or_default()
                    .push(idx);
            }
        }

        self.access_times.insert(idx + 1000000, Instant::now()); // Offset for right side
        self.apply_memory_management();
        self.stats.peak_size = self.stats.peak_size.max(self.right_tokens.len());

        idx
    }

    /// Find matching tokens using indices
    pub fn find_matches_indexed(
        &mut self,
        token: &EnhancedToken,
        is_left: bool,
        join_vars: &[String],
    ) -> Vec<EnhancedToken> {
        self.stats.total_joins += 1;
        let mut matches = Vec::new();

        if join_vars.is_empty() {
            // Cartesian product if no join variables
            let tokens = if is_left {
                &self.right_tokens
            } else {
                &self.left_tokens
            };
            matches.extend(tokens.iter().cloned());
        } else {
            // Use indices for efficient lookup
            let indices = if is_left {
                &self.right_index
            } else {
                &self.left_index
            };
            let mut candidate_indices = HashSet::new();

            for var in join_vars {
                if let Some(value) = token.bindings.get(var) {
                    if let Some(var_index) = indices.get(var) {
                        if let Some(token_indices) = var_index.get(value) {
                            if candidate_indices.is_empty() {
                                candidate_indices.extend(token_indices);
                            } else {
                                // Intersection with existing candidates
                                candidate_indices.retain(|idx| token_indices.contains(idx));
                            }
                        }
                    }
                }
            }

            // Retrieve matching tokens
            let tokens = if is_left {
                &self.right_tokens
            } else {
                &self.left_tokens
            };
            for &idx in &candidate_indices {
                if let Some(match_token) = tokens.get(idx) {
                    matches.push(match_token.clone());

                    // Update access time for LRU
                    let access_key = if is_left { idx + 1000000 } else { idx };
                    self.access_times.insert(access_key, Instant::now());
                }
            }
        }

        if !matches.is_empty() {
            self.stats.successful_joins += 1;
        }

        matches
    }

    /// Get the memory strategy
    pub fn memory_strategy(&self) -> &MemoryStrategy {
        &self.memory_strategy
    }

    /// Set the memory strategy
    pub fn set_memory_strategy(&mut self, strategy: MemoryStrategy) {
        self.memory_strategy = strategy;
    }

    /// Apply memory management strategy
    fn apply_memory_management(&mut self) {
        match self.memory_strategy {
            MemoryStrategy::LimitCount(max_count) => {
                while self.left_tokens.len() + self.right_tokens.len() > max_count {
                    self.evict_oldest();
                }
            }
            MemoryStrategy::LimitAge(max_age) => {
                let now = Instant::now();
                self.left_tokens
                    .retain(|token| now.duration_since(token.timestamp) < max_age);
                self.right_tokens
                    .retain(|token| now.duration_since(token.timestamp) < max_age);
                self.rebuild_indices();
            }
            MemoryStrategy::LRU(max_count) => {
                while self.left_tokens.len() + self.right_tokens.len() > max_count {
                    self.evict_lru();
                }
            }
            MemoryStrategy::Adaptive => {
                // Simple adaptive strategy based on join success rate
                let success_rate = if self.stats.total_joins > 0 {
                    self.stats.successful_joins as f64 / self.stats.total_joins as f64
                } else {
                    1.0
                };

                // If success rate is low, be more aggressive with eviction
                if success_rate < 0.1 && self.left_tokens.len() + self.right_tokens.len() > 1000 {
                    self.evict_oldest();
                }
            }
            MemoryStrategy::Unlimited => {}
        }
    }

    /// Evict oldest token
    fn evict_oldest(&mut self) {
        if self.left_tokens.len() > self.right_tokens.len() {
            self.left_tokens.pop_front();
        } else if !self.right_tokens.is_empty() {
            self.right_tokens.pop_front();
        }
        self.stats.evictions += 1;
        self.rebuild_indices();
    }

    /// Evict least recently used token
    fn evict_lru(&mut self) {
        if let Some((&oldest_key, _)) = self.access_times.iter().min_by_key(|&(_, &time)| time) {
            if oldest_key < 1000000 {
                // Left side
                if oldest_key < self.left_tokens.len() {
                    self.left_tokens.remove(oldest_key);
                }
            } else {
                // Right side
                let idx = oldest_key - 1000000;
                if idx < self.right_tokens.len() {
                    self.right_tokens.remove(idx);
                }
            }

            self.stats.evictions += 1;
            self.rebuild_indices();
        }
    }

    /// Rebuild indices after eviction
    fn rebuild_indices(&mut self) {
        self.left_index.clear();
        self.right_index.clear();
        self.access_times.clear();

        // Rebuild left index
        for (idx, token) in self.left_tokens.iter().enumerate() {
            for (var, value) in &token.bindings {
                self.left_index
                    .entry(var.clone())
                    .or_default()
                    .entry(value.clone())
                    .or_default()
                    .push(idx);
            }
            self.access_times.insert(idx, Instant::now());
        }

        // Rebuild right index
        for (idx, token) in self.right_tokens.iter().enumerate() {
            for (var, value) in &token.bindings {
                self.right_index
                    .entry(var.clone())
                    .or_default()
                    .entry(value.clone())
                    .or_default()
                    .push(idx);
            }
            self.access_times.insert(idx + 1000000, Instant::now());
        }
    }
}

/// Enhanced beta join node
#[derive(Debug)]
pub struct BetaJoinNode {
    /// Node ID
    pub id: usize,
    /// Left parent node ID
    pub left_parent: usize,
    /// Right parent node ID
    pub right_parent: usize,
    /// Variables to join on
    pub join_variables: Vec<String>,
    /// Additional join conditions
    pub conditions: Vec<JoinCondition>,
    /// Beta memory
    pub memory: BetaMemory,
    /// Children nodes
    pub children: Vec<usize>,
    /// Conflict resolution strategy
    pub conflict_resolution: ConflictResolution,
}

/// Join condition types
#[derive(Debug, Clone)]
pub enum JoinCondition {
    /// Variable equality (already handled by join_variables)
    VarEquality { left_var: String, right_var: String },
    /// Comparison between variables
    VarComparison {
        left_var: String,
        right_var: String,
        op: ComparisonOp,
    },
    /// Comparison between a variable and a constant value
    VarConstComparison {
        var: String,
        constant: Term,
        op: ComparisonOp,
    },
    /// Builtin predicate
    Builtin {
        predicate: String,
        args: Vec<JoinArg>,
    },
    /// Negation
    Not(Box<JoinCondition>),
    /// Conjunction
    And(Vec<JoinCondition>),
    /// Disjunction
    Or(Vec<JoinCondition>),
}

#[derive(Debug, Clone)]
pub enum JoinArg {
    LeftVar(String),
    RightVar(String),
    Constant(Term),
}

#[derive(Debug, Clone, Copy)]
pub enum ComparisonOp {
    Equal,
    NotEqual,
    Less,
    LessEqual,
    Greater,
    GreaterEqual,
}

impl BetaJoinNode {
    pub fn new(
        id: usize,
        left_parent: usize,
        right_parent: usize,
        memory_strategy: MemoryStrategy,
        conflict_resolution: ConflictResolution,
    ) -> Self {
        Self {
            id,
            left_parent,
            right_parent,
            join_variables: Vec::new(),
            conditions: Vec::new(),
            memory: BetaMemory::new(memory_strategy),
            children: Vec::new(),
            conflict_resolution,
        }
    }

    /// Check if this is a filter-only beta node (same left and right parent)
    pub fn is_filter_only(&self) -> bool {
        self.left_parent == self.right_parent
    }

    /// Perform the join operation
    pub fn join(&mut self, token: EnhancedToken, from_left: bool) -> Result<Vec<EnhancedToken>> {
        // Special handling for filter-only nodes
        if self.is_filter_only() {
            return self.apply_filter_only(token);
        }

        let mut results = Vec::new();

        if from_left {
            // Add to left memory
            self.memory.add_left(token.clone(), &self.join_variables);

            // Find matches in right memory
            let matches = self
                .memory
                .find_matches_indexed(&token, true, &self.join_variables);

            for right_token in matches {
                if let Ok(joined) = self.try_join(&token, &right_token) {
                    results.push(joined);
                }
            }
        } else {
            // Add to right memory
            self.memory.add_right(token.clone(), &self.join_variables);

            // Find matches in left memory
            let matches = self
                .memory
                .find_matches_indexed(&token, false, &self.join_variables);

            for left_token in matches {
                if let Ok(joined) = self.try_join(&left_token, &token) {
                    results.push(joined);
                }
            }
        }

        // Apply conflict resolution if multiple results
        if results.len() > 1 {
            results = self.apply_conflict_resolution(results);
        }

        Ok(results)
    }

    /// Apply filter conditions only (for filter-only beta nodes)
    /// This is used when the beta node has the same left and right parent
    fn apply_filter_only(&self, token: EnhancedToken) -> Result<Vec<EnhancedToken>> {
        // Check all filter conditions against the token
        // For filter-only nodes, we pass the same token as both left and right
        for condition in &self.conditions {
            if !self.evaluate_condition(condition, &token, &token)? {
                // Filter condition failed, return empty
                return Ok(Vec::new());
            }
        }

        // All conditions passed, return the token
        Ok(vec![token])
    }

    /// Try to join two tokens
    fn try_join(&self, left: &EnhancedToken, right: &EnhancedToken) -> Result<EnhancedToken> {
        // First check join variables match
        for var in &self.join_variables {
            if let (Some(left_val), Some(right_val)) =
                (left.bindings.get(var), right.bindings.get(var))
            {
                if !terms_compatible(left_val, right_val) {
                    return Err(anyhow!("Join variable {} doesn't match", var));
                }
            }
        }

        // Then check additional conditions
        for condition in &self.conditions {
            if !self.evaluate_condition(condition, left, right)? {
                return Err(anyhow!("Join condition failed"));
            }
        }

        // Merge tokens
        EnhancedToken::merge(left, right)
    }

    /// Evaluate a join condition
    #[allow(clippy::only_used_in_recursion)]
    fn evaluate_condition(
        &self,
        condition: &JoinCondition,
        left: &EnhancedToken,
        right: &EnhancedToken,
    ) -> Result<bool> {
        match condition {
            JoinCondition::VarEquality {
                left_var,
                right_var,
            } => {
                let left_val = left.bindings.get(left_var);
                let right_val = right.bindings.get(right_var);
                Ok(match (left_val, right_val) {
                    (Some(lv), Some(rv)) => terms_compatible(lv, rv),
                    _ => false,
                })
            }
            JoinCondition::VarComparison {
                left_var,
                right_var,
                op,
            } => {
                let left_val = left.bindings.get(left_var);
                let right_val = right.bindings.get(right_var);
                Ok(match (left_val, right_val) {
                    (Some(lv), Some(rv)) => evaluate_comparison(lv, rv, *op)?,
                    _ => false,
                })
            }
            JoinCondition::VarConstComparison { var, constant, op } => {
                // Look up the variable in both left and right token bindings
                let var_val = left.bindings.get(var).or_else(|| right.bindings.get(var));
                Ok(match var_val {
                    Some(val) => evaluate_comparison(val, constant, *op)?,
                    None => false,
                })
            }
            JoinCondition::Builtin { predicate, args } => {
                evaluate_builtin(predicate, args, left, right)
            }
            JoinCondition::Not(cond) => Ok(!self.evaluate_condition(cond, left, right)?),
            JoinCondition::And(conds) => {
                for cond in conds {
                    if !self.evaluate_condition(cond, left, right)? {
                        return Ok(false);
                    }
                }
                Ok(true)
            }
            JoinCondition::Or(conds) => {
                for cond in conds {
                    if self.evaluate_condition(cond, left, right)? {
                        return Ok(true);
                    }
                }
                Ok(false)
            }
        }
    }

    /// Apply conflict resolution strategy
    fn apply_conflict_resolution(&self, mut tokens: Vec<EnhancedToken>) -> Vec<EnhancedToken> {
        match self.conflict_resolution {
            ConflictResolution::First => {
                tokens.truncate(1);
                tokens
            }
            ConflictResolution::Recency => {
                tokens.sort_by_key(|t| std::cmp::Reverse(t.timestamp));
                tokens.truncate(1);
                tokens
            }
            ConflictResolution::Specificity => {
                tokens.sort_by_key(|t| std::cmp::Reverse(t.specificity));
                tokens.truncate(1);
                tokens
            }
            ConflictResolution::Priority => {
                tokens.sort_by_key(|t| std::cmp::Reverse(t.priority));
                tokens.truncate(1);
                tokens
            }
            ConflictResolution::Combined => {
                // Combined scoring: priority * 1000 + specificity * 10 + recency
                tokens.sort_by_key(|t| {
                    let recency_score = t.timestamp.elapsed().as_secs() as i32;
                    std::cmp::Reverse(t.priority * 1000 + t.specificity as i32 * 10 - recency_score)
                });
                tokens.truncate(1);
                tokens
            }
        }
    }

    /// Get memory statistics
    pub fn get_stats(&self) -> &MemoryStats {
        &self.memory.stats
    }
}

/// Check if two terms are compatible (can unify)
fn terms_compatible(t1: &Term, t2: &Term) -> bool {
    match (t1, t2) {
        (Term::Variable(_), _) | (_, Term::Variable(_)) => true,
        (Term::Constant(c1), Term::Constant(c2)) => c1 == c2,
        (Term::Literal(l1), Term::Literal(l2)) => l1 == l2,
        _ => false,
    }
}

/// Evaluate a comparison between terms
fn evaluate_comparison(left: &Term, right: &Term, op: ComparisonOp) -> Result<bool> {
    // Try to parse as numbers for numeric comparison
    let left_num = parse_numeric(left);
    let right_num = parse_numeric(right);

    match (left_num, right_num) {
        (Some(l), Some(r)) => Ok(match op {
            ComparisonOp::Equal => (l - r).abs() < f64::EPSILON,
            ComparisonOp::NotEqual => (l - r).abs() >= f64::EPSILON,
            ComparisonOp::Less => l < r,
            ComparisonOp::LessEqual => l <= r,
            ComparisonOp::Greater => l > r,
            ComparisonOp::GreaterEqual => l >= r,
        }),
        _ => {
            // String comparison
            let left_str = term_to_string(left);
            let right_str = term_to_string(right);
            Ok(match op {
                ComparisonOp::Equal => left_str == right_str,
                ComparisonOp::NotEqual => left_str != right_str,
                ComparisonOp::Less => left_str < right_str,
                ComparisonOp::LessEqual => left_str <= right_str,
                ComparisonOp::Greater => left_str > right_str,
                ComparisonOp::GreaterEqual => left_str >= right_str,
            })
        }
    }
}

/// Parse a term as a numeric value
fn parse_numeric(term: &Term) -> Option<f64> {
    match term {
        Term::Literal(s) | Term::Constant(s) => s.parse::<f64>().ok(),
        _ => None,
    }
}

/// Convert term to string for comparison
fn term_to_string(term: &Term) -> String {
    match term {
        Term::Variable(v) => format!("?{v}"),
        Term::Constant(c) => c.clone(),
        Term::Literal(l) => l.clone(),
        Term::Function { name, args } => {
            let arg_strings: Vec<String> = args.iter().map(term_to_string).collect();
            format!("{}({})", name, arg_strings.join(","))
        }
    }
}

/// Evaluate builtin predicates
fn evaluate_builtin(
    predicate: &str,
    args: &[JoinArg],
    left: &EnhancedToken,
    right: &EnhancedToken,
) -> Result<bool> {
    // Get argument values
    let arg_values: Vec<Option<Term>> = args
        .iter()
        .map(|arg| match arg {
            JoinArg::LeftVar(var) => left.bindings.get(var).cloned(),
            JoinArg::RightVar(var) => right.bindings.get(var).cloned(),
            JoinArg::Constant(term) => Some(term.clone()),
        })
        .collect();

    // Check all arguments are bound
    if arg_values.iter().any(|v| v.is_none()) {
        return Ok(false);
    }

    let values: Vec<Term> = arg_values
        .into_iter()
        .map(|v| v.expect("argument values verified to be Some"))
        .collect();

    // Evaluate builtin
    match predicate {
        "regex" => {
            if values.len() >= 2 {
                if let (Term::Literal(text), Term::Literal(pattern)) = (&values[0], &values[1]) {
                    let re =
                        regex::Regex::new(pattern).map_err(|e| anyhow!("Invalid regex: {}", e))?;
                    Ok(re.is_match(text))
                } else {
                    Ok(false)
                }
            } else {
                Ok(false)
            }
        }
        "contains" => {
            if values.len() >= 2 {
                let s1 = term_to_string(&values[0]);
                let s2 = term_to_string(&values[1]);
                Ok(s1.contains(&s2))
            } else {
                Ok(false)
            }
        }
        "starts_with" => {
            if values.len() >= 2 {
                let s1 = term_to_string(&values[0]);
                let s2 = term_to_string(&values[1]);
                Ok(s1.starts_with(&s2))
            } else {
                Ok(false)
            }
        }
        "numeric_add" => {
            if values.len() >= 3 {
                if let (Some(n1), Some(n2), Some(result)) = (
                    parse_numeric(&values[0]),
                    parse_numeric(&values[1]),
                    parse_numeric(&values[2]),
                ) {
                    Ok((n1 + n2 - result).abs() < f64::EPSILON)
                } else {
                    Ok(false)
                }
            } else {
                Ok(false)
            }
        }
        _ => {
            warn!("Unknown builtin predicate: {}", predicate);
            Ok(false)
        }
    }
}

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

    #[test]
    fn test_enhanced_token_merge() -> Result<(), Box<dyn std::error::Error>> {
        let mut token1 = EnhancedToken::new();
        token1
            .bindings
            .insert("X".to_string(), Term::Constant("a".to_string()));
        token1
            .bindings
            .insert("Y".to_string(), Term::Constant("b".to_string()));

        let mut token2 = EnhancedToken::new();
        token2
            .bindings
            .insert("Y".to_string(), Term::Constant("b".to_string()));
        token2
            .bindings
            .insert("Z".to_string(), Term::Constant("c".to_string()));

        let merged = EnhancedToken::merge(&token1, &token2)?;
        assert_eq!(merged.bindings.len(), 3);
        assert_eq!(
            merged.bindings.get("X"),
            Some(&Term::Constant("a".to_string()))
        );
        assert_eq!(
            merged.bindings.get("Y"),
            Some(&Term::Constant("b".to_string()))
        );
        assert_eq!(
            merged.bindings.get("Z"),
            Some(&Term::Constant("c".to_string()))
        );
        Ok(())
    }

    #[test]
    fn test_beta_memory_indexed_lookup() {
        let mut memory = BetaMemory::new(MemoryStrategy::Unlimited);

        // Add some tokens
        let mut token1 = EnhancedToken::new();
        token1
            .bindings
            .insert("X".to_string(), Term::Constant("a".to_string()));
        memory.add_left(token1, &["X".to_string()]);

        let mut token2 = EnhancedToken::new();
        token2
            .bindings
            .insert("X".to_string(), Term::Constant("b".to_string()));
        memory.add_left(token2, &["X".to_string()]);

        // Search for matching tokens
        let mut search_token = EnhancedToken::new();
        search_token
            .bindings
            .insert("X".to_string(), Term::Constant("a".to_string()));

        let matches = memory.find_matches_indexed(&search_token, false, &["X".to_string()]);
        assert_eq!(matches.len(), 1);
        assert_eq!(
            matches[0].bindings.get("X"),
            Some(&Term::Constant("a".to_string()))
        );
    }

    #[test]
    fn test_memory_eviction_strategies() {
        // Test count limit
        let mut memory = BetaMemory::new(MemoryStrategy::LimitCount(2));

        for i in 0..5 {
            let mut token = EnhancedToken::new();
            token
                .bindings
                .insert("X".to_string(), Term::Constant(i.to_string()));
            memory.add_left(token, &["X".to_string()]);
        }

        assert!(memory.left_tokens.len() <= 2);
        assert!(memory.stats.evictions > 0);
    }

    #[test]
    fn test_join_conditions() -> Result<(), Box<dyn std::error::Error>> {
        let node = BetaJoinNode::new(
            1,
            0,
            0,
            MemoryStrategy::Unlimited,
            ConflictResolution::First,
        );

        let mut left = EnhancedToken::new();
        left.bindings
            .insert("X".to_string(), Term::Literal("5".to_string()));

        let mut right = EnhancedToken::new();
        right
            .bindings
            .insert("Y".to_string(), Term::Literal("10".to_string()));

        // Test numeric comparison
        let cond = JoinCondition::VarComparison {
            left_var: "X".to_string(),
            right_var: "Y".to_string(),
            op: ComparisonOp::Less,
        };

        assert!(node.evaluate_condition(&cond, &left, &right)?);
        Ok(())
    }

    #[test]
    fn test_builtin_evaluation() -> Result<(), Box<dyn std::error::Error>> {
        let mut left = EnhancedToken::new();
        left.bindings
            .insert("text".to_string(), Term::Literal("hello world".to_string()));

        let right = EnhancedToken::new();

        // Test regex builtin
        let args = vec![
            JoinArg::LeftVar("text".to_string()),
            JoinArg::Constant(Term::Literal("hello.*".to_string())),
        ];

        assert!(evaluate_builtin("regex", &args, &left, &right)?);

        // Test contains builtin
        let args = vec![
            JoinArg::LeftVar("text".to_string()),
            JoinArg::Constant(Term::Literal("world".to_string())),
        ];

        assert!(evaluate_builtin("contains", &args, &left, &right)?);
        Ok(())
    }

    #[test]
    fn test_memory_strategy_getter_setter() {
        let mut memory = BetaMemory::new(MemoryStrategy::Unlimited);

        // Test getter
        assert!(matches!(
            memory.memory_strategy(),
            &MemoryStrategy::Unlimited
        ));

        // Test setter
        memory.set_memory_strategy(MemoryStrategy::LimitCount(100));
        assert!(matches!(
            memory.memory_strategy(),
            &MemoryStrategy::LimitCount(100)
        ));

        // Test with other strategies
        memory.set_memory_strategy(MemoryStrategy::LRU(50));
        assert!(matches!(memory.memory_strategy(), &MemoryStrategy::LRU(50)));
    }
}