[][src]Trait sophia::graph::Graph

pub trait Graph<'a> {
    type Triple: Triple<'a>;
    type Error: CoercibleWith<Error> + CoercibleWith<Never>;
    fn triples(&'a self) -> GTripleSource<'a, Self>;

    fn triples_with_s<T>(&'a self, s: &'a Term<T>) -> GTripleSource<'a, Self>
    where
        T: TermData
, { ... }
fn triples_with_p<T>(&'a self, p: &'a Term<T>) -> GTripleSource<'a, Self>
    where
        T: TermData
, { ... }
fn triples_with_o<T>(&'a self, o: &'a Term<T>) -> GTripleSource<'a, Self>
    where
        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
, { ... }
fn triples_with_so<T, U>(
        &'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
, { ... }
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
, { ... }
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
, { ... }
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
, { ... }
fn subjects(&'a self) -> GResultTermSet<'a, Self> { ... }
fn predicates(&'a self) -> GResultTermSet<'a, Self> { ... }
fn objects(&'a self) -> GResultTermSet<'a, Self> { ... }
fn iris(&'a self) -> GResultTermSet<'a, Self> { ... }
fn bnodes(&'a self) -> GResultTermSet<'a, Self> { ... }
fn literals(&'a self) -> GResultTermSet<'a, Self> { ... }
fn variables(&'a self) -> GResultTermSet<'a, Self> { ... }
fn borrow_as_dataset(&self) -> GraphAsDataset<Self, &Self> { ... }
fn borrow_mut_as_dataset(&mut self) -> GraphAsDataset<Self, &mut Self> { ... }
fn as_dataset(self) -> GraphAsDataset<Self, Self>
    where
        Self: Sized
, { ... } }

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:

This example deliberately fails to compile
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 Triples that the methods of this graph will yield.

type Error: CoercibleWith<Error> + CoercibleWith<Never>

The error type that this graph may raise.

Must be either Never (for infallible graphs) or Error.

Loading content...

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 Results, an error may occur at any time during the iteration.

Loading content...

Provided methods

fn triples_with_s<T>(&'a self, s: &'a Term<T>) -> GTripleSource<'a, Self> where
    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

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

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

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

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

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

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

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

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

Dataset adapter taking ownership of this graph

Loading content...

Implementations on Foreign Types

impl<'a, T> Graph<'a> for [T] where
    T: Triple<'a> + 'a, 
[src]

type Triple = &'a T

type Error = Never

impl<'a, T> Graph<'a> for Vec<T> where
    T: Triple<'a> + 'a, 
[src]

type Triple = &'a T

type Error = Never

impl<'a, T, BH> Graph<'a> for HashSet<T, BH> where
    T: Eq + Hash + Triple<'a> + 'a,
    BH: BuildHasher
[src]

type Triple = &'a T

type Error = Never

Loading content...

Implementors

impl<'a, D: ?Sized, E, M> Graph<'a> for DatasetGraph<D, E, M> where
    D: Dataset<'a>,
    E: Borrow<D>,
    M: GraphNameMatcher
[src]

type Triple = QuadAsTriple<D::Quad>

type Error = D::Error

impl<'a, I> Graph<'a> for HashGraph<I> where
    I: TermIndexMap,
    I::Index: Hash,
    <I::Factory as TermFactory>::TermData: 'static, 
[src]

type Triple = [&'a Term<Self::TermData>; 3]

type Error = Never

impl<'a, T> Graph<'a> for OpsWrapper<T> where
    T: IndexedGraph + Graph<'a, Triple = [&'a Term<<T as IndexedGraph>::TermData>; 3]>, 
[src]

type Triple = <Self::Wrapped as Graph<'a>>::Triple

type Error = <Self::Wrapped as Graph<'a>>::Error

impl<'a, T> Graph<'a> for SpoWrapper<T> where
    T: IndexedGraph + Graph<'a, Triple = [&'a Term<<T as IndexedGraph>::TermData>; 3]>, 
[src]

type Triple = <Self::Wrapped as Graph<'a>>::Triple

type Error = <Self::Wrapped as Graph<'a>>::Error

Loading content...