Skip to content

Commit

Permalink
fix: Improved pyrodof to show representations
Browse files Browse the repository at this point in the history
  • Loading branch information
labra committed Oct 31, 2024
1 parent f6bd439 commit 5c359e3
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 25 deletions.
18 changes: 13 additions & 5 deletions python/examples/dctap2shex.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,19 @@


rudof = Rudof(RudofConfig())
rudof.read_dctap_str("""shapeId,shapeLabel,propertyId,Mandatory,Repeatable,valueDatatype,valueShape
Person,Shape or person,name,true,false,xsd:string,
,,birthdate,false,false,xsd:date""")
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
"""
rudof.read_dctap_str(dctap_str)

dctap = rudof.get_dctap()
print(f"DCTAP\n{dctap}")

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

4 changes: 2 additions & 2 deletions python/examples/shex2uml.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
from pyrudof import Rudof, RudofConfig, UmlGenerationMode, ShExFormat

rudof = Rudof(RudofConfig())

rudof.read_shex_str("""
Expand All @@ -16,8 +15,9 @@
:name xsd:string ;
:employee @:Person * ;
}
""", ShExFormat.shexc())
""")

uml_str = rudof.shex2plantuml(UmlGenerationMode.all())

print(uml_str)

6 changes: 6 additions & 0 deletions python/examples/show_pyrydof_classes.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import pyrudof
import inspect

classes = [cls_name for cls_name, cls_obj in inspect.getmembers(pyrudof)
if inspect.isclass(cls_obj)]
print(classes)
64 changes: 46 additions & 18 deletions python/src/pyrudof_lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,11 @@ impl PyRudofConfig {
}
}

/// Main class to handle `rudof` features.
/// It is currently `unsendable` and doesn't support multiple threads.
/// There should be only one instance of `rudof` per program.
///
// TODO: review the unsendable constraint and check if we can remove it in the future
#[pyclass(unsendable, name = "Rudof")]
pub struct PyRudof {
inner: Rudof,
Expand Down Expand Up @@ -306,7 +311,7 @@ impl PyRudof {
pub fn shex2plantuml(&self, uml_mode: &PyUmlGenerationMode) -> PyResult<String> {
let mut v = Vec::new();
self.inner
.shex2plant_uml(&uml_mode.inner, &mut v)
.shex2plant_uml(&uml_mode.into(), &mut v)
.map_err(|e| RudofError::ShEx2PlantUmlError {
error: format!("Error generating UML: {e}"),
})
Expand Down Expand Up @@ -515,31 +520,51 @@ impl PyShapeMapFormatter {
}
}

#[pyclass(frozen, name = "UmlGenerationMode")]
pub struct PyUmlGenerationMode {
inner: UmlGenerationMode,
#[pyclass(name = "UmlGenerationMode")]
pub enum PyUmlGenerationMode {
/// Generate UML for all nodes
#[pyo3(name = "AllNodes")]
PyAllNodes {},

/// Generate UML only for the neighbours of a shape
#[pyo3(constructor = (node), name ="Neighs")]
PyNeighs { node: String },
}

#[pymethods]
impl PyUmlGenerationMode {
#[new]
pub fn __init__(py: Python<'_>) -> Self {
py.allow_threads(|| Self {
inner: UmlGenerationMode::all(),
})
py.allow_threads(|| PyUmlGenerationMode::PyAllNodes {})
}

#[staticmethod]
pub fn all() -> Self {
Self {
inner: UmlGenerationMode::all(),
}
PyUmlGenerationMode::PyAllNodes {}
}

#[staticmethod]
pub fn neighs(node: &str) -> Self {
Self {
inner: UmlGenerationMode::neighs(node),
PyUmlGenerationMode::PyNeighs {
node: node.to_string(),
}
}
}

impl From<&PyUmlGenerationMode> for UmlGenerationMode {
fn from(m: &PyUmlGenerationMode) -> UmlGenerationMode {
match m {
PyUmlGenerationMode::PyAllNodes {} => UmlGenerationMode::AllNodes,
PyUmlGenerationMode::PyNeighs { node } => UmlGenerationMode::Neighs(node.to_string()),
}
}
}

impl From<UmlGenerationMode> for PyUmlGenerationMode {
fn from(value: UmlGenerationMode) -> Self {
match value {
UmlGenerationMode::AllNodes => PyUmlGenerationMode::PyAllNodes {},
UmlGenerationMode::Neighs(node) => PyUmlGenerationMode::PyNeighs { node },
}
}
}
Expand All @@ -549,6 +574,7 @@ pub struct PyShExSchema {
inner: ShExSchema,
}

#[pymethods]
impl PyShExSchema {
pub fn __repr__(&self) -> String {
format!("{}", self.inner)
Expand All @@ -560,23 +586,24 @@ pub struct PyDCTAP {
inner: DCTAP,
}

#[pymethods]
impl PyDCTAP {
pub fn __repr__(&self) -> String {
format!("{}", self.inner)
}

pub fn __str__(&self) -> String {
format!("{}", self.inner)
}
}

#[pyclass(name = "QueryShapeMap")]
pub struct PyQueryShapeMap {
inner: QueryShapeMap,
}

#[pymethods]
impl PyQueryShapeMap {
pub fn serialize(&self, _format: &ShExFormat) -> String {
let result = &self.inner;
format!("{result:?}")
}

fn __repr__(&self) -> String {
format!("{}", self.inner)
}
Expand All @@ -587,6 +614,7 @@ pub struct PyShaclSchema {
inner: ShaclSchema,
}

#[pymethods]
impl PyShaclSchema {
pub fn __repr__(&self) -> String {
format!("{}", self.inner)
Expand Down Expand Up @@ -661,7 +689,7 @@ impl PyValidationStatus {
}
}

#[pyclass]
#[pyclass(name = "RudofError")]
/// Wrapper for `RudofError`
pub struct PyRudofError {
error: RudofError,
Expand Down

0 comments on commit 5c359e3

Please sign in to comment.