knowledge-graphshacl

SHACL validation with FilerShape


Data 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 : <http://mapping.example.com/> .
@prefix dc: <http://purl.org/dc/elements/1.1#> . 

<http://example.org/9> a "http://example.org/class1";
  dc:title "Example9";
  schema:identifier "YES";
  schema:size "12345" .

<http://example.org/10> a "http://example.org/class1";
  dc:title "Example10";
  schema:identifier "NO";
  schema:size "12345" .

<http://example.org/11> a "http://example.org/class1";
  dc:title "Example11";
  schema:identifier "NO";
  schema:size "12" .

<http://example.org/12> a "http://example.org/class1";
  dc:title "Example12";
  schema:identifier "YES";
  schema:size "123" .

I only want to check the class members of class1 that have the value "YES" for the property schema:identifier. These class members should then have the property Schema:size with a length of at least 4 characters. Therefore my Shapes Graph looks like this:

@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 : <http://mapping.example.com/> .
@prefix dc: <http://purl.org/dc/elements/1.1#> . 

:test1
    a sh:NodeShape ;
    sh:targetClass "http://example.org/class1" ;
    sh:filterShape [
        a sh:NodeShape ;
        sh:property [
            sh:path schema:identifier;
            sh:hasValue "YES" ;
        ] ;
    ] ;
    sh:property [
        sh:path schema:size ;
        sh:minLength 4 ;
    ] .

I get a constrain violation for <http://example.org/11> and <http://example.org/12> . But should only get a constrain violation for <http://example.org/12>. Cause <http://example.org/11> has the value "NO" for schema:identifier.

What have I done wrong ?


Solution

  • Two techniques could be used to express the same logic without sh:filterShape.

    1. Using SHACL Core, define a sh:or constraint which basically says 'either identifier != "YES" OR size has minLength 4'

    2. Use SHACL-AF custom targets, see https://datashapes.org/dash.html#HasValueTarget

    The latter requires an engine with SHACL-AF support, so I think you may want to start with the first option.