2

Is it possible to use SHACL to formulate constraints about the entire data scope?

For example, can I require the presence of a triple conforming to a certain shape in the data?

A code example of what I had in mind:

# DEMO code, currently raises an error!!

@prefix ex: <http://example.org/ns#> .
@prefix sh:   <http://www.w3.org/ns/shacl#> .

ex:ObligatoryShape
    a sh:NodeShape ;
    minCount 1 . # What I want

My idea is that the above code would raise an error for every data graph that does not include at least one instance of data triple conforming to ex:ObligatoryShape -- this includes the empty data graph.

1 Answers1

5

In general, SHACL validation requires target statements that provide a starting point.

If you don't have any specific target node that naturally would serve as trigger for the validation, use something like

ex:MyShape
    sh:targetNode ex:DummyNode ;
    sh:sparql [ ... ] .

Your original question doesn't provide enough details on what specifically you are testing for - what would be "an instance of a shape"? Maybe you mean "does my graph contain any instance of class X". The following shape checks if there is at least one instance of Person:

ex:PersonCountShape
    a sh:NodeShape ;
    sh:targetNode ex:Person ;
    sh:property [
        sh:path [ sh:inversePath rdf:type ] ;
        sh:minCount 1 ;
    ] .

Maybe your you data shapes have similar triples that could be used as starting point. Above we used rdf:type triples, but often sh:targetSubjectsOf and sh:targetObjectsOf are helpful.

Holger Knublauch
  • 1,176
  • 5
  • 4
  • Thank you, this solved my problem. I edited the question slightly to correct my terminology. – Jerome Broadlane Apr 26 '19 at 08:53
  • I am still surprised that this solution works. If there is no node in the graph that conforms to ```sh:targetNode ex:Person```, how can a node's properties be checked for conformance to the minCount constraint? Apparently the minimality constraint of the property percolates up to a minimality requirement of a node-whose-property-should-be-evaluated? I would appreciate (a reference to) some background explanation on this. – Jerome Broadlane Apr 26 '19 at 08:55
  • The SHACL W3C spec probably does not explain these things directly - it's just a formal document from which such details can be inferred. In a nutshell, there is no notion of node existence, so even if a node doesn't appear in any triple, it can still be used as a target node. This is defined by sh:targetNode, which just adds its objects to the set of target nodes. – Holger Knublauch Apr 27 '19 at 09:10