rdf_types/dataset/graph/
mod.rs1use crate::{Triple, pattern::LinearTriplePattern};
3
4mod r#async;
5mod fallible;
6mod r#impl;
7
8pub use r#async::*;
9pub use fallible::*;
10pub use r#impl::*;
11
12pub trait Graph {
18 type Resource;
20}
21
22pub trait FiniteGraph: Graph {
24 type Triples<'a>: Iterator<Item = Triple<&'a Self::Resource>>
26 where
27 Self: 'a;
28
29 fn triples(&self) -> Self::Triples<'_>;
31
32 fn triples_count(&self) -> usize {
34 self.triples().count()
35 }
36}
37
38pub trait ResourceFiniteGraph: Graph {
41 type GraphResources<'a>: Iterator<Item = &'a Self::Resource>
43 where
44 Self: 'a;
45
46 fn graph_resources(&self) -> Self::GraphResources<'_>;
48
49 fn graph_resource_count(&self) -> usize {
51 self.graph_resources().count()
52 }
53}
54
55pub trait SubjectFiniteGraph: Graph {
57 type GraphSubjects<'a>: Iterator<Item = &'a Self::Resource>
59 where
60 Self: 'a;
61
62 fn graph_subjects(&self) -> Self::GraphSubjects<'_>;
64
65 fn graph_subject_count(&self) -> usize {
67 self.graph_subjects().count()
68 }
69}
70
71pub trait PredicateFiniteGraph: Graph {
73 type GraphPredicates<'a>: Iterator<Item = &'a Self::Resource>
75 where
76 Self: 'a;
77
78 fn graph_predicates(&self) -> Self::GraphPredicates<'_>;
80
81 fn graph_predicate_count(&self) -> usize {
83 self.graph_predicates().count()
84 }
85}
86
87pub trait ObjectFiniteGraph: Graph {
89 type GraphObjects<'a>: Iterator<Item = &'a Self::Resource>
91 where
92 Self: 'a;
93
94 fn graph_objects(&self) -> Self::GraphObjects<'_>;
96
97 fn graph_object_count(&self) -> usize {
99 self.graph_objects().count()
100 }
101}
102
103pub trait MultiPatternMatchingGraph: Graph {
108 type TripleMultiPatternMatching<'a, 'p>: Iterator<Item = Triple<&'a Self::Resource>>
110 where
111 Self: 'a,
112 Self::Resource: 'p;
113
114 fn triple_multi_pattern_matching<'p, P: IntoIterator<Item = &'p Self::Resource>>(
117 &self,
118 pattern: LinearTriplePattern<P>,
119 ) -> Self::TripleMultiPatternMatching<'_, 'p>;
120}
121
122pub trait PatternMatchingGraph: Graph {
127 type TriplePatternMatching<'a, 'p>: Iterator<Item = Triple<&'a Self::Resource>>
129 where
130 Self: 'a,
131 Self::Resource: 'p;
132
133 fn triple_pattern_matching<'p>(
136 &self,
137 pattern: LinearTriplePattern<&'p Self::Resource>,
138 ) -> Self::TriplePatternMatching<'_, 'p>;
139
140 fn contains_triple(&self, triple: Triple<&Self::Resource>) -> bool {
142 self.triple_pattern_matching(triple.into()).next().is_some()
143 }
144
145 fn contains_triple_subject(&self, subject: &Self::Resource) -> bool {
147 self.triple_pattern_matching(Triple(Some(subject), None, None))
148 .next()
149 .is_some()
150 }
151
152 fn contains_triple_predicate(&self, predicate: &Self::Resource) -> bool {
154 self.triple_pattern_matching(Triple(None, Some(predicate), None))
155 .next()
156 .is_some()
157 }
158
159 fn contains_triple_object(&self, object: &Self::Resource) -> bool {
161 self.triple_pattern_matching(Triple(None, None, Some(object)))
162 .next()
163 .is_some()
164 }
165
166 fn triple_predicates_objects<'p>(
168 &self,
169 subject: &'p Self::Resource,
170 ) -> TriplePredicatesObjects<'_, 'p, Self>
171 where
172 Self: PredicateFiniteGraph,
173 Self::Resource: 'p,
174 {
175 TriplePredicatesObjects {
176 subject,
177 predicates: self.graph_predicates(),
178 graph: self,
179 }
180 }
181
182 fn triple_objects<'p>(
184 &self,
185 subject: &'p Self::Resource,
186 predicate: &'p Self::Resource,
187 ) -> TripleObjects<'_, 'p, Self> {
188 TripleObjects {
189 first: None,
190 inner: self.triple_pattern_matching(Triple(Some(subject), Some(predicate), None)),
191 }
192 }
193}
194
195pub struct TriplePredicatesObjects<
200 'a,
201 'p,
202 G: 'a + ?Sized + PredicateFiniteGraph + PatternMatchingGraph,
203> {
204 subject: &'p G::Resource,
205 predicates: G::GraphPredicates<'a>,
206 graph: &'a G,
207}
208
209impl<'a: 'p, 'p, G: 'a + ?Sized + PredicateFiniteGraph + PatternMatchingGraph> Iterator
210 for TriplePredicatesObjects<'a, 'p, G>
211where
212 G::Resource: 'p,
213{
214 type Item = (&'a G::Resource, TripleObjects<'p, 'p, G>);
215
216 fn next(&mut self) -> Option<Self::Item> {
217 for predicate in &mut self.predicates {
218 let pattern = Triple(Some(self.subject), Some(predicate), None);
219
220 let mut iter = self.graph.triple_pattern_matching(pattern);
221 if let Some(Triple(_, _, o)) = iter.next() {
222 return Some((
223 predicate,
224 TripleObjects {
225 first: Some(o),
226 inner: iter,
227 },
228 ));
229 }
230 }
231
232 None
233 }
234}
235
236pub struct TripleObjects<'a, 'p, D: 'a + ?Sized + PatternMatchingGraph>
241where
242 D::Resource: 'p,
243{
244 first: Option<&'a D::Resource>,
245 inner: D::TriplePatternMatching<'a, 'p>,
246}
247
248impl<'a, 'p, D: 'a + ?Sized + PatternMatchingGraph> Iterator for TripleObjects<'a, 'p, D>
249where
250 D::Resource: 'p,
251{
252 type Item = &'a D::Resource;
253
254 fn next(&mut self) -> Option<Self::Item> {
255 self.first
256 .take()
257 .or_else(|| self.inner.next().map(Triple::into_object))
258 }
259}
260
261pub trait PatternMatchingGraphMut: PatternMatchingGraph {
263 type ExtractMatchingTriples<'a, 'p>: Iterator<Item = Triple<Self::Resource>>
265 where
266 Self: 'a,
267 Self::Resource: 'p;
268
269 fn extract_matching_triples<'p>(
276 &mut self,
277 pattern: impl Into<LinearTriplePattern<&'p Self::Resource>>,
278 ) -> Self::ExtractMatchingTriples<'_, 'p>
279 where
280 Self::Resource: 'p;
281}
282
283pub trait GraphMut: Graph {
285 fn insert(&mut self, triple: Triple<Self::Resource>);
287
288 fn remove(&mut self, triple: Triple<&Self::Resource>);
290}