grdf/
graph.rs

1use rdf_types::Triple;
2
3mod view;
4
5pub use view::*;
6
7/// gRDF graph.
8///
9/// A graph is a collection of RDF triples.
10/// It also defines a set of iterator to easily explore the graph.
11pub trait Graph {
12	type Subject;
13	type Predicate;
14	type Object;
15
16	/// Triple iterators.
17	type Triples<'a>: Iterator<
18		Item = Triple<&'a Self::Subject, &'a Self::Predicate, &'a Self::Object>,
19	> where
20		Self: 'a;
21
22	/// Graph subjects iterator.
23	///
24	/// Each subject is given with its associated predicates (and objects).
25	type Subjects<'a>: Iterator<Item = (&'a Self::Subject, Self::Predicates<'a>)>
26	where
27		Self: 'a;
28
29	/// Subject predicates iterator.
30	///
31	/// Iterate through all the predicates associated to a given subject.
32	/// Each predicate is also given with the associated objects.
33	type Predicates<'a>: Iterator<Item = (&'a Self::Predicate, Self::Objects<'a>)>
34	where
35		Self: 'a;
36
37	/// Objects iterator.
38	///
39	/// Iterate through a set of objects.
40	type Objects<'a>: Iterator<Item = &'a Self::Object>
41	where
42		Self: 'a;
43
44	type PatternMatching<'a, 'p>: Iterator<
45		Item = Triple<&'a Self::Subject, &'a Self::Predicate, &'a Self::Object>,
46	> where
47		Self: 'a,
48		Self::Subject: 'p,
49		Self::Predicate: 'p,
50		Self::Object: 'p;
51
52	/// Returns the number of triples in the graph.
53	fn len(&self) -> usize;
54
55	/// Checks if the graph is empty.
56	fn is_empty(&self) -> bool {
57		self.len() == 0
58	}
59
60	// Iterate through all the triples defined in the graph.
61	fn triples(&self) -> Self::Triples<'_>;
62
63	/// Iterate through all the subjects of the graph.
64	fn subjects(&self) -> Self::Subjects<'_>;
65
66	/// Iterate through all the predicates associated to the given subject.
67	fn predicates(&self, subject: &Self::Subject) -> Self::Predicates<'_>;
68
69	/// Iterate through all the objects associated to the given subject and
70	/// predicate.
71	fn objects(&self, subject: &Self::Subject, predicate: &Self::Predicate) -> Self::Objects<'_>;
72
73	/// Checks if the given triple is defined in the graph.
74	fn contains(&self, triple: Triple<&Self::Subject, &Self::Predicate, &Self::Object>) -> bool;
75
76	/// Returns an iterator over all the triples matching the given pattern.
77	#[allow(clippy::type_complexity)]
78	fn pattern_matching<'p>(
79		&self,
80		pattern: Triple<
81			Option<&'p Self::Subject>,
82			Option<&'p Self::Predicate>,
83			Option<&'p Self::Object>,
84		>,
85	) -> Self::PatternMatching<'_, 'p>;
86
87	/// Returns any triple matching the given pattern.
88	#[allow(clippy::type_complexity)]
89	fn any_match(
90		&self,
91		pattern: Triple<Option<&Self::Subject>, Option<&Self::Predicate>, Option<&Self::Object>>,
92	) -> Option<Triple<&Self::Subject, &Self::Predicate, &Self::Object>> {
93		self.pattern_matching(pattern).next()
94	}
95
96	/// Creates a view for the dataset, using the given subject from the given
97	/// graph.
98	fn view<'a, A>(&'a self, subject: &'a Self::Subject, access: A) -> GraphView<Self, A> {
99		GraphView::new(self, subject, access)
100	}
101}
102
103/// Sized gRDF graph that can be converted into iterators.
104///
105/// Defines a set of iterators the graph can be consumed into.
106pub trait SizedGraph: Graph + Sized {
107	/// Consuming triples iterator.
108	type IntoTriples: Iterator<
109		Item = Triple<
110			<Self as Graph>::Subject,
111			<Self as Graph>::Predicate,
112			<Self as Graph>::Object,
113		>,
114	>;
115
116	/// Consuming subjects iterator.
117	type IntoSubjects: Iterator<Item = (<Self as Graph>::Subject, Self::IntoPredicates)>;
118
119	/// Consuming predicates iterator.
120	type IntoPredicates: Iterator<Item = (<Self as Graph>::Predicate, Self::IntoObjects)>;
121
122	/// Consuming objects iterator.
123	type IntoObjects: Iterator<Item = <Self as Graph>::Object>;
124
125	/// Consumes the graph and returns an iterator over its triples.
126	fn into_triples(self) -> Self::IntoTriples;
127
128	/// Consumes the graph and returns an iterator over its subjects.
129	fn into_subjects(self) -> Self::IntoSubjects;
130
131	/// Consumes the graph and returns an iterator over the predicates of the
132	/// given subject.
133	fn into_predicates(self, subject: &<Self as Graph>::Subject) -> Self::IntoPredicates;
134
135	/// Consumes the graph and returns an iterator over the objects of the given
136	/// subject and predicate.
137	fn into_objects(
138		self,
139		subject: &<Self as Graph>::Subject,
140		predicate: &<Self as Graph>::Predicate,
141	) -> Self::IntoObjects;
142}
143
144/// Mutable gRDF graph.
145pub trait MutableGraph: Graph {
146	/// Insert the given triple into the graph.
147	fn insert(
148		&mut self,
149		triple: Triple<
150			<Self as Graph>::Subject,
151			<Self as Graph>::Predicate,
152			<Self as Graph>::Object,
153		>,
154	) -> bool;
155
156	fn remove(
157		&mut self,
158		triple: Triple<
159			&<Self as Graph>::Subject,
160			&<Self as Graph>::Predicate,
161			&<Self as Graph>::Object,
162		>,
163	);
164
165	/// Absorb the given other graph.
166	///
167	/// Adds all the triples of `other` in the graph.
168	fn absorb<
169		G: SizedGraph<
170			Subject = <Self as Graph>::Subject,
171			Predicate = <Self as Graph>::Predicate,
172			Object = <Self as Graph>::Object,
173		>,
174	>(
175		&mut self,
176		other: G,
177	);
178}
179
180pub trait GraphTake<
181	T: ?Sized = <Self as Graph>::Subject,
182	U: ?Sized = <Self as Graph>::Predicate,
183	V: ?Sized = <Self as Graph>::Object,
184>: Graph
185{
186	#[allow(clippy::type_complexity)]
187	fn take(
188		&mut self,
189		triple: Triple<&T, &U, &V>,
190	) -> Option<Triple<Self::Subject, Self::Predicate, Self::Object>>;
191
192	#[allow(clippy::type_complexity)]
193	fn take_match(
194		&mut self,
195		triple: Triple<Option<&T>, Option<&U>, Option<&V>>,
196	) -> Option<Triple<Self::Subject, Self::Predicate, Self::Object>>;
197}