field33_rdftk_core_temporary_fork/model/data_set/
mod.rs

1/*!
2Provides an implementation of the W3C
3[RDF 1.1: On Semantics of RDF Datasets](https://www.w3.org/TR/rdf11-datasets/) recommendation.
4Additional semantics taken from [RDF 1.1 TriG](https://www.w3.org/TR/trig/), _RDF Dataset Language_.
5
6# Example
7
8```rust
9use field33_rdftk_core_temporary_fork::model::data_set::{DataSet, DataSetRef};
10use field33_rdftk_core_temporary_fork::model::graph::GraphRef;
11use field33_rdftk_core_temporary_fork::model::statement::StatementRef;
12
13fn simple_dataset_writer(data_set: &DataSetRef)
14{
15    let data_set = data_set.borrow();
16    if let Some(graph) = data_set.default_graph() {
17        println!("{{");
18        simple_graph_writer(graph);
19        println!("}}");
20    }
21    for (name, graph) in data_set.graphs() {
22        println!("{} {{", name);
23        simple_graph_writer(graph);
24        println!("}}");
25    }
26}
27
28fn simple_graph_writer(graph: &GraphRef)
29{
30    let graph = graph.borrow();
31    for statement in graph.statements() {
32        println!("    {}", statement);
33    }
34}
35```
36
37*/
38
39use crate::model::features::Featured;
40use crate::model::graph::{GraphFactoryRef, GraphRef};
41use crate::model::Provided;
42use std::cell::RefCell;
43use std::collections::HashMap;
44use std::fmt::Debug;
45use std::rc::Rc;
46use std::sync::Arc;
47
48// ------------------------------------------------------------------------------------------------
49// Public Types
50// ------------------------------------------------------------------------------------------------
51
52///
53/// A data set factory provides an interface to create a new data set. This allows for
54/// implementations where underlying shared resources are required and so may be owned by the
55/// factory.
56///
57pub trait DataSetFactory: Debug + Provided {
58    ///
59    /// Create a new graph instance.
60    ///
61    fn data_set(&self, default_graph: Option<GraphRef>) -> DataSetRef;
62
63    ///
64    ///  Create a new graph instance from the given statements and prefix mappings.
65    ///
66    fn data_set_from(
67        &self,
68        default_graph: Option<GraphRef>,
69        graphs: HashMap<GraphNameRef, GraphRef>,
70    ) -> DataSetRef {
71        let data_set = self.data_set(default_graph);
72        {
73            let mut data_set = data_set.borrow_mut();
74            for (name, graph) in graphs {
75                data_set.insert(name, graph);
76            }
77        }
78        data_set
79    }
80}
81
82///
83/// The reference type for a graph factory returned by a graph.
84///
85pub type DataSetFactoryRef = Arc<dyn DataSetFactory>;
86
87///
88/// A `DataSet` is a mapping from `GraphName` to `Graph`; this introduces the notion of a named graph
89/// although in actuality the graph itself is not named as the name is the key within the data set.
90/// Note that this trait represents an immutable data set, a type should also implement the
91/// `MutableDataSet` trait for mutation.
92///
93pub trait DataSet: Debug + Featured {
94    ///
95    /// Returns `true` if there are no graphs in this data set, else `false`.
96    ///
97    fn is_empty(&self) -> bool;
98
99    ///
100    /// Return the number of graphs in this data set.
101    ///
102    fn len(&self) -> usize;
103
104    ///
105    /// Return `true` if this data set has a default graph, else `false`.
106    ///
107    fn has_default_graph(&self) -> bool;
108
109    ///
110    /// Return the default graph for this data set, if it exists.
111    ///
112    fn default_graph(&self) -> Option<&GraphRef>;
113
114    ///
115    /// Return `true` if this data set has a graph with the provided name, else `false`.
116    ///
117    fn has_graph_named(&self, name: &GraphNameRef) -> bool;
118
119    ///
120    /// Return the graph with the provided name from this data set, if it exists.
121    ///
122    fn graph_named(&self, name: &GraphNameRef) -> Option<&GraphRef>;
123
124    ///
125    /// Return an iterator over graph-name/graph pairs.
126    ///
127    fn graphs<'a>(&'a self) -> Box<dyn Iterator<Item = (&'a GraphNameRef, &'a GraphRef)> + 'a>;
128
129    ///
130    /// Set the provided graph as the default, unnamed graph, for this data set. Only one graph may
131    /// be the default.
132    ///
133    fn set_default_graph(&mut self, graph: GraphRef);
134
135    ///
136    /// Remove any graph that may be set as the current default. This operation has no effect if
137    /// no default graph is present.
138    fn unset_default_graph(&mut self);
139
140    ///
141    /// Insert a new graph with it's associated name into the data set.
142    ///
143    fn insert(&mut self, name: GraphNameRef, graph: GraphRef);
144
145    ///
146    /// Remove the graph with the provided name from this data set. This operation has no effect if
147    /// no such graph is present.
148    ///
149    fn remove(&mut self, name: &GraphNameRef);
150
151    ///
152    /// Remove all graphs from this data set.
153    ///
154    fn clear(&mut self);
155
156    ///
157    /// Return the factory that creates data sets using the same provider as `self`.
158    ///
159    /// Note that this uses Arc as a reference as factories are explicitly intended for cross-thread
160    /// usage.
161    ///
162    fn factory(&self) -> DataSetFactoryRef;
163
164    ///
165    /// Return the factory that creates graphs managed by data set's of this kind.
166    ///
167    /// Note that this uses Arc as a reference as factories are explicitly intended for cross-thread
168    /// usage.
169    ///
170    fn graph_factory(&self) -> GraphFactoryRef;
171}
172
173///
174/// The reference type for a graph data set.
175///
176pub type DataSetRef = Rc<RefCell<dyn DataSet>>;
177
178// ------------------------------------------------------------------------------------------------
179// Modules
180// ------------------------------------------------------------------------------------------------
181
182pub mod name;
183pub use name::{GraphName, GraphNameRef};