[−][src]Trait sophia::graph::Graph
Generic trait for RDF graphs.
For convenience, this trait is implemented by standard collections of triples.
NB: the semantics of this trait allows a graph to contain duplicate triples;
see also SetGraph
.
How to use Graph
in a trait bound?
TL;DR: If the compiler complains about lifetimes,
replace G: Graph<'a>
with G: for<'x> Graph<'x>
.
The lifetime parameter of Graph
has a very specific semantics:
it is the lifetime for which the graph can be borrowed when iterating over its triples.
Since lifetime parameters in traits are
invariant,
an instance of Graph<'a>
will only be usable with the exact lifetime 'a
,
but not with shorter lifetimes, which is counter-intuitive.
In many situations, when you write a function accepting a graph, you can not define the appropriate lifetime in the function's signature. For example, you may need to borrow the graph for the lifetime of a local variable of the function:
use sophia::error::Never; use sophia::graph::Graph; use sophia::term::*; fn count_str_mentions<'a, G>(g: &G, txt: &str) -> usize where G: Graph<'a, Error=Never> { let literal = RefTerm::from(txt); g.triples_with_o(&literal).count() // fails to compile because `literal` does not live as long as 'a . }
NB: the compilers's error messages are not very helpful here;
they suggest to add lifetime 'a
to the function parameters,
but that does not solve the problem.
In this kind of situation, the solution consists in using a
Higher-Rank Trait Bound
for G
:
use sophia::error::Never; use sophia::graph::Graph; use sophia::term::*; fn count_str_mentions<G>(g: &G, txt: &str) -> usize where G: for<'x> Graph<'x, Error=Never> // <-- higher-rank trait bound { let literal = RefTerm::from(txt); g.triples_with_o(&literal).count() }
NB:
The Higher-Ranked Type Bound above states
"G
implements Graph<'x>
for every lifetime 'x
",
which allows us to not define the lifetime in the function's signature.
This has its own drawback, though:
only implementations of Graph
who own their data will match this bound
(fortunately, it is the case of most implementations).
Associated Types
type Triple: Triple<'a>
The type of Triple
s
that the methods of this graph will yield.
type Error: CoercibleWith<Error> + CoercibleWith<Never>
Required methods
fn triples(&'a self) -> GTripleSource<'a, Self>
An iterator visiting all triples of this graph in arbitrary order.
This iterator is fallible:
its items are Result
s,
an error may occur at any time during the iteration.
Provided methods
fn triples_with_s<T>(&'a self, s: &'a Term<T>) -> GTripleSource<'a, Self> where
T: TermData,
T: TermData,
An iterator visiting all triples with the given subject.
See also triples
.
fn triples_with_p<T>(&'a self, p: &'a Term<T>) -> GTripleSource<'a, Self> where
T: TermData,
T: TermData,
An iterator visiting all triples with the given predicate.
See also triples
.
fn triples_with_o<T>(&'a self, o: &'a Term<T>) -> GTripleSource<'a, Self> where
T: TermData,
T: TermData,
An iterator visiting all triples with the given object.
See also triples
.
fn triples_with_sp<T, U>(
&'a self,
s: &'a Term<T>,
p: &'a Term<U>
) -> GTripleSource<'a, Self> where
T: TermData,
U: TermData,
&'a self,
s: &'a Term<T>,
p: &'a Term<U>
) -> GTripleSource<'a, Self> where
T: TermData,
U: TermData,
An iterator visiting all triples with the given subject and predicate.
See also triples
.
fn triples_with_so<T, U>(
&'a self,
s: &'a Term<T>,
o: &'a Term<U>
) -> GTripleSource<'a, Self> where
T: TermData,
U: TermData,
&'a self,
s: &'a Term<T>,
o: &'a Term<U>
) -> GTripleSource<'a, Self> where
T: TermData,
U: TermData,
An iterator visiting all triples with the given subject and object.
See also triples
.
fn triples_with_po<T, U>(
&'a self,
p: &'a Term<T>,
o: &'a Term<U>
) -> GTripleSource<'a, Self> where
T: TermData,
U: TermData,
&'a self,
p: &'a Term<T>,
o: &'a Term<U>
) -> GTripleSource<'a, Self> where
T: TermData,
U: TermData,
An iterator visiting all triples with the given predicate and object.
See also triples
.
fn triples_with_spo<T, U, V>(
&'a self,
s: &'a Term<T>,
p: &'a Term<U>,
o: &'a Term<V>
) -> GTripleSource<'a, Self> where
T: TermData,
U: TermData,
V: TermData,
&'a self,
s: &'a Term<T>,
p: &'a Term<U>,
o: &'a Term<V>
) -> GTripleSource<'a, Self> where
T: TermData,
U: TermData,
V: TermData,
An iterator visiting all triples with the given subject, predicate and object.
See also triples
.
fn contains<T, U, V>(
&'a self,
s: &'a Term<T>,
p: &'a Term<U>,
o: &'a Term<V>
) -> GResult<'a, Self, bool> where
T: TermData,
U: TermData,
V: TermData,
&'a self,
s: &'a Term<T>,
p: &'a Term<U>,
o: &'a Term<V>
) -> GResult<'a, Self, bool> where
T: TermData,
U: TermData,
V: TermData,
Return true
if this graph contains the given triple.
fn triples_matching<S: ?Sized, P: ?Sized, O: ?Sized>(
&'a self,
ms: &'a S,
mp: &'a P,
mo: &'a O
) -> GTripleSource<'a, Self> where
S: TermMatcher,
P: TermMatcher,
O: TermMatcher,
&'a self,
ms: &'a S,
mp: &'a P,
mo: &'a O
) -> GTripleSource<'a, Self> where
S: TermMatcher,
P: TermMatcher,
O: TermMatcher,
An iterator visiting all triples matching the given subject, predicate and object.
See also triples
.
fn subjects(&'a self) -> GResultTermSet<'a, Self>
Build a Hashset of all the terms used as subject in this Graph.
fn predicates(&'a self) -> GResultTermSet<'a, Self>
Build a Hashset of all the terms used as predicate in this Graph.
fn objects(&'a self) -> GResultTermSet<'a, Self>
Build a Hashset of all the terms used as object in this Graph.
fn iris(&'a self) -> GResultTermSet<'a, Self>
Build a Hashset of all the IRIs used in this Graph.
fn bnodes(&'a self) -> GResultTermSet<'a, Self>
Build a Hashset of all the BNodes used in this Graph.
fn literals(&'a self) -> GResultTermSet<'a, Self>
Build a Hashset of all the Literals used in this Graph.
fn variables(&'a self) -> GResultTermSet<'a, Self>
Build a Hashset of all the variables used in this Graph.
fn borrow_as_dataset(&self) -> GraphAsDataset<Self, &Self>
Dataset
adapter borrowing this graph
fn borrow_mut_as_dataset(&mut self) -> GraphAsDataset<Self, &mut Self>
Dataset
adapter borrowing this graph mutably
fn as_dataset(self) -> GraphAsDataset<Self, Self> where
Self: Sized,
Self: Sized,
Dataset
adapter taking ownership of this graph
Implementations on Foreign Types
impl<'a, T> Graph<'a> for [T] where
T: Triple<'a> + 'a,
[src]
T: Triple<'a> + 'a,
impl<'a, T> Graph<'a> for Vec<T> where
T: Triple<'a> + 'a,
[src]
T: Triple<'a> + 'a,
impl<'a, T, BH> Graph<'a> for HashSet<T, BH> where
T: Eq + Hash + Triple<'a> + 'a,
BH: BuildHasher,
[src]
Loading content...
T: Eq + Hash + Triple<'a> + 'a,
BH: BuildHasher,
Implementors
impl<'a, D: ?Sized, E, M> Graph<'a> for DatasetGraph<D, E, M> where
D: Dataset<'a>,
E: Borrow<D>,
M: GraphNameMatcher,
[src]
D: Dataset<'a>,
E: Borrow<D>,
M: GraphNameMatcher,
type Triple = QuadAsTriple<D::Quad>
type Error = D::Error
fn triples(&'a self) -> GTripleSource<'a, Self>
[src]
fn triples_with_s<T>(&'a self, s: &'a Term<T>) -> GTripleSource<'a, Self> where
T: TermData,
[src]
T: TermData,
fn triples_with_p<T>(&'a self, p: &'a Term<T>) -> GTripleSource<'a, Self> where
T: TermData,
[src]
T: TermData,
fn triples_with_o<T>(&'a self, o: &'a Term<T>) -> GTripleSource<'a, Self> where
T: TermData,
[src]
T: TermData,
fn triples_with_sp<T, U>(
&'a self,
s: &'a Term<T>,
p: &'a Term<U>
) -> GTripleSource<'a, Self> where
T: TermData,
U: TermData,
[src]
&'a self,
s: &'a Term<T>,
p: &'a Term<U>
) -> GTripleSource<'a, Self> where
T: TermData,
U: TermData,
fn triples_with_so<T, U>(
&'a self,
s: &'a Term<T>,
o: &'a Term<U>
) -> GTripleSource<'a, Self> where
T: TermData,
U: TermData,
[src]
&'a self,
s: &'a Term<T>,
o: &'a Term<U>
) -> GTripleSource<'a, Self> where
T: TermData,
U: TermData,
fn triples_with_po<T, U>(
&'a self,
p: &'a Term<T>,
o: &'a Term<U>
) -> GTripleSource<'a, Self> where
T: TermData,
U: TermData,
[src]
&'a self,
p: &'a Term<T>,
o: &'a Term<U>
) -> GTripleSource<'a, Self> where
T: TermData,
U: TermData,
fn triples_with_spo<T, U, V>(
&'a self,
s: &'a Term<T>,
p: &'a Term<U>,
o: &'a Term<V>
) -> GTripleSource<'a, Self> where
T: TermData,
U: TermData,
V: TermData,
[src]
&'a self,
s: &'a Term<T>,
p: &'a Term<U>,
o: &'a Term<V>
) -> GTripleSource<'a, Self> where
T: TermData,
U: TermData,
V: TermData,
impl<'a, I> Graph<'a> for HashGraph<I> where
I: TermIndexMap,
I::Index: Hash,
<I::Factory as TermFactory>::TermData: 'static,
[src]
I: TermIndexMap,
I::Index: Hash,
<I::Factory as TermFactory>::TermData: 'static,
type Triple = [&'a Term<Self::TermData>; 3]
type Error = Never
fn triples(&'a self) -> GTripleSource<'a, Self>
[src]
impl<'a, T> Graph<'a> for OpsWrapper<T> where
T: IndexedGraph + Graph<'a, Triple = [&'a Term<<T as IndexedGraph>::TermData>; 3]>,
[src]
T: IndexedGraph + Graph<'a, Triple = [&'a Term<<T as IndexedGraph>::TermData>; 3]>,
type Triple = <Self::Wrapped as Graph<'a>>::Triple
type Error = <Self::Wrapped as Graph<'a>>::Error
fn triples(&'a self) -> GTripleSource<'a, Self>
[src]
fn triples_with_s<T_>(&'a self, s: &'a Term<T_>) -> GTripleSource<'a, Self> where
T_: TermData,
[src]
T_: TermData,
fn triples_with_p<T_>(&'a self, p: &'a Term<T_>) -> GTripleSource<'a, Self> where
T_: TermData,
[src]
T_: TermData,
fn triples_with_o<T_>(&'a self, o: &'a Term<T_>) -> GTripleSource<'a, Self> where
T_: TermData,
[src]
T_: TermData,
fn triples_with_sp<T_, U_>(
&'a self,
s: &'a Term<T_>,
p: &'a Term<U_>
) -> GTripleSource<'a, Self> where
T_: TermData,
U_: TermData,
[src]
&'a self,
s: &'a Term<T_>,
p: &'a Term<U_>
) -> GTripleSource<'a, Self> where
T_: TermData,
U_: TermData,
fn triples_with_so<T_, U_>(
&'a self,
s: &'a Term<T_>,
o: &'a Term<U_>
) -> GTripleSource<'a, Self> where
T_: TermData,
U_: TermData,
[src]
&'a self,
s: &'a Term<T_>,
o: &'a Term<U_>
) -> GTripleSource<'a, Self> where
T_: TermData,
U_: TermData,
fn triples_with_po<T_, U_>(
&'a self,
p: &'a Term<T_>,
o: &'a Term<U_>
) -> GTripleSource<'a, Self> where
T_: TermData,
U_: TermData,
[src]
&'a self,
p: &'a Term<T_>,
o: &'a Term<U_>
) -> GTripleSource<'a, Self> where
T_: TermData,
U_: TermData,
fn triples_with_spo<T_, U_, V_>(
&'a self,
s: &'a Term<T_>,
p: &'a Term<U_>,
o: &'a Term<V_>
) -> GTripleSource<'a, Self> where
T_: TermData,
U_: TermData,
V_: TermData,
[src]
&'a self,
s: &'a Term<T_>,
p: &'a Term<U_>,
o: &'a Term<V_>
) -> GTripleSource<'a, Self> where
T_: TermData,
U_: TermData,
V_: TermData,
fn contains<T_, U_, V_>(
&'a self,
s: &'a Term<T_>,
p: &'a Term<U_>,
o: &'a Term<V_>
) -> GResult<'a, Self, bool> where
T_: TermData,
U_: TermData,
V_: TermData,
[src]
&'a self,
s: &'a Term<T_>,
p: &'a Term<U_>,
o: &'a Term<V_>
) -> GResult<'a, Self, bool> where
T_: TermData,
U_: TermData,
V_: TermData,
fn subjects(&'a self) -> GResult<'a, Self, HashSet<GTerm<'a, Self>>>
[src]
fn predicates(&'a self) -> GResult<'a, Self, HashSet<GTerm<'a, Self>>>
[src]
fn objects(&'a self) -> GResult<'a, Self, HashSet<GTerm<'a, Self>>>
[src]
fn iris(&'a self) -> GResult<'a, Self, HashSet<GTerm<'a, Self>>>
[src]
fn bnodes(&'a self) -> GResult<'a, Self, HashSet<GTerm<'a, Self>>>
[src]
fn literals(&'a self) -> GResult<'a, Self, HashSet<GTerm<'a, Self>>>
[src]
fn variables(&'a self) -> GResult<'a, Self, HashSet<GTerm<'a, Self>>>
[src]
impl<'a, T> Graph<'a> for SpoWrapper<T> where
T: IndexedGraph + Graph<'a, Triple = [&'a Term<<T as IndexedGraph>::TermData>; 3]>,
[src]
T: IndexedGraph + Graph<'a, Triple = [&'a Term<<T as IndexedGraph>::TermData>; 3]>,
type Triple = <Self::Wrapped as Graph<'a>>::Triple
type Error = <Self::Wrapped as Graph<'a>>::Error
fn triples(&'a self) -> GTripleSource<'a, Self>
[src]
fn triples_with_s<T_>(&'a self, s: &'a Term<T_>) -> GTripleSource<'a, Self> where
T_: TermData,
[src]
T_: TermData,
fn triples_with_p<T_>(&'a self, p: &'a Term<T_>) -> GTripleSource<'a, Self> where
T_: TermData,
[src]
T_: TermData,
fn triples_with_o<T_>(&'a self, o: &'a Term<T_>) -> GTripleSource<'a, Self> where
T_: TermData,
[src]
T_: TermData,
fn triples_with_sp<T_, U_>(
&'a self,
s: &'a Term<T_>,
p: &'a Term<U_>
) -> GTripleSource<'a, Self> where
T_: TermData,
U_: TermData,
[src]
&'a self,
s: &'a Term<T_>,
p: &'a Term<U_>
) -> GTripleSource<'a, Self> where
T_: TermData,
U_: TermData,
fn triples_with_so<T_, U_>(
&'a self,
s: &'a Term<T_>,
o: &'a Term<U_>
) -> GTripleSource<'a, Self> where
T_: TermData,
U_: TermData,
[src]
&'a self,
s: &'a Term<T_>,
o: &'a Term<U_>
) -> GTripleSource<'a, Self> where
T_: TermData,
U_: TermData,
fn triples_with_po<T_, U_>(
&'a self,
p: &'a Term<T_>,
o: &'a Term<U_>
) -> GTripleSource<'a, Self> where
T_: TermData,
U_: TermData,
[src]
&'a self,
p: &'a Term<T_>,
o: &'a Term<U_>
) -> GTripleSource<'a, Self> where
T_: TermData,
U_: TermData,
fn triples_with_spo<T_, U_, V_>(
&'a self,
s: &'a Term<T_>,
p: &'a Term<U_>,
o: &'a Term<V_>
) -> GTripleSource<'a, Self> where
T_: TermData,
U_: TermData,
V_: TermData,
[src]
&'a self,
s: &'a Term<T_>,
p: &'a Term<U_>,
o: &'a Term<V_>
) -> GTripleSource<'a, Self> where
T_: TermData,
U_: TermData,
V_: TermData,
fn contains<T_, U_, V_>(
&'a self,
s: &'a Term<T_>,
p: &'a Term<U_>,
o: &'a Term<V_>
) -> GResult<'a, Self, bool> where
T_: TermData,
U_: TermData,
V_: TermData,
[src]
&'a self,
s: &'a Term<T_>,
p: &'a Term<U_>,
o: &'a Term<V_>
) -> GResult<'a, Self, bool> where
T_: TermData,
U_: TermData,
V_: TermData,