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
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
//! RDF-star TRIPLE Functions
//!
//! This module implements RDF-star (RDF 1.2) triple term functions
//! for constructing and deconstructing quoted triples.
//!
//! Based on the RDF-star specification and Apache Jena ARQ implementation.

use crate::algebra::{Term, TriplePattern};
use crate::extensions::{CustomFunction, ExecutionContext, Value, ValueType};
use anyhow::{bail, Result};

/// Convert Value to Term for triple construction
fn value_to_term(value: &Value) -> Result<Term> {
    match value {
        Value::Iri(iri) => {
            use oxirs_core::model::NamedNode;
            let node = NamedNode::new(iri)?;
            Ok(Term::Iri(node))
        }
        Value::Literal {
            value,
            language,
            datatype,
        } => {
            use oxirs_core::model::NamedNode;
            let dt = if let Some(dt_str) = datatype {
                Some(NamedNode::new(dt_str)?)
            } else {
                None
            };
            Ok(Term::Literal(crate::algebra::Literal {
                value: value.clone(),
                language: language.clone(),
                datatype: dt,
            }))
        }
        Value::BlankNode(id) => Ok(Term::BlankNode(id.clone())),
        _ => bail!("Cannot convert {} to RDF term", value.type_name()),
    }
}

/// Convert Term to Value for function results
#[allow(dead_code)]
fn term_to_value(term: &Term) -> Value {
    match term {
        Term::Iri(iri) => Value::Iri(iri.as_str().to_string()),
        Term::Literal(lit) => Value::Literal {
            value: lit.value.clone(),
            language: lit.language.clone(),
            datatype: lit.datatype.as_ref().map(|dt| dt.as_str().to_string()),
        },
        Term::BlankNode(id) => Value::BlankNode(id.clone()),
        Term::Variable(_v) => {
            // Variables should not appear in concrete triples
            // Return as a string representation
            Value::String(format!("{}", term))
        }
        Term::QuotedTriple(triple) => {
            // Return as a special QuotedTriple value
            // For now, represent as a string
            Value::String(format!(
                "<<{} {} {}>>",
                triple.subject, triple.predicate, triple.object
            ))
        }
        Term::PropertyPath(path) => Value::String(format!("{}", path)),
    }
}

// ============================================================================
// TRIPLE Function - Construct a quoted triple
// ============================================================================

/// TRIPLE(?s, ?p, ?o) - Construct a quoted triple from three terms
///
/// Creates an RDF-star quoted triple (embedded triple) from subject,
/// predicate, and object terms.
///
/// # Examples
/// ```sparql
/// TRIPLE(?s, ?p, ?o) → <<s p o>>
/// ```
#[derive(Debug, Clone)]
pub struct TripleFunction;

impl CustomFunction for TripleFunction {
    fn name(&self) -> &str {
        "http://www.w3.org/2001/sw/DataAccess/rq23#triple"
    }

    fn arity(&self) -> Option<usize> {
        Some(3)
    }

    fn parameter_types(&self) -> Vec<ValueType> {
        vec![
            ValueType::Union(vec![
                ValueType::Iri,
                ValueType::BlankNode,
                ValueType::Custom("QuotedTriple".to_string()),
            ]),
            ValueType::Iri,
            ValueType::Union(vec![
                ValueType::Iri,
                ValueType::BlankNode,
                ValueType::Literal,
                ValueType::Custom("QuotedTriple".to_string()),
            ]),
        ]
    }

    fn return_type(&self) -> ValueType {
        ValueType::Custom("QuotedTriple".to_string())
    }

    fn documentation(&self) -> &str {
        "Constructs a quoted triple (RDF-star) from subject, predicate, and object"
    }

    fn clone_function(&self) -> Box<dyn CustomFunction> {
        Box::new(self.clone())
    }

    fn execute(&self, args: &[Value], _context: &ExecutionContext) -> Result<Value> {
        if args.len() != 3 {
            bail!("TRIPLE() requires exactly 3 arguments (subject, predicate, object)");
        }

        let subject = value_to_term(&args[0])?;
        let predicate = value_to_term(&args[1])?;
        let object = value_to_term(&args[2])?;

        // Validate subject, predicate, object constraints
        match &subject {
            Term::Literal(_) => bail!("TRIPLE subject cannot be a literal"),
            Term::PropertyPath(_) => bail!("TRIPLE subject cannot be a property path"),
            _ => {}
        }

        match &predicate {
            Term::Literal(_) => bail!("TRIPLE predicate cannot be a literal"),
            Term::BlankNode(_) => bail!("TRIPLE predicate cannot be a blank node"),
            Term::QuotedTriple(_) => bail!("TRIPLE predicate cannot be a quoted triple"),
            Term::PropertyPath(_) => bail!("TRIPLE predicate cannot be a property path"),
            _ => {}
        }

        // Create quoted triple
        let triple = TriplePattern {
            subject,
            predicate,
            object,
        };

        // Return as formatted string (in a full implementation, this would be a proper QuotedTriple value)
        Ok(Value::String(format!(
            "<<{} {} {}>>",
            triple.subject, triple.predicate, triple.object
        )))
    }
}

// ============================================================================
// SUBJECT Function - Extract subject from quoted triple
// ============================================================================

/// SUBJECT(?triple) - Extract subject from a quoted triple
///
/// Returns the subject component of an RDF-star quoted triple.
///
/// # Examples
/// ```sparql
/// SUBJECT(<<:s :p :o>>) → :s
/// ```
#[derive(Debug, Clone)]
pub struct SubjectFunction;

impl CustomFunction for SubjectFunction {
    fn name(&self) -> &str {
        "http://www.w3.org/2001/sw/DataAccess/rq23#subject"
    }

    fn arity(&self) -> Option<usize> {
        Some(1)
    }

    fn parameter_types(&self) -> Vec<ValueType> {
        vec![ValueType::Custom("QuotedTriple".to_string())]
    }

    fn return_type(&self) -> ValueType {
        ValueType::Union(vec![
            ValueType::Iri,
            ValueType::BlankNode,
            ValueType::Custom("QuotedTriple".to_string()),
        ])
    }

    fn documentation(&self) -> &str {
        "Extracts the subject from a quoted triple (RDF-star)"
    }

    fn clone_function(&self) -> Box<dyn CustomFunction> {
        Box::new(self.clone())
    }

    fn execute(&self, args: &[Value], _context: &ExecutionContext) -> Result<Value> {
        if args.len() != 1 {
            bail!("SUBJECT() requires exactly 1 argument");
        }

        // In a full implementation, we would parse the quoted triple from the value
        // For now, we'll check for a string representation
        match &args[0] {
            Value::String(s) if s.starts_with("<<") && s.ends_with(">>") => {
                // Simple parsing for demonstration
                // Real implementation would use proper triple term parsing
                let inner = &s[2..s.len() - 2];
                let parts: Vec<&str> = inner.split_whitespace().collect();
                if parts.len() >= 3 {
                    Ok(Value::Iri(parts[0].to_string()))
                } else {
                    bail!("Invalid quoted triple format");
                }
            }
            _ => bail!("SUBJECT() requires a quoted triple argument"),
        }
    }
}

// ============================================================================
// PREDICATE Function - Extract predicate from quoted triple
// ============================================================================

/// PREDICATE(?triple) - Extract predicate from a quoted triple
///
/// Returns the predicate component of an RDF-star quoted triple.
///
/// # Examples
/// ```sparql
/// PREDICATE(<<:s :p :o>>) → :p
/// ```
#[derive(Debug, Clone)]
pub struct PredicateFunction;

impl CustomFunction for PredicateFunction {
    fn name(&self) -> &str {
        "http://www.w3.org/2001/sw/DataAccess/rq23#predicate"
    }

    fn arity(&self) -> Option<usize> {
        Some(1)
    }

    fn parameter_types(&self) -> Vec<ValueType> {
        vec![ValueType::Custom("QuotedTriple".to_string())]
    }

    fn return_type(&self) -> ValueType {
        ValueType::Iri
    }

    fn documentation(&self) -> &str {
        "Extracts the predicate from a quoted triple (RDF-star)"
    }

    fn clone_function(&self) -> Box<dyn CustomFunction> {
        Box::new(self.clone())
    }

    fn execute(&self, args: &[Value], _context: &ExecutionContext) -> Result<Value> {
        if args.len() != 1 {
            bail!("PREDICATE() requires exactly 1 argument");
        }

        match &args[0] {
            Value::String(s) if s.starts_with("<<") && s.ends_with(">>") => {
                let inner = &s[2..s.len() - 2];
                let parts: Vec<&str> = inner.split_whitespace().collect();
                if parts.len() >= 3 {
                    Ok(Value::Iri(parts[1].to_string()))
                } else {
                    bail!("Invalid quoted triple format");
                }
            }
            _ => bail!("PREDICATE() requires a quoted triple argument"),
        }
    }
}

// ============================================================================
// OBJECT Function - Extract object from quoted triple
// ============================================================================

/// OBJECT(?triple) - Extract object from a quoted triple
///
/// Returns the object component of an RDF-star quoted triple.
///
/// # Examples
/// ```sparql
/// OBJECT(<<:s :p :o>>) → :o
/// ```
#[derive(Debug, Clone)]
pub struct ObjectFunction;

impl CustomFunction for ObjectFunction {
    fn name(&self) -> &str {
        "http://www.w3.org/2001/sw/DataAccess/rq23#object"
    }

    fn arity(&self) -> Option<usize> {
        Some(1)
    }

    fn parameter_types(&self) -> Vec<ValueType> {
        vec![ValueType::Custom("QuotedTriple".to_string())]
    }

    fn return_type(&self) -> ValueType {
        ValueType::Union(vec![
            ValueType::Iri,
            ValueType::BlankNode,
            ValueType::Literal,
            ValueType::Custom("QuotedTriple".to_string()),
        ])
    }

    fn documentation(&self) -> &str {
        "Extracts the object from a quoted triple (RDF-star)"
    }

    fn clone_function(&self) -> Box<dyn CustomFunction> {
        Box::new(self.clone())
    }

    fn execute(&self, args: &[Value], _context: &ExecutionContext) -> Result<Value> {
        if args.len() != 1 {
            bail!("OBJECT() requires exactly 1 argument");
        }

        match &args[0] {
            Value::String(s) if s.starts_with("<<") && s.ends_with(">>") => {
                let inner = &s[2..s.len() - 2];
                let parts: Vec<&str> = inner.split_whitespace().collect();
                if parts.len() >= 3 {
                    // Join remaining parts for the object (in case it has spaces)
                    Ok(Value::Iri(parts[2..].join(" ")))
                } else {
                    bail!("Invalid quoted triple format");
                }
            }
            _ => bail!("OBJECT() requires a quoted triple argument"),
        }
    }
}

// ============================================================================
// isTRIPLE Function - Check if value is a quoted triple
// ============================================================================

/// isTRIPLE(?x) - Check if a value is a quoted triple
///
/// Returns true if the argument is an RDF-star quoted triple,
/// false otherwise.
///
/// # Examples
/// ```sparql
/// isTRIPLE(<<:s :p :o>>) → true
/// isTRIPLE(:s) → false
/// ```
#[derive(Debug, Clone)]
pub struct IsTripleFunction;

impl CustomFunction for IsTripleFunction {
    fn name(&self) -> &str {
        "http://www.w3.org/2001/sw/DataAccess/rq23#isTRIPLE"
    }

    fn arity(&self) -> Option<usize> {
        Some(1)
    }

    fn parameter_types(&self) -> Vec<ValueType> {
        vec![ValueType::Union(vec![
            ValueType::Iri,
            ValueType::BlankNode,
            ValueType::Literal,
            ValueType::Custom("QuotedTriple".to_string()),
        ])]
    }

    fn return_type(&self) -> ValueType {
        ValueType::Boolean
    }

    fn documentation(&self) -> &str {
        "Returns true if the argument is a quoted triple (RDF-star), false otherwise"
    }

    fn clone_function(&self) -> Box<dyn CustomFunction> {
        Box::new(self.clone())
    }

    fn execute(&self, args: &[Value], _context: &ExecutionContext) -> Result<Value> {
        if args.len() != 1 {
            bail!("isTRIPLE() requires exactly 1 argument");
        }

        let is_triple = match &args[0] {
            Value::String(s) => s.starts_with("<<") && s.ends_with(">>"),
            _ => false,
        };

        Ok(Value::Boolean(is_triple))
    }
}

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

    fn create_test_context() -> ExecutionContext {
        ExecutionContext {
            variables: HashMap::new(),
            namespaces: HashMap::new(),
            base_iri: None,
            dataset_context: None,
            query_time: chrono::Utc::now(),
            optimization_level: crate::extensions::OptimizationLevel::None,
            memory_limit: None,
            time_limit: None,
        }
    }

    #[test]
    fn test_triple_function_construction() {
        let func = TripleFunction;
        let ctx = create_test_context();

        let subject = Value::Iri("http://example.org/subject".to_string());
        let predicate = Value::Iri("http://example.org/predicate".to_string());
        let object = Value::Iri("http://example.org/object".to_string());

        let result = func.execute(&[subject, predicate, object], &ctx);
        assert!(result.is_ok());

        if let Ok(Value::String(s)) = result {
            assert!(s.starts_with("<<"));
            assert!(s.ends_with(">>"));
        } else {
            panic!("Expected string result");
        }
    }

    #[test]
    fn test_triple_function_invalid_subject() {
        let func = TripleFunction;
        let ctx = create_test_context();

        // Literal as subject should fail
        let subject = Value::Literal {
            value: "literal".to_string(),
            language: None,
            datatype: None,
        };
        let predicate = Value::Iri("http://example.org/predicate".to_string());
        let object = Value::Iri("http://example.org/object".to_string());

        let result = func.execute(&[subject, predicate, object], &ctx);
        assert!(result.is_err());
    }

    #[test]
    fn test_subject_function() {
        let func = SubjectFunction;
        let ctx = create_test_context();

        let triple = Value::String(
            "<<http://example.org/s http://example.org/p http://example.org/o>>".to_string(),
        );

        let result = func.execute(&[triple], &ctx);
        assert!(result.is_ok());

        if let Ok(Value::Iri(iri)) = result {
            assert_eq!(iri, "http://example.org/s");
        } else {
            panic!("Expected IRI result");
        }
    }

    #[test]
    fn test_predicate_function() {
        let func = PredicateFunction;
        let ctx = create_test_context();

        let triple = Value::String(
            "<<http://example.org/s http://example.org/p http://example.org/o>>".to_string(),
        );

        let result = func.execute(&[triple], &ctx);
        assert!(result.is_ok());

        if let Ok(Value::Iri(iri)) = result {
            assert_eq!(iri, "http://example.org/p");
        } else {
            panic!("Expected IRI result");
        }
    }

    #[test]
    fn test_object_function() {
        let func = ObjectFunction;
        let ctx = create_test_context();

        let triple = Value::String(
            "<<http://example.org/s http://example.org/p http://example.org/o>>".to_string(),
        );

        let result = func.execute(&[triple], &ctx);
        assert!(result.is_ok());

        if let Ok(Value::Iri(iri)) = result {
            assert_eq!(iri, "http://example.org/o");
        } else {
            panic!("Expected IRI result");
        }
    }

    #[test]
    fn test_is_triple_function() {
        let func = IsTripleFunction;
        let ctx = create_test_context();

        // Test with quoted triple
        let triple = Value::String(
            "<<http://example.org/s http://example.org/p http://example.org/o>>".to_string(),
        );
        let result = func.execute(&[triple], &ctx).unwrap();
        assert_eq!(result, Value::Boolean(true));

        // Test with non-triple
        let non_triple = Value::Iri("http://example.org/resource".to_string());
        let result = func.execute(&[non_triple], &ctx).unwrap();
        assert_eq!(result, Value::Boolean(false));
    }

    #[test]
    fn test_triple_function_arity() {
        let func = TripleFunction;
        assert_eq!(func.arity(), Some(3));
        assert_eq!(
            func.name(),
            "http://www.w3.org/2001/sw/DataAccess/rq23#triple"
        );
    }

    #[test]
    fn test_accessor_function_arities() {
        assert_eq!(SubjectFunction.arity(), Some(1));
        assert_eq!(PredicateFunction.arity(), Some(1));
        assert_eq!(ObjectFunction.arity(), Some(1));
        assert_eq!(IsTripleFunction.arity(), Some(1));
    }
}