Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

can one sparql constraint generate multiple violations in pySHACL? #240

Open
habdirad opened this issue Aug 26, 2024 · 3 comments
Open

can one sparql constraint generate multiple violations in pySHACL? #240

habdirad opened this issue Aug 26, 2024 · 3 comments

Comments

@habdirad
Copy link

habdirad commented Aug 26, 2024

Hello all,
I am experimenting with pySHACL and comparing validation outputs against the topquadrant's validator.

Issue/Question:
I have some sparql constraints that check a parent node against the child nodes in a graph. I noticed that pySHACL returns only one of the offending childs in the violation report. Can one sparql constraint generate multiple violations in pySHACL?

Reproducible example:
the following example should generate 3 violations (one for each person) but only returns one (only for ex:John).
graph:

@prefix ex: <http://example.org/ns#> .
@prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> .
@prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#> .
@prefix schema: <http://schema.org/> .
@prefix xsd: <http://www.w3.org/2001/XMLSchema#> .

ex:Bob
    a schema:Person ;
    schema:givenName "Robert" ;
    schema:familyName "Junior" ;
	schema:family ex:JuniorFamily .

ex:Molly
    a schema:Person ;
    schema:givenName "Molly" ;
    schema:familyName "Junior2" ;
	schema:family ex:JuniorFamily .
	
ex:John
    a schema:Person ;
    schema:givenName "John" ;
    schema:familyName "Junior3" ;
	schema:family ex:JuniorFamily .

	
ex:JuniorFamily
	a schema:Family;
	schema:CorrectfamilyName "Junior" .

shapes:

@prefix dash: <http://datashapes.org/dash#> .
@prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> .
@prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#> .
@prefix schema: <http://schema.org/> .
@prefix sh: <http://www.w3.org/ns/shacl#> .
@prefix xsd: <http://www.w3.org/2001/XMLSchema#> .
@prefix ex: <http://example.org/ns#> .

schema:PersonShape
    a sh:NodeShape ;
    sh:targetClass schema:Family;
sh:property schema:family-familyname.

schema:family-familyname
        rdf:type     sh:PropertyShape ;
        sh:path      schema:CorrectfamilyName ;
        sh:name      "familyname" ;
        sh:sparql    schema:onefamilyname ;
        sh:nodeKind  sh:Literal .

schema:onefamilyname 
        rdfs:label  "onefamilyname " ;
        rdf:type    sh:SPARQLConstraint ;
        sh:message  "{?this} {?person}" ;
        sh:select   "prefix dash: <http://datashapes.org/dash#>\r\n prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>\r\n prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#>\r\n prefix schema: <http://schema.org/>\r\n prefix sh: <http://www.w3.org/ns/shacl#>\r\n prefix xsd: <http://www.w3.org/2001/XMLSchema#>\r\n prefix ex: <http://example.org/ns#>\r\n\r\n\r\nSELECT ?this ?person\r\nWHERE {\r\n  ?this a schema:Family.\r\n ?person schema:family ?this.\r\n}" .

script:

conforms, results_graph, results_text = validate(
    graph,
    shacl_graph=shapes,

    data_graph_format="turtle",
    shacl_graph_format="turtle",
    inference=None,
    advanced = True,
    debug=True,
)

output showing 1 violation from pySHACL:
'Validation Report\nConforms: False\nResults (1):\nConstraint Violation in SPARQLConstraintComponent (http://www.w3.org/ns/shacl#SPARQLConstraintComponent):\n\tSeverity: sh:Violation\n\tSource Shape: schema1:family-familyname\n\tFocus Node: ex:JuniorFamily\n\tResult Path: schema1:CorrectfamilyName\n\tSource Constraint: schema1:onefamilyname\n\tMessage: http://example.org/ns#JuniorFamily http://example.org/ns#John\n'

output from Topquadrant's validator showing 3 violations:
image

@ashleysommer
Copy link
Collaborator

Hi @habdirad
Thanks for this report.
Yes, you're right, indeed a single sparql constraint can/should be able to return multiple violations.
I thought there were tests to ensure this behaviour in the SHACL test suite, but perhaps there are not.
I think this may be caused by a bug due to the use of SPARQLConstraint on a sh:PropertyShape (which is valid, but not well tested). I'll look into it.

@ashleysommer
Copy link
Collaborator

ashleysommer commented Sep 25, 2024

Hi @habdirad
I have finally got a chance to investigate your described issue.
I tested your reproduction shapes and datagraph as you've given it, and I am seeing the 3 errors reported same as you show from TopQuadrant validator.

Validation Report
Conforms: False
Results (3):
Constraint Violation in SPARQLConstraintComponent (http://www.w3.org/ns/shacl#SPARQLConstraintComponent):
	Severity: sh:Violation
	Source Shape: schema1:family-familyname
	Focus Node: ex:JuniorFamily
	Result Path: schema1:CorrectfamilyName
	Source Constraint: schema1:onefamilyname
	Message: http://example.org/ns#JuniorFamily http://example.org/ns#Bob
Constraint Violation in SPARQLConstraintComponent (http://www.w3.org/ns/shacl#SPARQLConstraintComponent):
	Severity: sh:Violation
	Source Shape: schema1:family-familyname
	Focus Node: ex:JuniorFamily
	Result Path: schema1:CorrectfamilyName
	Source Constraint: schema1:onefamilyname
	Message: http://example.org/ns#JuniorFamily http://example.org/ns#John
Constraint Violation in SPARQLConstraintComponent (http://www.w3.org/ns/shacl#SPARQLConstraintComponent):
	Severity: sh:Violation
	Source Shape: schema1:family-familyname
	Focus Node: ex:JuniorFamily
	Result Path: schema1:CorrectfamilyName
	Source Constraint: schema1:onefamilyname
	Message: http://example.org/ns#JuniorFamily http://example.org/ns#Molly

See my gist for exactly the PySHACL code I am running:
https://gist.github.com/ashleysommer/62704fd8cba732ffcabf5f07de8a74a9

ashleysommer added a commit that referenced this issue Sep 25, 2024
@habdirad
Copy link
Author

Thank you @ashleysommer. I was using version 0.23. 0 inadvertently. Updating to 0.26.0 fixed this issue. Apologies for the false alarm.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants