Skip to content

Commit

Permalink
Update sophia to 0.8.0
Browse files Browse the repository at this point in the history
  • Loading branch information
seakayone committed Aug 2, 2024
1 parent 1a6e570 commit 11739e7
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 65 deletions.
10 changes: 2 additions & 8 deletions dsp-meta/src/api/convert/rdf/error.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,7 @@
use crate::error::DspMetaError;

impl From<sophia::iri::error::InvalidIri> for DspMetaError {
fn from(error: sophia::iri::error::InvalidIri) -> Self {
impl From<sophia::iri::InvalidIri> for DspMetaError {
fn from(error: sophia::iri::InvalidIri) -> Self {

Check warning on line 4 in dsp-meta/src/api/convert/rdf/error.rs

View check run for this annotation

Codecov / codecov/patch

dsp-meta/src/api/convert/rdf/error.rs#L4

Added line #L4 was not covered by tests
DspMetaError::SerializeToRdf(error.0)
}
}

impl From<sophia::term::TermError> for DspMetaError {
fn from(error: sophia::term::TermError) -> Self {
DspMetaError::SerializeToRdf(error.to_string())
}
}
24 changes: 14 additions & 10 deletions dsp-meta/src/api/convert/rdf/project.rs
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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);

Check warning on line 18 in dsp-meta/src/api/convert/rdf/project.rs

View check run for this annotation

Codecov / codecov/patch

dsp-meta/src/api/convert/rdf/project.rs#L18

Added line #L18 was not covered by tests

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.");

Check warning on line 26 in dsp-meta/src/api/convert/rdf/project.rs

View check run for this annotation

Codecov / codecov/patch

dsp-meta/src/api/convert/rdf/project.rs#L20-L26

Added lines #L20 - L26 were not covered by tests

graph
.insert(&project_iri, &rdf::type_, &project_class)
.insert(&project_iri, rdf::type_, project_class)

Check warning on line 29 in dsp-meta/src/api/convert/rdf/project.rs

View check run for this annotation

Codecov / codecov/patch

dsp-meta/src/api/convert/rdf/project.rs#L29

Added line #L29 was not covered by tests
.expect("insert of project triples into graph failed.");

let shortcode_graph = ShortcodeGraphDto(&self.0.shortcode).to_graph(&project_iri);

Check warning on line 32 in dsp-meta/src/api/convert/rdf/project.rs

View check run for this annotation

Codecov / codecov/patch

dsp-meta/src/api/convert/rdf/project.rs#L32

Added line #L32 was not covered by tests
graph
.insert_all(shortcode_graph.triples())
.expect("insert of project triples into graph failed.");
Expand Down
54 changes: 23 additions & 31 deletions dsp-meta/src/api/convert/rdf/project_metadata.rs
Original file line number Diff line number Diff line change
@@ -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,
}
Expand Down Expand Up @@ -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<PrefixMapPair> = vec![

Check warning on line 81 in dsp-meta/src/api/convert/rdf/project_metadata.rs

View check run for this annotation

Codecov / codecov/patch

dsp-meta/src/api/convert/rdf/project_metadata.rs#L81

Added line #L81 was not covered by tests
(
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()),

Check warning on line 84 in dsp-meta/src/api/convert/rdf/project_metadata.rs

View check run for this annotation

Codecov / codecov/patch

dsp-meta/src/api/convert/rdf/project_metadata.rs#L83-L84

Added lines #L83 - L84 were not covered by tests
),
(
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()),

Check warning on line 88 in dsp-meta/src/api/convert/rdf/project_metadata.rs

View check run for this annotation

Codecov / codecov/patch

dsp-meta/src/api/convert/rdf/project_metadata.rs#L87-L88

Added lines #L87 - L88 were not covered by tests
),
(
PrefixBox::new_unchecked("sdo".into()),
IriBox::new_unchecked("https://schema.org/".into()),
Prefix::new_unchecked("sdo".into()),
Iri::new_unchecked("https://schema.org/".into()),

Check warning on line 92 in dsp-meta/src/api/convert/rdf/project_metadata.rs

View check run for this annotation

Codecov / codecov/patch

dsp-meta/src/api/convert/rdf/project_metadata.rs#L91-L92

Added lines #L91 - L92 were not covered by tests
),
(
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()),

Check warning on line 96 in dsp-meta/src/api/convert/rdf/project_metadata.rs

View check run for this annotation

Codecov / codecov/patch

dsp-meta/src/api/convert/rdf/project_metadata.rs#L95-L96

Added lines #L95 - L96 were not covered by tests
),
(
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()),

Check warning on line 100 in dsp-meta/src/api/convert/rdf/project_metadata.rs

View check run for this annotation

Codecov / codecov/patch

dsp-meta/src/api/convert/rdf/project_metadata.rs#L99-L100

Added lines #L99 - L100 were not covered by tests
),
(
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()),

Check warning on line 104 in dsp-meta/src/api/convert/rdf/project_metadata.rs

View check run for this annotation

Codecov / codecov/patch

dsp-meta/src/api/convert/rdf/project_metadata.rs#L103-L104

Added lines #L103 - L104 were not covered by tests
),
];

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

Check warning on line 112 in dsp-meta/src/api/convert/rdf/project_metadata.rs

View check run for this annotation

Codecov / codecov/patch

dsp-meta/src/api/convert/rdf/project_metadata.rs#L111-L112

Added lines #L111 - L112 were not covered by tests
.serialize_graph(&self.graph)
.expect("Error serializing graph to turtle.")
.to_string();

out
}
}
Expand All @@ -138,14 +137,7 @@ impl From<ProjectMetadataGraphWrapper> 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();

Check warning on line 140 in dsp-meta/src/api/convert/rdf/project_metadata.rs

View check run for this annotation

Codecov / codecov/patch

dsp-meta/src/api/convert/rdf/project_metadata.rs#L140

Added line #L140 was not covered by tests
let result = ProjectMetadataGraph { graph };

trace!("The resulting graph\n{}", result.to_turtle_string());
Expand Down
30 changes: 14 additions & 16 deletions dsp-meta/src/api/convert/rdf/value/shortcode.rs
Original file line number Diff line number Diff line change
@@ -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<String>) -> LightGraph {

Check warning on line 10 in dsp-meta/src/api/convert/rdf/value/shortcode.rs

View check run for this annotation

Codecov / codecov/patch

dsp-meta/src/api/convert/rdf/value/shortcode.rs#L10

Added line #L10 was not covered by tests
// 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::<String>::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.");

Check warning on line 16 in dsp-meta/src/api/convert/rdf/value/shortcode.rs

View check run for this annotation

Codecov / codecov/patch

dsp-meta/src/api/convert/rdf/value/shortcode.rs#L14-L16

Added lines #L14 - L16 were not covered by tests

// mutating the graph
let mut graph: LightGraph = LightGraph::new();

graph
.insert(project_iri, has_shortcode, self.0.as_string().as_str())
.expect("triple inserted");

Check warning on line 23 in dsp-meta/src/api/convert/rdf/value/shortcode.rs

View check run for this annotation

Codecov / codecov/patch

dsp-meta/src/api/convert/rdf/value/shortcode.rs#L18-L23

Added lines #L18 - L23 were not covered by tests
graph
}
}

0 comments on commit 11739e7

Please sign in to comment.