RDFLib / pySHACL

A Python validator for SHACL
Apache License 2.0
241 stars 63 forks source link

SHACL Advanced Feature sh:SPARQLRule --> sh:construct #235

Closed HendrikBorgelt closed 2 weeks ago

HendrikBorgelt commented 2 weeks ago

Hello All,

I am currently trying to find a tool that has basically all SHACL-Advanced Features implemented because I want to set up a repo with SHACL-based inferencing Shapes and thus I stumbled over pyshacl, which looks very promising. Great work so far.

However, due to my niche application, I require a SHACL tool that not only can validate SHACL queries but also applies SPARQL based rules on the dataset. I Thus tested whether pyshacl is capable of validating/inferencing SPARQL-Rules based on the sh:construct feature.

While pyshacl does not give back errors for construct based SHACL-SPARQL queries, I can not see or save the results in a feasible manner.

I am currently testing with these shapes:

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

ex:RectangleShape
    a sh:NodeShape ;
    sh:targetClass ex:Rectangle ;
    sh:property [
        sh:path ex:width ;
        sh:datatype xsd:integer ;
        sh:minCount 1 ;
        sh:maxCount 1 ;
    ] ;
    sh:property [
        sh:path ex:height ;
        sh:datatype xsd:integer ;
        sh:minCount 1 ;
        sh:maxCount 1 ;
    ] .

ex:RectangleRulesShape
    a sh:NodeShape ;
    sh:targetClass ex:Rectangle ;
    sh:rule [
        a sh:SPARQLRule ;
        sh:prefixes ex: ;
        sh:construct """
            CONSTRUCT {
                $this ex:area ?area .
            }
            WHERE {
                $this ex:width ?width .
                $this ex:height ?height .
                BIND (?width * ?height AS ?area) .
            }
            """ ;
        sh:condition ex:RectangleShape ;    # Rule only applies to Rectangles that conform to ex:RectangleShape
    ] ;
.

and this datagraph

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

ex:ExampleRectangle
    a ex:Rectangle ;
    ex:width 7 ;
    ex:height 8 .

ex:InvalidRectangle
    a ex:Rectangle ;
    ex:width 7 .

both are based on the technical report of SHACL Advanced Features

and should infer

ex:ExampleRectangle ex:area 56 .

As there is currently neither a text occurring in the Validation Report nor an export function of the inferred graph (at least not that I found that option), I would appreciate it if you could help me.

P.S.: I use pyshacl SHACL_AF_data_example.ttl -s SHACL_AF_example.ttl --advanced -i owlrl -o test_shacl3.tt as the pythn command to perform the validation.

ashleysommer commented 2 weeks ago

Hi @HendrikBorgelt This is a duplicate of https://github.com/RDFLib/pySHACL/issues/189, that has been asked many times and answered many times (https://github.com/RDFLib/pySHACL/issues/78, https://github.com/RDFLib/pySHACL/issues/148, https://github.com/RDFLib/pySHACL/issues/20, https://github.com/RDFLib/pySHACL/issues/189), with additional discussion in https://github.com/RDFLib/pySHACL/issues/60.

Please see my detailed reply in https://github.com/RDFLib/pySHACL/issues/189#issuecomment-1626387650 for more detail, and a potential work around to achieve your goal.

HendrikBorgelt commented 2 weeks ago

Hi @ashleysommer and thank you for your fast reply,

as I don't want to send to many comments and start a large discussion, I decided to responded here instead of issue #60. If you prefer that I respond in an other issue I will do so.

While I can understand your reply and also the intent that pyshacl follows primarily shacl core rules, I think that the advanced features allow for modification of copied data graph and should thus have the possibility of returning the modified graph. At least access to the modified data graph should be provided in some way as thus the user does not have the possibility of validating the correctness of the modified data graph.

A SHACL rules engine is a computer procedure that takes as input a [data graph](https://www.w3.org/TR/shacl-af/#dfn-data-graph) and a [shapes graph](https://www.w3.org/TR/shacl-af/#dfn-shapes-graph) and is capable of adding [triples](https://www.w3.org/TR/shacl-af/#dfn-triple) to the [data graph](https://www.w3.org/TR/shacl-af/#dfn-data-graph). The new [triples](https://www.w3.org/TR/shacl-af/#dfn-triple) that are produced by a rules engine are called the inferred triples.

Note that, from a logical perspective, the [data graph](https://www.w3.org/TR/shacl-af/#dfn-data-graph) will be modified if [triples](https://www.w3.org/TR/shacl-af/#dfn-triple) get inferred. This means that rules can trigger after other triples have been inferred. However, in cases where the original data should not be modified, implementations may construct a logical [data graph](https://www.w3.org/TR/shacl-af/#dfn-data-graph) that has the original data as one subgraph and a dedicated inferences graph as another subgraph, and where the inferred triples get added to the inferences graph only.

as seen in SHACL Advanced Features - 8.4 General Execution Instructions for SHACL Rules

I also want to point out the statement "However, in cases where the original data should not be modified,..." where it is implied that a direct modification of the data graph would be feasible with the SHACL-SPARQL advanced features.

I would like to hear your thoughts on this.

thanks in advance.

ashleysommer commented 2 weeks ago

While I can understand your reply and also the intent that pyshacl follows primarily shacl core rules, I think that the advanced features allow for modification of copied data graph and should thus have the possibility of returning the modified graph.

I'm not against the use of the SHACL-Advanced Specification. Actually PySHACL was the first non-TopQuadrant SHACL validator to implement the SHACL Advanced Spec, and is still the only fully open-source SHACL validator that has support for all features of the SHACL Advanced Spec.

The situation however is that PySHACL is a SHACL validator. It operates in accordance with the SHACL Specification and the SHACL Advanced Spec, for the role of validating a target datagraph against given shapes and constraints. PySHACL supports the use of SHACLRules (SPARQLRules and JSRules) on an internal working-copy of the datagraph, for purposes of enhancing the validation of the datagraph. A SHACL Validator outputs a ValidationReport, and strictly does not modify the source datagraph.

There is an argument to be made about introducing alternate "ways-of-working" for PySHACL, so that it can operate outside the role of simply a SHACL Validator. Eg, as a stand-alone SHACL Rules engine, or a graph-expander using SHACL Rules. That is a separate conversations, if you want to contribute that conversation, go to #60.

At least access to the modified data graph should be provided in some way.

Please read again the conversation in #189, specifically this comment, that shows a workaround (using in-place validation mode) to achieve that.