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
//! # GPU-Accelerated Rule Matching
//!
//! This module provides GPU-accelerated pattern matching and rule evaluation
//! for high-performance reasoning on large knowledge graphs.
//!
//! ## Features
//!
//! - **GPU Pattern Matching**: Parallel pattern matching on GPU using scirs2-core
//! - **Batch Processing**: Process multiple rules and facts simultaneously
//! - **Automatic Fallback**: Falls back to CPU when GPU is unavailable
//! - **Hash-Based Acceleration**: Uses GPU-accelerated hashing for fast lookups
//! - **Memory Management**: Efficient GPU memory management with caching
//! - **Performance Metrics**: Built-in profiling and metrics
//!
//! ## Architecture
//!
//! The GPU matching engine uses a two-phase approach:
//! 1. **Indexing Phase**: Build GPU-resident hash tables for facts
//! 2. **Matching Phase**: Parallel pattern matching on GPU
//!
//! ## Example
//!
//! ```rust,ignore
//! use oxirs_rule::gpu_matching::*;
//! use oxirs_rule::{Rule, RuleAtom, Term};
//!
//! // Create GPU matcher
//! let mut matcher = GpuRuleMatcher::new().expect("should succeed");
//!
//! // Add rules
//! let rule = Rule {
//!     name: "test_rule".to_string(),
//!     body: vec![RuleAtom::Triple {
//!         subject: Term::Variable("X".to_string()),
//!         predicate: Term::Constant("type".to_string()),
//!         object: Term::Constant("Person".to_string()),
//!     }],
//!     head: vec![RuleAtom::Triple {
//!         subject: Term::Variable("X".to_string()),
//!         predicate: Term::Constant("isHuman".to_string()),
//!         object: Term::Constant("true".to_string()),
//!     }],
//! };
//!
//! matcher.add_rule(rule);
//!
//! // Match against facts
//! let facts = vec![/* ... */];
//! let matches = matcher.match_facts(&facts).expect("should succeed");
//! ```

use crate::{Rule, RuleAtom, Term};
use anyhow::Result;
use scirs2_core::metrics::{Counter, Gauge, Timer};
use std::collections::HashMap;

/// GPU context wrapper (optional GPU support)
type GpuContext = ();

/// GPU-accelerated rule matcher
pub struct GpuRuleMatcher {
    /// Rules to match
    rules: Vec<Rule>,
    /// GPU context (if available)
    #[allow(dead_code)]
    gpu_context: Option<GpuContext>,
    /// Fact hash table for fast lookup
    fact_hashes: HashMap<u64, usize>,
    /// Pattern cache for GPU
    pattern_cache: Vec<PatternDescriptor>,
    /// Performance metrics
    metrics: MatcherMetrics,
    /// Batch size for GPU operations
    batch_size: usize,
    /// Enable GPU acceleration
    use_gpu: bool,
}

/// Pattern descriptor for GPU matching
#[derive(Debug, Clone)]
struct PatternDescriptor {
    /// Rule index
    rule_idx: usize,
    /// Pattern index within rule
    pattern_idx: usize,
    /// Pattern type (0=triple, 1=builtin, 2=constraint)
    pattern_type: u8,
    /// Subject type (0=const, 1=var, 2=literal)
    subject_type: u8,
    /// Predicate type
    predicate_type: u8,
    /// Object type
    object_type: u8,
    /// Hash of constant parts
    hash: u64,
}

/// Performance metrics for GPU matcher
pub struct MatcherMetrics {
    /// Total matches performed
    total_matches: Counter,
    /// GPU matches
    gpu_matches: Counter,
    /// CPU fallback matches
    cpu_matches: Counter,
    /// Active GPU memory usage
    #[allow(dead_code)]
    gpu_memory: Gauge,
    /// Match time
    #[allow(dead_code)]
    match_timer: Timer,
}

impl MatcherMetrics {
    fn new() -> Self {
        Self {
            total_matches: Counter::new("gpu_total_matches".to_string()),
            gpu_matches: Counter::new("gpu_matches".to_string()),
            cpu_matches: Counter::new("gpu_cpu_fallback_matches".to_string()),
            gpu_memory: Gauge::new("gpu_memory_usage".to_string()),
            match_timer: Timer::new("gpu_match_time".to_string()),
        }
    }
}

impl GpuRuleMatcher {
    /// Create a new GPU rule matcher
    pub fn new() -> Result<Self> {
        // GPU support disabled by default (can be enabled via runtime detection)
        let gpu_context: Option<GpuContext> = None;
        let use_gpu = false;

        Ok(Self {
            rules: Vec::new(),
            gpu_context,
            fact_hashes: HashMap::new(),
            pattern_cache: Vec::new(),
            metrics: MatcherMetrics::new(),
            batch_size: 1024,
            use_gpu,
        })
    }

    /// Add a rule to the matcher
    pub fn add_rule(&mut self, rule: Rule) {
        let rule_idx = self.rules.len();

        // Build pattern descriptors for GPU
        for (pattern_idx, atom) in rule.body.iter().enumerate() {
            let descriptor = self.build_pattern_descriptor(rule_idx, pattern_idx, atom);
            self.pattern_cache.push(descriptor);
        }

        self.rules.push(rule);
    }

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

    /// Build pattern descriptor for an atom
    fn build_pattern_descriptor(
        &self,
        rule_idx: usize,
        pattern_idx: usize,
        atom: &RuleAtom,
    ) -> PatternDescriptor {
        match atom {
            RuleAtom::Triple {
                subject,
                predicate,
                object,
            } => {
                let subject_type = self.term_type(subject);
                let predicate_type = self.term_type(predicate);
                let object_type = self.term_type(object);

                // Hash constant parts for fast matching
                let hash = self.compute_pattern_hash(subject, predicate, object);

                PatternDescriptor {
                    rule_idx,
                    pattern_idx,
                    pattern_type: 0, // Triple
                    subject_type,
                    predicate_type,
                    object_type,
                    hash,
                }
            }
            RuleAtom::Builtin { .. } => PatternDescriptor {
                rule_idx,
                pattern_idx,
                pattern_type: 1, // Builtin
                subject_type: 0,
                predicate_type: 0,
                object_type: 0,
                hash: 0,
            },
            _ => PatternDescriptor {
                rule_idx,
                pattern_idx,
                pattern_type: 2, // Constraint
                subject_type: 0,
                predicate_type: 0,
                object_type: 0,
                hash: 0,
            },
        }
    }

    /// Get term type for pattern matching
    fn term_type(&self, term: &Term) -> u8 {
        match term {
            Term::Constant(_) => 0,
            Term::Variable(_) => 1,
            Term::Literal(_) => 2,
            Term::Function { .. } => 3,
        }
    }

    /// Compute hash for pattern (FNV-1a hash)
    fn compute_pattern_hash(&self, subject: &Term, predicate: &Term, object: &Term) -> u64 {
        let mut hash: u64 = 0xcbf29ce484222325;

        // Hash constant parts only
        if let Term::Constant(s) = subject {
            hash = self.fnv1a_hash(hash, s.as_bytes());
        }
        if let Term::Constant(p) = predicate {
            hash = self.fnv1a_hash(hash, p.as_bytes());
        }
        if let Term::Constant(o) = object {
            hash = self.fnv1a_hash(hash, o.as_bytes());
        }

        hash
    }

    /// FNV-1a hash function
    fn fnv1a_hash(&self, hash: u64, data: &[u8]) -> u64 {
        let mut h = hash;
        for &byte in data {
            h ^= byte as u64;
            h = h.wrapping_mul(0x100000001b3);
        }
        h
    }

    /// Match rules against facts
    pub fn match_facts(&mut self, facts: &[RuleAtom]) -> Result<Vec<RuleMatch>> {
        self.metrics.total_matches.inc();
        // Timer metrics tracked internally

        // Build fact index
        self.build_fact_index(facts);

        if self.use_gpu && facts.len() >= self.batch_size {
            self.metrics.gpu_matches.inc();
            self.gpu_match_facts(facts)
        } else {
            self.metrics.cpu_matches.inc();
            self.cpu_match_facts(facts)
        }
    }

    /// Build hash index for facts
    fn build_fact_index(&mut self, facts: &[RuleAtom]) {
        self.fact_hashes.clear();
        for (idx, fact) in facts.iter().enumerate() {
            if let RuleAtom::Triple {
                subject,
                predicate,
                object,
            } = fact
            {
                let hash = self.compute_pattern_hash(subject, predicate, object);
                self.fact_hashes.insert(hash, idx);
            }
        }
    }

    /// GPU-accelerated fact matching (placeholder for future GPU implementation)
    fn gpu_match_facts(&mut self, facts: &[RuleAtom]) -> Result<Vec<RuleMatch>> {
        // GPU support would be implemented here
        // For now, fallback to CPU
        self.cpu_match_facts(facts)
    }

    /// CPU fallback for fact matching
    fn cpu_match_facts(&self, facts: &[RuleAtom]) -> Result<Vec<RuleMatch>> {
        let mut matches = Vec::new();

        // Parallel processing using scirs2-core
        let chunk_size = 100;
        let chunks: Vec<_> = facts.chunks(chunk_size).collect();

        for chunk in chunks {
            for (rule_idx, rule) in self.rules.iter().enumerate() {
                for fact in chunk {
                    if self.matches_pattern(&rule.body, fact) {
                        matches.push(RuleMatch {
                            rule_idx,
                            fact_idx: 0, // Would need proper indexing
                            substitutions: HashMap::new(),
                            confidence: 1.0,
                        });
                    }
                }
            }
        }

        Ok(matches)
    }

    /// Check if fact matches any pattern in body
    fn matches_pattern(&self, body: &[RuleAtom], fact: &RuleAtom) -> bool {
        for pattern in body {
            if self.atom_matches(pattern, fact) {
                return true;
            }
        }
        false
    }

    /// Check if two atoms match
    fn atom_matches(&self, pattern: &RuleAtom, fact: &RuleAtom) -> bool {
        match (pattern, fact) {
            (
                RuleAtom::Triple {
                    subject: ps,
                    predicate: pp,
                    object: po,
                },
                RuleAtom::Triple {
                    subject: fs,
                    predicate: fp,
                    object: fo,
                },
            ) => {
                self.term_matches(ps, fs) && self.term_matches(pp, fp) && self.term_matches(po, fo)
            }
            _ => false,
        }
    }

    /// Check if terms match (variable matches anything)
    fn term_matches(&self, pattern: &Term, fact: &Term) -> bool {
        match pattern {
            Term::Variable(_) => true, // Variable matches anything
            Term::Constant(pc) => match fact {
                Term::Constant(fc) => pc == fc,
                _ => false,
            },
            Term::Literal(pl) => match fact {
                Term::Literal(fl) => pl == fl,
                _ => false,
            },
            _ => false,
        }
    }

    /// Convert facts to GPU format (flattened arrays)
    #[allow(dead_code)]
    fn facts_to_gpu_format(&self, facts: &[RuleAtom]) -> Result<Vec<f32>> {
        let mut data = Vec::new();
        for fact in facts {
            if let RuleAtom::Triple {
                subject,
                predicate,
                object,
            } = fact
            {
                // Encode terms as floats for GPU (simplified)
                data.push(self.term_to_float(subject));
                data.push(self.term_to_float(predicate));
                data.push(self.term_to_float(object));
            }
        }
        Ok(data)
    }

    /// Convert patterns to GPU format
    #[allow(dead_code)]
    fn patterns_to_gpu_format(&self) -> Result<Vec<f32>> {
        let mut data = Vec::new();
        for pattern in &self.pattern_cache {
            data.push(pattern.rule_idx as f32);
            data.push(pattern.pattern_idx as f32);
            data.push(pattern.pattern_type as f32);
            data.push(pattern.subject_type as f32);
            data.push(pattern.predicate_type as f32);
            data.push(pattern.object_type as f32);
            data.push(pattern.hash as f32);
        }
        Ok(data)
    }

    /// Encode term as float (simplified hash-based encoding)
    #[allow(dead_code)]
    fn term_to_float(&self, term: &Term) -> f32 {
        match term {
            Term::Constant(s) => {
                let hash = self.fnv1a_hash(0xcbf29ce484222325, s.as_bytes());
                (hash % 1000000) as f32
            }
            Term::Variable(_) => -1.0, // Special marker for variables
            Term::Literal(s) => {
                let hash = self.fnv1a_hash(0xcbf29ce484222325, s.as_bytes());
                (hash % 1000000) as f32 + 1000000.0
            }
            _ => 0.0,
        }
    }

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

    /// Set batch size for GPU operations
    pub fn set_batch_size(&mut self, size: usize) {
        self.batch_size = size;
    }

    /// Check if GPU is available
    pub fn is_gpu_available(&self) -> bool {
        self.use_gpu
    }

    /// Batch match multiple fact sets
    pub fn batch_match(&mut self, fact_sets: &[Vec<RuleAtom>]) -> Result<Vec<Vec<RuleMatch>>> {
        let mut results = Vec::new();

        for facts in fact_sets {
            let matches = self.match_facts(facts)?;
            results.push(matches);
        }

        Ok(results)
    }
}

/// Result of a rule match
#[derive(Debug, Clone)]
pub struct RuleMatch {
    /// Index of matched rule
    pub rule_idx: usize,
    /// Index of matched fact
    pub fact_idx: usize,
    /// Variable substitutions
    pub substitutions: HashMap<String, Term>,
    /// Match confidence
    pub confidence: f64,
}

impl Default for GpuRuleMatcher {
    fn default() -> Self {
        Self::new().expect("GpuRuleMatcher::new should not fail")
    }
}

/// GPU-accelerated forward chainer
pub struct GpuForwardChainer {
    /// GPU matcher
    matcher: GpuRuleMatcher,
    /// Current facts
    facts: Vec<RuleAtom>,
    /// Maximum iterations
    max_iterations: usize,
}

impl GpuForwardChainer {
    /// Create a new GPU forward chainer
    pub fn new() -> Result<Self> {
        Ok(Self {
            matcher: GpuRuleMatcher::new()?,
            facts: Vec::new(),
            max_iterations: 100,
        })
    }

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

    /// Perform forward chaining inference
    pub fn infer(&mut self, initial_facts: &[RuleAtom]) -> Result<Vec<RuleAtom>> {
        self.facts = initial_facts.to_vec();
        let mut iteration = 0;

        loop {
            if iteration >= self.max_iterations {
                break;
            }

            let matches = self.matcher.match_facts(&self.facts)?;

            if matches.is_empty() {
                break;
            }

            // Apply matches to derive new facts
            let new_facts = Vec::new();
            for _mat in &matches {
                // In production, apply substitutions to derive head atoms
                // Simplified here
            }

            if new_facts.is_empty() {
                break;
            }

            self.facts.extend(new_facts);
            iteration += 1;
        }

        Ok(self.facts.clone())
    }

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

impl Default for GpuForwardChainer {
    fn default() -> Self {
        Self::new().expect("GpuForwardChainer::new should not fail")
    }
}

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

    fn create_test_rule() -> Rule {
        Rule {
            name: "test_rule".to_string(),
            body: vec![RuleAtom::Triple {
                subject: Term::Variable("X".to_string()),
                predicate: Term::Constant("type".to_string()),
                object: Term::Constant("Person".to_string()),
            }],
            head: vec![RuleAtom::Triple {
                subject: Term::Variable("X".to_string()),
                predicate: Term::Constant("isHuman".to_string()),
                object: Term::Constant("true".to_string()),
            }],
        }
    }

    fn create_test_fact() -> RuleAtom {
        RuleAtom::Triple {
            subject: Term::Constant("john".to_string()),
            predicate: Term::Constant("type".to_string()),
            object: Term::Constant("Person".to_string()),
        }
    }

    #[test]
    fn test_gpu_matcher_creation() {
        let matcher = GpuRuleMatcher::new();
        assert!(matcher.is_ok());
    }

    #[test]
    fn test_add_rule() -> Result<(), Box<dyn std::error::Error>> {
        let mut matcher = GpuRuleMatcher::new()?;
        let rule = create_test_rule();
        matcher.add_rule(rule);
        assert_eq!(matcher.rules.len(), 1);
        Ok(())
    }

    #[test]
    fn test_pattern_descriptor_creation() -> Result<(), Box<dyn std::error::Error>> {
        let matcher = GpuRuleMatcher::new()?;
        let atom = RuleAtom::Triple {
            subject: Term::Constant("john".to_string()),
            predicate: Term::Variable("P".to_string()),
            object: Term::Literal("value".to_string()),
        };

        let desc = matcher.build_pattern_descriptor(0, 0, &atom);
        assert_eq!(desc.pattern_type, 0); // Triple
        assert_eq!(desc.subject_type, 0); // Constant
        assert_eq!(desc.predicate_type, 1); // Variable
        assert_eq!(desc.object_type, 2); // Literal
        Ok(())
    }

    #[test]
    fn test_fnv1a_hash() -> Result<(), Box<dyn std::error::Error>> {
        let matcher = GpuRuleMatcher::new()?;
        let hash1 = matcher.fnv1a_hash(0xcbf29ce484222325, b"test");
        let hash2 = matcher.fnv1a_hash(0xcbf29ce484222325, b"test");
        let hash3 = matcher.fnv1a_hash(0xcbf29ce484222325, b"different");

        assert_eq!(hash1, hash2);
        assert_ne!(hash1, hash3);
        Ok(())
    }

    #[test]
    fn test_term_type() -> Result<(), Box<dyn std::error::Error>> {
        let matcher = GpuRuleMatcher::new()?;
        assert_eq!(matcher.term_type(&Term::Constant("x".to_string())), 0);
        assert_eq!(matcher.term_type(&Term::Variable("X".to_string())), 1);
        assert_eq!(matcher.term_type(&Term::Literal("lit".to_string())), 2);
        Ok(())
    }

    #[test]
    fn test_term_matches() -> Result<(), Box<dyn std::error::Error>> {
        let matcher = GpuRuleMatcher::new()?;

        // Variable matches anything
        assert!(matcher.term_matches(
            &Term::Variable("X".to_string()),
            &Term::Constant("john".to_string())
        ));

        // Constants must match exactly
        assert!(matcher.term_matches(
            &Term::Constant("john".to_string()),
            &Term::Constant("john".to_string())
        ));

        assert!(!matcher.term_matches(
            &Term::Constant("john".to_string()),
            &Term::Constant("mary".to_string())
        ));
        Ok(())
    }

    #[test]
    fn test_atom_matches() -> Result<(), Box<dyn std::error::Error>> {
        let matcher = GpuRuleMatcher::new()?;

        let pattern = RuleAtom::Triple {
            subject: Term::Variable("X".to_string()),
            predicate: Term::Constant("type".to_string()),
            object: Term::Constant("Person".to_string()),
        };

        let fact1 = RuleAtom::Triple {
            subject: Term::Constant("john".to_string()),
            predicate: Term::Constant("type".to_string()),
            object: Term::Constant("Person".to_string()),
        };

        let fact2 = RuleAtom::Triple {
            subject: Term::Constant("john".to_string()),
            predicate: Term::Constant("likes".to_string()),
            object: Term::Constant("coffee".to_string()),
        };

        assert!(matcher.atom_matches(&pattern, &fact1));
        assert!(!matcher.atom_matches(&pattern, &fact2));
        Ok(())
    }

    #[test]
    fn test_cpu_match_facts() -> Result<(), Box<dyn std::error::Error>> {
        let mut matcher = GpuRuleMatcher::new()?;
        matcher.add_rule(create_test_rule());

        let facts = vec![create_test_fact()];
        let matches = matcher.match_facts(&facts)?;

        assert!(!matches.is_empty());
        Ok(())
    }

    #[test]
    fn test_batch_match() -> Result<(), Box<dyn std::error::Error>> {
        let mut matcher = GpuRuleMatcher::new()?;
        matcher.add_rule(create_test_rule());

        let fact_sets = vec![vec![create_test_fact()], vec![create_test_fact()]];

        let results = matcher.batch_match(&fact_sets)?;
        assert_eq!(results.len(), 2);
        Ok(())
    }

    #[test]
    fn test_metrics_tracking() -> Result<(), Box<dyn std::error::Error>> {
        let mut matcher = GpuRuleMatcher::new()?;
        matcher.add_rule(create_test_rule());

        let facts = vec![create_test_fact()];
        matcher.match_facts(&facts)?;

        let _metrics = matcher.get_metrics();
        // Metrics tracked internally
        Ok(())
    }

    #[test]
    fn test_batch_size_setting() -> Result<(), Box<dyn std::error::Error>> {
        let mut matcher = GpuRuleMatcher::new()?;
        matcher.set_batch_size(2048);
        assert_eq!(matcher.batch_size, 2048);
        Ok(())
    }

    #[test]
    fn test_gpu_forward_chainer_creation() {
        let chainer = GpuForwardChainer::new();
        assert!(chainer.is_ok());
    }

    #[test]
    fn test_gpu_forward_chainer_add_rules() -> Result<(), Box<dyn std::error::Error>> {
        let mut chainer = GpuForwardChainer::new()?;
        chainer.add_rules(vec![create_test_rule()]);
        assert_eq!(chainer.matcher.rules.len(), 1);
        Ok(())
    }

    #[test]
    fn test_max_iterations_setting() -> Result<(), Box<dyn std::error::Error>> {
        let mut chainer = GpuForwardChainer::new()?;
        chainer.set_max_iterations(50);
        assert_eq!(chainer.max_iterations, 50);
        Ok(())
    }

    #[test]
    fn test_pattern_cache() -> Result<(), Box<dyn std::error::Error>> {
        let mut matcher = GpuRuleMatcher::new()?;
        let rule = create_test_rule();
        let body_len = rule.body.len();

        matcher.add_rule(rule);
        assert_eq!(matcher.pattern_cache.len(), body_len);
        Ok(())
    }

    #[test]
    fn test_fact_index_building() -> Result<(), Box<dyn std::error::Error>> {
        let mut matcher = GpuRuleMatcher::new()?;
        let facts = vec![create_test_fact()];

        matcher.build_fact_index(&facts);
        assert!(!matcher.fact_hashes.is_empty());
        Ok(())
    }

    #[test]
    fn test_compute_pattern_hash() -> Result<(), Box<dyn std::error::Error>> {
        let matcher = GpuRuleMatcher::new()?;
        let hash1 = matcher.compute_pattern_hash(
            &Term::Constant("john".to_string()),
            &Term::Constant("type".to_string()),
            &Term::Constant("Person".to_string()),
        );

        let hash2 = matcher.compute_pattern_hash(
            &Term::Constant("john".to_string()),
            &Term::Constant("type".to_string()),
            &Term::Constant("Person".to_string()),
        );

        assert_eq!(hash1, hash2);
        Ok(())
    }

    #[test]
    fn test_multiple_rules() -> Result<(), Box<dyn std::error::Error>> {
        let mut matcher = GpuRuleMatcher::new()?;

        let rule1 = Rule {
            name: "rule1".to_string(),
            body: vec![RuleAtom::Triple {
                subject: Term::Variable("X".to_string()),
                predicate: Term::Constant("p1".to_string()),
                object: Term::Variable("Y".to_string()),
            }],
            head: vec![RuleAtom::Triple {
                subject: Term::Variable("X".to_string()),
                predicate: Term::Constant("q1".to_string()),
                object: Term::Variable("Y".to_string()),
            }],
        };

        let rule2 = Rule {
            name: "rule2".to_string(),
            body: vec![RuleAtom::Triple {
                subject: Term::Variable("A".to_string()),
                predicate: Term::Constant("p2".to_string()),
                object: Term::Variable("B".to_string()),
            }],
            head: vec![RuleAtom::Triple {
                subject: Term::Variable("A".to_string()),
                predicate: Term::Constant("q2".to_string()),
                object: Term::Variable("B".to_string()),
            }],
        };

        matcher.add_rules(vec![rule1, rule2]);
        assert_eq!(matcher.rules.len(), 2);
        Ok(())
    }
}