Skip to content

Commit

Permalink
Merge pull request #285 from RDFLib/fix_target_sh_or
Browse files Browse the repository at this point in the history
Fix when using targeted shapes, when shape has sh:or, sh:xone or sh:and
  • Loading branch information
ashleysommer authored Feb 9, 2025
2 parents ed3667a + 24da2d4 commit 7184d66
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 4 deletions.
7 changes: 6 additions & 1 deletion pyshacl/shapes_graph.py
Original file line number Diff line number Diff line change
Expand Up @@ -416,7 +416,12 @@ def _gather_shapes(shapes_nodes: Sequence[Union[rdflib.URIRef, rdflib.BNode]], r
for _p in has_shape_expecting_p.keys():
property_entries = list(g.objects(s, _p))
for p_e in property_entries:
if isinstance(p_e, rdflib.BNode):
if _p in (SH_or, SH_xone, SH_and):
# These are list-expecting variants of shape-expecting constraints.
for item in g.items(p_e):
if isinstance(item, rdflib.BNode):
_found_child_bnodes.append(item)
elif isinstance(p_e, rdflib.BNode):
_found_child_bnodes.append(p_e)
if len(_found_child_bnodes) > 0:
_gather_shapes(_found_child_bnodes, recurse_depth=recurse_depth + 1)
Expand Down
22 changes: 19 additions & 3 deletions test/test_manual_targeting.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,10 +84,11 @@
exShape:AnimalShape a sh:NodeShape ;
sh:property [
sh:datatype xsd:integer ;
sh:path exOnt:nLegs ;
sh:maxInclusive 4 ;
sh:minInclusive 1 ;
sh:or (
[ sh:datatype xsd:integer ; sh:minInclusive 0 ; sh:maxInclusive 0 ]
[ sh:datatype xsd:integer ; sh:minInclusive 1 ; sh:maxInclusive 8 ]
) ;
] ;
sh:targetClass exOnt:Animal .
"""
Expand Down Expand Up @@ -195,6 +196,21 @@ def test_validate_fail_manual_targeting_shape():
assert "Results (2)" in string
assert not conforms

def test_validate_fail_manual_targeting_shape_or():
res = validate(
data_file_text_bad,
shacl_graph=shacl_file_text,
data_graph_format='turtle',
shacl_graph_format='turtle',
ont_graph=ontology_file_text,
ont_graph_format="turtle",
inference='rdfs',
use_shapes=["exShape:AnimalShape"],
debug=True,
)
conforms, graph, string = res
assert "Results (2)" in string
assert not conforms

def test_validate_fail_manual_targeting_focus_with_shape():
res = validate(
Expand Down

0 comments on commit 7184d66

Please sign in to comment.