Search code examples
rdfturtle-rdfshacl

Can SHACL express constraints about the entire data scope?


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.


Solution

  • 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.