Skip to content

Commit

Permalink
Added RDF data serializer to rudof
Browse files Browse the repository at this point in the history
  • Loading branch information
labra committed Nov 13, 2024
1 parent 2eea1cb commit b557624
Show file tree
Hide file tree
Showing 15 changed files with 149 additions and 54 deletions.
17 changes: 7 additions & 10 deletions python/examples/dctap2shex.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
from pyrudof import Rudof, RudofConfig, ShExFormat, ShExFormatter, DCTapFormat


from pyrudof import Rudof, RudofConfig, ShExFormatter

rudof = Rudof(RudofConfig())
dctap_str = """shapeId,propertyId,Mandatory,Repeatable,valueDatatype,valueShape
Person,name,true,false,xsd:string,
,birthdate,false,false,xsd:date,
,worksFor,false,true,,Company
Company,name,true,false,xsd:string,
,employee,false,true,,Person
Person,name,true,false,xsd:string,
,birthdate,false,false,xsd:date,
,enrolledIn,false,true,,Course
Course,name,true,false,xsd:string,
,student,false,true,,Person
"""
rudof.read_dctap_str(dctap_str)

Expand All @@ -17,5 +15,4 @@

rudof.dctap2shex()
result = rudof.serialize_shex(ShExFormatter())
print(f"DCTAP converted to ShEx\n{result}")

print(f"DCTAP converted to ShEx\n{result}")
10 changes: 10 additions & 0 deletions python/examples/endpoint.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
from pyrudof import Rudof, RudofConfig, RDFFormat

endpoint = "https://sparql.uniprot.org/sparql"
rudof = Rudof(RudofConfig())

rudof.add_endpoint(endpoint)

result = rudof.serialize_data(format = RDFFormat.NTriples)

print(result)
25 changes: 25 additions & 0 deletions python/examples/rdf_data.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
from pyrudof import Rudof, RudofConfig, RDFFormat

data_str = """prefix xsd: <http://www.w3.org/2001/XMLSchema#>
prefix : <http://example.org/>
:alice :name "Alice" ;
:birthdate "1980-03-02"^^xsd:date ;
:enrolledIn :cs101 ;
:knows :bob .
:bob :name "Robert" ;
:birthdate "1981-03-02"^^xsd:date ;
:enrolledIn :cs101 ;
:knows :alice .
:cs101 :name "Computer Science 101";
:student :alice, :bob .
"""
rudof = Rudof(RudofConfig())

rudof.read_data_str(data_str)

result = rudof.serialize_data(format = RDFFormat.NTriples)

print(result)
2 changes: 1 addition & 1 deletion python/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ pub mod pyrudof {
PyDCTAP, PyDCTapFormat, PyRDFFormat, PyReaderMode, PyRudof, PyRudofConfig, PyRudofError,
PyShExFormat, PyShExFormatter, PyShaclFormat, PyShaclValidationMode, PyShapeMapFormat,
PyShapeMapFormatter, PyShapesGraphSource, PyUmlGenerationMode, PyValidationReport,
PyValidationStatus,
PyValidationStatus,
};

#[pymodule_init]
Expand Down
19 changes: 19 additions & 0 deletions python/src/pyrudof_lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -283,6 +283,25 @@ impl PyRudof {
Ok(())
}

/// Serialize the current ShEx schema
#[pyo3(signature = (format = &PyRDFFormat::Turtle))]
pub fn serialize_data(&self, format: &PyRDFFormat) -> PyResult<String> {
let mut v = Vec::new();
let format = cnv_rdf_format(format);
self.inner
.serialize_data(&format, &mut v)
.map_err(|e| RudofError::SerializingData {
error: format!("{e}"),
})
.map_err(cnv_err)?;
let str = String::from_utf8(v)
.map_err(|e| RudofError::SerializingData {
error: format!("{e}"),
})
.map_err(cnv_err)?;
Ok(str)
}

/// Reads the current Shapemap from a String
#[pyo3(signature = (input,format = &PyShapeMapFormat::Compact))]
pub fn read_shapemap_str(&mut self, input: &str, format: &PyShapeMapFormat) -> PyResult<()> {
Expand Down
9 changes: 3 additions & 6 deletions rudof_cli/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,7 @@ use shex_ast::object_value::ObjectValue;
use shex_ast::{ShapeExprLabel, SimpleReprSchema};
use sparql_service::{QueryConfig, RdfData, ServiceDescription};
use srdf::srdf_graph::SRDFGraph;
use srdf::{
QuerySolution, RDFFormat, RdfDataConfig, ReaderMode, SRDFBuilder, SRDFSparql, VarName, SRDF,
};
use srdf::{QuerySolution, RDFFormat, RdfDataConfig, ReaderMode, SRDFSparql, VarName, SRDF};
use std::collections::HashMap;
use std::fs::{File, OpenOptions};
use std::io::{self, BufWriter, Read, Write};
Expand Down Expand Up @@ -1346,9 +1344,8 @@ fn run_data(
let (mut writer, _color) = get_writer(output, force_overwrite)?;
let mut rudof = Rudof::new(config);
get_data_rudof(&mut rudof, data, data_format, &None, reader_mode, config)?;
rudof
.get_rdf_data()
.serialize(RDFFormat::from(*result_format), &mut writer)?;
let format: RDFFormat = RDFFormat::from(*result_format);
rudof.get_rdf_data().serialize(&format, &mut writer)?;
Ok(())
}

Expand Down
10 changes: 9 additions & 1 deletion rudof_lib/src/rudof.rs
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,14 @@ impl Rudof {
}
}

pub fn serialize_data<W: io::Write>(&self, format: &RDFFormat, writer: &mut W) -> Result<()> {
self.rdf_data
.serialize(format, writer)
.map_err(|e| RudofError::SerializingData {
error: format!("{e}"),
})
}

/// Serialize the current ShapeMap
pub fn serialize_shapemap<W: io::Write>(
&self,
Expand Down Expand Up @@ -255,7 +263,7 @@ impl Rudof {
shacl: format!("{:?}", shacl.clone()),
error: format!("{e}"),
})?;
shacl_writer.serialize(data_format, writer).map_err(|e| {
shacl_writer.serialize(&data_format, writer).map_err(|e| {
RudofError::SerializingSHACL {
error: format!("{e}"),
shacl: format!("{:?}", shacl.clone()),
Expand Down
3 changes: 3 additions & 0 deletions rudof_lib/src/rudof_error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,9 @@ pub enum RudofError {
#[error("Serializing ShEx: {error}")]
SerializingShEx { error: String },

#[error("Serializing RDF data: {error}")]
SerializingData { error: String },

#[error("Serializing ShEx: {error}")]
SerializingShacl { error: String },

Expand Down
2 changes: 1 addition & 1 deletion shacl_ast/src/converter/shacl_to_rdf/shacl_writer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ where
Ok(())
}

pub fn serialize<W: Write>(&self, format: RDFFormat, writer: &mut W) -> Result<(), RDF::Err> {
pub fn serialize<W: Write>(&self, format: &RDFFormat, writer: &mut W) -> Result<(), RDF::Err> {
self.rdf.serialize(format, writer)
}
}
Expand Down
60 changes: 30 additions & 30 deletions shapemap/src/result_shape_map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,8 +71,8 @@ impl ResultShapeMap {
shape_label: ShapeLabel,
status: ValidationStatus,
) -> Result<(), ShapemapError> {
let cn = node.clone();
let sl = shape_label.clone();
let _cn = node.clone();
let _sl = shape_label.clone();
match self.result.entry(node) {
Entry::Occupied(mut c) => {
let map = c.get_mut();
Expand All @@ -89,37 +89,37 @@ impl ResultShapeMap {
)
}
(
ValidationStatus::Conformant(conformant_info),
ValidationStatus::NonConformant(non_conformant_info),
ValidationStatus::Conformant(_conformant_info),
ValidationStatus::NonConformant(_non_conformant_info),
) => todo!(),
(
ValidationStatus::Conformant(conformant_info),
ValidationStatus::Conformant(_conformant_info),
ValidationStatus::Pending,
) => {}
(
ValidationStatus::Conformant(conformant_info),
ValidationStatus::Conformant(_conformant_info),
ValidationStatus::Inconsistent(
conformant_info2,
non_conformant_info2,
_conformant_info2,
_non_conformant_info2,
),
) => todo!(),
(
ValidationStatus::NonConformant(non_conformant_info),
ValidationStatus::Conformant(conformant_info),
ValidationStatus::NonConformant(_non_conformant_info),
ValidationStatus::Conformant(_conformant_info),
) => todo!(),
(
ValidationStatus::NonConformant(non_conformant_info),
ValidationStatus::NonConformant(non_conformant_info2),
ValidationStatus::NonConformant(_non_conformant_info),
ValidationStatus::NonConformant(_non_conformant_info2),
) => {}
(
ValidationStatus::NonConformant(non_conformant_info),
ValidationStatus::NonConformant(_non_conformant_info),
ValidationStatus::Pending,
) => {}
(
ValidationStatus::NonConformant(non_conformant_info),
ValidationStatus::NonConformant(_non_conformant_info),
ValidationStatus::Inconsistent(
conformant_info2,
non_conformant_info2,
_conformant_info2,
_non_conformant_info2,
),
) => todo!(),
(
Expand All @@ -136,39 +136,39 @@ impl ResultShapeMap {
(
ValidationStatus::Pending,
ValidationStatus::Inconsistent(
conformant_info,
non_conformant_info,
_conformant_info,
_non_conformant_info,
),
) => todo!(),
(
ValidationStatus::Inconsistent(
conformant_info,
non_conformant_info,
_conformant_info,
_non_conformant_info,
),
ValidationStatus::Conformant(conformant_info2),
ValidationStatus::Conformant(_conformant_info2),
) => todo!(),
(
ValidationStatus::Inconsistent(
conformant_info,
non_conformant_info,
_conformant_info,
_non_conformant_info,
),
ValidationStatus::NonConformant(non_conformant_info2),
ValidationStatus::NonConformant(_non_conformant_info2),
) => todo!(),
(
ValidationStatus::Inconsistent(
conformant_info,
non_conformant_info,
_conformant_info,
_non_conformant_info,
),
ValidationStatus::Pending,
) => todo!(),
(
ValidationStatus::Inconsistent(
conformant_info,
non_conformant_info,
_conformant_info,
_non_conformant_info,
),
ValidationStatus::Inconsistent(
conformant_info2,
non_conformant_info2,
_conformant_info2,
_non_conformant_info2,
),
) => todo!(),
};
Expand Down
21 changes: 20 additions & 1 deletion sparql_service/src/srdf_data/rdf_data.rs
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,25 @@ impl RdfData {
let str: String = format!("{}", lit);
format!("{}", str.red())
}

pub fn serialize<W: io::Write>(
&self,
format: &RDFFormat,
writer: &mut W,
) -> Result<(), RdfDataError> {
if let Some(graph) = &self.graph {
graph
.serialize(format, writer)
.map_err(|e| RdfDataError::Serializing {
format: *format,
error: format!("{e}"),
})?
}
for e in self.endpoints.iter() {
writeln!(writer, "Endpoint {}", e.iri())?
}
Ok(())
}
}

impl Default for RdfData {
Expand Down Expand Up @@ -670,7 +689,7 @@ impl SRDFBuilder for RdfData {

fn serialize<W: std::io::Write>(
&self,
format: RDFFormat,
format: &RDFFormat,
writer: &mut W,
) -> Result<(), Self::Err> {
if let Some(graph) = &self.graph {
Expand Down
5 changes: 4 additions & 1 deletion sparql_service/src/srdf_data/rdf_data_error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use oxigraph::{
};
use thiserror::Error;

use srdf::{SRDFGraphError, SRDFSparqlError};
use srdf::{RDFFormat, SRDFGraphError, SRDFSparqlError};

#[derive(Debug, Error)]
pub enum RdfDataError {
Expand All @@ -28,6 +28,9 @@ pub enum RdfDataError {
err: io::Error,
},

#[error("Serializing RDF Data as {format}: {error}")]
Serializing { error: String, format: RDFFormat },

#[error(transparent)]
StorageError {
#[from]
Expand Down
14 changes: 14 additions & 0 deletions srdf/src/rdf_format.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use std::fmt::Display;
use std::str::FromStr;

use crate::RDFParseError;
Expand Down Expand Up @@ -31,3 +32,16 @@ impl FromStr for RDFFormat {
}
}
}

impl Display for RDFFormat {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
RDFFormat::Turtle => write!(f, "Turtle"),
RDFFormat::NTriples => write!(f, "N-Triples"),
RDFFormat::RDFXML => write!(f, "RDF/XML"),
RDFFormat::TriG => write!(f, "TriG"),
RDFFormat::N3 => write!(f, "N3"),
RDFFormat::NQuads => write!(f, "NQuads"),
}
}
}
2 changes: 1 addition & 1 deletion srdf/src/srdf_builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,5 +39,5 @@ pub trait SRDFBuilder: SRDF {
fn add_type(&mut self, node: &RDFNode, type_: Self::Term) -> Result<(), Self::Err>;

/// Serialize the current graph to a Write implementation
fn serialize<W: Write>(&self, format: RDFFormat, writer: &mut W) -> Result<(), Self::Err>;
fn serialize<W: Write>(&self, format: &RDFFormat, writer: &mut W) -> Result<(), Self::Err>;
}
4 changes: 2 additions & 2 deletions srdf/src/srdf_graph/srdfgraph.rs
Original file line number Diff line number Diff line change
Expand Up @@ -705,7 +705,7 @@ impl SRDFBuilder for SRDFGraph {
}
}

fn serialize<W: Write>(&self, format: RDFFormat, write: &mut W) -> Result<(), Self::Err> {
fn serialize<W: Write>(&self, format: &RDFFormat, write: &mut W) -> Result<(), Self::Err> {
let mut serializer = RdfSerializer::from_format(cnv_rdf_format(format));

for (prefix, iri) in &self.pm.map {
Expand All @@ -721,7 +721,7 @@ impl SRDFBuilder for SRDFGraph {
}
}

fn cnv_rdf_format(rdf_format: RDFFormat) -> RdfFormat {
fn cnv_rdf_format(rdf_format: &RDFFormat) -> RdfFormat {
match rdf_format {
RDFFormat::NTriples => RdfFormat::NTriples,
RDFFormat::Turtle => RdfFormat::Turtle,
Expand Down

0 comments on commit b557624

Please sign in to comment.