oxirs-arq 0.2.4

Jena-style SPARQL algebra with extension points and query optimization
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
//! SPARQL-star Integration Module
//!
//! This module provides integration between oxirs-arq and oxirs-star for SPARQL 1.2 / SPARQL-star support.
//! It enables:
//! - Quoted triple patterns in SPARQL queries
//! - RDF-star specific built-in functions (TRIPLE, isTRIPLE, SUBJECT, PREDICATE, OBJECT)
//! - Conversion between ARQ algebra terms and RDF-star terms
//! - Query execution over RDF-star datasets

#[cfg(feature = "star")]
use oxirs_star::{StarConfig, StarError, StarResult, StarStore, StarTerm, StarTriple};

use crate::algebra::{Literal, Term, TriplePattern};
use anyhow::Result;
use oxirs_core::model::{NamedNode, Variable};
use std::collections::HashMap;

/// SPARQL-star executor with RDF-star support
#[cfg(feature = "star")]
pub struct SparqlStarExecutor {
    /// Underlying star store
    star_store: StarStore,
    /// Configuration for RDF-star processing
    #[allow(dead_code)]
    config: StarConfig,
}

#[cfg(feature = "star")]
impl SparqlStarExecutor {
    /// Create a new SPARQL-star executor
    pub fn new() -> Self {
        Self {
            star_store: StarStore::new(),
            config: StarConfig::default(),
        }
    }

    /// Create a SPARQL-star executor with custom configuration
    pub fn with_config(config: StarConfig) -> Self {
        Self {
            star_store: StarStore::with_config(config.clone()),
            config,
        }
    }

    /// Get the underlying star store
    pub fn store(&self) -> &StarStore {
        &self.star_store
    }

    /// Get mutable reference to the underlying star store
    pub fn store_mut(&mut self) -> &mut StarStore {
        &mut self.star_store
    }

    /// Convert ARQ Term to StarTerm
    pub fn term_to_star_term(term: &Term) -> StarResult<StarTerm> {
        match term {
            Term::Iri(iri) => StarTerm::iri(iri.as_str())
                .map_err(|e| StarError::invalid_term_type(format!("Invalid IRI: {}", e))),
            Term::Literal(lit) => {
                if let Some(ref lang) = lit.language {
                    StarTerm::literal_with_language(&lit.value, lang).map_err(|e| {
                        StarError::invalid_term_type(format!("Invalid literal: {}", e))
                    })
                } else if let Some(ref datatype) = lit.datatype {
                    StarTerm::literal_with_datatype(&lit.value, datatype.as_str()).map_err(|e| {
                        StarError::invalid_term_type(format!("Invalid literal: {}", e))
                    })
                } else {
                    StarTerm::literal(&lit.value).map_err(|e| {
                        StarError::invalid_term_type(format!("Invalid literal: {}", e))
                    })
                }
            }
            Term::BlankNode(id) => StarTerm::blank_node(id)
                .map_err(|e| StarError::invalid_term_type(format!("Invalid blank node: {}", e))),
            Term::QuotedTriple(triple) => {
                let star_triple = Self::triple_pattern_to_star_triple(triple)?;
                Ok(StarTerm::quoted_triple(star_triple))
            }
            Term::Variable(var) => {
                // Variables are not directly representable in StarTerm
                // This is a pattern matching context
                Err(StarError::invalid_term_type(format!(
                    "Cannot convert variable {} to StarTerm",
                    var
                )))
            }
            Term::PropertyPath(_) => Err(StarError::invalid_term_type(
                "Cannot convert property path to StarTerm".to_string(),
            )),
        }
    }

    /// Convert TriplePattern to StarTriple
    pub fn triple_pattern_to_star_triple(pattern: &TriplePattern) -> StarResult<StarTriple> {
        let subject = Self::term_to_star_term(&pattern.subject)?;
        let predicate = Self::term_to_star_term(&pattern.predicate)?;
        let object = Self::term_to_star_term(&pattern.object)?;

        let triple = StarTriple::new(subject, predicate, object);
        triple.validate()?;
        Ok(triple)
    }

    /// Convert StarTerm to ARQ Term
    pub fn star_term_to_term(star_term: &StarTerm) -> Result<Term> {
        match star_term {
            StarTerm::NamedNode(node) => Ok(Term::Iri(NamedNode::new(&node.iri)?)),
            StarTerm::Literal(lit) => {
                let literal = if let Some(ref lang) = lit.language {
                    Literal::with_language(lit.value.clone(), lang.clone())
                } else if let Some(ref datatype) = lit.datatype {
                    Literal::new(
                        lit.value.clone(),
                        None,
                        Some(NamedNode::new(&datatype.iri)?),
                    )
                } else {
                    Literal::new(lit.value.clone(), None, None)
                };
                Ok(Term::Literal(literal))
            }
            StarTerm::BlankNode(bn) => Ok(Term::BlankNode(bn.id.clone())),
            StarTerm::QuotedTriple(triple) => {
                let subject = Self::star_term_to_term(&triple.subject)?;
                let predicate = Self::star_term_to_term(&triple.predicate)?;
                let object = Self::star_term_to_term(&triple.object)?;
                Ok(Term::QuotedTriple(Box::new(TriplePattern::new(
                    subject, predicate, object,
                ))))
            }
            StarTerm::Variable(var) => {
                // Variables in StarTerm are not directly representable in ARQ Term
                // This shouldn't happen in practice for query results
                Err(anyhow::anyhow!(
                    "Cannot convert StarTerm::Variable {} to ARQ Term",
                    var.name
                ))
            }
        }
    }

    /// Execute a SPARQL-star query against the store
    pub fn execute_sparql_star_query(&self, query: &str) -> Result<Vec<HashMap<Variable, Term>>> {
        // Use oxirs-star's QueryExecutor
        use oxirs_star::query::{QueryExecutor, QueryParser};

        let mut executor = QueryExecutor::new(self.star_store.clone());

        // Parse the query (simplified - in production would use full SPARQL parser)
        let (select_vars, bgp) = QueryParser::parse_simple_select(query)
            .map_err(|e| anyhow::anyhow!("SPARQL-star parsing failed: {}", e))?;

        let star_results = executor
            .execute_select(&bgp, &select_vars)
            .map_err(|e| anyhow::anyhow!("SPARQL-star execution failed: {}", e))?;

        // Convert results to ARQ format
        let mut results = Vec::new();
        for star_binding in star_results {
            let mut binding = HashMap::new();
            for (var_name, star_term) in star_binding {
                let variable = Variable::new(&var_name)?;
                let term = Self::star_term_to_term(&star_term)?;
                binding.insert(variable, term);
            }
            results.push(binding);
        }

        Ok(results)
    }
}

#[cfg(feature = "star")]
impl Default for SparqlStarExecutor {
    fn default() -> Self {
        Self::new()
    }
}

/// SPARQL-star built-in functions
///
/// Note: These functions are already implemented in `crate::triple_functions` module
/// and registered via `crate::builtin::register_builtin_functions()`.
/// This module provides additional utility functions for working with quoted triples.
#[cfg(feature = "star")]
pub mod sparql_star_functions {
    use super::*;

    /// Check if a term is a quoted triple
    pub fn is_quoted_triple(term: &Term) -> bool {
        matches!(term, Term::QuotedTriple(_))
    }

    /// Extract the subject from a quoted triple term
    pub fn get_subject(term: &Term) -> Option<&Term> {
        if let Term::QuotedTriple(triple) = term {
            Some(&triple.subject)
        } else {
            None
        }
    }

    /// Extract the predicate from a quoted triple term
    pub fn get_predicate(term: &Term) -> Option<&Term> {
        if let Term::QuotedTriple(triple) = term {
            Some(&triple.predicate)
        } else {
            None
        }
    }

    /// Extract the object from a quoted triple term
    pub fn get_object(term: &Term) -> Option<&Term> {
        if let Term::QuotedTriple(triple) = term {
            Some(&triple.object)
        } else {
            None
        }
    }
}

/// Helper functions for SPARQL-star query patterns
#[cfg(feature = "star")]
pub mod pattern_matching {
    use super::*;

    /// Check if a triple pattern contains quoted triples
    pub fn has_quoted_triples(pattern: &TriplePattern) -> bool {
        matches!(pattern.subject, Term::QuotedTriple(_))
            || matches!(pattern.object, Term::QuotedTriple(_))
    }

    /// Extract all quoted triples from a term (recursively)
    pub fn extract_quoted_triples(term: &Term) -> Vec<TriplePattern> {
        let mut triples = Vec::new();
        extract_quoted_triples_recursive(term, &mut triples);
        triples
    }

    fn extract_quoted_triples_recursive(term: &Term, triples: &mut Vec<TriplePattern>) {
        if let Term::QuotedTriple(triple) = term {
            triples.push((**triple).clone());
            // Recursively extract from nested quoted triples
            extract_quoted_triples_recursive(&triple.subject, triples);
            extract_quoted_triples_recursive(&triple.predicate, triples);
            extract_quoted_triples_recursive(&triple.object, triples);
        }
    }

    /// Calculate the nesting depth of a quoted triple
    pub fn nesting_depth(term: &Term) -> usize {
        match term {
            Term::QuotedTriple(triple) => {
                let subject_depth = nesting_depth(&triple.subject);
                let predicate_depth = nesting_depth(&triple.predicate);
                let object_depth = nesting_depth(&triple.object);
                1 + subject_depth.max(predicate_depth).max(object_depth)
            }
            _ => 0,
        }
    }
}

/// Statistics and performance tracking for SPARQL-star queries
#[cfg(feature = "star")]
pub mod star_statistics {
    use serde::{Deserialize, Serialize};

    /// Statistics for SPARQL-star query execution
    #[derive(Debug, Clone, Default, Serialize, Deserialize)]
    pub struct SparqlStarStatistics {
        /// Number of quoted triple patterns matched
        pub quoted_patterns_matched: usize,
        /// Maximum nesting depth encountered
        pub max_nesting_depth: usize,
        /// Number of SPARQL-star functions evaluated
        pub star_functions_evaluated: usize,
        /// Query execution time in microseconds
        pub execution_time_us: u64,
        /// Number of results returned
        pub result_count: usize,
    }

    impl SparqlStarStatistics {
        pub fn new() -> Self {
            Self::default()
        }

        /// Record a quoted pattern match
        pub fn record_quoted_pattern(&mut self, depth: usize) {
            self.quoted_patterns_matched += 1;
            self.max_nesting_depth = self.max_nesting_depth.max(depth);
        }

        /// Record a SPARQL-star function evaluation
        pub fn record_star_function(&mut self) {
            self.star_functions_evaluated += 1;
        }

        /// Record query execution time
        pub fn record_execution_time(&mut self, duration_us: u64) {
            self.execution_time_us = duration_us;
        }

        /// Record result count
        pub fn record_results(&mut self, count: usize) {
            self.result_count = count;
        }

        /// Get average time per result (in microseconds)
        pub fn avg_time_per_result(&self) -> Option<f64> {
            if self.result_count > 0 {
                Some(self.execution_time_us as f64 / self.result_count as f64)
            } else {
                None
            }
        }
    }
}

#[cfg(test)]
#[cfg(feature = "star")]
mod tests {
    use super::*;

    #[test]
    fn test_sparql_star_executor_creation() {
        let executor = SparqlStarExecutor::new();
        assert_eq!(executor.store().len(), 0);
    }

    #[test]
    fn test_term_conversion() {
        let iri_term = Term::Iri(NamedNode::new("http://example.org/test").unwrap());
        let star_term = SparqlStarExecutor::term_to_star_term(&iri_term).unwrap();
        assert!(star_term.is_named_node());

        let converted_back = SparqlStarExecutor::star_term_to_term(&star_term).unwrap();
        assert!(matches!(converted_back, Term::Iri(_)));
    }

    #[test]
    fn test_quoted_triple_conversion() {
        let subject = Term::Iri(NamedNode::new("http://example.org/s").unwrap());
        let predicate = Term::Iri(NamedNode::new("http://example.org/p").unwrap());
        let object = Term::Iri(NamedNode::new("http://example.org/o").unwrap());

        let pattern = TriplePattern::new(subject, predicate, object);
        let star_triple = SparqlStarExecutor::triple_pattern_to_star_triple(&pattern).unwrap();

        assert!(star_triple.subject.is_named_node());
        assert!(star_triple.predicate.is_named_node());
        assert!(star_triple.object.is_named_node());
    }

    #[test]
    fn test_nesting_depth_calculation() {
        use pattern_matching::nesting_depth;

        // Simple IRI has depth 0
        let simple_term = Term::Iri(NamedNode::new("http://example.org/test").unwrap());
        assert_eq!(nesting_depth(&simple_term), 0);

        // Quoted triple has depth 1
        let inner_triple = TriplePattern::new(
            Term::Iri(NamedNode::new("http://example.org/s").unwrap()),
            Term::Iri(NamedNode::new("http://example.org/p").unwrap()),
            Term::Iri(NamedNode::new("http://example.org/o").unwrap()),
        );
        let quoted_term = Term::QuotedTriple(Box::new(inner_triple));
        assert_eq!(nesting_depth(&quoted_term), 1);

        // Nested quoted triple has depth 2
        let nested_triple = TriplePattern::new(
            quoted_term.clone(),
            Term::Iri(NamedNode::new("http://example.org/certainty").unwrap()),
            Term::Literal(Literal::new("0.9".to_string(), None, None)),
        );
        let nested_term = Term::QuotedTriple(Box::new(nested_triple));
        assert_eq!(nesting_depth(&nested_term), 2);
    }

    #[test]
    fn test_statistics_tracking() {
        use star_statistics::SparqlStarStatistics;

        let mut stats = SparqlStarStatistics::new();
        assert_eq!(stats.quoted_patterns_matched, 0);

        stats.record_quoted_pattern(1);
        stats.record_quoted_pattern(2);
        stats.record_star_function();
        stats.record_execution_time(1000);
        stats.record_results(10);

        assert_eq!(stats.quoted_patterns_matched, 2);
        assert_eq!(stats.max_nesting_depth, 2);
        assert_eq!(stats.star_functions_evaluated, 1);
        assert_eq!(stats.execution_time_us, 1000);
        assert_eq!(stats.result_count, 10);
        assert_eq!(stats.avg_time_per_result(), Some(100.0));
    }

    #[test]
    fn test_sparql_star_utility_functions() {
        use sparql_star_functions::*;

        let subject = Term::Iri(NamedNode::new("http://example.org/s").unwrap());
        let predicate = Term::Iri(NamedNode::new("http://example.org/p").unwrap());
        let object = Term::Literal(Literal::new("value".to_string(), None, None));

        let triple_pattern = TriplePattern::new(subject.clone(), predicate.clone(), object.clone());
        let quoted_term = Term::QuotedTriple(Box::new(triple_pattern));

        assert!(is_quoted_triple(&quoted_term));
        assert!(!is_quoted_triple(&subject));

        assert!(get_subject(&quoted_term).is_some());
        assert!(get_predicate(&quoted_term).is_some());
        assert!(get_object(&quoted_term).is_some());

        assert!(get_subject(&subject).is_none());
    }
}