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
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
//! SPARQL-star query pattern matching.
//!
//! This module provides pattern types and an evaluator for SPARQL-star query
//! patterns over RDF-star graphs:
//!
//! - [`EmbeddedTriplePattern`]: represents `<< s p o >>` in a WHERE clause.
//! - [`AnnotationPattern`]: represents the `?s |p| ?o` annotation syntax.
//! - [`SparqlStarEvaluator`]: evaluates patterns over a graph slice.
//!
//! # Example
//!
//! ```
//! use oxirs_star::sparql::{EmbeddedTriplePattern, SparqlStarEvaluator};
//! use oxirs_star::model::StarTerm;
//!
//! let pattern = EmbeddedTriplePattern::new(
//!     StarTerm::variable("s").unwrap(),
//!     StarTerm::iri("http://example.org/age").unwrap(),
//!     StarTerm::variable("o").unwrap(),
//! );
//!
//! let evaluator = SparqlStarEvaluator::new();
//! // (graph would contain RDF-star triples here)
//! let bindings = evaluator.evaluate_embedded_pattern(&pattern, &[]);
//! assert!(bindings.is_empty());
//! ```

use std::collections::HashMap;

use crate::model::{StarTerm, StarTriple};

/// A variable binding map: maps variable names to [`StarTerm`] values.
pub type Binding = HashMap<String, StarTerm>;

// ============================================================================
// EmbeddedTriplePattern
// ============================================================================

/// A pattern for matching embedded (quoted) triples in SPARQL-star queries.
///
/// Each position (subject, predicate, object) can be either a concrete
/// [`StarTerm`] or a variable (`StarTerm::Variable`). Variables bind to any
/// matching term; concrete terms must match exactly.
///
/// Corresponds to the `<< s p o >>` construct in SPARQL-star WHERE clauses.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct EmbeddedTriplePattern {
    /// Subject pattern (variable or concrete term).
    pub subject: StarTerm,
    /// Predicate pattern (variable or concrete term).
    pub predicate: StarTerm,
    /// Object pattern (variable or concrete term).
    pub object: StarTerm,
}

impl EmbeddedTriplePattern {
    /// Create a new embedded triple pattern.
    pub fn new(subject: StarTerm, predicate: StarTerm, object: StarTerm) -> Self {
        Self {
            subject,
            predicate,
            object,
        }
    }

    /// Returns `true` if all positions are variables (completely unbound pattern).
    pub fn is_fully_unbound(&self) -> bool {
        self.subject.is_variable() && self.predicate.is_variable() && self.object.is_variable()
    }

    /// Returns `true` if all positions are concrete terms (no variables).
    pub fn is_ground(&self) -> bool {
        !self.subject.is_variable() && !self.predicate.is_variable() && !self.object.is_variable()
    }

    /// Attempt to unify this pattern against `triple`, returning a
    /// [`Binding`] on success or `None` on failure.
    ///
    /// Unification rules:
    /// - A variable in the pattern binds to the corresponding triple term.
    /// - A concrete term in the pattern must equal the corresponding triple term.
    /// - If the same variable appears in multiple positions, all bound values
    ///   must be equal (occurs check).
    pub fn unify(&self, triple: &StarTriple) -> Option<Binding> {
        let mut bindings = Binding::new();
        unify_term(&self.subject, &triple.subject, &mut bindings)?;
        unify_term(&self.predicate, &triple.predicate, &mut bindings)?;
        unify_term(&self.object, &triple.object, &mut bindings)?;
        Some(bindings)
    }

    /// Match this pattern against a quoted triple embedded inside another triple.
    ///
    /// Given a term that is expected to be a [`StarTerm::QuotedTriple`], attempt
    /// to unify the inner triple with this pattern.
    pub fn unify_embedded(&self, term: &StarTerm) -> Option<Binding> {
        if let StarTerm::QuotedTriple(inner) = term {
            self.unify(inner)
        } else {
            None
        }
    }
}

// ============================================================================
// AnnotationPattern
// ============================================================================

/// An annotation pattern matching the SPARQL-star annotation syntax:
///
/// ```sparql
/// ?s |ex:certainty| ?confidence
/// ```
///
/// This matches triples where the subject is a quoted triple `<< ?s_inner ?p_inner
/// ?o_inner >>` that is annotated with the annotation predicate and object.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct AnnotationPattern {
    /// Pattern for the subject position of the inner (embedded) triple.
    pub embedded_subject: StarTerm,
    /// Pattern for the predicate position of the inner (embedded) triple.
    pub embedded_predicate: StarTerm,
    /// Pattern for the object position of the inner (embedded) triple.
    pub embedded_object: StarTerm,
    /// The annotation predicate (may be a variable).
    pub annotation_predicate: StarTerm,
    /// The annotation object (may be a variable).
    pub annotation_object: StarTerm,
}

impl AnnotationPattern {
    /// Create a new annotation pattern.
    pub fn new(
        embedded_subject: StarTerm,
        embedded_predicate: StarTerm,
        embedded_object: StarTerm,
        annotation_predicate: StarTerm,
        annotation_object: StarTerm,
    ) -> Self {
        Self {
            embedded_subject,
            embedded_predicate,
            embedded_object,
            annotation_predicate,
            annotation_object,
        }
    }

    /// Attempt to match a graph triple against this annotation pattern.
    ///
    /// The outer triple must have a [`StarTerm::QuotedTriple`] as its subject.
    /// The embedded triple pattern is unified against the inner triple, and the
    /// annotation predicate and object are unified against the outer triple's
    /// predicate and object.
    pub fn match_triple(&self, outer: &StarTriple) -> Option<Binding> {
        // The outer triple's subject must be a quoted triple
        let inner = match &outer.subject {
            StarTerm::QuotedTriple(inner) => inner.as_ref(),
            _ => return None,
        };

        let mut bindings = Binding::new();

        // Unify embedded triple pattern
        unify_term(&self.embedded_subject, &inner.subject, &mut bindings)?;
        unify_term(&self.embedded_predicate, &inner.predicate, &mut bindings)?;
        unify_term(&self.embedded_object, &inner.object, &mut bindings)?;

        // Unify annotation predicate and object
        unify_term(&self.annotation_predicate, &outer.predicate, &mut bindings)?;
        unify_term(&self.annotation_object, &outer.object, &mut bindings)?;

        Some(bindings)
    }
}

// ============================================================================
// Unification helper
// ============================================================================

/// Unify `pattern_term` against `data_term`, merging results into `bindings`.
///
/// Returns `None` if unification fails (type mismatch or occurs check failure).
fn unify_term(pattern_term: &StarTerm, data_term: &StarTerm, bindings: &mut Binding) -> Option<()> {
    match pattern_term {
        StarTerm::Variable(var) => {
            // Check occurs: if already bound, the bound value must match
            if let Some(existing) = bindings.get(&var.name) {
                if existing != data_term {
                    return None;
                }
            } else {
                bindings.insert(var.name.clone(), data_term.clone());
            }
            Some(())
        }
        StarTerm::QuotedTriple(pattern_inner) => {
            // Match a nested quoted triple pattern
            if let StarTerm::QuotedTriple(data_inner) = data_term {
                unify_term(&pattern_inner.subject, &data_inner.subject, bindings)?;
                unify_term(&pattern_inner.predicate, &data_inner.predicate, bindings)?;
                unify_term(&pattern_inner.object, &data_inner.object, bindings)?;
                Some(())
            } else {
                None
            }
        }
        concrete => {
            // Concrete term: must equal the data term exactly
            if concrete == data_term {
                Some(())
            } else {
                None
            }
        }
    }
}

// ============================================================================
// SparqlStarEvaluator
// ============================================================================

/// Evaluates SPARQL-star patterns over RDF-star graphs.
///
/// Provides high-level evaluation functions that return all matching
/// [`Binding`]s.
pub struct SparqlStarEvaluator {
    /// Whether to also recurse into nested quoted triples during evaluation.
    pub recurse_nested: bool,
}

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

impl SparqlStarEvaluator {
    /// Create a new evaluator (recursion disabled by default).
    pub fn new() -> Self {
        Self {
            recurse_nested: false,
        }
    }

    /// Create an evaluator that also searches inside nested quoted triples.
    pub fn with_recursion() -> Self {
        Self {
            recurse_nested: true,
        }
    }

    // ------------------------------------------------------------------
    // evaluate_embedded_pattern
    // ------------------------------------------------------------------

    /// Find all triples in `graph` whose *subject* is a quoted triple matching
    /// `pattern`, and return the variable bindings for each match.
    ///
    /// For every outer triple `<< s p o >> q_pred q_obj`, attempt to unify the
    /// embedded `<< s p o >>` against `pattern`.  If the outer triple's subject
    /// is not a quoted triple it is skipped.
    pub fn evaluate_embedded_pattern(
        &self,
        pattern: &EmbeddedTriplePattern,
        graph: &[StarTriple],
    ) -> Vec<Binding> {
        let mut results = Vec::new();

        for triple in graph {
            // Try to match quoted subject
            if let Some(bindings) = pattern.unify_embedded(&triple.subject) {
                results.push(bindings);
            }

            // Try to match quoted object
            if let Some(bindings) = pattern.unify_embedded(&triple.object) {
                results.push(bindings);
            }

            // Optionally recurse into nested quoted triples
            if self.recurse_nested {
                self.recurse_embedded(&triple.subject, pattern, &mut results);
                self.recurse_embedded(&triple.object, pattern, &mut results);
            }
        }

        results
    }

    fn recurse_embedded(
        &self,
        term: &StarTerm,
        pattern: &EmbeddedTriplePattern,
        results: &mut Vec<Binding>,
    ) {
        if let StarTerm::QuotedTriple(inner) = term {
            if let Some(bindings) = pattern.unify(inner) {
                results.push(bindings);
            }
            // Go deeper
            self.recurse_embedded(&inner.subject, pattern, results);
            self.recurse_embedded(&inner.predicate, pattern, results);
            self.recurse_embedded(&inner.object, pattern, results);
        }
    }

    // ------------------------------------------------------------------
    // evaluate_annotation_pattern
    // ------------------------------------------------------------------

    /// Find all triples in `graph` that match `pattern` (annotation syntax).
    ///
    /// An annotation triple has a quoted-triple subject and carries annotation
    /// metadata in its predicate and object positions.
    pub fn evaluate_annotation_pattern(
        &self,
        pattern: &AnnotationPattern,
        graph: &[StarTriple],
    ) -> Vec<Binding> {
        let mut results = Vec::new();

        for triple in graph {
            if let Some(bindings) = pattern.match_triple(triple) {
                results.push(bindings);
            }
        }

        results
    }

    // ------------------------------------------------------------------
    // evaluate_direct_pattern (bonus: match non-embedded triples)
    // ------------------------------------------------------------------

    /// Find all triples in `graph` that directly match `pattern` (treating each
    /// graph triple as a potential match, not requiring embedded subjects).
    ///
    /// This is analogous to a basic SPARQL triple pattern.
    pub fn evaluate_direct_pattern(
        &self,
        pattern: &EmbeddedTriplePattern,
        graph: &[StarTriple],
    ) -> Vec<Binding> {
        let mut results = Vec::new();

        for triple in graph {
            if let Some(bindings) = pattern.unify(triple) {
                results.push(bindings);
            }
        }

        results
    }
}

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

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

    fn iri(s: &str) -> StarTerm {
        StarTerm::iri(s).expect("iri")
    }
    fn lit(s: &str) -> StarTerm {
        StarTerm::literal(s).expect("lit")
    }
    fn var(name: &str) -> StarTerm {
        StarTerm::variable(name).expect("var")
    }

    fn simple_triple(s: &str, p: &str, o: &str) -> StarTriple {
        StarTriple::new(iri(s), iri(p), iri(o))
    }

    fn quoted_triple(s: &str, p: &str, o: &str) -> StarTerm {
        StarTerm::quoted_triple(simple_triple(s, p, o))
    }

    // -------------------------------------------------------------------
    // EmbeddedTriplePattern unification
    // -------------------------------------------------------------------

    #[test]
    fn test_unify_fully_variable_pattern() {
        let pattern = EmbeddedTriplePattern::new(var("s"), var("p"), var("o"));
        let triple = simple_triple("http://a", "http://b", "http://c");
        let bindings = pattern.unify(&triple).expect("should match");
        assert_eq!(bindings.get("s"), Some(&iri("http://a")));
        assert_eq!(bindings.get("p"), Some(&iri("http://b")));
        assert_eq!(bindings.get("o"), Some(&iri("http://c")));
    }

    #[test]
    fn test_unify_concrete_predicate_match() {
        let pattern = EmbeddedTriplePattern::new(var("s"), iri("http://p"), var("o"));
        let triple = simple_triple("http://a", "http://p", "http://c");
        assert!(pattern.unify(&triple).is_some());
    }

    #[test]
    fn test_unify_concrete_predicate_mismatch() {
        let pattern = EmbeddedTriplePattern::new(var("s"), iri("http://x"), var("o"));
        let triple = simple_triple("http://a", "http://p", "http://c");
        assert!(pattern.unify(&triple).is_none());
    }

    #[test]
    fn test_unify_all_concrete_match() {
        let pattern = EmbeddedTriplePattern::new(iri("http://a"), iri("http://p"), iri("http://c"));
        let triple = simple_triple("http://a", "http://p", "http://c");
        assert!(pattern.unify(&triple).is_some());
    }

    #[test]
    fn test_unify_all_concrete_no_match() {
        let pattern =
            EmbeddedTriplePattern::new(iri("http://a"), iri("http://p"), iri("http://DIFFERENT"));
        let triple = simple_triple("http://a", "http://p", "http://c");
        assert!(pattern.unify(&triple).is_none());
    }

    #[test]
    fn test_unify_literal_object() {
        let pattern = EmbeddedTriplePattern::new(var("s"), var("p"), lit("hello"));
        let triple = StarTriple::new(iri("http://a"), iri("http://p"), lit("hello"));
        assert!(pattern.unify(&triple).is_some());
    }

    #[test]
    fn test_unify_literal_mismatch() {
        let pattern = EmbeddedTriplePattern::new(var("s"), var("p"), lit("hello"));
        let triple = StarTriple::new(iri("http://a"), iri("http://p"), lit("world"));
        assert!(pattern.unify(&triple).is_none());
    }

    #[test]
    fn test_unify_same_variable_in_subject_and_object_consistent() {
        // ?s p ?s — subject and object must be the same value
        let pattern = EmbeddedTriplePattern::new(var("x"), iri("http://p"), var("x"));
        let same = StarTriple::new(iri("http://a"), iri("http://p"), iri("http://a"));
        assert!(pattern.unify(&same).is_some());

        let different = StarTriple::new(iri("http://a"), iri("http://p"), iri("http://b"));
        assert!(pattern.unify(&different).is_none());
    }

    #[test]
    fn test_embedded_triple_pattern_is_ground() {
        let ground = EmbeddedTriplePattern::new(iri("http://a"), iri("http://p"), iri("http://o"));
        assert!(ground.is_ground());
        assert!(!ground.is_fully_unbound());
    }

    #[test]
    fn test_embedded_triple_pattern_is_fully_unbound() {
        let unbound = EmbeddedTriplePattern::new(var("s"), var("p"), var("o"));
        assert!(unbound.is_fully_unbound());
        assert!(!unbound.is_ground());
    }

    // -------------------------------------------------------------------
    // EmbeddedTriplePattern::unify_embedded
    // -------------------------------------------------------------------

    #[test]
    fn test_unify_embedded_matches_quoted_subject() {
        let pattern = EmbeddedTriplePattern::new(var("s"), var("p"), var("o"));
        let embedded = quoted_triple("http://a", "http://p", "http://c");
        let bindings = pattern.unify_embedded(&embedded).expect("should match");
        assert!(bindings.contains_key("s"));
    }

    #[test]
    fn test_unify_embedded_non_quoted_term_returns_none() {
        let pattern = EmbeddedTriplePattern::new(var("s"), var("p"), var("o"));
        let plain = iri("http://example.org/plain");
        assert!(pattern.unify_embedded(&plain).is_none());
    }

    // -------------------------------------------------------------------
    // SparqlStarEvaluator::evaluate_embedded_pattern
    // -------------------------------------------------------------------

    #[test]
    fn test_evaluate_embedded_pattern_empty_graph() {
        let evaluator = SparqlStarEvaluator::new();
        let pattern = EmbeddedTriplePattern::new(var("s"), var("p"), var("o"));
        let results = evaluator.evaluate_embedded_pattern(&pattern, &[]);
        assert!(results.is_empty());
    }

    #[test]
    fn test_evaluate_embedded_pattern_finds_quoted_subject() {
        let evaluator = SparqlStarEvaluator::new();
        let pattern = EmbeddedTriplePattern::new(var("s"), var("p"), var("o"));

        let inner = simple_triple("http://alice", "http://age", "http://v");
        let outer = StarTriple::new(
            StarTerm::quoted_triple(inner),
            iri("http://certainty"),
            lit("high"),
        );
        let results = evaluator.evaluate_embedded_pattern(&pattern, &[outer]);
        assert_eq!(results.len(), 1);
        assert!(results[0].contains_key("s"));
    }

    #[test]
    fn test_evaluate_embedded_pattern_with_predicate_filter() {
        let evaluator = SparqlStarEvaluator::new();
        let pattern = EmbeddedTriplePattern::new(var("s"), iri("http://age"), var("o"));

        let matching_inner = simple_triple("http://alice", "http://age", "http://v");
        let non_matching_inner = simple_triple("http://alice", "http://name", "http://v");

        let t1 = StarTriple::new(
            StarTerm::quoted_triple(matching_inner),
            iri("http://cert"),
            lit("hi"),
        );
        let t2 = StarTriple::new(
            StarTerm::quoted_triple(non_matching_inner),
            iri("http://cert"),
            lit("hi"),
        );

        let results = evaluator.evaluate_embedded_pattern(&pattern, &[t1, t2]);
        assert_eq!(results.len(), 1, "only the age triple should match");
    }

    #[test]
    fn test_evaluate_embedded_pattern_multiple_matches() {
        let evaluator = SparqlStarEvaluator::new();
        let pattern = EmbeddedTriplePattern::new(var("s"), var("p"), var("o"));

        let t1 = StarTriple::new(
            quoted_triple("http://a", "http://p", "http://b"),
            iri("http://meta"),
            lit("x"),
        );
        let t2 = StarTriple::new(
            quoted_triple("http://c", "http://p", "http://d"),
            iri("http://meta"),
            lit("y"),
        );
        let results = evaluator.evaluate_embedded_pattern(&pattern, &[t1, t2]);
        assert_eq!(results.len(), 2);
    }

    // -------------------------------------------------------------------
    // AnnotationPattern
    // -------------------------------------------------------------------

    #[test]
    fn test_annotation_pattern_matches_valid_annotation() {
        let pattern = AnnotationPattern::new(
            var("s"),
            iri("http://age"),
            var("age_val"),
            iri("http://certainty"),
            var("cert"),
        );

        let inner = StarTriple::new(iri("http://alice"), iri("http://age"), lit("30"));
        let outer = StarTriple::new(
            StarTerm::quoted_triple(inner),
            iri("http://certainty"),
            lit("0.9"),
        );

        let bindings = pattern.match_triple(&outer).expect("should match");
        assert_eq!(bindings.get("s"), Some(&iri("http://alice")));
        assert_eq!(bindings.get("age_val"), Some(&lit("30")));
        assert_eq!(bindings.get("cert"), Some(&lit("0.9")));
    }

    #[test]
    fn test_annotation_pattern_no_match_plain_subject() {
        let pattern =
            AnnotationPattern::new(var("s"), var("p"), var("o"), var("ann_p"), var("ann_o"));

        // Outer triple has a plain IRI as subject, not a quoted triple
        let plain_outer = StarTriple::new(iri("http://alice"), iri("http://certainty"), lit("0.9"));

        assert!(pattern.match_triple(&plain_outer).is_none());
    }

    #[test]
    fn test_annotation_pattern_predicate_filter() {
        let pattern = AnnotationPattern::new(
            var("s"),
            var("p"),
            var("o"),
            iri("http://source"), // must match annotation predicate
            var("ann_o"),
        );

        let inner = simple_triple("http://a", "http://p", "http://b");

        // Matching annotation predicate
        let outer_match = StarTriple::new(
            StarTerm::quoted_triple(inner.clone()),
            iri("http://source"),
            lit("db"),
        );
        assert!(pattern.match_triple(&outer_match).is_some());

        // Non-matching annotation predicate
        let outer_no_match = StarTriple::new(
            StarTerm::quoted_triple(inner),
            iri("http://certainty"), // different predicate
            lit("db"),
        );
        assert!(pattern.match_triple(&outer_no_match).is_none());
    }

    // -------------------------------------------------------------------
    // SparqlStarEvaluator::evaluate_annotation_pattern
    // -------------------------------------------------------------------

    #[test]
    fn test_evaluate_annotation_pattern_empty_graph() {
        let evaluator = SparqlStarEvaluator::new();
        let pattern = AnnotationPattern::new(var("s"), var("p"), var("o"), var("ap"), var("ao"));
        let results = evaluator.evaluate_annotation_pattern(&pattern, &[]);
        assert!(results.is_empty());
    }

    #[test]
    fn test_evaluate_annotation_pattern_finds_annotations() {
        let evaluator = SparqlStarEvaluator::new();
        let pattern = AnnotationPattern::new(
            var("s"),
            var("p"),
            var("o"),
            iri("http://certainty"),
            var("cert"),
        );

        let inner = simple_triple("http://alice", "http://age", "http://v");
        let annotated = StarTriple::new(
            StarTerm::quoted_triple(inner),
            iri("http://certainty"),
            lit("high"),
        );

        let results = evaluator.evaluate_annotation_pattern(&pattern, &[annotated]);
        assert_eq!(results.len(), 1);
        assert_eq!(results[0].get("cert"), Some(&lit("high")));
    }

    // -------------------------------------------------------------------
    // SparqlStarEvaluator::evaluate_direct_pattern
    // -------------------------------------------------------------------

    #[test]
    fn test_evaluate_direct_pattern_matches_plain_triples() {
        let evaluator = SparqlStarEvaluator::new();
        let pattern = EmbeddedTriplePattern::new(var("s"), iri("http://age"), var("o"));

        let t1 = StarTriple::new(iri("http://alice"), iri("http://age"), lit("30"));
        let t2 = StarTriple::new(iri("http://bob"), iri("http://name"), lit("Bob"));

        let results = evaluator.evaluate_direct_pattern(&pattern, &[t1, t2]);
        assert_eq!(results.len(), 1, "only the age triple matches");
        assert_eq!(results[0].get("s"), Some(&iri("http://alice")));
    }

    #[test]
    fn test_evaluate_direct_pattern_empty_graph() {
        let evaluator = SparqlStarEvaluator::new();
        let pattern = EmbeddedTriplePattern::new(var("s"), var("p"), var("o"));
        let results = evaluator.evaluate_direct_pattern(&pattern, &[]);
        assert!(results.is_empty());
    }
}