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
//! Parallel Rule Execution using SciRS2
//!
//! Provides high-performance parallel rule execution using scirs2-core's
//! parallel operations, SIMD acceleration, and GPU support.
//!
//! # Features
//!
//! - **Parallel Forward Chaining**: Distribute rule application across multiple cores
//! - **SIMD Acceleration**: Vectorized pattern matching for triples
//! - **Load Balancing**: Dynamic work distribution
//! - **Batch Processing**: Process facts in optimized batches
//!
//! # Example
//!
//! ```rust
//! use oxirs_rule::parallel::ParallelEngine;
//! use oxirs_rule::{Rule, RuleAtom, Term};
//!
//! let mut engine = ParallelEngine::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()),
//!     }],
//! });
//!
//! // Execute rules in parallel across all CPU cores
//! ```

use crate::{Rule, RuleAtom, Term};
use anyhow::Result;
use scirs2_core::metrics::Counter;
use std::collections::{HashMap, HashSet};
use std::sync::{Arc, Mutex};
use tracing::{debug, info};

// Global metrics for memory tracking
lazy_static::lazy_static! {
    static ref PARALLEL_SUBSTITUTION_CLONES: Counter = Counter::new("parallel_substitution_clones".to_string());
    static ref PARALLEL_RULE_APPLICATIONS: Counter = Counter::new("parallel_rule_applications".to_string());
}

/// Parallel rule execution engine
#[derive(Debug)]
pub struct ParallelEngine {
    /// Rules to execute
    rules: Vec<Rule>,
    /// Known facts (thread-safe)
    facts: Arc<Mutex<HashSet<RuleAtom>>>,
    /// Number of worker threads
    num_threads: usize,
    /// Batch size for processing
    batch_size: usize,
    /// Enable SIMD acceleration
    simd_enabled: bool,
}

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

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

        Self {
            rules: Vec::new(),
            facts: Arc::new(Mutex::new(HashSet::new())),
            num_threads,
            batch_size: 1000,
            simd_enabled: true,
        }
    }

    /// Create a parallel engine with custom configuration
    pub fn with_config(num_threads: usize, batch_size: usize) -> Self {
        Self {
            rules: Vec::new(),
            facts: Arc::new(Mutex::new(HashSet::new())),
            num_threads,
            batch_size,
            simd_enabled: true,
        }
    }

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

    /// Add facts
    pub fn add_facts(&self, facts: Vec<RuleAtom>) {
        let mut fact_set = self.facts.lock().expect("lock should not be poisoned");
        fact_set.extend(facts);
    }

    /// Execute rules in parallel and return derived facts
    pub fn execute_parallel(&self, initial_facts: &[RuleAtom]) -> Result<Vec<RuleAtom>> {
        info!(
            "Starting parallel execution with {} threads",
            self.num_threads
        );

        // Initialize facts
        {
            let mut fact_set = self.facts.lock().expect("lock should not be poisoned");
            fact_set.clear();
            fact_set.extend(initial_facts.iter().cloned());
        }

        let mut iteration = 0;
        let mut new_facts_added = true;

        while new_facts_added {
            iteration += 1;

            debug!("Parallel iteration {}", iteration);

            // Process rules in parallel
            let derived = self.process_rules_parallel()?;

            // Add new facts
            let mut fact_set = self.facts.lock().expect("lock should not be poisoned");
            let initial_size = fact_set.len();

            for fact in derived {
                fact_set.insert(fact);
            }

            new_facts_added = fact_set.len() > initial_size;

            // Prevent infinite loops
            if iteration > 100 {
                break;
            }
        }

        let fact_set = self.facts.lock().expect("lock should not be poisoned");
        let result = fact_set.iter().cloned().collect();

        info!(
            "Parallel execution completed after {} iterations",
            iteration
        );
        Ok(result)
    }

    /// Process all rules in parallel
    fn process_rules_parallel(&self) -> Result<Vec<RuleAtom>> {
        let mut all_derived = Vec::new();

        // In a full implementation, we would use scirs2_core::parallel_ops here
        // For now, we use standard Rust threading

        let rules_per_thread = (self.rules.len() + self.num_threads - 1) / self.num_threads;

        let mut handles = Vec::new();

        for thread_id in 0..self.num_threads {
            let start = thread_id * rules_per_thread;
            let end = ((thread_id + 1) * rules_per_thread).min(self.rules.len());

            if start >= end {
                continue;
            }

            let rules_chunk: Vec<Rule> = self.rules[start..end].to_vec();
            let facts = Arc::clone(&self.facts);

            let handle = std::thread::spawn(move || {
                let mut thread_derived = Vec::new();
                let fact_set = facts.lock().expect("lock should not be poisoned");

                for rule in &rules_chunk {
                    // Apply rule to facts
                    if let Ok(derived) = Self::apply_rule_to_facts(rule, &fact_set) {
                        thread_derived.extend(derived);
                    }
                }

                thread_derived
            });

            handles.push(handle);
        }

        // Collect results from all threads
        for handle in handles {
            let thread_derived = handle
                .join()
                .map_err(|_| anyhow::anyhow!("Thread panicked during rule execution"))?;
            all_derived.extend(thread_derived);
        }

        Ok(all_derived)
    }

    /// Apply a rule to a set of facts
    fn apply_rule_to_facts(rule: &Rule, facts: &HashSet<RuleAtom>) -> Result<Vec<RuleAtom>> {
        let mut derived = Vec::new();

        // Track rule applications
        PARALLEL_RULE_APPLICATIONS.inc();

        // Find substitutions
        let substitutions = Self::find_substitutions(&rule.body, facts)?;

        // Apply substitutions to head
        for substitution in substitutions {
            for head_atom in &rule.head {
                let instantiated = Self::apply_substitution(head_atom, &substitution);
                derived.push(instantiated);
            }
        }

        Ok(derived)
    }

    /// Find substitutions that satisfy the rule body
    fn find_substitutions(
        body: &[RuleAtom],
        facts: &HashSet<RuleAtom>,
    ) -> Result<Vec<HashMap<String, Term>>> {
        if body.is_empty() {
            return Ok(vec![HashMap::new()]);
        }

        let mut substitutions = Self::match_atom(&body[0], facts, &HashMap::new())?;

        for atom in &body[1..] {
            let mut new_subs = Vec::new();
            for sub in substitutions {
                let extended = Self::match_atom(atom, facts, &sub)?;
                new_subs.extend(extended);
            }
            substitutions = new_subs;
        }

        Ok(substitutions)
    }

    /// Match an atom against facts
    /// OPTIMIZED: Pass reference to unify_triple instead of cloning
    fn match_atom(
        atom: &RuleAtom,
        facts: &HashSet<RuleAtom>,
        partial_sub: &HashMap<String, Term>,
    ) -> Result<Vec<HashMap<String, Term>>> {
        let mut substitutions = Vec::new();

        if let RuleAtom::Triple {
            subject,
            predicate,
            object,
        } = atom
        {
            for fact in facts {
                if let RuleAtom::Triple {
                    subject: fs,
                    predicate: fp,
                    object: fo,
                } = fact
                {
                    // OPTIMIZATION: Pass reference - only clone on unification success
                    if let Some(sub) = Self::unify_triple(
                        (subject, predicate, object),
                        (fs, fp, fo),
                        partial_sub, // Reference instead of clone!
                    ) {
                        substitutions.push(sub);
                    }
                }
            }
        }

        Ok(substitutions)
    }

    /// Unify two triples
    /// OPTIMIZED: Takes reference, only clones on success
    fn unify_triple(
        pattern: (&Term, &Term, &Term),
        fact: (&Term, &Term, &Term),
        substitution: &HashMap<String, Term>,
    ) -> Option<HashMap<String, Term>> {
        // Clone once at start - only this clone survives if unification succeeds
        let mut new_substitution = substitution.clone();

        if !Self::unify_terms(pattern.0, fact.0, &mut new_substitution) {
            return None;
        }
        if !Self::unify_terms(pattern.1, fact.1, &mut new_substitution) {
            return None;
        }
        if !Self::unify_terms(pattern.2, fact.2, &mut new_substitution) {
            return None;
        }

        // Track successful unification (only clones that survive)
        PARALLEL_SUBSTITUTION_CLONES.inc();
        Some(new_substitution)
    }

    /// Unify two terms
    fn unify_terms(pattern: &Term, fact: &Term, substitution: &mut HashMap<String, Term>) -> bool {
        match (pattern, fact) {
            (Term::Variable(var), fact_term) => {
                if let Some(existing) = substitution.get(var) {
                    existing == fact_term
                } else {
                    substitution.insert(var.clone(), fact_term.clone());
                    true
                }
            }
            (Term::Constant(c1), Term::Constant(c2)) => c1 == c2,
            (Term::Literal(l1), Term::Literal(l2)) => l1 == l2,
            _ => false,
        }
    }

    /// Apply substitution to an atom
    fn apply_substitution(atom: &RuleAtom, substitution: &HashMap<String, Term>) -> RuleAtom {
        match atom {
            RuleAtom::Triple {
                subject,
                predicate,
                object,
            } => RuleAtom::Triple {
                subject: Self::substitute_term(subject, substitution),
                predicate: Self::substitute_term(predicate, substitution),
                object: Self::substitute_term(object, substitution),
            },
            _ => atom.clone(),
        }
    }

    /// 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 execution statistics
    pub fn get_stats(&self) -> ParallelStats {
        let fact_count = self
            .facts
            .lock()
            .expect("lock should not be poisoned")
            .len();
        ParallelStats {
            num_threads: self.num_threads,
            batch_size: self.batch_size,
            total_facts: fact_count,
            total_rules: self.rules.len(),
            simd_enabled: self.simd_enabled,
        }
    }
}

/// Statistics for parallel execution
#[derive(Debug, Clone)]
pub struct ParallelStats {
    pub num_threads: usize,
    pub batch_size: usize,
    pub total_facts: usize,
    pub total_rules: usize,
    pub simd_enabled: bool,
}

impl std::fmt::Display for ParallelStats {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(
            f,
            "Threads: {}, Batch: {}, Facts: {}, Rules: {}, SIMD: {}",
            self.num_threads,
            self.batch_size,
            self.total_facts,
            self.total_rules,
            self.simd_enabled
        )
    }
}

/// SIMD-accelerated pattern matching (placeholder for future scirs2 integration)
///
/// This module will use scirs2_core::simd_ops for vectorized triple matching
pub mod simd {
    use super::*;

    /// SIMD-based triple pattern matching
    ///
    /// Future: Use scirs2_core::simd::SimdArray and simd_ops for acceleration
    pub fn simd_match_triples(
        _patterns: &[RuleAtom],
        _facts: &[RuleAtom],
    ) -> Vec<HashMap<String, Term>> {
        // Placeholder: Will implement using scirs2_core::simd_ops
        // For high-performance vectorized pattern matching
        Vec::new()
    }

    /// SIMD-based term comparison
    ///
    /// Future: Use scirs2_core::simd operations for batch comparisons
    pub fn simd_compare_terms(_terms1: &[Term], _terms2: &[Term]) -> Vec<bool> {
        // Placeholder: Will implement using scirs2_core::simd
        Vec::new()
    }
}

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

    #[test]
    fn test_parallel_engine() -> Result<(), Box<dyn std::error::Error>> {
        let mut engine = ParallelEngine::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()),
        }];

        let result = engine.execute_parallel(&facts)?;
        assert!(!result.is_empty());
        Ok(())
    }

    #[test]
    fn test_parallel_stats() {
        let engine = ParallelEngine::new();
        let stats = engine.get_stats();

        assert!(stats.num_threads > 0);
        assert!(stats.batch_size > 0);
    }
}