use crate::graph::GraphRef;
use std::cell::RefCell;
use std::collections::HashMap;
use std::fmt::{Display, Formatter};
use std::rc::Rc;
use std::sync::Arc;
#[derive(Clone, Debug, PartialEq, Eq, Hash)]
pub enum DataSetIndex {
#[allow(missing_docs)]
Graph,
#[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,
#[allow(missing_docs)]
SubjectGraph,
#[allow(missing_docs)]
PredicateGraph,
#[allow(missing_docs)]
ObjectGraph,
#[allow(missing_docs)]
SubjectPredicateGraph,
#[allow(missing_docs)]
SubjectPredicateObjectGraph,
#[allow(missing_docs)]
SubjectObjectGraph,
#[allow(missing_docs)]
PredicateObjectGraph,
}
pub trait DataSetFactory {
fn new_data_set(&self, default_graph: Option<GraphRef>) -> DataSetRef;
fn data_set_from(
&self,
default_graph: Option<GraphRef>,
graphs: HashMap<GraphNameRef, GraphRef>,
) -> DataSetRef {
let data_set = self.new_data_set(default_graph);
{
let mut data_set = data_set.borrow_mut();
for (name, graph) in graphs {
data_set.insert(name, graph);
}
}
data_set
}
}
pub type DataSetFactoryRef = Arc<dyn DataSetFactory>;
pub type DataSetRef = Rc<RefCell<dyn DataSet>>;
pub trait DataSet {
fn is_empty(&self) -> bool;
fn len(&self) -> usize;
fn has_default_graph(&self) -> bool;
fn default_graph(&self) -> Option<&GraphRef>;
fn has_graph_named(&self, name: &GraphNameRef) -> bool;
fn graph_named(&self, name: &GraphNameRef) -> Option<&GraphRef>;
fn graphs<'a>(&'a self) -> Box<dyn Iterator<Item = (&'a GraphNameRef, &'a GraphRef)> + 'a>;
fn has_index(&self, index: &DataSetIndex) -> bool;
fn has_indices(&self, indices: &[DataSetIndex]) -> bool {
indices.iter().all(|i| self.has_index(i))
}
fn set_default_graph(&mut self, graph: GraphRef);
fn unset_default_graph(&mut self);
fn insert(&mut self, name: GraphNameRef, graph: GraphRef);
fn remove(&mut self, name: &GraphNameRef);
fn clear(&mut self);
fn factory(&self) -> DataSetFactoryRef;
}
impl Display for DataSetIndex {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
write!(
f,
"{}",
match self {
DataSetIndex::Subject => "S",
DataSetIndex::Predicate => "P",
DataSetIndex::Object => "O",
DataSetIndex::SubjectPredicate => "SP",
DataSetIndex::SubjectPredicateObject => "SPO",
DataSetIndex::SubjectObject => "SO",
DataSetIndex::PredicateObject => "PO",
DataSetIndex::Graph => "G",
DataSetIndex::SubjectGraph => "SG",
DataSetIndex::PredicateGraph => "PG",
DataSetIndex::ObjectGraph => "OG",
DataSetIndex::SubjectPredicateGraph => "SPG",
DataSetIndex::SubjectPredicateObjectGraph => "SPOG",
DataSetIndex::SubjectObjectGraph => "SGO",
DataSetIndex::PredicateObjectGraph => "POG",
}
)
}
}
pub mod name;
pub use name::{GraphName, GraphNameRef};