TopQuadrant / shacl-js

SHACL API in JavaScript
Apache License 2.0
70 stars 18 forks source link

rdfs:subClassOf property inheritance #22

Closed verweijs closed 4 years ago

verweijs commented 4 years ago

I am messing around in the shacl playground and added a subclass to the Person class in the example: Child

@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 owl:   <http://www.w3.org/2002/07/owl#>  .

schema:Person
    a sh:NodeShape , owl:Class ;
    sh:property [
        sh:path schema:givenName ;
        sh:datatype xsd:string ;
        sh:name "given name" ;
    ] ;
    sh:property [
        sh:path schema:birthDate ;
        sh:lessThan schema:deathDate ;
        sh:maxCount 1 ;
    ] ;
    sh:property [
        sh:path schema:gender ;
        sh:in ( "female" "male" ) ;
    ] ;
    sh:property [
        sh:path schema:address ;
        sh:node schema:Address ;
    ] .

schema:Child
    a sh:NodeShape , owl:Class ;
    rdfs:subClassOf schema:Person ;
    .

Now my understanding of subclasses is that the properties of the parent class are inherited. However, when I change the schema:Person into schema:Child in the data graph it does not seem like the properties are inherited correctly (it seems to ignore the sh:lessThan constraint that is violated:

@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:Child ;
    schema:givenName "Robert" ;
    schema:familyName "Junior" ;
    schema:birthDate "1971-07-07"^^xsd:date ;
    schema:deathDate "1970-09-10"^^xsd:date ;
    schema:address ex:BobsAddress .

Why are the properties of Person not inherited to child in this case?

HolgerKnublauch commented 4 years ago

You have only added the subClassOf statement into the shapes graph. This is good but not sufficient, because the statements also need to be available to the data graph. In a typical scenario, the data graph would import the shapes graph and thus also include the subClassOf triples. But the Playground doesn't do that. Adding schema:Child rdfs:subClassOf schema:Person into the data graph should solve it for this toy example.

Formal background is in the spec: https://www.w3.org/TR/shacl/#implicit-targetClass note the use of the term "data graph" in the textual definition.

verweijs commented 4 years ago

In a typical scenario, the data graph would import the shapes graph and thus also include the subClassOf triples. But the Playground doesn't do that.

I see, but then how does the standard data graph example in the playground 'read' that a person should have another shape: address? Or is this just how SHACL is implemented in JavaScript?

Thanks again for your patience, it is much appreciated!

HolgerKnublauch commented 4 years ago

The address is validated because there is a sh:node constraint in

sh:property [ sh:path schema:address ; sh:node schema:AddressShape ; ]

which causes the engine to walk from the Person into the Address shape.

If this answers your questions, feel free to close the ticket. Thanks.