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
/// RDF-star pattern matching for nested triple queries.
///
/// Provides pattern matching over RDF-star triples, including variable binding,
/// nested pattern matching, unification, and variable substitution.
use std::collections::HashMap;

// ---------------------------------------------------------------------------
// Public types
// ---------------------------------------------------------------------------

/// A term in an RDF-star pattern — may be a variable, ground term or a
/// nested quoted-triple pattern.
#[derive(Debug, Clone, PartialEq)]
pub enum StarTerm {
    /// A SPARQL-style variable (without the `?` prefix, e.g. `"subject"`).
    Var(String),
    /// A ground IRI (e.g. `"http://example.org/knows"`).
    Iri(String),
    /// A ground literal (e.g. `"hello"` or `"42"`).
    Literal(String),
    /// A blank node identifier.
    Blank(String),
    /// A nested quoted-triple pattern.
    Nested(Box<StarPattern>),
}

/// A triple-shaped pattern where each position is a `StarTerm`.
#[derive(Debug, Clone, PartialEq)]
pub struct StarPattern {
    pub subject: StarTerm,
    pub predicate: StarTerm,
    pub object: StarTerm,
}

/// A set of variable bindings produced by matching.
pub type Bindings = HashMap<String, String>;

// ---------------------------------------------------------------------------
// Matcher
// ---------------------------------------------------------------------------

/// Stateless RDF-star pattern matcher.
pub struct StarPatternMatcher;

impl StarPatternMatcher {
    /// Try to match `term` against concrete string `value`.
    ///
    /// - If `term` is `Var(name)`:
    ///   - If `name` is already bound in `bindings` to something other than
    ///     `value`, the match fails.
    ///   - Otherwise the variable is bound to `value` and `true` is returned.
    /// - For ground terms the match succeeds iff the value equals the term's
    ///   string representation.
    /// - `Nested` terms cannot be matched against a plain string; returns `false`.
    pub fn match_term(term: &StarTerm, value: &str, bindings: &mut Bindings) -> bool {
        match term {
            StarTerm::Var(name) => {
                if let Some(existing) = bindings.get(name) {
                    existing == value
                } else {
                    bindings.insert(name.clone(), value.to_string());
                    true
                }
            }
            StarTerm::Iri(iri) => iri == value,
            StarTerm::Literal(lit) => lit == value,
            StarTerm::Blank(id) => id == value,
            StarTerm::Nested(_) => false,
        }
    }

    /// Try to match `pattern` against a concrete ground triple `(s, p, o)`.
    ///
    /// Each position is matched independently with `match_term`, accumulating
    /// bindings.  If any position fails the overall match fails and `bindings`
    /// may be partially updated (callers should clone before calling if they
    /// need rollback).
    pub fn match_pattern(
        pattern: &StarPattern,
        s: &str,
        p: &str,
        o: &str,
        bindings: &mut Bindings,
    ) -> bool {
        Self::match_term(&pattern.subject, s, bindings)
            && Self::match_term(&pattern.predicate, p, bindings)
            && Self::match_term(&pattern.object, o, bindings)
    }

    /// Match `pattern` against a nested (quoted) triple represented by the
    /// component strings `nested_s`, `nested_p`, `nested_o`.
    ///
    /// This is a convenience wrapper over `match_pattern` intended for use when
    /// the triple being matched originates from a quoted position.
    pub fn match_nested(
        pattern: &StarPattern,
        nested_s: &str,
        nested_p: &str,
        nested_o: &str,
        bindings: &mut Bindings,
    ) -> bool {
        Self::match_pattern(pattern, nested_s, nested_p, nested_o, bindings)
    }

    /// Collect all variable names that appear anywhere in `pattern`
    /// (including inside `Nested` patterns recursively).
    pub fn variables(pattern: &StarPattern) -> Vec<String> {
        let mut vars: Vec<String> = Vec::new();
        Self::collect_vars_term(&pattern.subject, &mut vars);
        Self::collect_vars_term(&pattern.predicate, &mut vars);
        Self::collect_vars_term(&pattern.object, &mut vars);
        vars
    }

    /// Return `true` when `pattern` contains no variables (all positions are
    /// ground terms or nested ground patterns).
    pub fn is_ground(pattern: &StarPattern) -> bool {
        Self::variables(pattern).is_empty()
    }

    /// Substitute variables in `term` with values from `bindings`.  Variables
    /// not present in `bindings` are returned unchanged.
    pub fn apply_bindings(term: &StarTerm, bindings: &Bindings) -> StarTerm {
        match term {
            StarTerm::Var(name) => {
                if let Some(val) = bindings.get(name) {
                    // Heuristic: IRIs look like URLs or use prefixes
                    if val.starts_with("http://")
                        || val.starts_with("https://")
                        || val.contains(':')
                    {
                        StarTerm::Iri(val.clone())
                    } else {
                        StarTerm::Literal(val.clone())
                    }
                } else {
                    term.clone()
                }
            }
            StarTerm::Nested(inner) => {
                let subst = StarPattern {
                    subject: Self::apply_bindings(&inner.subject, bindings),
                    predicate: Self::apply_bindings(&inner.predicate, bindings),
                    object: Self::apply_bindings(&inner.object, bindings),
                };
                StarTerm::Nested(Box::new(subst))
            }
            other => other.clone(),
        }
    }

    /// Attempt to unify two patterns into a common binding set.
    ///
    /// Returns `Some(bindings)` if the patterns can be unified, `None` if they
    /// contain incompatible ground terms.
    pub fn unify(pattern: &StarPattern, other: &StarPattern) -> Option<Bindings> {
        let mut bindings = Bindings::new();
        if !Self::unify_term(&pattern.subject, &other.subject, &mut bindings) {
            return None;
        }
        if !Self::unify_term(&pattern.predicate, &other.predicate, &mut bindings) {
            return None;
        }
        if !Self::unify_term(&pattern.object, &other.object, &mut bindings) {
            return None;
        }
        Some(bindings)
    }

    // -----------------------------------------------------------------------
    // Private helpers
    // -----------------------------------------------------------------------

    fn collect_vars_term(term: &StarTerm, vars: &mut Vec<String>) {
        match term {
            StarTerm::Var(name) if !vars.contains(name) => {
                vars.push(name.clone());
            }
            StarTerm::Nested(inner) => {
                Self::collect_vars_term(&inner.subject, vars);
                Self::collect_vars_term(&inner.predicate, vars);
                Self::collect_vars_term(&inner.object, vars);
            }
            _ => {}
        }
    }

    /// Unify two terms, updating `bindings`.
    fn unify_term(a: &StarTerm, b: &StarTerm, bindings: &mut Bindings) -> bool {
        match (a, b) {
            (StarTerm::Var(name_a), StarTerm::Var(name_b)) => {
                if name_a == name_b {
                    true
                } else {
                    // Bind one to the other (using a placeholder value)
                    bindings
                        .entry(name_a.clone())
                        .or_insert_with(|| format!("?{name_b}"));
                    true
                }
            }
            (StarTerm::Var(name), other) | (other, StarTerm::Var(name)) => {
                let val = Self::term_to_string(other);
                if let Some(existing) = bindings.get(name) {
                    existing == &val
                } else {
                    bindings.insert(name.clone(), val);
                    true
                }
            }
            (StarTerm::Iri(a), StarTerm::Iri(b)) => a == b,
            (StarTerm::Literal(a), StarTerm::Literal(b)) => a == b,
            (StarTerm::Blank(a), StarTerm::Blank(b)) => a == b,
            (StarTerm::Nested(pa), StarTerm::Nested(pb)) => {
                Self::unify_term(&pa.subject, &pb.subject, bindings)
                    && Self::unify_term(&pa.predicate, &pb.predicate, bindings)
                    && Self::unify_term(&pa.object, &pb.object, bindings)
            }
            _ => false,
        }
    }

    fn term_to_string(term: &StarTerm) -> String {
        match term {
            StarTerm::Iri(s) | StarTerm::Literal(s) | StarTerm::Blank(s) | StarTerm::Var(s) => {
                s.clone()
            }
            StarTerm::Nested(_) => "<nested>".to_string(),
        }
    }
}

// ===========================================================================
// Tests
// ===========================================================================

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

    fn empty() -> Bindings {
        Bindings::new()
    }

    fn ground_pattern(s: &str, p: &str, o: &str) -> StarPattern {
        StarPattern {
            subject: StarTerm::Iri(s.to_string()),
            predicate: StarTerm::Iri(p.to_string()),
            object: StarTerm::Iri(o.to_string()),
        }
    }

    fn var_pattern(s: &str, p: &str, o: &str) -> StarPattern {
        StarPattern {
            subject: StarTerm::Var(s.to_string()),
            predicate: StarTerm::Var(p.to_string()),
            object: StarTerm::Var(o.to_string()),
        }
    }

    // --- match_term ---
    #[test]
    fn test_match_term_var_unbound_binds() {
        let mut b = empty();
        assert!(StarPatternMatcher::match_term(
            &StarTerm::Var("x".to_string()),
            "value",
            &mut b
        ));
        assert_eq!(b.get("x").map(String::as_str), Some("value"));
    }

    #[test]
    fn test_match_term_var_already_bound_same() {
        let mut b: Bindings = [("x".to_string(), "value".to_string())]
            .into_iter()
            .collect();
        assert!(StarPatternMatcher::match_term(
            &StarTerm::Var("x".to_string()),
            "value",
            &mut b
        ));
    }

    #[test]
    fn test_match_term_var_already_bound_different() {
        let mut b: Bindings = [("x".to_string(), "old".to_string())].into_iter().collect();
        assert!(!StarPatternMatcher::match_term(
            &StarTerm::Var("x".to_string()),
            "new",
            &mut b
        ));
    }

    #[test]
    fn test_match_term_iri_match() {
        let mut b = empty();
        assert!(StarPatternMatcher::match_term(
            &StarTerm::Iri("http://example.org/".to_string()),
            "http://example.org/",
            &mut b
        ));
    }

    #[test]
    fn test_match_term_iri_no_match() {
        let mut b = empty();
        assert!(!StarPatternMatcher::match_term(
            &StarTerm::Iri("http://a/".to_string()),
            "http://b/",
            &mut b
        ));
    }

    #[test]
    fn test_match_term_literal_match() {
        let mut b = empty();
        assert!(StarPatternMatcher::match_term(
            &StarTerm::Literal("hello".to_string()),
            "hello",
            &mut b
        ));
    }

    #[test]
    fn test_match_term_blank_match() {
        let mut b = empty();
        assert!(StarPatternMatcher::match_term(
            &StarTerm::Blank("b1".to_string()),
            "b1",
            &mut b
        ));
    }

    #[test]
    fn test_match_term_nested_returns_false() {
        let mut b = empty();
        let nested = StarTerm::Nested(Box::new(ground_pattern("s", "p", "o")));
        assert!(!StarPatternMatcher::match_term(&nested, "anything", &mut b));
    }

    // --- match_pattern ---
    #[test]
    fn test_match_pattern_all_variables() {
        let pattern = var_pattern("s", "p", "o");
        let mut b = empty();
        assert!(StarPatternMatcher::match_pattern(
            &pattern, "S", "P", "O", &mut b
        ));
        assert_eq!(b.get("s").map(String::as_str), Some("S"));
        assert_eq!(b.get("p").map(String::as_str), Some("P"));
        assert_eq!(b.get("o").map(String::as_str), Some("O"));
    }

    #[test]
    fn test_match_pattern_all_ground_success() {
        let pattern = ground_pattern("http://s/", "http://p/", "http://o/");
        let mut b = empty();
        assert!(StarPatternMatcher::match_pattern(
            &pattern,
            "http://s/",
            "http://p/",
            "http://o/",
            &mut b
        ));
    }

    #[test]
    fn test_match_pattern_ground_fail() {
        let pattern = ground_pattern("http://s/", "http://p/", "http://o/");
        let mut b = empty();
        assert!(!StarPatternMatcher::match_pattern(
            &pattern,
            "http://s/",
            "http://p/",
            "http://X/",
            &mut b
        ));
    }

    #[test]
    fn test_match_pattern_mixed() {
        let pattern = StarPattern {
            subject: StarTerm::Iri("http://s/".to_string()),
            predicate: StarTerm::Var("p".to_string()),
            object: StarTerm::Var("o".to_string()),
        };
        let mut b = empty();
        assert!(StarPatternMatcher::match_pattern(
            &pattern,
            "http://s/",
            "http://knows/",
            "Alice",
            &mut b
        ));
        assert_eq!(b.get("p").map(String::as_str), Some("http://knows/"));
        assert_eq!(b.get("o").map(String::as_str), Some("Alice"));
    }

    // --- match_nested ---
    #[test]
    fn test_match_nested_delegates_to_match_pattern() {
        let pattern = var_pattern("s", "p", "o");
        let mut b = empty();
        assert!(StarPatternMatcher::match_nested(
            &pattern, "S", "P", "O", &mut b
        ));
        assert_eq!(b.get("s").map(String::as_str), Some("S"));
    }

    // --- variables ---
    #[test]
    fn test_variables_all_vars() {
        let pattern = var_pattern("s", "p", "o");
        let mut vars = StarPatternMatcher::variables(&pattern);
        vars.sort();
        assert_eq!(vars, vec!["o", "p", "s"]);
    }

    #[test]
    fn test_variables_no_vars() {
        let pattern = ground_pattern("s", "p", "o");
        assert!(StarPatternMatcher::variables(&pattern).is_empty());
    }

    #[test]
    fn test_variables_partial_vars() {
        let pattern = StarPattern {
            subject: StarTerm::Var("s".to_string()),
            predicate: StarTerm::Iri("http://p/".to_string()),
            object: StarTerm::Var("o".to_string()),
        };
        let mut vars = StarPatternMatcher::variables(&pattern);
        vars.sort();
        assert_eq!(vars, vec!["o", "s"]);
    }

    #[test]
    fn test_variables_nested() {
        let inner = ground_pattern("a", "b", "c");
        let pattern = StarPattern {
            subject: StarTerm::Nested(Box::new(inner)),
            predicate: StarTerm::Iri("http://p/".to_string()),
            object: StarTerm::Var("x".to_string()),
        };
        let vars = StarPatternMatcher::variables(&pattern);
        assert!(vars.contains(&"x".to_string()));
    }

    // --- is_ground ---
    #[test]
    fn test_is_ground_all_iri() {
        assert!(StarPatternMatcher::is_ground(&ground_pattern(
            "s", "p", "o"
        )));
    }

    #[test]
    fn test_is_ground_false_with_var() {
        let pattern = StarPattern {
            subject: StarTerm::Var("s".to_string()),
            predicate: StarTerm::Iri("p".to_string()),
            object: StarTerm::Iri("o".to_string()),
        };
        assert!(!StarPatternMatcher::is_ground(&pattern));
    }

    // --- apply_bindings ---
    #[test]
    fn test_apply_bindings_iri_substitution() {
        let mut b = Bindings::new();
        b.insert("x".to_string(), "http://example.org/Bob".to_string());
        let term = StarTerm::Var("x".to_string());
        let result = StarPatternMatcher::apply_bindings(&term, &b);
        assert_eq!(result, StarTerm::Iri("http://example.org/Bob".to_string()));
    }

    #[test]
    fn test_apply_bindings_literal_substitution() {
        let mut b = Bindings::new();
        b.insert("x".to_string(), "hello".to_string());
        let term = StarTerm::Var("x".to_string());
        let result = StarPatternMatcher::apply_bindings(&term, &b);
        assert_eq!(result, StarTerm::Literal("hello".to_string()));
    }

    #[test]
    fn test_apply_bindings_unbound_var_unchanged() {
        let b = Bindings::new();
        let term = StarTerm::Var("x".to_string());
        let result = StarPatternMatcher::apply_bindings(&term, &b);
        assert_eq!(result, StarTerm::Var("x".to_string()));
    }

    #[test]
    fn test_apply_bindings_iri_passthrough() {
        let b = Bindings::new();
        let term = StarTerm::Iri("http://x/".to_string());
        let result = StarPatternMatcher::apply_bindings(&term, &b);
        assert_eq!(result, StarTerm::Iri("http://x/".to_string()));
    }

    // --- unify ---
    #[test]
    fn test_unify_two_vars() {
        let a = var_pattern("s", "p", "o");
        let b = ground_pattern("S", "P", "O");
        let bindings = StarPatternMatcher::unify(&a, &b).unwrap();
        assert_eq!(bindings.get("s").map(String::as_str), Some("S"));
        assert_eq!(bindings.get("p").map(String::as_str), Some("P"));
        assert_eq!(bindings.get("o").map(String::as_str), Some("O"));
    }

    #[test]
    fn test_unify_identical_ground() {
        let a = ground_pattern("S", "P", "O");
        let b = ground_pattern("S", "P", "O");
        let bindings = StarPatternMatcher::unify(&a, &b);
        assert!(bindings.is_some());
    }

    #[test]
    fn test_unify_incompatible_ground() {
        let a = ground_pattern("S", "P", "O1");
        let b = ground_pattern("S", "P", "O2");
        assert!(StarPatternMatcher::unify(&a, &b).is_none());
    }

    #[test]
    fn test_unify_partial_vars() {
        let a = StarPattern {
            subject: StarTerm::Var("s".to_string()),
            predicate: StarTerm::Iri("http://p/".to_string()),
            object: StarTerm::Var("o".to_string()),
        };
        let b = StarPattern {
            subject: StarTerm::Iri("http://s/".to_string()),
            predicate: StarTerm::Iri("http://p/".to_string()),
            object: StarTerm::Literal("42".to_string()),
        };
        let bindings = StarPatternMatcher::unify(&a, &b).unwrap();
        assert_eq!(bindings.get("s").map(String::as_str), Some("http://s/"));
        assert_eq!(bindings.get("o").map(String::as_str), Some("42"));
    }
}