Firstly -- here's the data (from your email that went the moderator):

You've fixed the aso:GArent vs aso:User.


----------------------
@prefix rdf:   <http://www.w3.org/1999/02/22-rdf-syntax-ns#> .
@prefix sh:    <http://www.w3.org/ns/shacl#> .
@prefix xsd:   <http://www.w3.org/2001/XMLSchema#> .
@prefix rdfs:  <http://www.w3.org/2000/01/rdf-schema#> .
@prefix ex:    <http://www.example.org/#> .
@prefix aso:   <http://www.semanticweb.org/aso#> .
@prefix schema: <http://schema.org/#> .

aso:alice a aso:User ;
   schema:givenName "Alice" ;
   schema:familyName "Cooper" ;
   schema:name "Alice Cooper" .

aso:bob a aso:User ;
   schema:givenName "Bob" ;
   schema:familyName "Smith" ;
   schema:name "Robert Smith" .
----------------------
and shapes
----------------------
@prefix rdf:   <http://www.w3.org/1999/02/22-rdf-syntax-ns#> .
@prefix sh:    <http://www.w3.org/ns/shacl#> .
@prefix xsd:   <http://www.w3.org/2001/XMLSchema#> .
@prefix rdfs:  <http://www.w3.org/2000/01/rdf-schema#> .
@prefix ex:    <http://www.example.org/#> .
@prefix owl:   <http://www.w3.org/2002/07/owl#> .
@prefix aso:   <http://www.semanticweb.org/aso#> .
@prefix schema: <http://schema.org/#> .

aso:UserShape a sh:NodeShape ;
  sh:targetClass aso:User ;
  sh:sparql [
    a sh:SPARQLConstraint ;
    sh:message
    "schema:name must equal schema:givenName + schema:familyName";
  sh:prefixes [
    sh:declare [
      sh:prefix "schema" ;
      sh:namespace "http://schema.org/"^^xsd:anyURI ;
    ]
  ] ;
  sh:select
    """SELECT $this (schema:name AS ?path) (?name as ?value)
       WHERE {
          $this schema:name ?name .
          $this schema:givenName ?givenName .
          $this schema:familyName ?familyName .
          FILTER (!isLiteral (?name) ||
         !isLiteral (?givenName) ||
         !isLiteral (?familyName) ||
         concat( str(?givenName), ' ',  str(?familyName)) !=?name)
       }
    """ ;
  ] .

There are 2 problems:

1: The sh:namespace is wrong, missing a "#"

"http://schema.org/#"^^xsd:anyURI

2: The SPARQL query has an undefined usage:

FILTER (!isLiteral (?name)

but ?name isn't defined until SELECT (?name as ?value)
making the filter false always

Fixed version:

@prefix rdf:   <http://www.w3.org/1999/02/22-rdf-syntax-ns#> .
@prefix sh:    <http://www.w3.org/ns/shacl#> .
@prefix xsd:   <http://www.w3.org/2001/XMLSchema#> .
@prefix rdfs:  <http://www.w3.org/2000/01/rdf-schema#> .
@prefix ex:    <http://www.example.org/#> .
@prefix owl:   <http://www.w3.org/2002/07/owl#> .
@prefix aso:   <http://www.semanticweb.org/aso#> .
@prefix schema: <http://schema.org/#> .

aso:UserShape a sh:NodeShape ;
  sh:targetClass aso:User ;

  sh:sparql [
    a sh:SPARQLConstraint ;
sh:message "schema:name must equal schema:givenName + schema:familyName";
  sh:prefixes [
    sh:declare [
      sh:prefix "schema" ;
      ##1 sh:namespace "http://schema.org/"^^xsd:anyURI ;
      sh:namespace "http://schema.org/#"^^xsd:anyURI ;
    ]
  ] ;
  sh:select
    ##2  FILTER
    """SELECT $this (schema:name AS ?path) (?name as ?value)
       WHERE {
          $this schema:name ?name .
          $this schema:givenName ?givenName .
          $this schema:familyName ?familyName .
          FILTER (
                  !isLiteral (?givenName) ||
                  !isLiteral (?familyName) ||
          concat( str(?givenName), ' ',  str(?familyName)) !=?name)
       }
    """ ;
  ] .

gives:

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

[ rdf:type     sh:ValidationReport ;
  sh:conforms  false ;
  sh:result
     [ rdf:type                      sh:ValidationResult ;
       sh:focusNode                  aso:bob ;
       sh:resultMessage
    "schema:name must equal schema:givenName + schema:familyName" ;
       sh:resultPath                 schema:name ;
       sh:resultSeverity             sh:Violation ;
       sh:sourceConstraintComponent  sh:SPARQLConstraintComponent ;
       sh:sourceShape                aso:UserShape ;
       sh:value                      "Robert Smith"
    ]
] .

        Andy


On 05/08/2021 19:44, Alani, Yasir wrote:
Hello,



Thank you for your previous email.



I am trying to use SHACL-SPARQL to validate some data and it does not seem to work as 
intended. I am trying to validate an example (shape and data) found in the book 
"Validating RDF data".





The shape is as follows:



:UserShape a sh:NodeShape ;

sh:targetClass :User ; //all instances of class user must conform to the 
following SPARQL constraint

sh:sparql [

a sh:SPARQLConstraint ;

sh:message "schema:name must equal schema:givenName + schema:familyName"; 
//this is what I would like the constraint to do

sh:prefixes [

sh:declare [

sh:prefix "schema" ;

sh:namespace "http://schema.org/"^^xsd:anyURI ;

]

] ;

sh:select"""

SELECT $this (schema:name AS ?path) (?name as ?value)

WHERE {

$this schema:name ?name .

$this schema:givenName ?givenName .

$this schema:familyName ?familyName .

FILTER (!isLiteral (?value) ||

     !isLiteral (?givenName) ||

     !isLiteral (?familyName) ||

     concat( str(?givenName), ' ',  str(?familyName)) !=?name

)

}""" ;

] .





The data graph is as follows:



:Alice a :User ;

schema:givenName "Alice" ;

schema:familyName "Cooper" ;

schema:name "Alice Cooper" .



:Bob a :User ;

schema:givenName "Bob" ;

schema:familyName "Smith" ;

schema:name "Robert Smith" .





The Jena code is as follows:



public class ValidationTest {



             public static void main(String[] args) {



                         String SHAPE = "C:\\Users\\Test.txt" ;

                         String DATA = "C:\\Users\\TestData.txt" ;



                         Graph shapesGraph = RDFDataMgr.loadGraph(SHAPE, 
Lang.TURTLE) ;

                         Graph dataGraph = RDFDataMgr.loadGraph(DATA, 
Lang.TURTLE) ;



                         Shapes shapes = Shapes.parse(shapesGraph) ;



                         ShaclValidator validator = ShaclValidator.get()  ;

                         ValidationReport report = 
validator.validate(shapesGraph, dataGraph) ;



                         System.out.println() ;

                         RDFDataMgr.write(System.out, report.getModel(), 
Lang.TTL) ;

                         }

}



The validation result is as follows:





[ a            sh:ValidationReport ;

   sh:conforms  true

] .



As you can see, one node should conform and one node should not, but this is 
not what I am getting as a validation result. Please advise.



Thank you very much

Yasir


Reply via email to