diff --git a/dsp-meta/src/api/convert/rdf/error.rs b/dsp-meta/src/api/convert/rdf/error.rs index 86c12584..bd014cc4 100644 --- a/dsp-meta/src/api/convert/rdf/error.rs +++ b/dsp-meta/src/api/convert/rdf/error.rs @@ -1,13 +1,7 @@ use crate::error::DspMetaError; -impl From for DspMetaError { - fn from(error: sophia::iri::error::InvalidIri) -> Self { +impl From for DspMetaError { + fn from(error: sophia::iri::InvalidIri) -> Self { DspMetaError::SerializeToRdf(error.0) } } - -impl From for DspMetaError { - fn from(error: sophia::term::TermError) -> Self { - DspMetaError::SerializeToRdf(error.to_string()) - } -} diff --git a/dsp-meta/src/api/convert/rdf/project.rs b/dsp-meta/src/api/convert/rdf/project.rs index 7dbb382c..45481f90 100644 --- a/dsp-meta/src/api/convert/rdf/project.rs +++ b/dsp-meta/src/api/convert/rdf/project.rs @@ -1,10 +1,10 @@ // placeholder for project use dsp_domain::metadata::entity::project::Project; -use sophia::graph::inmem::LightGraph; -use sophia::graph::{Graph, MutableGraph}; -use sophia::ns::{rdf, Namespace}; -use sophia::term::SimpleIri; +use sophia::api::graph::{Graph, MutableGraph}; +use sophia::api::ns::{rdf, Namespace}; +use sophia::inmem::graph::LightGraph; +use sophia::iri::Iri; use crate::api::convert::rdf::constance::DSP_NAMESPACE_STRING; use crate::api::convert::rdf::value::shortcode::ShortcodeGraphDto; @@ -15,17 +15,21 @@ impl ProjectGraphDto { let mut graph = LightGraph::new(); // http://ns.dasch.swiss/repository#dsp-081C-project a http://ns.dasch.swiss/repository#Project - let dsp = Namespace::new_unchecked(DSP_NAMESPACE_STRING); - let project_iri_suffix = format!("dsp-{}-project", self.0.shortcode.0); - let project_iri = SimpleIri::new_unchecked(dsp.as_ref(), Some(project_iri_suffix.as_str())); - let project_class = SimpleIri::new_unchecked(dsp.as_ref(), Some("Project")); + let ns = Namespace::new_unchecked(DSP_NAMESPACE_STRING); - let shortcode_graph = ShortcodeGraphDto(&self.0.shortcode).to_graph(&project_iri); + let project_iri = ns + .get(format!("{}dsp-{}-project", ns.as_str(), self.0.shortcode.0).as_str()) + .expect("project_iri creation failed.") + .to_string(); + let project_iri = Iri::new(project_iri).expect("project_iri creation failed."); + + let project_class = ns.get("Project").expect("project_class creation failed."); graph - .insert(&project_iri, &rdf::type_, &project_class) + .insert(&project_iri, rdf::type_, project_class) .expect("insert of project triples into graph failed."); + let shortcode_graph = ShortcodeGraphDto(&self.0.shortcode).to_graph(&project_iri); graph .insert_all(shortcode_graph.triples()) .expect("insert of project triples into graph failed."); diff --git a/dsp-meta/src/api/convert/rdf/project_metadata.rs b/dsp-meta/src/api/convert/rdf/project_metadata.rs index d950080c..f5dbfe4b 100644 --- a/dsp-meta/src/api/convert/rdf/project_metadata.rs +++ b/dsp-meta/src/api/convert/rdf/project_metadata.rs @@ -1,18 +1,17 @@ use dsp_domain::metadata::entity::project_metadata::ProjectMetadata; -use sophia::graph::inmem::LightGraph; -use sophia::graph::*; -use sophia::iri::IriBox; -use sophia::ns::Namespace; -use sophia::prefix::PrefixBox; -use sophia::serializer::turtle::{TurtleConfig, TurtleSerializer}; -use sophia::serializer::*; +use sophia::api::ns::Namespace; +use sophia::api::prefix::{Prefix, PrefixMapPair}; +use sophia::api::prelude::Stringifier; +use sophia::api::serializer::TripleSerializer; +use sophia::inmem::graph::LightGraph; +use sophia::iri::Iri; +use sophia::turtle::serializer::turtle::{TurtleConfig, TurtleSerializer}; use tracing::trace; use crate::api::convert::rdf::constance::{ DSP_NAMESPACE_STRING, PROV_NAMESPACE_STRING, SCHEMA_NAMESPACE_STRING, XSD_NAMESPACE_STRING, }; use crate::api::convert::rdf::project::ProjectGraphDto; - pub struct ProjectMetadataGraph { graph: LightGraph, } @@ -79,41 +78,41 @@ impl ProjectMetadataGraph { /// sdo:url "https://admin.dasch.swiss/project/081C" ] . /// ``` pub fn to_turtle_string(&self) -> String { - let prefix_map = vec![ + let prefix_map: Vec = vec![ ( - PrefixBox::new_unchecked("dsp".into()), - IriBox::new_unchecked("http://ns.dasch.swiss/repository#".into()), + Prefix::new_unchecked("dsp".into()), + Iri::new_unchecked("http://ns.dasch.swiss/repository#".into()), ), ( - PrefixBox::new_unchecked("prov".into()), - IriBox::new_unchecked("http://www.w3.org/ns/prov#".into()), + Prefix::new_unchecked("prov".into()), + Iri::new_unchecked("http://www.w3.org/ns/prov#".into()), ), ( - PrefixBox::new_unchecked("sdo".into()), - IriBox::new_unchecked("https://schema.org/".into()), + Prefix::new_unchecked("sdo".into()), + Iri::new_unchecked("https://schema.org/".into()), ), ( - PrefixBox::new_unchecked("rdf".into()), - IriBox::new_unchecked("http://www.w3.org/1999/02/22-rdf-syntax-ns#".into()), + Prefix::new_unchecked("rdf".into()), + Iri::new_unchecked("http://www.w3.org/1999/02/22-rdf-syntax-ns#".into()), ), ( - PrefixBox::new_unchecked("rdfs".into()), - IriBox::new_unchecked("http://www.w3.org/2000/01/rdf-schema#".into()), + Prefix::new_unchecked("rdfs".into()), + Iri::new_unchecked("http://www.w3.org/2000/01/rdf-schema#".into()), ), ( - PrefixBox::new_unchecked("xsd".into()), - IriBox::new_unchecked("http://www.w3.org/2001/XMLSchema#".into()), + Prefix::new_unchecked("xsd".into()), + Iri::new_unchecked("http://www.w3.org/2001/XMLSchema#".into()), ), ]; let config = TurtleConfig::new() .with_pretty(true) .with_own_prefix_map(prefix_map); - let out = TurtleSerializer::new_stringifier_with_config(config) + let mut serializer = TurtleSerializer::new_stringifier_with_config(config); + let out = serializer .serialize_graph(&self.graph) .expect("Error serializing graph to turtle.") .to_string(); - out } } @@ -138,14 +137,7 @@ impl From for ProjectMetadataGraph { let _xsd = Namespace::new_unchecked(XSD_NAMESPACE_STRING); - let mut graph: LightGraph = LightGraph::new(); - - let project_graph = ProjectGraphDto(value.0.project).to_graph(); - - graph - .insert_all(project_graph.triples()) - .expect("insert of project triples into project metadata graph failed"); - + let graph = ProjectGraphDto(value.0.project).to_graph(); let result = ProjectMetadataGraph { graph }; trace!("The resulting graph\n{}", result.to_turtle_string()); diff --git a/dsp-meta/src/api/convert/rdf/value/shortcode.rs b/dsp-meta/src/api/convert/rdf/value/shortcode.rs index 9938fd4a..6823004c 100644 --- a/dsp-meta/src/api/convert/rdf/value/shortcode.rs +++ b/dsp-meta/src/api/convert/rdf/value/shortcode.rs @@ -1,28 +1,26 @@ use dsp_domain::metadata::value::Shortcode; -use sophia::graph::inmem::LightGraph; -use sophia::graph::MutableGraph; -use sophia::ns::Namespace; -use sophia::term::literal::Literal; -use sophia::term::SimpleIri; +use sophia::api::graph::MutableGraph; +use sophia::api::ns::Namespace; +use sophia::inmem::graph::LightGraph; +use sophia::iri::Iri; pub(crate) struct ShortcodeGraphDto<'a>(pub &'a Shortcode); impl<'a> ShortcodeGraphDto<'a> { - pub fn to_graph(&self, project_iri: &SimpleIri) -> LightGraph { - let mut graph: LightGraph = LightGraph::new(); - + pub fn to_graph(&self, project_iri: &Iri) -> LightGraph { // http://ns.dasch.swiss/repository#dsp-081C-project http://ns.dasch.swiss/repository#hasShortcode "081C"^^http://www.w3.org/2001/XMLSchema#string let dsp = Namespace::new_unchecked("http://ns.dasch.swiss/repository#"); - let has_shortcode_property = SimpleIri::new_unchecked(dsp.as_ref(), Some("hasShortcode")); - graph - .insert( - project_iri, - &has_shortcode_property, - &Literal::::new_lang_unchecked(&self.0.as_string(), "en"), - ) - .expect("insert of shortcode triples into graph failed."); + let has_shortcode = dsp + .get("hasShortcode") + .expect("has_shortcode_property creation failed."); + // mutating the graph + let mut graph: LightGraph = LightGraph::new(); + + graph + .insert(project_iri, has_shortcode, self.0.as_string().as_str()) + .expect("triple inserted"); graph } }