1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243
/*! Traits which describe the core capabilities of a graph. Note that this crate does not provide an implementation of these traits as they are very dependent on their usage for performance, and any backing storage. # Example ```rust use rdftk_core::Graph; fn simple_graph_writer<'a>(graph: &'a impl Graph<'a>) { for statement in graph.statements() { println!("{}", statement); } } ``` */ use crate::statement::{ObjectNodeRef, StatementRef, SubjectNodeRef}; use rdftk_iri::IRIRef; use std::collections::HashSet; use std::fmt::{Display, Formatter}; use std::rc::Rc; // ------------------------------------------------------------------------------------------------ // Public Types // ------------------------------------------------------------------------------------------------ /// /// This enumeration describes the combination of triples that may be indexed by the `Graph`. The /// indexes actually supported by a graph implementation can be retrieved using /// `Graph::has_index` or `Graph::has_indices`. /// #[derive(Clone, Debug, PartialEq, Eq, Hash)] pub enum GraphIndex { #[allow(missing_docs)] Subject, #[allow(missing_docs)] Predicate, #[allow(missing_docs)] Object, #[allow(missing_docs)] SubjectPredicate, #[allow(missing_docs)] SubjectPredicateObject, #[allow(missing_docs)] SubjectObject, #[allow(missing_docs)] PredicateObject, } /// /// A graph is an unordered list of statements and may include duplicates. /// Note that this trait represents an immutable graph, a type should also implement the /// `MutableGraph` trait for mutation. /// pub trait Graph<'a> { /// /// The type used to return an iterator over the statements in this graph. /// type StatementIter: Iterator<Item = &'a StatementRef>; /// /// The type used to return an filtered iterator over the statements in this graph. /// type FilteredIter: Iterator<Item = &'a StatementRef>; /// /// Returns `true` if there are no statements in this graph, else `false`. /// fn is_empty(&self) -> bool; /// /// Return the number of statements in this graph. /// fn len(&self) -> usize; /// /// Returns `true` if this graph contains any statement with the provided subject, else `false`. /// fn contains_subject(&self, subject: &SubjectNodeRef) -> bool; /// /// Returns `true` if this graph contains any statement with the provided IRI as subject, else /// `false`. /// fn contains_individual(&self, subject: &IRIRef) -> bool; /// /// Returns `true` if this graph contains the provided statement, else `false`. /// fn contains(&self, statement: &StatementRef) -> bool; /// /// Returns `true` if this graph contains the any statement with the provided subject, /// predicate, and object, else `false`. /// fn contains_all( &self, subject: &SubjectNodeRef, predicate: &IRIRef, object: &ObjectNodeRef, ) -> bool; /// /// Return an iterator over all the statements in the graph. /// fn statements(&'a self) -> Self::StatementIter; /// /// Return an iterator over the statements in the graph that match the provided predicate. /// fn filter(&'a self, predicate: fn(&&StatementRef) -> bool) -> Self::FilteredIter; /// /// Return a set of all subjects in the graph, note that this is a set so that it removes /// duplicates. /// fn subjects(&self) -> HashSet<&SubjectNodeRef>; /// /// Return a set of all predicate in the graph, note that this is a set so that it removes /// duplicates. /// fn predicates(&self) -> HashSet<&IRIRef>; /// /// Return a set of all predicate referenced by the provided subject in the graph, note that /// this is a set so that it removes duplicates. /// fn predicates_for(&self, subject: &SubjectNodeRef) -> HashSet<&IRIRef>; /// /// Return a set of all objects in the graph, note that this is a set so that it removes /// duplicates. /// fn objects(&self) -> HashSet<&ObjectNodeRef>; /// /// Return a set of all objects referenced by the provided subject and predicate in the graph, /// note that this is a set so that it removes duplicates. /// fn objects_for(&self, subject: &SubjectNodeRef, predicate: &IRIRef) -> HashSet<&ObjectNodeRef>; /// /// Returns `true` if this graph has an index of the specified kind, else `false`. /// fn has_index(&self, index: &GraphIndex) -> bool; /// /// Returns `true` if this graph has **all* the specified index kinds, else `false`. /// fn has_indices(&self, indices: &[GraphIndex]) -> bool { indices.iter().all(|i| self.has_index(i)) } /// /// Returns the set of prefix mappings held by the graph. /// fn prefix_mappings(&self) -> Rc<dyn PrefixMappings>; /// /// Returns the value factory that is associated with this graph, if present. /// fn value_factory(&self) -> Option<Rc<dyn ValueFactory>> { None } } /// /// This trait provides the set of common mutation operations on a graph. /// pub trait MutableGraph<'a>: Graph<'a> { /// /// Insert a new statement into the graph. /// fn insert(&mut self, statement: StatementRef); /// /// Merge another graph into this one. Note that the graphs are required to have the same /// implementation type based in the type qualifiers for `StatementIter` and `FilteredIter`. /// fn merge( &mut self, other: &'a Rc< dyn Graph<'a, StatementIter = Self::StatementIter, FilteredIter = Self::FilteredIter>, >, ); /// /// Remove any duplicates within the graph, replacing any number of identical statements with /// just one. /// fn dedup(&mut self); /// /// Remove any statement that matches the provided. If a graph has duplicates this method does /// not differentiate between them. /// fn remove(&mut self, statement: &StatementRef); /// /// Remove all statements from this graph that have the provided subject. /// fn remove_all_for(&mut self, subject: &SubjectNodeRef); /// /// Remove all statements from this graph. /// fn clear(&mut self); } // ------------------------------------------------------------------------------------------------ // Implementations // ------------------------------------------------------------------------------------------------ impl Display for GraphIndex { fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result { write!( f, "{}", match self { GraphIndex::Subject => "S", GraphIndex::Predicate => "P", GraphIndex::Object => "O", GraphIndex::SubjectPredicate => "SP", GraphIndex::SubjectPredicateObject => "SPO", GraphIndex::SubjectObject => "SO", GraphIndex::PredicateObject => "PO", } ) } } // ------------------------------------------------------------------------------------------------ // Modules // ------------------------------------------------------------------------------------------------ pub mod caching; pub use caching::ValueFactory; pub mod mapping; pub use mapping::{Prefix, PrefixMappings};