Skip to content

Commit

Permalink
Merge branch 'master' into rule_mode
Browse files Browse the repository at this point in the history
  • Loading branch information
ashleysommer committed Oct 11, 2024
2 parents acbd5a2 + 81b91a4 commit 850b162
Show file tree
Hide file tree
Showing 8 changed files with 295 additions and 10 deletions.
51 changes: 41 additions & 10 deletions pyshacl/constraints/core/logical_constraints.py
Original file line number Diff line number Diff line change
Expand Up @@ -160,10 +160,19 @@ def constraint_name(cls) -> str:
return "AndConstraintComponent"

def make_generic_messages(self, datagraph: GraphLike, focus_node, value_node) -> List[rdflib.Literal]:
and_list = " , ".join(
stringify_node(self.shape.sg.graph, a_c) for a in self.and_list for a_c in self.shape.sg.graph.items(a)
)
m = "Node {} must conform to all shapes in {}".format(stringify_node(datagraph, value_node), and_list)
if len(self.and_list) < 2:
and_node_string = " , ".join(
stringify_node(self.shape.sg.graph, a_c) for a_c in self.shape.sg.graph.items(self.and_list[0])
)
else:
and_node_strings = []
for a in self.and_list:
and_node_string1 = " , ".join(
stringify_node(self.shape.sg.graph, a_c) for a_c in self.shape.sg.graph.items(a)
)
and_node_strings.append(f"({and_node_string1})")
and_node_string = " and ".join(and_node_strings)
m = "Node {} must conform to all shapes in {}".format(stringify_node(datagraph, value_node), and_node_string)
return [rdflib.Literal(m)]

def evaluate(
Expand Down Expand Up @@ -256,10 +265,21 @@ def constraint_name(cls) -> str:
return "OrConstraintComponent"

def make_generic_messages(self, datagraph: GraphLike, focus_node, value_node) -> List[rdflib.Literal]:
or_list = " , ".join(
stringify_node(self.shape.sg.graph, o_c) for o in self.or_list for o_c in self.shape.sg.graph.items(o)
if len(self.or_list) < 2:
or_node_string = " , ".join(
stringify_node(self.shape.sg.graph, o_c) for o_c in self.shape.sg.graph.items(self.or_list[0])
)
else:
or_node_strings = []
for a in self.or_list:
or_node_string1 = " , ".join(
stringify_node(self.shape.sg.graph, a_c) for a_c in self.shape.sg.graph.items(a)
)
or_node_strings.append(f"({or_node_string1})")
or_node_string = " and ".join(or_node_strings)
m = "Node {} must conform to one or more shapes in {}".format(
stringify_node(datagraph, value_node), or_node_string
)
m = "Node {} must conform to one or more shapes in {}".format(stringify_node(datagraph, value_node), or_list)
return [rdflib.Literal(m)]

def evaluate(
Expand Down Expand Up @@ -352,10 +372,21 @@ def constraint_name(cls) -> str:
return "XoneConstraintComponent"

def make_generic_messages(self, datagraph: GraphLike, focus_node, value_node) -> List[rdflib.Literal]:
xone_list = " , ".join(
stringify_node(self.shape.sg.graph, a_c) for a in self.xone_nodes for a_c in self.shape.sg.graph.items(a)
if len(self.xone_nodes) < 2:
xone_node_string = " , ".join(
stringify_node(self.shape.sg.graph, a_c) for a_c in self.shape.sg.graph.items(self.xone_nodes[0])
)
else:
xone_node_strings = []
for a in self.xone_nodes:
xone_node_string1 = " , ".join(
stringify_node(self.shape.sg.graph, a_c) for a_c in self.shape.sg.graph.items(a)
)
xone_node_strings.append(f"({xone_node_string1})")
xone_node_string = " and ".join(xone_node_strings)
m = "Node {} must conform to exactly one shape in {}".format(
stringify_node(datagraph, value_node), xone_node_string
)
m = "Node {} must conform to exactly one shape in {}".format(stringify_node(datagraph, value_node), xone_list)
return [rdflib.Literal(m)]

def evaluate(
Expand Down
35 changes: 35 additions & 0 deletions test/issues/test_220/LICENSE.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
# NIST Software Licensing Statement

NIST-developed software is provided by NIST as a public service.
You may use, copy, and distribute copies of the software in any
medium, provided that you keep intact this entire notice. You may
improve, modify, and create derivative works of the software or
any portion of the software, and you may copy and distribute such
modifications or works. Modified works should carry a notice
stating that you changed the software and should note the date
and nature of any such change. Please explicitly acknowledge the
National Institute of Standards and Technology as the source of
the software.

NIST-developed software is expressly provided "AS IS." NIST MAKES
NO WARRANTY OF ANY KIND, EXPRESS, IMPLIED, IN FACT, OR ARISING BY
OPERATION OF LAW, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
WARRANTY OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE,
NON-INFRINGEMENT, AND DATA ACCURACY. NIST NEITHER REPRESENTS NOR
WARRANTS THAT THE OPERATION OF THE SOFTWARE WILL BE UNINTERRUPTED
OR ERROR-FREE, OR THAT ANY DEFECTS WILL BE CORRECTED. NIST DOES
NOT WARRANT OR MAKE ANY REPRESENTATIONS REGARDING THE USE OF THE
SOFTWARE OR THE RESULTS THEREOF, INCLUDING BUT NOT LIMITED TO THE
CORRECTNESS, ACCURACY, RELIABILITY, OR USEFULNESS OF THE
SOFTWARE.

You are solely responsible for determining the appropriateness of
using and distributing the software and you assume all risks
associated with its use, including but not limited to the risks
and costs of program errors, compliance with applicable laws,
damage to or loss of data, programs or equipment, and the
unavailability or interruption of operation. This software is not
intended to be used in any situation where a failure could cause
risk of injury or damage to property. The software developed by
NIST employees is not subject to copyright protection within the
United States.
30 changes: 30 additions & 0 deletions test/issues/test_220/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
# -*- coding: utf-8 -*-
#
"""
https://github.com/RDFLib/pySHACL/issues/220
"""
from pyshacl import validate
from pyshacl.rdfutil import load_from_source

data_graph1 = load_from_source("./test/issues/test_220/kb-REPRODUCTION-1.ttl")
shacl_graph1 = load_from_source("./test/issues/test_220/sh-REPRODUCTION-1.ttl")
data_graph2 = load_from_source("./test/issues/test_220/kb-REPRODUCTION-2.ttl")
shacl_graph2 = load_from_source("./test/issues/test_220/sh-REPRODUCTION-2.ttl")
ont_graph = load_from_source("./test/issues/test_220/owl-REPRODUCTION.ttl")


def test_220_1():
conforms, g, s = validate(data_graph=data_graph1, shacl_graph=shacl_graph1, ont_graph=ont_graph, inference='none')
assert conforms


def test_220_2():
conforms, g, s = validate(data_graph=data_graph2, shacl_graph=shacl_graph2, ont_graph=ont_graph, inference='none')
assert not conforms
assert "Results (2)" in s


if __name__ == "__main__":
test_220_1()
test_220_2()
14 changes: 14 additions & 0 deletions test/issues/test_220/kb-REPRODUCTION-1.ttl
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
@prefix ex: <http://example.org/ontology/> .
@prefix kb: <http://example.org/kb/> .
@prefix owl: <http://www.w3.org/2002/07/owl#> .
@prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> .
@prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#> .
@prefix xsd: <http://www.w3.org/2001/XMLSchema#> .

kb:thing-PASS-2
a
ex:ThingC ,
ex:ThingE
;
rdfs:comment "This should raise no validation results."@en ;
.
29 changes: 29 additions & 0 deletions test/issues/test_220/kb-REPRODUCTION-2.ttl
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
@prefix ex: <http://example.org/ontology/> .
@prefix kb: <http://example.org/kb/> .
@prefix owl: <http://www.w3.org/2002/07/owl#> .
@prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> .
@prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#> .
@prefix xsd: <http://www.w3.org/2001/XMLSchema#> .

kb:thing-PASS-1
a ex:ThingA ;
rdfs:comment "This should raise no validation results."@en ;
.

kb:thing-PASS-2
a
ex:ThingC ,
ex:ThingE
;
rdfs:comment "This should raise no validation results."@en ;
.

kb:thing-XFAIL-1
a ex:OneXorThing ;
rdfs:comment "This should raise a validation result, due to not being more specifically a ThingA or ThingB as the SHACL requires when reviewing the OWL disjoint union definition of OneXorThing."@en ;
.

kb:thing-XFAIL-2
a ex:ThingC ;
rdfs:comment "This should raise a validation result, due to not being a ThingE or ThingF as the SHACL requires when reviewing the entailed superclass TwoXorThing."@en ;
.
63 changes: 63 additions & 0 deletions test/issues/test_220/owl-REPRODUCTION.ttl
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
@prefix ex: <http://example.org/ontology/> .
@prefix owl: <http://www.w3.org/2002/07/owl#> .
@prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> .
@prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#> .
@prefix xsd: <http://www.w3.org/2001/XMLSchema#> .

ex:OneXorThing
a owl:Class ;
owl:disjointUnionOf (
ex:ThingA
ex:ThingB
) ;
.

ex:ThingA
a owl:Class ;
rdfs:subClassOf ex:OneXorThing ;
owl:disjointWith ex:ThingB ;
.

ex:ThingB
a owl:Class ;
rdfs:subClassOf ex:OneXorThing ;
owl:disjointWith ex:ThingA ;
.

ex:ThingC
a owl:Class ;
rdfs:subClassOf ex:TwoXorThing ;
owl:disjointWith ex:ThingD ;
.

ex:ThingD
a owl:Class ;
rdfs:subClassOf ex:TwoXorThing ;
owl:disjointWith ex:ThingC ;
.

ex:ThingE
a owl:Class ;
rdfs:subClassOf ex:TwoXorThing ;
owl:disjointWith ex:ThingF ;
.

ex:ThingF
a owl:Class ;
rdfs:subClassOf ex:TwoXorThing ;
owl:disjointWith ex:ThingE ;
.

ex:TwoXorThing
a owl:Class ;
owl:disjointUnionOf
(
ex:ThingC
ex:ThingD
) ,
(
ex:ThingE
ex:ThingF
)
;
.
34 changes: 34 additions & 0 deletions test/issues/test_220/sh-REPRODUCTION-1.ttl
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
@prefix ex: <http://example.org/ontology/> .
@prefix sh: <http://www.w3.org/ns/shacl#> .
@prefix sh-ex: <http://example.org/shapes/> .
@prefix owl: <http://www.w3.org/2002/07/owl#> .
@prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> .
@prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#> .
@prefix xsd: <http://www.w3.org/2001/XMLSchema#> .

sh-ex:TwoXorThing-shape
a sh:NodeShape ;
sh:targetClass ex:TwoXorThing ;
sh:xone
(
[
a sh:NodeShape ;
sh:class ex:ThingC ;
]
[
a sh:NodeShape ;
sh:class ex:ThingD ;
]
) ,
(
[
a sh:NodeShape ;
sh:class ex:ThingE ;
]
[
a sh:NodeShape ;
sh:class ex:ThingF ;
]
)
;
.
49 changes: 49 additions & 0 deletions test/issues/test_220/sh-REPRODUCTION-2.ttl
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
@prefix ex: <http://example.org/ontology/> .
@prefix sh: <http://www.w3.org/ns/shacl#> .
@prefix sh-ex: <http://example.org/shapes/> .
@prefix owl: <http://www.w3.org/2002/07/owl#> .
@prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> .
@prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#> .
@prefix xsd: <http://www.w3.org/2001/XMLSchema#> .

sh-ex:OneXorThing-shape
a sh:NodeShape ;
sh:targetClass ex:OneXorThing ;
sh:xone (
[
a sh:NodeShape ;
sh:class ex:ThingA ;
]
[
a sh:NodeShape ;
sh:class ex:ThingB ;
]
) ;
.

sh-ex:TwoXorThing-shape
a sh:NodeShape ;
sh:targetClass ex:TwoXorThing ;
sh:xone
(
[
a sh:NodeShape ;
sh:class ex:ThingC ;
]
[
a sh:NodeShape ;
sh:class ex:ThingD ;
]
) ,
(
[
a sh:NodeShape ;
sh:class ex:ThingE ;
]
[
a sh:NodeShape ;
sh:class ex:ThingF ;
]
)
;
.

0 comments on commit 850b162

Please sign in to comment.