Skip to content

Commit

Permalink
further ingetration
Browse files Browse the repository at this point in the history
  • Loading branch information
recalcitrantsupplant committed Jan 23, 2024
1 parent 40799e7 commit 21deaee
Showing 1 changed file with 41 additions and 33 deletions.
74 changes: 41 additions & 33 deletions temp/shacl-nodeshapes2sparql.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand All @@ -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):
Expand Down

0 comments on commit 21deaee

Please sign in to comment.