Skip to content

Commit 69ccc09

Browse files
committed
handle rdfs subclassof in template construction
1 parent da60f6e commit 69ccc09

File tree

2 files changed

+33
-1
lines changed

2 files changed

+33
-1
lines changed

buildingmotif/utils.py

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
from rdflib.term import Node
1515

1616
from buildingmotif.database.errors import TemplateNotFound
17-
from buildingmotif.namespaces import OWL, PARAM, RDF, SH, XSD, bind_prefixes
17+
from buildingmotif.namespaces import OWL, PARAM, RDF, RDFS, SH, XSD, bind_prefixes
1818

1919
if TYPE_CHECKING:
2020
from buildingmotif.dataclasses import Library, Template
@@ -224,6 +224,7 @@ def get_template_parts_from_shape(
224224
- sh:property with sh:qualifiedMinCount
225225
- sh:class
226226
- sh:node
227+
- rdfs:subClassOf
227228
228229
:param shape_name: name of shape
229230
:type shape_name: URIRef
@@ -297,6 +298,16 @@ def process_shape(shape_node: Node, focus_param: URIRef):
297298
if isinstance(cls, URIRef):
298299
body.add((focus_param, RDF.type, cls))
299300

301+
superclass_nodes: Set[Node] = set()
302+
for graph in [shape_graph, *depedency_graphs.values()]:
303+
superclass_nodes.update(graph.objects(shape_node, RDFS["subClassOf"]))
304+
305+
for superclass in superclass_nodes:
306+
if isinstance(superclass, URIRef) and is_nodeshape(superclass):
307+
add_dependency(superclass, focus_param)
308+
elif isinstance(superclass, URIRef):
309+
body.add((focus_param, RDF.type, superclass))
310+
300311
for node_shape in shape_graph.objects(shape_node, SH["node"]):
301312
if isinstance(node_shape, URIRef):
302313
add_dependency(node_shape, focus_param)

tests/unit/test_utils.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,27 @@ def test_get_template_parts_from_shape():
7979
# assert (PARAM['name'], BRICK.hasPoint,
8080

8181

82+
def test_get_template_parts_from_shape_rdfs_subclass():
83+
shape_graph = Graph()
84+
shape_graph.parse(
85+
data=PREAMBLE
86+
+ """
87+
:parentShape a owl:Class, sh:NodeShape .
88+
89+
:childShape a owl:Class, sh:NodeShape ;
90+
rdfs:subClassOf :parentShape, brick:Equipment .
91+
"""
92+
)
93+
94+
body, deps = get_template_parts_from_shape(MODEL["childShape"], shape_graph)
95+
96+
assert len(deps) == 1
97+
assert deps[0]["template"] == str(MODEL["parentShape"])
98+
assert deps[0]["args"] == {"name": PARAM["name"]}
99+
assert (PARAM["name"], A, MODEL["parentShape"]) in body
100+
assert (PARAM["name"], A, BRICK.Equipment) in body
101+
102+
82103
def test_replace_nodes():
83104
g = Graph()
84105
g.parse(

0 commit comments

Comments
 (0)