use std::collections::BTreeMap;
use std::path::Path;
use sbol_core::document::{ObjectStore, RawDocument};
use crate::client::{
Activity, Agent, Association, Attachment, BinaryPrefix, Collection, CombinatorialDerivation,
Component, ComponentDefinition, CompoundUnit, Cut, Experiment, ExperimentalData,
FunctionalComponent, GenericLocation, GenericTopLevel, IdentifiedExtension, Implementation,
Interaction, MapsTo, Measure, Model, Module, ModuleDefinition, Participation, Plan, Prefix,
PrefixedUnit, Range, SIPrefix, Sbol2Object, Sequence, SequenceAnnotation, SequenceConstraint,
SingularUnit, TryFromObject, Unit, UnitDivision, UnitExponentiation, UnitMultiplication, Usage,
VariableComponent,
};
use crate::error::{ReadError, WriteError};
use crate::object::{canonicalize_literals, collect_objects};
use crate::validation::Validator;
use crate::validation::{ValidationConfig, ValidationOptions, ValidationReport};
use crate::{Iri, Object, RdfFormat, RdfGraph, Resource};
#[derive(Clone, Debug)]
#[non_exhaustive]
pub struct Document {
raw: RawDocument,
typed: Vec<Sbol2Object>,
}
macro_rules! typed_doc_iter {
($method:ident, $variant:ident, $ty:ty) => {
pub fn $method(&self) -> impl Iterator<Item = &$ty> {
self.typed.iter().filter_map(|o| match o {
Sbol2Object::$variant(v) => Some(v),
_ => None,
})
}
};
}
impl Document {
pub fn read(input: &str, format: RdfFormat) -> Result<Self, ReadError> {
let graph = RdfGraph::parse(input, format).map_err(ReadError::Rdf)?;
Ok(Self::from_rdf_graph(graph))
}
pub fn read_path(path: impl AsRef<Path>) -> Result<Self, ReadError> {
let path = path.as_ref();
let format = RdfFormat::from_path(path).ok_or_else(|| ReadError::UnknownFormat {
path: path.to_path_buf(),
extension: path
.extension()
.and_then(|ext| ext.to_str())
.map(str::to_owned),
})?;
let input = std::fs::read_to_string(path).map_err(|source| ReadError::Io {
path: path.to_path_buf(),
source,
})?;
Self::read(&input, format)
}
pub fn read_turtle(input: &str) -> Result<Self, ReadError> {
Self::read(input, RdfFormat::Turtle)
}
pub fn from_rdf_graph(graph: RdfGraph) -> Self {
let graph = canonicalize_literals(&graph);
let objects = collect_objects(&graph);
let typed = objects
.values()
.filter_map(Sbol2Object::try_from_object)
.collect();
Self {
raw: RawDocument::from_parts(graph, objects),
typed,
}
}
pub(crate) fn from_parts(
graph: RdfGraph,
objects: BTreeMap<Resource, Object>,
typed: Vec<Sbol2Object>,
) -> Self {
Self {
raw: RawDocument::from_parts(graph, objects),
typed,
}
}
pub fn into_raw(self) -> RawDocument {
self.raw
}
pub fn write(&self, format: RdfFormat) -> Result<String, WriteError> {
self.raw.write(format)
}
pub fn write_path(&self, path: impl AsRef<Path>, format: RdfFormat) -> Result<(), WriteError> {
self.raw.write_path(path, format)
}
pub fn write_turtle(&self) -> Result<String, WriteError> {
self.raw.write_turtle()
}
pub fn rdf_graph(&self) -> &RdfGraph {
self.raw.rdf_graph()
}
pub fn objects(&self) -> &BTreeMap<Resource, Object> {
self.raw.objects()
}
pub fn get(&self, identity: &Resource) -> Option<&Object> {
self.raw.get(identity)
}
pub fn diff<S: ObjectStore + ?Sized>(&self, other: &S) -> crate::Diff {
crate::Diff::compute(self, other)
}
pub fn typed_objects(&self) -> &[Sbol2Object] {
&self.typed
}
pub fn resolve(&self, identity: &Resource) -> Option<&Sbol2Object> {
self.typed.iter().find(|o| o.identity() == identity)
}
pub fn find_by_display_id(&self, namespace: &str, display_id: &str) -> Option<&Sbol2Object> {
let prefix = if namespace.ends_with('/') {
namespace.to_owned()
} else {
format!("{namespace}/")
};
let exact = format!("{prefix}{display_id}");
let versioned = format!("/{display_id}/");
let tail = format!("/{display_id}");
self.typed.iter().find(|object| {
let iri = match object.identity().as_iri() {
Some(iri) => iri.as_str(),
None => return false,
};
if iri == exact {
return true;
}
iri.starts_with(&prefix) && (iri.contains(&versioned) || iri.ends_with(&tail))
})
}
typed_doc_iter!(sequences, Sequence, Sequence);
typed_doc_iter!(
component_definitions,
ComponentDefinition,
ComponentDefinition
);
typed_doc_iter!(module_definitions, ModuleDefinition, ModuleDefinition);
typed_doc_iter!(models, Model, Model);
typed_doc_iter!(collections, Collection, Collection);
typed_doc_iter!(
combinatorial_derivations,
CombinatorialDerivation,
CombinatorialDerivation
);
typed_doc_iter!(implementations, Implementation, Implementation);
typed_doc_iter!(attachments, Attachment, Attachment);
typed_doc_iter!(experimental_data, ExperimentalData, ExperimentalData);
typed_doc_iter!(experiments, Experiment, Experiment);
typed_doc_iter!(generic_top_levels, GenericTopLevel, GenericTopLevel);
typed_doc_iter!(components, Component, Component);
typed_doc_iter!(
functional_components,
FunctionalComponent,
FunctionalComponent
);
typed_doc_iter!(modules, Module, Module);
typed_doc_iter!(maps_tos, MapsTo, MapsTo);
typed_doc_iter!(sequence_annotations, SequenceAnnotation, SequenceAnnotation);
typed_doc_iter!(sequence_constraints, SequenceConstraint, SequenceConstraint);
typed_doc_iter!(variable_components, VariableComponent, VariableComponent);
typed_doc_iter!(interactions, Interaction, Interaction);
typed_doc_iter!(participations, Participation, Participation);
typed_doc_iter!(ranges, Range, Range);
typed_doc_iter!(cuts, Cut, Cut);
typed_doc_iter!(generic_locations, GenericLocation, GenericLocation);
typed_doc_iter!(activities, Activity, Activity);
typed_doc_iter!(agents, Agent, Agent);
typed_doc_iter!(plans, Plan, Plan);
typed_doc_iter!(associations, Association, Association);
typed_doc_iter!(usages, Usage, Usage);
typed_doc_iter!(measures, Measure, Measure);
typed_doc_iter!(units, Unit, Unit);
typed_doc_iter!(singular_units, SingularUnit, SingularUnit);
typed_doc_iter!(compound_units, CompoundUnit, CompoundUnit);
typed_doc_iter!(unit_multiplications, UnitMultiplication, UnitMultiplication);
typed_doc_iter!(unit_divisions, UnitDivision, UnitDivision);
typed_doc_iter!(unit_exponentiations, UnitExponentiation, UnitExponentiation);
typed_doc_iter!(prefixed_units, PrefixedUnit, PrefixedUnit);
typed_doc_iter!(prefixes, Prefix, Prefix);
typed_doc_iter!(si_prefixes, SIPrefix, SIPrefix);
typed_doc_iter!(binary_prefixes, BinaryPrefix, BinaryPrefix);
typed_doc_iter!(
identified_extensions,
IdentifiedExtension,
IdentifiedExtension
);
pub fn top_levels(&self) -> impl Iterator<Item = &Sbol2Object> {
self.typed.iter().filter(|o| o.is_top_level_object())
}
pub fn validate(&self) -> ValidationReport {
self.validate_with(ValidationOptions::default())
}
pub fn validate_with(&self, options: ValidationOptions) -> ValidationReport {
let mut validator = Validator::new(self, options);
validator.validate();
validator.finish()
}
pub fn validate_with_config(&self, config: &ValidationConfig) -> ValidationReport {
self.validate_with(ValidationOptions::default().with_config(*config))
}
pub fn check(&self) -> Result<ValidationReport, ValidationReport> {
self.check_with(ValidationOptions::default())
}
pub fn check_with(
&self,
options: ValidationOptions,
) -> Result<ValidationReport, ValidationReport> {
let report = self.validate_with(options);
if report.has_errors() {
Err(report)
} else {
Ok(report)
}
}
pub fn namespaces(&self) -> Vec<Iri> {
let mut seen: std::collections::BTreeSet<String> = std::collections::BTreeSet::new();
let mut out = Vec::new();
for object in &self.typed {
if let Some(ns) = object.top_level_namespace()
&& seen.insert(ns.as_str().to_owned())
{
out.push(ns);
}
}
out
}
}
impl ObjectStore for Document {
fn objects(&self) -> &BTreeMap<Resource, Object> {
self.raw.objects()
}
fn get(&self, identity: &Resource) -> Option<&Object> {
self.raw.get(identity)
}
}