diff --git a/python/examples/dctap2shex.py b/python/examples/dctap2shex.py index cab285a7..de43e4ec 100644 --- a/python/examples/dctap2shex.py +++ b/python/examples/dctap2shex.py @@ -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) @@ -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}") \ No newline at end of file diff --git a/python/examples/endpoint.py b/python/examples/endpoint.py new file mode 100644 index 00000000..3474bfb7 --- /dev/null +++ b/python/examples/endpoint.py @@ -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) \ No newline at end of file diff --git a/python/examples/rdf_data.py b/python/examples/rdf_data.py new file mode 100644 index 00000000..b1783546 --- /dev/null +++ b/python/examples/rdf_data.py @@ -0,0 +1,25 @@ +from pyrudof import Rudof, RudofConfig, RDFFormat + +data_str = """prefix xsd: +prefix : + +: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) \ No newline at end of file diff --git a/python/src/lib.rs b/python/src/lib.rs index 0e9147c1..5ad8f7f5 100644 --- a/python/src/lib.rs +++ b/python/src/lib.rs @@ -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] diff --git a/python/src/pyrudof_lib.rs b/python/src/pyrudof_lib.rs index b1f47799..1b54e0a2 100644 --- a/python/src/pyrudof_lib.rs +++ b/python/src/pyrudof_lib.rs @@ -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 { + 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<()> { diff --git a/rudof_cli/src/main.rs b/rudof_cli/src/main.rs index 524b6c13..90ff419d 100755 --- a/rudof_cli/src/main.rs +++ b/rudof_cli/src/main.rs @@ -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}; @@ -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(()) } diff --git a/rudof_lib/src/rudof.rs b/rudof_lib/src/rudof.rs index 42446578..5fc69f5a 100644 --- a/rudof_lib/src/rudof.rs +++ b/rudof_lib/src/rudof.rs @@ -166,6 +166,14 @@ impl Rudof { } } + pub fn serialize_data(&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( &self, @@ -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()), diff --git a/rudof_lib/src/rudof_error.rs b/rudof_lib/src/rudof_error.rs index 8c5da86b..e5abff86 100644 --- a/rudof_lib/src/rudof_error.rs +++ b/rudof_lib/src/rudof_error.rs @@ -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 }, diff --git a/shacl_ast/src/converter/shacl_to_rdf/shacl_writer.rs b/shacl_ast/src/converter/shacl_to_rdf/shacl_writer.rs index 81a227fb..966b9c41 100644 --- a/shacl_ast/src/converter/shacl_to_rdf/shacl_writer.rs +++ b/shacl_ast/src/converter/shacl_to_rdf/shacl_writer.rs @@ -35,7 +35,7 @@ where Ok(()) } - pub fn serialize(&self, format: RDFFormat, writer: &mut W) -> Result<(), RDF::Err> { + pub fn serialize(&self, format: &RDFFormat, writer: &mut W) -> Result<(), RDF::Err> { self.rdf.serialize(format, writer) } } diff --git a/shapemap/src/result_shape_map.rs b/shapemap/src/result_shape_map.rs index 49bc2cf3..80e4877d 100644 --- a/shapemap/src/result_shape_map.rs +++ b/shapemap/src/result_shape_map.rs @@ -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(); @@ -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!(), ( @@ -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!(), }; diff --git a/sparql_service/src/srdf_data/rdf_data.rs b/sparql_service/src/srdf_data/rdf_data.rs index b0266d65..6d296f96 100644 --- a/sparql_service/src/srdf_data/rdf_data.rs +++ b/sparql_service/src/srdf_data/rdf_data.rs @@ -159,6 +159,25 @@ impl RdfData { let str: String = format!("{}", lit); format!("{}", str.red()) } + + pub fn serialize( + &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 { @@ -670,7 +689,7 @@ impl SRDFBuilder for RdfData { fn serialize( &self, - format: RDFFormat, + format: &RDFFormat, writer: &mut W, ) -> Result<(), Self::Err> { if let Some(graph) = &self.graph { diff --git a/sparql_service/src/srdf_data/rdf_data_error.rs b/sparql_service/src/srdf_data/rdf_data_error.rs index dba76b58..3d444731 100644 --- a/sparql_service/src/srdf_data/rdf_data_error.rs +++ b/sparql_service/src/srdf_data/rdf_data_error.rs @@ -6,7 +6,7 @@ use oxigraph::{ }; use thiserror::Error; -use srdf::{SRDFGraphError, SRDFSparqlError}; +use srdf::{RDFFormat, SRDFGraphError, SRDFSparqlError}; #[derive(Debug, Error)] pub enum RdfDataError { @@ -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] diff --git a/srdf/src/rdf_format.rs b/srdf/src/rdf_format.rs index 6fadd0e8..8f634bbc 100644 --- a/srdf/src/rdf_format.rs +++ b/srdf/src/rdf_format.rs @@ -1,3 +1,4 @@ +use std::fmt::Display; use std::str::FromStr; use crate::RDFParseError; @@ -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"), + } + } +} diff --git a/srdf/src/srdf_builder.rs b/srdf/src/srdf_builder.rs index c1c4a647..51d57cc2 100644 --- a/srdf/src/srdf_builder.rs +++ b/srdf/src/srdf_builder.rs @@ -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(&self, format: RDFFormat, writer: &mut W) -> Result<(), Self::Err>; + fn serialize(&self, format: &RDFFormat, writer: &mut W) -> Result<(), Self::Err>; } diff --git a/srdf/src/srdf_graph/srdfgraph.rs b/srdf/src/srdf_graph/srdfgraph.rs index 26a70409..0fb2fb00 100644 --- a/srdf/src/srdf_graph/srdfgraph.rs +++ b/srdf/src/srdf_graph/srdfgraph.rs @@ -705,7 +705,7 @@ impl SRDFBuilder for SRDFGraph { } } - fn serialize(&self, format: RDFFormat, write: &mut W) -> Result<(), Self::Err> { + fn serialize(&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 { @@ -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,