shacl

Can a SHACL NodeShape target an intersection?


Is it possible for a NodeShape to target the intersection of a SHACL-core sh:targetClass and a SHACL-AF sh:target?

e.g.

this shapes graph:

@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 ex: <http://bob.com/> .

ex:URITarget a sh:SPARQLTarget ;
  sh:select """
   SELECT ?this
   WHERE {
    ?this ?p ?o .
    filter(contains(str(?this),'https'))
   }
   """ ;
.

ex:Shape0 a sh:NodeShape ;
 sh:target ex:URITarget ;
 sh:targetClass ex:Dad ; 
 sh:property [
   sh:path ex:name ;
   sh:minCount 1 ;
 ] ;
.

on this data graph:

@prefix ex: <http://bob.com/> .
@prefix exs: <https://bob.com/> .

exs:fred a ex:Dad .
ex:alf a ex:Dad .

produces:

[ a            sh:ValidationReport ;
  sh:conforms  false ;
  sh:result    [ a                             sh:ValidationResult ;
                 sh:focusNode                  exs:fred ;
                 sh:resultMessage              "Property needs to have at least 1 value" ;
                 sh:resultPath                 ex:name ;
                 sh:resultSeverity             sh:Violation ;
                 sh:sourceConstraintComponent  sh:MinCountConstraintComponent ;
                 sh:sourceShape                _:b0
               ] ;
  sh:result    [ a                             sh:ValidationResult ;
                 sh:focusNode                  ex:alf ;
                 sh:resultMessage              "Property needs to have at least 1 value" ;
                 sh:resultPath                 ex:name ;
                 sh:resultSeverity             sh:Violation ;
                 sh:sourceConstraintComponent  sh:MinCountConstraintComponent ;
                 sh:sourceShape                _:b0
               ]
] .

So it is targeting the union of sh:target ex:URITarget and sh:targetClass ex:Dad. How do you target the intersection of those two?

Targeting the intersection of those two would produce a validation report with only exs:fred as a focus node (since that node has https in its URI and is a Dad).


Solution

  • SHACL targets will always be applied as a union, i.e. one target cannot cancel out another target. In your case, you would need to remove the sh:targetClass and add a filter to the SPARQL-based target to drop anything that is NOT an instance of ex:Dad.