Skip to main content

rdf_model/
statement_pattern.rs

1// This is free and unencumbered software released into the public domain.
2
3use crate::{QuadPattern, Term, TriplePattern};
4
5/// An RDF statement pattern.
6///
7/// See: <https://www.w3.org/TR/rdf12-concepts/#dfn-rdf-statement>
8pub trait StatementPattern {
9    type Term: Term + Clone;
10
11    fn matches(
12        &self,
13        subject: impl Term,
14        predicate: impl Term,
15        object: impl Term,
16        context: Option<impl Term>,
17    ) -> bool {
18        if let Some(s) = self.subject() {
19            if s.value_str() != subject.value_str() {
20                return false;
21            }
22        }
23        if let Some(p) = self.predicate() {
24            if p.value_str() != predicate.value_str() {
25                return false;
26            }
27        }
28        if let Some(o) = self.object() {
29            if o.value_str() != object.value_str() {
30                return false;
31            }
32        }
33        if let Some(c) = self.context() {
34            if c.value_str() != context.unwrap().value_str() {
35                // FIXME
36                return false;
37            }
38        }
39        true
40    }
41
42    /// Whether the pattern has a constant subject.
43    fn has_subject(&self) -> bool {
44        self.subject().is_some()
45    }
46
47    /// Whether the pattern has a constant predicate.
48    fn has_predicate(&self) -> bool {
49        self.predicate().is_some()
50    }
51
52    /// Whether the pattern has a constant object.
53    fn has_object(&self) -> bool {
54        self.object().is_some()
55    }
56
57    /// Whether the pattern has a constant context (graph).
58    fn has_context(&self) -> bool {
59        self.context().is_some()
60    }
61
62    fn subject(&self) -> Option<&Self::Term> {
63        None
64    }
65
66    fn predicate(&self) -> Option<&Self::Term> {
67        None
68    }
69
70    fn object(&self) -> Option<&Self::Term> {
71        None
72    }
73
74    fn context(&self) -> Option<&Self::Term> {
75        None
76    }
77
78    fn to_triple_pattern(&self) -> TriplePattern<Self::Term> {
79        TriplePattern::new(
80            self.subject().cloned(),
81            self.predicate().cloned(),
82            self.object().cloned(),
83        )
84    }
85
86    fn to_quad_pattern(&self) -> QuadPattern<Self::Term> {
87        QuadPattern::new(
88            self.subject().cloned(),
89            self.predicate().cloned(),
90            self.object().cloned(),
91            self.context().cloned(),
92        )
93    }
94}
95
96// impl core::fmt::Debug for dyn StatementPattern<Term = HeapTerm> {
97//     fn fmt(&self, f: &mut core::fmt::Formatter) -> core::fmt::Result {
98//         f.debug_struct("StatementPattern")
99//             .field("subject", &self.subject().map(|t| t.as_str()))
100//             .field("predicate", &self.predicate().map(|t| t.as_str()))
101//             .field("object", &self.object().map(|t| t.as_str()))
102//             .field("context", &self.context().map(|t| t.as_str()))
103//             .finish()
104//     }
105// }