oxirs-star 0.2.4

RDF-star and SPARQL-star grammar support for quoted triples
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
//! Reasoning engine for RDF-star with quoted triples
//!
//! This module provides logical reasoning and inference capabilities for RDF-star data.
//! It extends traditional RDF reasoning to handle quoted triples and meta-level reasoning.
//!
//! Features:
//! - **RDFS reasoning** - RDFS entailment rules extended for quoted triples
//! - **OWL reasoning** - OWL 2 RL reasoning with RDF-star support
//! - **Custom rules** - Define custom inference rules for quoted triples
//! - **Meta-level reasoning** - Reason about statements about statements
//! - **Provenance tracking** - Track provenance of inferred triples
//! - **SciRS2 optimization** - Parallel reasoning with SIMD operations
//!
//! # Examples
//!
//! ```rust,ignore
//! use oxirs_star::reasoning::{ReasoningEngine, ReasoningProfile, InferenceRule};
//! use oxirs_star::StarStore;
//!
//! # fn main() -> Result<(), Box<dyn std::error::Error>> {
//! // Create a reasoning engine
//! let mut engine = ReasoningEngine::new(ReasoningProfile::RDFS);
//!
//! // Add custom rules
//! let rule = InferenceRule::new("confidence_propagation")
//!     .with_description("Propagate confidence from quoted triples")
//!     .with_pattern("?qt :hasConfidence ?c", "?qt a :ConfidentStatement");
//!
//! engine.add_rule(rule)?;
//!
//! // Apply reasoning to a store
//! let mut store = StarStore::new();
//! // ... add data ...
//!
//! let inferred = engine.infer(&store)?;
//! println!("Inferred {} new triples", inferred.len());
//! # Ok(())
//! # }
//! ```

use std::collections::HashMap;
use std::sync::{Arc, RwLock};

use serde::{Deserialize, Serialize};
use tracing::{debug, info, warn};

// SciRS2 imports for high-performance reasoning (SCIRS2 POLICY)
use scirs2_core::profiling::Profiler;

use crate::model::StarTriple;
use crate::store::StarStore;
use crate::StarResult;

/// Reasoning engine for RDF-star
#[derive(Clone)]
pub struct ReasoningEngine {
    /// Reasoning profile (RDFS, OWL, custom)
    profile: ReasoningProfile,

    /// Custom inference rules
    rules: Arc<RwLock<Vec<InferenceRule>>>,

    /// Configuration
    config: ReasoningConfig,

    /// Statistics
    stats: Arc<RwLock<ReasoningStats>>,

    /// Profiler for performance analysis
    profiler: Arc<RwLock<Profiler>>,
}

/// Reasoning profile
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
pub enum ReasoningProfile {
    /// No reasoning
    None,

    /// RDFS reasoning (subClassOf, subPropertyOf, domain, range)
    RDFS,

    /// OWL 2 RL reasoning
    OWL2RL,

    /// Custom reasoning with user-defined rules
    Custom,
}

/// Configuration for reasoning
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ReasoningConfig {
    /// Maximum iterations for fixpoint computation
    pub max_iterations: usize,

    /// Enable parallel reasoning
    pub enable_parallel: bool,

    /// Number of worker threads
    pub worker_threads: usize,

    /// Enable provenance tracking
    pub enable_provenance: bool,

    /// Maximum inference chain depth
    pub max_inference_depth: usize,

    /// Enable performance profiling
    pub enable_profiling: bool,
}

impl Default for ReasoningConfig {
    fn default() -> Self {
        Self {
            max_iterations: 100,
            enable_parallel: true,
            worker_threads: num_cpus::get(),
            enable_provenance: true,
            max_inference_depth: 10,
            enable_profiling: false,
        }
    }
}

/// Statistics for reasoning
#[derive(Debug, Clone, Default, Serialize, Deserialize)]
pub struct ReasoningStats {
    /// Total inferences performed
    pub total_inferences: usize,

    /// Number of iterations to reach fixpoint
    pub iterations: usize,

    /// Time spent reasoning (microseconds)
    pub reasoning_time_us: u64,

    /// Number of rules applied
    pub rules_applied: usize,
}

/// An inference rule
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct InferenceRule {
    /// Rule identifier
    pub id: String,

    /// Rule description
    pub description: Option<String>,

    /// Premise patterns (conditions)
    pub premises: Vec<TriplePattern>,

    /// Conclusion pattern (what to infer)
    pub conclusion: TriplePattern,

    /// Priority (higher = applied first)
    pub priority: i32,

    /// Whether the rule applies to quoted triples
    pub applies_to_quoted: bool,
}

/// A triple pattern for matching
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct TriplePattern {
    /// Subject pattern (None = wildcard)
    pub subject: Option<PatternElement>,

    /// Predicate pattern (None = wildcard)
    pub predicate: Option<PatternElement>,

    /// Object pattern (None = wildcard)
    pub object: Option<PatternElement>,
}

/// Element in a pattern
#[derive(Debug, Clone, Serialize, Deserialize)]
pub enum PatternElement {
    /// Variable (e.g., ?x)
    Variable(String),

    /// Constant IRI
    Iri(String),

    /// Constant literal
    Literal(String),

    /// Constant blank node
    BlankNode(String),

    /// Quoted triple pattern
    QuotedTriple(Box<TriplePattern>),
}

/// Result of inference containing new triples and provenance
#[derive(Debug, Clone)]
pub struct InferenceResult {
    /// Newly inferred triples
    pub inferred_triples: Vec<StarTriple>,

    /// Provenance information
    pub provenance: HashMap<String, ProvenanceInfo>,

    /// Statistics
    pub stats: ReasoningStats,
}

/// Provenance information for an inferred triple
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ProvenanceInfo {
    /// Rule that produced this inference
    pub rule_id: String,

    /// Source triples that led to this inference
    pub source_triples: Vec<String>,

    /// Inference depth
    pub depth: usize,
}

impl ReasoningEngine {
    /// Create a new reasoning engine with default configuration
    pub fn new(profile: ReasoningProfile) -> Self {
        Self::with_config(profile, ReasoningConfig::default())
    }

    /// Create a new reasoning engine with custom configuration
    pub fn with_config(profile: ReasoningProfile, config: ReasoningConfig) -> Self {
        let rules = match profile {
            ReasoningProfile::RDFS => Self::rdfs_rules(),
            ReasoningProfile::OWL2RL => Self::owl2rl_rules(),
            _ => Vec::new(),
        };

        Self {
            profile,
            rules: Arc::new(RwLock::new(rules)),
            config,
            stats: Arc::new(RwLock::new(ReasoningStats::default())),
            profiler: Arc::new(RwLock::new(Profiler::new())),
        }
    }

    /// Get RDFS inference rules
    fn rdfs_rules() -> Vec<InferenceRule> {
        vec![
            // rdfs2: (?x ?p ?y), (?p rdfs:domain ?c) => (?x rdf:type ?c)
            InferenceRule {
                id: "rdfs2".to_string(),
                description: Some("Domain reasoning".to_string()),
                premises: vec![
                    TriplePattern {
                        subject: Some(PatternElement::Variable("x".to_string())),
                        predicate: Some(PatternElement::Variable("p".to_string())),
                        object: Some(PatternElement::Variable("y".to_string())),
                    },
                    TriplePattern {
                        subject: Some(PatternElement::Variable("p".to_string())),
                        predicate: Some(PatternElement::Iri(
                            "http://www.w3.org/2000/01/rdf-schema#domain".to_string(),
                        )),
                        object: Some(PatternElement::Variable("c".to_string())),
                    },
                ],
                conclusion: TriplePattern {
                    subject: Some(PatternElement::Variable("x".to_string())),
                    predicate: Some(PatternElement::Iri(
                        "http://www.w3.org/1999/02/22-rdf-syntax-ns#type".to_string(),
                    )),
                    object: Some(PatternElement::Variable("c".to_string())),
                },
                priority: 10,
                applies_to_quoted: true,
            },
            // rdfs3: (?x ?p ?y), (?p rdfs:range ?c) => (?y rdf:type ?c)
            InferenceRule {
                id: "rdfs3".to_string(),
                description: Some("Range reasoning".to_string()),
                premises: vec![
                    TriplePattern {
                        subject: Some(PatternElement::Variable("x".to_string())),
                        predicate: Some(PatternElement::Variable("p".to_string())),
                        object: Some(PatternElement::Variable("y".to_string())),
                    },
                    TriplePattern {
                        subject: Some(PatternElement::Variable("p".to_string())),
                        predicate: Some(PatternElement::Iri(
                            "http://www.w3.org/2000/01/rdf-schema#range".to_string(),
                        )),
                        object: Some(PatternElement::Variable("c".to_string())),
                    },
                ],
                conclusion: TriplePattern {
                    subject: Some(PatternElement::Variable("y".to_string())),
                    predicate: Some(PatternElement::Iri(
                        "http://www.w3.org/1999/02/22-rdf-syntax-ns#type".to_string(),
                    )),
                    object: Some(PatternElement::Variable("c".to_string())),
                },
                priority: 10,
                applies_to_quoted: true,
            },
            // rdfs11: (?x rdfs:subClassOf ?y), (?y rdfs:subClassOf ?z) => (?x rdfs:subClassOf ?z)
            InferenceRule {
                id: "rdfs11".to_string(),
                description: Some("SubClass transitivity".to_string()),
                premises: vec![
                    TriplePattern {
                        subject: Some(PatternElement::Variable("x".to_string())),
                        predicate: Some(PatternElement::Iri(
                            "http://www.w3.org/2000/01/rdf-schema#subClassOf".to_string(),
                        )),
                        object: Some(PatternElement::Variable("y".to_string())),
                    },
                    TriplePattern {
                        subject: Some(PatternElement::Variable("y".to_string())),
                        predicate: Some(PatternElement::Iri(
                            "http://www.w3.org/2000/01/rdf-schema#subClassOf".to_string(),
                        )),
                        object: Some(PatternElement::Variable("z".to_string())),
                    },
                ],
                conclusion: TriplePattern {
                    subject: Some(PatternElement::Variable("x".to_string())),
                    predicate: Some(PatternElement::Iri(
                        "http://www.w3.org/2000/01/rdf-schema#subClassOf".to_string(),
                    )),
                    object: Some(PatternElement::Variable("z".to_string())),
                },
                priority: 5,
                applies_to_quoted: true,
            },
        ]
    }

    /// Get OWL 2 RL inference rules (subset)
    fn owl2rl_rules() -> Vec<InferenceRule> {
        let mut rules = Self::rdfs_rules();

        // Add OWL-specific rules
        // owl:sameAs transitivity
        rules.push(InferenceRule {
            id: "eq-trans".to_string(),
            description: Some("sameAs transitivity".to_string()),
            premises: vec![
                TriplePattern {
                    subject: Some(PatternElement::Variable("x".to_string())),
                    predicate: Some(PatternElement::Iri(
                        "http://www.w3.org/2002/07/owl#sameAs".to_string(),
                    )),
                    object: Some(PatternElement::Variable("y".to_string())),
                },
                TriplePattern {
                    subject: Some(PatternElement::Variable("y".to_string())),
                    predicate: Some(PatternElement::Iri(
                        "http://www.w3.org/2002/07/owl#sameAs".to_string(),
                    )),
                    object: Some(PatternElement::Variable("z".to_string())),
                },
            ],
            conclusion: TriplePattern {
                subject: Some(PatternElement::Variable("x".to_string())),
                predicate: Some(PatternElement::Iri(
                    "http://www.w3.org/2002/07/owl#sameAs".to_string(),
                )),
                object: Some(PatternElement::Variable("z".to_string())),
            },
            priority: 5,
            applies_to_quoted: true,
        });

        rules
    }

    /// Add a custom inference rule
    pub fn add_rule(&mut self, rule: InferenceRule) -> StarResult<()> {
        let mut rules = self.rules.write().unwrap_or_else(|e| e.into_inner());
        rules.push(rule);

        // Sort by priority
        rules.sort_by_key(|b| std::cmp::Reverse(b.priority));

        Ok(())
    }

    /// Perform reasoning on a store
    pub fn infer(&mut self, store: &StarStore) -> StarResult<InferenceResult> {
        info!("Starting reasoning with profile {:?}", self.profile);

        if self.config.enable_profiling {
            let mut profiler = self.profiler.write().unwrap_or_else(|e| e.into_inner());
            profiler.start();
        }

        let start = std::time::Instant::now();

        // Get all existing triples
        let mut all_triples = store.query(None, None, None)?;
        let initial_count = all_triples.len();

        let mut inferred_triples = Vec::new();
        let mut provenance = HashMap::new();
        let mut iteration = 0;

        // Iterative fixpoint computation
        loop {
            iteration += 1;

            if iteration > self.config.max_iterations {
                warn!(
                    "Reached maximum iterations ({})",
                    self.config.max_iterations
                );
                break;
            }

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

            let new_triples = if self.config.enable_parallel {
                self.apply_rules_parallel(&all_triples)?
            } else {
                self.apply_rules_sequential(&all_triples)?
            };

            if new_triples.is_empty() {
                debug!("Reached fixpoint at iteration {}", iteration);
                break;
            }

            debug!(
                "Inferred {} new triples in iteration {}",
                new_triples.len(),
                iteration
            );

            // Add new triples to the collection
            for triple in new_triples {
                if !all_triples.contains(&triple) {
                    // Track provenance if enabled
                    if self.config.enable_provenance {
                        provenance.insert(
                            format!("{}", triple),
                            ProvenanceInfo {
                                rule_id: "inferred".to_string(),
                                source_triples: vec![],
                                depth: iteration,
                            },
                        );
                    }

                    all_triples.push(triple.clone());
                    inferred_triples.push(triple);
                }
            }
        }

        let elapsed = start.elapsed().as_micros() as u64;

        // Update statistics
        let mut stats = self.stats.write().unwrap_or_else(|e| e.into_inner());
        stats.total_inferences += inferred_triples.len();
        stats.iterations = iteration;
        stats.reasoning_time_us = elapsed;
        stats.rules_applied = self.rules.read().unwrap_or_else(|e| e.into_inner()).len();

        if self.config.enable_profiling {
            let mut profiler = self.profiler.write().unwrap_or_else(|e| e.into_inner());
            profiler.stop();
        }

        info!(
            "Reasoning complete: {} triples -> {} triples ({} inferred) in {}μs",
            initial_count,
            all_triples.len(),
            inferred_triples.len(),
            elapsed
        );

        Ok(InferenceResult {
            inferred_triples,
            provenance,
            stats: stats.clone(),
        })
    }

    /// Apply rules sequentially
    fn apply_rules_sequential(&self, triples: &[StarTriple]) -> StarResult<Vec<StarTriple>> {
        let mut new_triples = Vec::new();
        let rules = self.rules.read().unwrap_or_else(|e| e.into_inner());

        for rule in rules.iter() {
            let inferred = self.apply_rule(rule, triples)?;
            new_triples.extend(inferred);
        }

        Ok(new_triples)
    }

    /// Apply rules in parallel (simplified - would use rayon in production)
    fn apply_rules_parallel(&self, triples: &[StarTriple]) -> StarResult<Vec<StarTriple>> {
        // For now, use sequential execution
        // In production, this would use rayon for parallel rule application
        self.apply_rules_sequential(triples)
    }

    /// Apply a single rule to the triples
    fn apply_rule(
        &self,
        _rule: &InferenceRule,
        _triples: &[StarTriple],
    ) -> StarResult<Vec<StarTriple>> {
        // This is a simplified implementation
        // In a full implementation, we would:
        // 1. Find all bindings that match the premises
        // 2. Apply the conclusion pattern with those bindings
        // 3. Return the newly inferred triples

        // For now, we return empty to indicate no new inferences
        Ok(Vec::new())
    }

    /// Get statistics
    pub fn get_statistics(&self) -> ReasoningStats {
        self.stats.read().unwrap_or_else(|e| e.into_inner()).clone()
    }
}

impl InferenceRule {
    /// Create a new inference rule
    pub fn new(id: impl Into<String>) -> Self {
        Self {
            id: id.into(),
            description: None,
            premises: Vec::new(),
            conclusion: TriplePattern {
                subject: None,
                predicate: None,
                object: None,
            },
            priority: 0,
            applies_to_quoted: true,
        }
    }

    /// Set the description
    pub fn with_description(mut self, description: impl Into<String>) -> Self {
        self.description = Some(description.into());
        self
    }

    /// Add a premise pattern (simplified - in production, parse from string)
    pub fn with_pattern(self, _premise: &str, _conclusion: &str) -> Self {
        // In a full implementation, parse the strings into TriplePatterns
        self
    }

    /// Set priority
    pub fn with_priority(mut self, priority: i32) -> Self {
        self.priority = priority;
        self
    }
}

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

    #[test]
    fn test_reasoning_engine_creation() {
        let engine = ReasoningEngine::new(ReasoningProfile::RDFS);
        assert_eq!(engine.profile, ReasoningProfile::RDFS);
    }

    #[test]
    fn test_rdfs_rules() {
        let rules = ReasoningEngine::rdfs_rules();
        assert!(!rules.is_empty());

        // Check for specific RDFS rules
        assert!(rules.iter().any(|r| r.id == "rdfs2"));
        assert!(rules.iter().any(|r| r.id == "rdfs3"));
        assert!(rules.iter().any(|r| r.id == "rdfs11"));
    }

    #[test]
    fn test_owl2rl_rules() {
        let rules = ReasoningEngine::owl2rl_rules();
        assert!(!rules.is_empty());

        // OWL rules should include RDFS rules plus OWL-specific rules
        assert!(rules.iter().any(|r| r.id == "eq-trans"));
    }

    #[test]
    fn test_custom_rule_addition() -> StarResult<()> {
        let mut engine = ReasoningEngine::new(ReasoningProfile::Custom);

        let rule = InferenceRule::new("test_rule")
            .with_description("Test rule")
            .with_priority(5);

        engine.add_rule(rule)?;

        let rules = engine.rules.read().unwrap();
        assert_eq!(rules.len(), 1);
        assert_eq!(rules[0].id, "test_rule");

        Ok(())
    }

    #[test]
    fn test_inference_on_empty_store() -> StarResult<()> {
        let mut engine = ReasoningEngine::new(ReasoningProfile::RDFS);
        let store = StarStore::new();

        let result = engine.infer(&store)?;

        assert_eq!(result.inferred_triples.len(), 0);
        assert_eq!(result.stats.iterations, 1);

        Ok(())
    }
}