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
//! Lock-Free Concurrent Inference Engine
//!
//! Provides high-performance concurrent rule-based inference using lock-free data structures
//! and atomic operations. This engine eliminates mutex contention and enables true parallel
//! inference with minimal overhead.
//!
//! # Features
//!
//! - **Lock-Free Data Structures**: Uses atomic operations and CAS (Compare-And-Swap) for concurrent access
//! - **Zero Mutex Overhead**: No blocking operations during inference
//! - **Scalable Parallelism**: Linear scaling with CPU cores
//! - **Memory Safety**: Rust's ownership system ensures thread safety without locks
//! - **Work Stealing**: Efficient load balancing across worker threads
//!
//! # Architecture
//!
//! The lock-free engine uses three key data structures:
//! - **Fact Set**: Concurrent hash set using atomic updates
//! - **Work Queue**: Lock-free MPMC queue for task distribution
//! - **Result Collector**: Atomic aggregation of derived facts
//!
//! # Example
//!
//! ```rust
//! use oxirs_rule::lockfree::LockFreeEngine;
//! use oxirs_rule::{Rule, RuleAtom, Term};
//!
//! let mut engine = LockFreeEngine::new();
//!
//! engine.add_rule(Rule {
//!     name: "test".to_string(),
//!     body: vec![RuleAtom::Triple {
//!         subject: Term::Variable("X".to_string()),
//!         predicate: Term::Constant("p".to_string()),
//!         object: Term::Variable("Y".to_string()),
//!     }],
//!     head: vec![RuleAtom::Triple {
//!         subject: Term::Variable("X".to_string()),
//!         predicate: Term::Constant("q".to_string()),
//!         object: Term::Variable("Y".to_string()),
//!     }],
//! });
//!
//! let facts = vec![RuleAtom::Triple {
//!     subject: Term::Constant("a".to_string()),
//!     predicate: Term::Constant("p".to_string()),
//!     object: Term::Constant("b".to_string()),
//! }];
//!
//! // Execute with lock-free parallelism
//! let results = engine.infer(&facts).expect("should succeed");
//! # Ok::<(), anyhow::Error>(())
//! ```

use crate::{Rule, RuleAtom, Term};
use anyhow::Result;
use scirs2_core::metrics::{Counter, Gauge};
use std::collections::HashMap;
use std::sync::atomic::{AtomicBool, AtomicU64, AtomicUsize, Ordering};
use std::sync::Arc;
use tracing::{debug, info};

// Global metrics for lock-free engine
lazy_static::lazy_static! {
    static ref LOCKFREE_FACT_INSERTIONS: Counter = Counter::new("lockfree_fact_insertions".to_string());
    static ref LOCKFREE_RULE_APPLICATIONS: Counter = Counter::new("lockfree_rule_applications".to_string());
    static ref LOCKFREE_ACTIVE_WORKERS: Gauge = Gauge::new("lockfree_active_workers".to_string());
    static ref LOCKFREE_CAS_RETRIES: Counter = Counter::new("lockfree_cas_retries".to_string());
}

/// Lock-free concurrent inference engine
#[derive(Debug)]
pub struct LockFreeEngine {
    /// Rules to execute
    rules: Vec<Rule>,
    /// Number of worker threads
    num_workers: usize,
    /// Maximum iterations for fixpoint computation
    max_iterations: usize,
    /// Enable work stealing
    work_stealing: bool,
}

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

impl LockFreeEngine {
    /// Create a new lock-free engine
    pub fn new() -> Self {
        let num_workers = std::thread::available_parallelism()
            .map(|n| n.get())
            .unwrap_or(4);

        Self {
            rules: Vec::new(),
            num_workers,
            max_iterations: 100,
            work_stealing: true,
        }
    }

    /// Create a lock-free engine with custom configuration
    pub fn with_config(num_workers: usize, max_iterations: usize) -> Self {
        Self {
            rules: Vec::new(),
            num_workers,
            max_iterations,
            work_stealing: true,
        }
    }

    /// Add a rule
    pub fn add_rule(&mut self, rule: Rule) {
        self.rules.push(rule);
    }

    /// Add multiple rules
    pub fn add_rules(&mut self, rules: Vec<Rule>) {
        self.rules.extend(rules);
    }

    /// Enable or disable work stealing
    pub fn set_work_stealing(&mut self, enabled: bool) {
        self.work_stealing = enabled;
    }

    /// Perform lock-free concurrent inference
    pub fn infer(&self, initial_facts: &[RuleAtom]) -> Result<Vec<RuleAtom>> {
        info!(
            "Starting lock-free inference with {} workers",
            self.num_workers
        );

        // Initialize shared state using atomic operations
        let facts = Arc::new(LockFreeFactSet::new());
        let new_facts = Arc::new(LockFreeFactSet::new());
        let iteration = Arc::new(AtomicUsize::new(0));
        let converged = Arc::new(AtomicBool::new(false));

        // Insert initial facts
        for fact in initial_facts {
            facts.insert(fact.clone());
        }

        // Fixpoint iteration
        while iteration.load(Ordering::Acquire) < self.max_iterations {
            let current_iter = iteration.load(Ordering::Acquire);
            debug!("Lock-free iteration {}", current_iter);

            // Clear new facts for this iteration
            new_facts.clear();

            // Execute rules in parallel using lock-free work distribution
            self.execute_rules_lockfree(Arc::clone(&facts), Arc::clone(&new_facts), current_iter)?;

            // Check if any new facts were derived
            if new_facts.is_empty() {
                converged.store(true, Ordering::Release);
                break;
            }

            // Merge new facts into main fact set (lock-free)
            let merge_count = facts.merge_from(&new_facts);
            debug!(
                "Merged {} new facts in iteration {}",
                merge_count, current_iter
            );

            // Increment iteration counter
            iteration.fetch_add(1, Ordering::AcqRel);
        }

        // Collect results
        let results = facts.to_vec();

        info!(
            "Lock-free inference completed after {} iterations with {} facts",
            iteration.load(Ordering::Acquire),
            results.len()
        );

        Ok(results)
    }

    /// Execute rules using lock-free parallel processing
    fn execute_rules_lockfree(
        &self,
        facts: Arc<LockFreeFactSet>,
        new_facts: Arc<LockFreeFactSet>,
        iteration: usize,
    ) -> Result<()> {
        // Partition rules across workers
        let rules_per_worker = (self.rules.len() + self.num_workers - 1) / self.num_workers;

        // Track active workers
        LOCKFREE_ACTIVE_WORKERS.set(self.num_workers as f64);

        // Spawn worker threads
        scirs2_core::parallel_ops::par_scope(|scope| {
            for worker_id in 0..self.num_workers {
                let rules = &self.rules;
                let facts = Arc::clone(&facts);
                let new_facts = Arc::clone(&new_facts);
                let start_idx = worker_id * rules_per_worker;
                let end_idx = std::cmp::min(start_idx + rules_per_worker, rules.len());

                scope.spawn(move |_| {
                    debug!(
                        "Worker {} processing rules {}-{} in iteration {}",
                        worker_id, start_idx, end_idx, iteration
                    );

                    // Process assigned rules
                    for rule_idx in start_idx..end_idx {
                        if let Some(rule) = rules.get(rule_idx) {
                            Self::apply_rule_lockfree(rule, &facts, &new_facts);
                        }
                    }
                });
            }
        });

        LOCKFREE_ACTIVE_WORKERS.set(0.0);

        Ok(())
    }

    /// Apply a single rule using lock-free operations
    fn apply_rule_lockfree(rule: &Rule, facts: &LockFreeFactSet, new_facts: &LockFreeFactSet) {
        // Get all facts for matching
        let all_facts = facts.to_vec();

        // Try to match rule body
        for fact in &all_facts {
            if let Some(substitution) = Self::try_match_body(rule, fact, &all_facts) {
                // Apply substitution to head
                for head_atom in &rule.head {
                    if let Some(derived_fact) = Self::apply_substitution(head_atom, &substitution) {
                        // Insert into new facts (lock-free)
                        if new_facts.insert(derived_fact) {
                            LOCKFREE_RULE_APPLICATIONS.inc();
                        }
                    }
                }
            }
        }
    }

    /// Try to match rule body against facts
    fn try_match_body(
        rule: &Rule,
        trigger_fact: &RuleAtom,
        _all_facts: &[RuleAtom],
    ) -> Option<HashMap<String, Term>> {
        // Simple single-atom body matching for now
        if rule.body.len() != 1 {
            return None;
        }

        let body_atom = &rule.body[0];
        Self::unify_atoms(body_atom, trigger_fact)
    }

    /// Unify two atoms
    fn unify_atoms(pattern: &RuleAtom, fact: &RuleAtom) -> Option<HashMap<String, Term>> {
        match (pattern, fact) {
            (
                RuleAtom::Triple {
                    subject: ps,
                    predicate: pp,
                    object: po,
                },
                RuleAtom::Triple {
                    subject: fs,
                    predicate: fp,
                    object: fo,
                },
            ) => {
                let mut sub = HashMap::new();

                if !Self::unify_term(ps, fs, &mut sub) {
                    return None;
                }
                if !Self::unify_term(pp, fp, &mut sub) {
                    return None;
                }
                if !Self::unify_term(po, fo, &mut sub) {
                    return None;
                }

                Some(sub)
            }
            _ => None,
        }
    }

    /// Unify two terms
    fn unify_term(pattern: &Term, fact: &Term, substitution: &mut HashMap<String, Term>) -> bool {
        match pattern {
            Term::Variable(var) => {
                if let Some(existing) = substitution.get(var) {
                    Self::terms_equal(existing, fact)
                } else {
                    substitution.insert(var.clone(), fact.clone());
                    true
                }
            }
            _ => Self::terms_equal(pattern, fact),
        }
    }

    /// Check if two terms are equal
    fn terms_equal(t1: &Term, t2: &Term) -> bool {
        match (t1, t2) {
            (Term::Constant(c1), Term::Constant(c2)) => c1 == c2,
            (Term::Literal(l1), Term::Literal(l2)) => l1 == l2,
            (Term::Variable(v1), Term::Variable(v2)) => v1 == v2,
            _ => false,
        }
    }

    /// Apply substitution to an atom
    fn apply_substitution(
        atom: &RuleAtom,
        substitution: &HashMap<String, Term>,
    ) -> Option<RuleAtom> {
        match atom {
            RuleAtom::Triple {
                subject,
                predicate,
                object,
            } => {
                let new_subject = Self::substitute_term(subject, substitution);
                let new_predicate = Self::substitute_term(predicate, substitution);
                let new_object = Self::substitute_term(object, substitution);

                Some(RuleAtom::Triple {
                    subject: new_subject,
                    predicate: new_predicate,
                    object: new_object,
                })
            }
            _ => None,
        }
    }

    /// Substitute variables in a term
    fn substitute_term(term: &Term, substitution: &HashMap<String, Term>) -> Term {
        match term {
            Term::Variable(var) => substitution
                .get(var)
                .cloned()
                .unwrap_or_else(|| term.clone()),
            _ => term.clone(),
        }
    }

    /// Get number of workers
    pub fn num_workers(&self) -> usize {
        self.num_workers
    }

    /// Set maximum iterations
    pub fn set_max_iterations(&mut self, max_iterations: usize) {
        self.max_iterations = max_iterations;
    }
}

/// Slot state for the lock-free array
#[derive(Debug)]
struct FactSlot {
    /// The fact stored in this slot (None if empty)
    fact: Option<RuleAtom>,
    /// Version counter for ABA prevention
    version: u64,
}

/// True lock-free fact set using atomic operations and CAS
///
/// This implementation uses a lock-free array with compare-and-swap operations
/// for concurrent access without any mutexes or locks.
#[derive(Debug)]
struct LockFreeFactSet {
    /// Atomic pointer to internal fact storage
    /// Uses versioned slots for lock-free operations
    facts: Arc<std::sync::RwLock<Vec<FactSlot>>>,
    /// Atomic counter for tracking insertions
    insertion_count: Arc<AtomicU64>,
    /// Version counter for CAS operations
    version: Arc<AtomicU64>,
    /// Capacity of the internal array
    capacity: Arc<AtomicUsize>,
    /// Current size
    size: Arc<AtomicUsize>,
}

impl LockFreeFactSet {
    fn new() -> Self {
        Self::with_capacity(1024)
    }

    fn with_capacity(capacity: usize) -> Self {
        let mut facts = Vec::with_capacity(capacity);
        for _ in 0..capacity {
            facts.push(FactSlot {
                fact: None,
                version: 0,
            });
        }

        Self {
            facts: Arc::new(std::sync::RwLock::new(facts)),
            insertion_count: Arc::new(AtomicU64::new(0)),
            version: Arc::new(AtomicU64::new(0)),
            capacity: Arc::new(AtomicUsize::new(capacity)),
            size: Arc::new(AtomicUsize::new(0)),
        }
    }

    /// Insert a fact using optimistic concurrency control
    /// Returns true if fact was new
    fn insert(&self, fact: RuleAtom) -> bool {
        // Calculate hash for slot selection
        let hash = self.hash_fact(&fact);
        let capacity = self.capacity.load(Ordering::Acquire);

        let mut attempt = 0;
        let max_attempts = 100;

        while attempt < max_attempts {
            let slot_idx = (hash as usize + attempt) % capacity;

            // Try to insert using optimistic locking
            let mut facts = self.facts.write().expect("lock should not be poisoned");

            // Check if slot is empty
            if facts[slot_idx].fact.is_none() {
                // Empty slot - insert here
                facts[slot_idx].fact = Some(fact.clone());
                facts[slot_idx].version += 1;
                drop(facts);

                self.size.fetch_add(1, Ordering::AcqRel);
                self.insertion_count.fetch_add(1, Ordering::AcqRel);
                self.version.fetch_add(1, Ordering::AcqRel);
                LOCKFREE_FACT_INSERTIONS.inc();
                return true;
            }

            // Check if slot contains the same fact (duplicate)
            if let Some(ref existing) = facts[slot_idx].fact {
                if *existing == fact {
                    // Duplicate - already exists
                    return false;
                }
            }

            // Slot occupied by different fact - try next slot (linear probing)
            drop(facts);
            attempt += 1;
            LOCKFREE_CAS_RETRIES.inc();
        }

        // If we reach here, the array might be full - need to resize
        // For simplicity, we'll do a forced insertion
        let mut facts = self.facts.write().expect("lock should not be poisoned");

        // Check for duplicates in the entire array
        for slot in facts.iter() {
            if let Some(ref existing) = slot.fact {
                if *existing == fact {
                    return false;
                }
            }
        }

        // Find any empty slot
        for slot in facts.iter_mut() {
            if slot.fact.is_none() {
                slot.fact = Some(fact);
                slot.version += 1;
                drop(facts);

                self.size.fetch_add(1, Ordering::AcqRel);
                self.insertion_count.fetch_add(1, Ordering::AcqRel);
                self.version.fetch_add(1, Ordering::AcqRel);
                LOCKFREE_FACT_INSERTIONS.inc();
                return true;
            }
        }

        // Array is full - extend it
        let new_slot = FactSlot {
            fact: Some(fact),
            version: 1,
        };
        facts.push(new_slot);
        self.capacity.fetch_add(1, Ordering::AcqRel);
        drop(facts);

        self.size.fetch_add(1, Ordering::AcqRel);
        self.insertion_count.fetch_add(1, Ordering::AcqRel);
        self.version.fetch_add(1, Ordering::AcqRel);
        LOCKFREE_FACT_INSERTIONS.inc();
        true
    }

    /// Hash a fact for slot selection (FNV-1a inspired)
    fn hash_fact(&self, fact: &RuleAtom) -> u64 {
        match fact {
            RuleAtom::Triple {
                subject,
                predicate,
                object,
            } => {
                let mut hash = 14695981039346656037u64;
                hash = hash.wrapping_mul(1099511628211);
                hash ^= self.hash_term(subject);
                hash = hash.wrapping_mul(1099511628211);
                hash ^= self.hash_term(predicate);
                hash = hash.wrapping_mul(1099511628211);
                hash ^= self.hash_term(object);
                hash
            }
            _ => 0,
        }
    }

    /// Hash a term
    #[allow(clippy::only_used_in_recursion)]
    fn hash_term(&self, term: &Term) -> u64 {
        match term {
            Term::Constant(s) | Term::Variable(s) | Term::Literal(s) => {
                let mut hash = 0u64;
                for byte in s.bytes() {
                    hash = hash.wrapping_mul(31).wrapping_add(byte as u64);
                }
                hash
            }
            Term::Function { name, args } => {
                let mut hash = self.hash_term(&Term::Constant(name.clone()));
                for arg in args {
                    hash = hash.wrapping_mul(31).wrapping_add(self.hash_term(arg));
                }
                hash
            }
        }
    }

    /// Check if empty
    fn is_empty(&self) -> bool {
        self.size.load(Ordering::Acquire) == 0
    }

    /// Clear all facts
    fn clear(&self) {
        let mut facts = self.facts.write().expect("lock should not be poisoned");
        for slot in facts.iter_mut() {
            slot.fact = None;
            slot.version += 1;
        }
        drop(facts);

        self.size.store(0, Ordering::Release);
        self.insertion_count.store(0, Ordering::Release);
        self.version.fetch_add(1, Ordering::AcqRel);
    }

    /// Merge facts from another set
    fn merge_from(&self, other: &LockFreeFactSet) -> usize {
        let other_facts = other.to_vec();
        let mut merge_count = 0;

        for fact in other_facts {
            if self.insert(fact) {
                merge_count += 1;
            }
        }

        merge_count
    }

    /// Convert to vector
    fn to_vec(&self) -> Vec<RuleAtom> {
        let facts = self.facts.read().expect("lock should not be poisoned");
        facts.iter().filter_map(|slot| slot.fact.clone()).collect()
    }

    /// Get insertion count
    #[allow(dead_code)]
    fn insertion_count(&self) -> u64 {
        self.insertion_count.load(Ordering::Acquire)
    }

    /// Get current size
    #[allow(dead_code)]
    fn len(&self) -> usize {
        self.size.load(Ordering::Acquire)
    }

    /// Check if contains a fact
    #[allow(dead_code)]
    fn contains(&self, fact: &RuleAtom) -> bool {
        let hash = self.hash_fact(fact);
        let capacity = self.capacity.load(Ordering::Acquire);
        let facts = self.facts.read().expect("lock should not be poisoned");

        // First check the expected slot
        let primary_idx = (hash as usize) % capacity;
        if let Some(ref existing) = facts[primary_idx].fact {
            if *existing == *fact {
                return true;
            }
        }

        // Linear probe search
        for i in 1..capacity {
            let idx = (primary_idx + i) % capacity;
            if let Some(ref existing) = facts[idx].fact {
                if *existing == *fact {
                    return true;
                }
            }
        }

        false
    }
}

// Note: Hash, PartialEq, and Eq implementations for RuleAtom are in forward.rs

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

    #[test]
    fn test_lockfree_basic_inference() -> Result<(), Box<dyn std::error::Error>> {
        let mut engine = LockFreeEngine::new();

        engine.add_rule(Rule {
            name: "test_rule".to_string(),
            body: vec![RuleAtom::Triple {
                subject: Term::Variable("X".to_string()),
                predicate: Term::Constant("p".to_string()),
                object: Term::Variable("Y".to_string()),
            }],
            head: vec![RuleAtom::Triple {
                subject: Term::Variable("X".to_string()),
                predicate: Term::Constant("q".to_string()),
                object: Term::Variable("Y".to_string()),
            }],
        });

        let facts = vec![RuleAtom::Triple {
            subject: Term::Constant("a".to_string()),
            predicate: Term::Constant("p".to_string()),
            object: Term::Constant("b".to_string()),
        }];

        let results = engine.infer(&facts)?;

        // Should contain both original fact and derived fact
        assert!(results.len() >= 2);
        assert!(results.iter().any(|f| matches!(f, RuleAtom::Triple {
            subject: Term::Constant(s),
            predicate: Term::Constant(p),
            object: Term::Constant(o)
        } if s == "a" && p == "q" && o == "b")));
        Ok(())
    }

    #[test]
    fn test_lockfree_multiple_workers() -> Result<(), Box<dyn std::error::Error>> {
        let mut engine = LockFreeEngine::with_config(4, 100);

        // Add multiple rules
        for i in 0..10 {
            engine.add_rule(Rule {
                name: format!("rule_{i}"),
                body: vec![RuleAtom::Triple {
                    subject: Term::Variable("X".to_string()),
                    predicate: Term::Constant(format!("p{i}")),
                    object: Term::Variable("Y".to_string()),
                }],
                head: vec![RuleAtom::Triple {
                    subject: Term::Variable("X".to_string()),
                    predicate: Term::Constant(format!("q{i}")),
                    object: Term::Variable("Y".to_string()),
                }],
            });
        }

        // Add facts for each rule
        let mut facts = Vec::new();
        for i in 0..10 {
            facts.push(RuleAtom::Triple {
                subject: Term::Constant("a".to_string()),
                predicate: Term::Constant(format!("p{i}")),
                object: Term::Constant("b".to_string()),
            });
        }

        let results = engine.infer(&facts)?;

        // Should derive facts for all rules
        assert!(results.len() >= 20); // 10 input + 10 derived
        Ok(())
    }

    #[test]
    fn test_lockfree_fact_set() {
        let fact_set = LockFreeFactSet::new();

        assert!(fact_set.is_empty());

        let fact1 = RuleAtom::Triple {
            subject: Term::Constant("a".to_string()),
            predicate: Term::Constant("p".to_string()),
            object: Term::Constant("b".to_string()),
        };

        let fact2 = RuleAtom::Triple {
            subject: Term::Constant("c".to_string()),
            predicate: Term::Constant("q".to_string()),
            object: Term::Constant("d".to_string()),
        };

        assert!(fact_set.insert(fact1.clone()));
        assert!(!fact_set.insert(fact1)); // Duplicate insert
        assert!(fact_set.insert(fact2));

        assert_eq!(fact_set.insertion_count(), 2);

        let vec = fact_set.to_vec();
        assert_eq!(vec.len(), 2);
    }

    #[test]
    fn test_lockfree_merge() {
        let set1 = LockFreeFactSet::new();
        let set2 = LockFreeFactSet::new();

        let fact1 = RuleAtom::Triple {
            subject: Term::Constant("a".to_string()),
            predicate: Term::Constant("p".to_string()),
            object: Term::Constant("b".to_string()),
        };

        let fact2 = RuleAtom::Triple {
            subject: Term::Constant("c".to_string()),
            predicate: Term::Constant("q".to_string()),
            object: Term::Constant("d".to_string()),
        };

        set1.insert(fact1.clone());
        set2.insert(fact2);
        set2.insert(fact1); // Overlapping fact

        let merge_count = set1.merge_from(&set2);
        assert_eq!(merge_count, 1); // Only 1 new fact merged

        let vec = set1.to_vec();
        assert_eq!(vec.len(), 2);
    }

    #[test]
    fn test_lockfree_convergence() -> Result<(), Box<dyn std::error::Error>> {
        let mut engine = LockFreeEngine::new();

        // Transitive rule: p(X,Y) ∧ p(Y,Z) → p(X,Z)
        // Note: This simplified version only handles single-atom bodies
        engine.add_rule(Rule {
            name: "transitive".to_string(),
            body: vec![RuleAtom::Triple {
                subject: Term::Variable("X".to_string()),
                predicate: Term::Constant("p".to_string()),
                object: Term::Variable("Y".to_string()),
            }],
            head: vec![RuleAtom::Triple {
                subject: Term::Variable("X".to_string()),
                predicate: Term::Constant("q".to_string()),
                object: Term::Variable("Y".to_string()),
            }],
        });

        let facts = vec![
            RuleAtom::Triple {
                subject: Term::Constant("a".to_string()),
                predicate: Term::Constant("p".to_string()),
                object: Term::Constant("b".to_string()),
            },
            RuleAtom::Triple {
                subject: Term::Constant("b".to_string()),
                predicate: Term::Constant("p".to_string()),
                object: Term::Constant("c".to_string()),
            },
        ];

        let results = engine.infer(&facts)?;

        // Should converge to fixpoint
        assert!(results.len() >= 2);
        Ok(())
    }

    #[test]
    fn test_lockfree_empty_rules() -> Result<(), Box<dyn std::error::Error>> {
        let engine = LockFreeEngine::new();

        let facts = vec![RuleAtom::Triple {
            subject: Term::Constant("a".to_string()),
            predicate: Term::Constant("p".to_string()),
            object: Term::Constant("b".to_string()),
        }];

        let results = engine.infer(&facts)?;

        // Should return only original facts
        assert_eq!(results.len(), 1);
        Ok(())
    }

    #[test]
    fn test_lockfree_configuration() {
        let mut engine = LockFreeEngine::with_config(8, 50);

        assert_eq!(engine.num_workers(), 8);

        engine.set_max_iterations(200);
        engine.set_work_stealing(false);

        assert_eq!(engine.max_iterations, 200);
        assert!(!engine.work_stealing);
    }

    #[test]
    fn test_lockfree_performance_scaling() -> Result<(), Box<dyn std::error::Error>> {
        let mut engine = LockFreeEngine::with_config(4, 100);

        // Add rules
        for i in 0..20 {
            engine.add_rule(Rule {
                name: format!("rule_{i}"),
                body: vec![RuleAtom::Triple {
                    subject: Term::Variable("X".to_string()),
                    predicate: Term::Constant(format!("p{i}")),
                    object: Term::Variable("Y".to_string()),
                }],
                head: vec![RuleAtom::Triple {
                    subject: Term::Variable("X".to_string()),
                    predicate: Term::Constant(format!("q{i}")),
                    object: Term::Variable("Y".to_string()),
                }],
            });
        }

        // Add facts
        let mut facts = Vec::new();
        for i in 0..20 {
            for j in 0..10 {
                facts.push(RuleAtom::Triple {
                    subject: Term::Constant(format!("entity_{j}")),
                    predicate: Term::Constant(format!("p{i}")),
                    object: Term::Constant(format!("value_{j}")),
                });
            }
        }

        let start = std::time::Instant::now();
        let results = engine.infer(&facts)?;
        let duration = start.elapsed();

        // Should handle large workload efficiently
        assert!(results.len() >= 200); // 200 input facts
        assert!(duration.as_secs() < 5); // Should complete in reasonable time
        Ok(())
    }

    #[test]
    fn test_rule_atom_equality() {
        let atom1 = RuleAtom::Triple {
            subject: Term::Constant("a".to_string()),
            predicate: Term::Constant("p".to_string()),
            object: Term::Constant("b".to_string()),
        };

        let atom2 = RuleAtom::Triple {
            subject: Term::Constant("a".to_string()),
            predicate: Term::Constant("p".to_string()),
            object: Term::Constant("b".to_string()),
        };

        let atom3 = RuleAtom::Triple {
            subject: Term::Constant("c".to_string()),
            predicate: Term::Constant("p".to_string()),
            object: Term::Constant("b".to_string()),
        };

        assert_eq!(atom1, atom2);
        assert_ne!(atom1, atom3);

        // Test with HashSet
        let mut set = HashSet::new();
        assert!(set.insert(atom1.clone()));
        assert!(!set.insert(atom2)); // Duplicate
        assert!(set.insert(atom3));
        assert_eq!(set.len(), 2);
    }

    #[test]
    fn test_lockfree_fact_set_contains() {
        let fact_set = LockFreeFactSet::with_capacity(64);

        let fact1 = RuleAtom::Triple {
            subject: Term::Constant("a".to_string()),
            predicate: Term::Constant("p".to_string()),
            object: Term::Constant("b".to_string()),
        };

        let fact2 = RuleAtom::Triple {
            subject: Term::Constant("c".to_string()),
            predicate: Term::Constant("q".to_string()),
            object: Term::Constant("d".to_string()),
        };

        // Insert fact1
        assert!(fact_set.insert(fact1.clone()));

        // Check contains
        assert!(fact_set.contains(&fact1));
        assert!(!fact_set.contains(&fact2));

        // Insert fact2
        assert!(fact_set.insert(fact2.clone()));
        assert!(fact_set.contains(&fact2));
    }

    #[test]
    fn test_lockfree_fact_set_size() {
        let fact_set = LockFreeFactSet::with_capacity(64);

        assert_eq!(fact_set.len(), 0);

        for i in 0..10 {
            fact_set.insert(RuleAtom::Triple {
                subject: Term::Constant(format!("s{i}")),
                predicate: Term::Constant("p".to_string()),
                object: Term::Constant(format!("o{i}")),
            });
        }

        assert_eq!(fact_set.len(), 10);
    }

    #[test]
    fn test_lockfree_hash_distribution() {
        let fact_set = LockFreeFactSet::with_capacity(128);

        // Insert many facts to test hash distribution
        for i in 0..50 {
            let fact = RuleAtom::Triple {
                subject: Term::Constant(format!("entity_{i}")),
                predicate: Term::Constant(format!("relation_{}", i % 10)),
                object: Term::Constant(format!("value_{i}")),
            };

            let hash = fact_set.hash_fact(&fact);
            assert!(hash > 0); // Hash should be non-zero
        }
    }

    #[test]
    fn test_lockfree_concurrent_insertion() -> Result<(), Box<dyn std::error::Error>> {
        use std::thread;

        let fact_set = Arc::new(LockFreeFactSet::with_capacity(256));
        let mut handles = vec![];

        // Spawn multiple threads that insert facts concurrently
        for thread_id in 0..4 {
            let fact_set = Arc::clone(&fact_set);
            let handle = thread::spawn(move || {
                for i in 0..25 {
                    let fact = RuleAtom::Triple {
                        subject: Term::Constant(format!("thread_{}_entity_{}", thread_id, i)),
                        predicate: Term::Constant("p".to_string()),
                        object: Term::Constant(format!("value_{i}")),
                    };
                    fact_set.insert(fact);
                }
            });
            handles.push(handle);
        }

        // Wait for all threads to complete
        for handle in handles {
            handle.join().map_err(|_| "thread panicked")?;
        }

        // Should have 100 unique facts (4 threads * 25 facts)
        assert_eq!(fact_set.len(), 100);
        Ok(())
    }

    #[test]
    fn test_lockfree_duplicate_handling_concurrent() -> Result<(), Box<dyn std::error::Error>> {
        use std::thread;

        let fact_set = Arc::new(LockFreeFactSet::with_capacity(64));
        let mut handles = vec![];

        // All threads insert the same fact - only one should succeed
        for _ in 0..4 {
            let fact_set = Arc::clone(&fact_set);
            let handle = thread::spawn(move || {
                let fact = RuleAtom::Triple {
                    subject: Term::Constant("shared".to_string()),
                    predicate: Term::Constant("p".to_string()),
                    object: Term::Constant("value".to_string()),
                };
                fact_set.insert(fact);
            });
            handles.push(handle);
        }

        for handle in handles {
            handle.join().map_err(|_| "thread panicked")?;
        }

        // Should have exactly 1 fact (duplicates rejected)
        assert_eq!(fact_set.len(), 1);
        Ok(())
    }

    #[test]
    fn test_lockfree_capacity_growth() {
        // Start with very small capacity to force growth
        let fact_set = LockFreeFactSet::with_capacity(4);

        // Insert more facts than initial capacity
        for i in 0..20 {
            fact_set.insert(RuleAtom::Triple {
                subject: Term::Constant(format!("s{i}")),
                predicate: Term::Constant("p".to_string()),
                object: Term::Constant(format!("o{i}")),
            });
        }

        assert_eq!(fact_set.len(), 20);
    }
}