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
// This is free and unencumbered software released into the public domain.
use crate::{QuadPattern, Term, TriplePattern};
/// An RDF statement pattern.
///
/// See: <https://www.w3.org/TR/rdf12-concepts/#dfn-rdf-statement>
pub trait StatementPattern {
type Term: Term + Clone;
fn matches(
&self,
subject: impl Term,
predicate: impl Term,
object: impl Term,
context: Option<impl Term>,
) -> bool {
if let Some(s) = self.subject() {
if s.value_str() != subject.value_str() {
return false;
}
}
if let Some(p) = self.predicate() {
if p.value_str() != predicate.value_str() {
return false;
}
}
if let Some(o) = self.object() {
if o.value_str() != object.value_str() {
return false;
}
}
if let Some(c) = self.context() {
if c.value_str() != context.unwrap().value_str() {
// FIXME
return false;
}
}
true
}
/// Whether the pattern has a constant subject.
fn has_subject(&self) -> bool {
self.subject().is_some()
}
/// Whether the pattern has a constant predicate.
fn has_predicate(&self) -> bool {
self.predicate().is_some()
}
/// Whether the pattern has a constant object.
fn has_object(&self) -> bool {
self.object().is_some()
}
/// Whether the pattern has a constant context (graph).
fn has_context(&self) -> bool {
self.context().is_some()
}
fn subject(&self) -> Option<&Self::Term> {
None
}
fn predicate(&self) -> Option<&Self::Term> {
None
}
fn object(&self) -> Option<&Self::Term> {
None
}
fn context(&self) -> Option<&Self::Term> {
None
}
fn to_triple_pattern(&self) -> TriplePattern<Self::Term> {
TriplePattern::new(
self.subject().cloned(),
self.predicate().cloned(),
self.object().cloned(),
)
}
fn to_quad_pattern(&self) -> QuadPattern<Self::Term> {
QuadPattern::new(
self.subject().cloned(),
self.predicate().cloned(),
self.object().cloned(),
self.context().cloned(),
)
}
}
// impl core::fmt::Debug for dyn StatementPattern<Term = HeapTerm> {
// fn fmt(&self, f: &mut core::fmt::Formatter) -> core::fmt::Result {
// f.debug_struct("StatementPattern")
// .field("subject", &self.subject().map(|t| t.as_str()))
// .field("predicate", &self.predicate().map(|t| t.as_str()))
// .field("object", &self.object().map(|t| t.as_str()))
// .field("context", &self.context().map(|t| t.as_str()))
// .finish()
// }
// }