use model::*; use Result; /// Trait for [RDF graphs](https://www.w3.org/TR/rdf11-concepts/#dfn-graph) /// /// This crate currently provides a simple stand-alone in memory implementation of the `Graph` trait. /// /// Usage example: /// ``` /// use rudf::model::*; /// use rudf::store::MemoryGraph; /// use std::str::FromStr; /// /// let graph = MemoryGraph::default(); /// let ex = NamedNode::from_str("http://example.com").unwrap(); /// let triple = Triple::new(ex.clone(), ex.clone(), ex.clone()); /// graph.insert(&triple); /// let results: Vec = graph.triples_for_subject(&ex.into()).unwrap().map(|t| t.unwrap()).collect(); /// assert_eq!(vec![triple], results); /// ``` pub trait Graph { type TriplesIterator: Iterator>; type TriplesForSubjectIterator: Iterator>; type ObjectsForSubjectPredicateIterator: Iterator>; type PredicatesForSubjectObjectIterator: Iterator>; type TriplesForPredicateIterator: Iterator>; type SubjectsForPredicateObjectIterator: Iterator>; type TriplesForObjectIterator: Iterator>; /// Returns all triples contained by the graph fn iter(&self) -> Result { self.triples() } /// Returns all triples contained by the graph fn triples(&self) -> Result; fn triples_for_subject( &self, subject: &NamedOrBlankNode, ) -> Result; fn objects_for_subject_predicate( &self, subject: &NamedOrBlankNode, predicate: &NamedNode, ) -> Result; fn object_for_subject_predicate( &self, subject: &NamedOrBlankNode, predicate: &NamedNode, ) -> Result> { //TODO use transpose when stable match self .objects_for_subject_predicate(subject, predicate)? .nth(0) { Some(object) => Ok(Some(object?)), None => Ok(None), } } fn predicates_for_subject_object( &self, subject: &NamedOrBlankNode, object: &Term, ) -> Result; fn triples_for_predicate( &self, predicate: &NamedNode, ) -> Result; fn subjects_for_predicate_object( &self, predicate: &NamedNode, object: &Term, ) -> Result; fn triples_for_object(&self, object: &Term) -> Result; /// Checks if the graph contains the given triple fn contains(&self, triple: &Triple) -> Result; /// Adds a triple to the graph fn insert(&self, triple: &Triple) -> Result<()>; /// Removes a concrete triple from the graph fn remove(&self, triple: &Triple) -> Result<()>; /// Returns the number of triples in this graph fn len(&self) -> Result; /// Checks if this graph contains a triple fn is_empty(&self) -> Result; } /// Trait for [RDF named graphs](https://www.w3.org/TR/rdf11-concepts/#dfn-named-graph) i.e. RDF graphs identified by an IRI pub trait NamedGraph: Graph { fn name(&self) -> &NamedOrBlankNode; } /// Trait for [RDF datasets](https://www.w3.org/TR/rdf11-concepts/#dfn-rdf-dataset) /// /// This crate currently provides two implementation of the `Dataset` traits: /// * One in memory: `rudf::store::MemoryDataset` /// * One disk-based using [RocksDB](https://rocksdb.org/): `rudf::store::RocksDbDataset` /// /// Usage example with `rudf::store::MemoryDataset`: /// ``` /// use rudf::model::*; /// use rudf::store::MemoryDataset; /// use std::str::FromStr; /// /// let dataset = MemoryDataset::default(); /// let default_graph = dataset.default_graph(); /// let ex = NamedNode::from_str("http://example.com").unwrap(); /// let triple = Triple::new(ex.clone(), ex.clone(), ex.clone()); /// default_graph.insert(&triple); /// let results: Vec = dataset.quads_for_subject(&ex.into()).unwrap().map(|t| t.unwrap()).collect(); /// assert_eq!(vec![triple.in_graph(None)], results); /// ``` /// /// The implementation backed by RocksDB if disabled by default and requires the `"rocksdb"` feature to be activated. /// A `RocksDbDataset` could be built using `RocksDbDataset::open` and works just like its in-memory equivalent: /// ```ignore /// use rudf::store::RocksDbDataset; /// let dataset = RocksDbDataset::open("foo.db"); /// ``` pub trait Dataset { type NamedGraph: NamedGraph; type DefaultGraph: Graph; type UnionGraph: Graph; type QuadsIterator: Iterator>; type QuadsForSubjectIterator: Iterator>; type QuadsForSubjectPredicateIterator: Iterator>; type QuadsForSubjectPredicateObjectIterator: Iterator>; type QuadsForSubjectObjectIterator: Iterator>; type QuadsForPredicateIterator: Iterator>; type QuadsForPredicateObjectIterator: Iterator>; type QuadsForObjectIterator: Iterator>; /// Returns an object for a [named graph](https://www.w3.org/TR/rdf11-concepts/#dfn-named-graph) of this dataset. /// /// This named graph may be empty if no triple is in the graph yet. fn named_graph(&self, name: &NamedOrBlankNode) -> Result; /// Returns an object for the [default graph](https://www.w3.org/TR/rdf11-concepts/#dfn-default-graph) of this dataset fn default_graph(&self) -> Self::DefaultGraph; /// Returns a graph that is the union of all graphs contained in this dataset, including the default graph fn union_graph(&self) -> Self::UnionGraph; /// Returns all quads contained by the graph fn iter(&self) -> Result { self.quads() } /// Returns all quads contained by the graph fn quads(&self) -> Result; fn quads_for_subject( &self, subject: &NamedOrBlankNode, ) -> Result; fn quads_for_subject_predicate( &self, subject: &NamedOrBlankNode, predicate: &NamedNode, ) -> Result; fn quads_for_subject_predicate_object( &self, subject: &NamedOrBlankNode, predicate: &NamedNode, object: &Term, ) -> Result; fn quads_for_subject_object( &self, subject: &NamedOrBlankNode, object: &Term, ) -> Result; fn quads_for_predicate(&self, predicate: &NamedNode) -> Result; fn quads_for_predicate_object( &self, predicate: &NamedNode, object: &Term, ) -> Result; fn quads_for_object(&self, object: &Term) -> Result; /// Checks if this dataset contains a given quad fn contains(&self, quad: &Quad) -> Result; /// Adds a quad to this dataset fn insert(&self, quad: &Quad) -> Result<()>; /// Removes a quad from this dataset fn remove(&self, quad: &Quad) -> Result<()>; /// Returns the number of quads in this dataset fn len(&self) -> Result; /// Checks if this dataset contains a quad fn is_empty(&self) -> Result; }