From 21deaeee2e1f101afe26afa57aba68e484e2848c Mon Sep 17 00:00:00 2001 From: david Date: Wed, 24 Jan 2024 09:57:18 +1000 Subject: [PATCH] further ingetration --- temp/shacl-nodeshapes2sparql.py | 74 ++++++++++++++++++--------------- 1 file changed, 41 insertions(+), 33 deletions(-) diff --git a/temp/shacl-nodeshapes2sparql.py b/temp/shacl-nodeshapes2sparql.py index ccb65970..a9eafafb 100644 --- a/temp/shacl-nodeshapes2sparql.py +++ b/temp/shacl-nodeshapes2sparql.py @@ -4,9 +4,9 @@ from pydantic import BaseModel from rdflib import URIRef -from rdflib.namespace import SH +from rdflib.namespace import SH, RDF -from temp.grammar import IRI +from temp.grammar import IRI, SimplifiedTriple, TriplesBlock class SHACL(BaseModel): @@ -20,38 +20,46 @@ def to_grammar(self): raise NotImplementedError("Subclasses must implement this method.") - class NodeShape(SHACL): - uri: URIRef - nodeTarget: Optional[URIRef] - classTarget: Optional[List[URIRef]] - subjectsOfTarget: Optional[URIRef] - objectsOfTarget: Optional[URIRef] - propertyShapes: Optional[List[PropertyShape]] - - def from_graph(self, graph): - self.nodeTarget = next(graph.objects(self.uri, SH.targetNode), None) - self.classTarget = list(graph.objects(self.uri, SH.targetClass)) - self.subjectsOfTarget = next(graph.value(self.uri, SH.targetSubjectsOf), None) - self.objectsOfTarget = next(graph.objects(self.uri, SH.targetObjectsOf), None) - self.propertyShapes = list(graph.objects(self.uri, SH.property)) - - def to_grammar(self): - if self.nodeTarget: - pass # do not need to add any specific triples or the like - if self.classTarget: - pass - if self.subjectsOfTarget: - pass - if self.objectsOfTarget: - pass - if self.propertyShapes: - pass - - def _process_node_target(self): - target_uri = IRI(value=self.nodeTarget) - - def _process_property_shapes(self, property_shapes): +class NodeShape(SHACL): + uri: URIRef + nodeTarget: Optional[URIRef] + classTarget: Optional[List[URIRef]] + subjectsOfTarget: Optional[URIRef] + objectsOfTarget: Optional[URIRef] + propertyShapes: Optional[List[PropertyShape]] + _triples: Optional[List[SimplifiedTriple]] + + def from_graph(self, graph): + self.nodeTarget = next(graph.objects(self.uri, SH.targetNode), None) + self.classTarget = list(graph.objects(self.uri, SH.targetClass)) + self.subjectsOfTarget = next(graph.value(self.uri, SH.targetSubjectsOf), None) + self.objectsOfTarget = next(graph.objects(self.uri, SH.targetObjectsOf), None) + self.propertyShapes = list(graph.objects(self.uri, SH.property)) + + def to_grammar(self) -> TriplesBlock: + if self.nodeTarget: + pass # do not need to add any specific triples or the like + if self.classTarget: + self._process_class_target() + if self.subjectsOfTarget: pass + if self.objectsOfTarget: + pass + if self.propertyShapes: + pass + + def _process_class_target(self): + for klass in self.classTarget: + self._triples.append( + SimplifiedTriple( + subject=self.focus_node, + predicate=IRI(value=RDF.type), + object=klass, + ) + ) + + def _process_property_shapes(self, property_shapes): + pass class PropertyShape(SHACL):