w3c / data-shapes

RDF Data Shapes WG repo
87 stars 33 forks source link

Named Property Shape With `sh:path` Only Within `sh:or` #147

Closed init-dcat-ap-de closed 1 year ago

init-dcat-ap-de commented 1 year ago

Hello,

we created SHACL-Shapes to validate the German application profile for DCAT-AP:

I validated them with https://www.itb.ec.europa.eu/shacl/shacl/upload One problem emereged where I can not find a solution to:

What do I try to archive? I want one NodeShape which targets the class and includes all named property 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 sh: <http://www.w3.org/ns/shacl#> .
@prefix ex: <http://example.org/ns#> .

ex:singlePathProperty
    sh:message "Custom message for ex:singlePathProperty!" ;
    sh:path ex:singlePath ;
    sh:minCount 1 ;
    sh:maxCount 1 ;
.

ex:orPathProperty
    sh:message "Custom message for ex:orPathProperty!" ;
    sh:or (
        [
            sh:path ex:orPathOne ;
            sh:minCount 1 ;
        ]
        [
            sh:path ex:orPathTwo ;
            sh:minCount 1 ;
        ] 
    )
.

ex:mainShape
    a sh:NodeShape ;
    sh:targetClass ex:TestClass ;
    sh:message "Custom message in mainShape!" ;

    sh:property ex:singlePathProperty ;
    sh:property ex:orPathProperty ;
.

This works on https://shacl.org/playground/ as planned, but does not validate, because grafik

If I use sh:node instead of sh:property for linking to ex:orPathProperty, it validates, but I loose the custom error message.

A workaround would be to double the NodeShape:

ex:AnotherNodeShape
    a sh:NodeShape ;
    sh:targetClass ex:TestClass ;
    sh:message "Custom message for ex:orPathProperty in ex:AnotherNodeShape :-( !" ;

    sh:or (
        [
            sh:path ex:orPathOne ;
            sh:minCount 1 ;
        ]
        [
            sh:path ex:orPathTwo ;
            sh:minCount 1 ;
        ] 
    ) ;
.

But this would mean that shapes using sh:or are treated differently... I would try to avoid this.


Data Graph for Testing

@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 xsd: <http://www.w3.org/2001/XMLSchema#> .

ex:Test_OK_1
    a ex:TestClass ;
    ex:singlePath "Test" ;
    ex:orPathOne  "Test 1" ;
.

ex:Test_OK_2
    a ex:TestClass ;
    ex:singlePath "Test" ;
    ex:orPathTwo  "Test 2" ;
.

ex:Test_OK_1_2
    a ex:TestClass ;
    ex:singlePath "Test" ;
    ex:orPathOne  "Test 1" ;
    ex:orPathTwo  "Test 2" ;
.

ex:Test_No_Or_Path
    a ex:TestClass ;
    ex:singlePath "Test" ;
.
init-dcat-ap-de commented 1 year ago

@volkerjaenisch gave me the decisive tip:

The problem is, that ex:orPathProperty has 0 or 2 property paths, but it should have exactly 1. I cannot use sh:or or sh:xone here, the solution is to use sh:alternativePath:

ex:orPathProperty
    sh:message "Custom message for ex:orPathProperty!" ;
    sh:path [ sh:alternativePath ( ex:orPathOne ex:orPathTwo ) ] ;
    sh:minCount 1 ;
.