Using sh:sparql validators

19 views
Skip to first unread message

Gary Murphy

unread,
Jul 28, 2020, 4:54:27 PM7/28/20
to TopBraid Suite Users
I am hoping to master the fine art of sh:sparql and sh:ask validators, and I have a situation where the source data can have geocoordinates as strings, eg "99.91°N"  - the following sparql code correctly detects values out of range:

SELECT *
WHERE {
  BIND("99.91°N" as ?value)
  BIND(abs(xsd:float(replace(str(?value),"[a-zA-Z°]",""))) as ?lat)
  FILTER( ?lat > "90.0"^^xsd:float )
}

So with a source graph schema:Geocoordinate

ex:BobPoint
  a schema:GeoCoordinates ;
  schema:latitude "-144.75N" ;
  schema:longitude "172" ;
.

I am expecting to detect that bad latitude with the following bit of shacl using sh:sparql to clean the ?value and cast it to float for comparison, just as with the above sparql code.  But it doesn't work ...

hmshape:GeoCoordinates-latitude-limits
  a sh:PropertyShape ;
  sh:path schema:latitude ;
  sh:propertyValidator [
        a sh:SPARQLSelectValidator ;
      sh:prefixes schema: ;
      sh:prefixes xsd: ;
      sh:select """
SELECT $this ?lat
WHERE {
  $this $PATH ?value .
  BIND(abs(xsd:float(replace(str(?value),\"[a-zA-Z° ]\",\"\"))) AS ?lat)
  FILTER( xsd:float(?lat) > xsd:float(90.0) )
}""" ;
    ] ;
.
hmshape:GeoCoordinatesShape
  a sh:NodeShape ;
  sh:property hmshape:GeoCoordinates-latitude-limits ;
  sh:targetClass schema:GeoCoordinates ;
.

Unfortunately, this rule always approves whatever value I give it, no warnings or errors.  I've tried variations, casting to xsd:float or not, using sh:validator with sh:ask, but it still accepts any value.  Just to be sure, I added sh:minCount 2 to assure myself the rule was being reached, and it is.

What am I missing?

Also, is this the correct use of sh:prefixes?  these are defined in the hmshape owl:Ontology with sh:declare bnodes
--
Gary Lawrence Murphy <ga...@schemaapp.com> - Hunch Manifest, 15 Wyndham N 'C', Guelph

Holger Knublauch

unread,
Jul 28, 2020, 6:58:50 PM7/28/20
to topbrai...@googlegroups.com

Hi Gary,

I have attached a (slightly) corrected version. Use sh:sparql instead of sh:propertyValidator. And for the prefixes I have just pointed at the dash namespace, which defines the correct things for the common namespaces such as xsd - I couldn't see if your declarations were correct as the example is a snippet only. Also I have used ?value as return variable in the SELECT because then it will be picked up by the validation report.

This may help https://www.topquadrant.com/technology/shacl/tutorial-sparql-constraints/ and of course the spec https://www.w3.org/TR/shacl/#sparql-constraints-example

HTH
Holger

--
You received this message because you are subscribed to the Google Groups "TopBraid Suite Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to topbraid-user...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/topbraid-users/CADnyxpu7Fsf%3DN0vc%2BmWG2%2BnkiMD%3DHH5-uXwditHvF5zABGqYrA%40mail.gmail.com.
sparqlConstraint.shapes.ttl

Gary Murphy

unread,
Jul 29, 2020, 9:06:00 AM7/29/20
to TopBraid Suite Users
Well ... I was close!  Thanks for the correction, I'll put that into play -- would the same structure also be true for sh:ask?  Thanks also for reminding me of the tutorial page, I should revisit that; I'm anxiously awaiting the first publication of "SHACL for Dummies" ;)

As a side question, what strategy do people use for documenting shacl rules?  I've been using rdfs:comment, but I'm open to best-practice suggestions.

Holger Knublauch

unread,
Jul 29, 2020, 7:04:41 PM7/29/20
to topbrai...@googlegroups.com


On 29/07/2020 23:05, Gary Murphy wrote:
Well ... I was close!  Thanks for the correction, I'll put that into play -- would the same structure also be true for sh:ask?

You cannot use sh:ask in SPARQL-based constraints. However, sh:ask is used by so-called SPARQL-based Constraint Components. Those are reusable extensions of SHACL Core that allow you to describe similar constraints using property values instead of writing SPARQL. See

https://www.w3.org/TR/shacl/#sparql-constraint-components

for examples of the syntax.

  Thanks also for reminding me of the tutorial page, I should revisit that; I'm anxiously awaiting the first publication of "SHACL for Dummies" ;)
I agree, we thought about writing a book but not sure when we would find the time.


As a side question, what strategy do people use for documenting shacl rules?  I've been using rdfs:comment, but I'm open to best-practice suggestions.

Yes, rdfs:comment is perfectly fine. sh:message is used for the actual messages that will be produced in the validation report.

Holger


Reply all
Reply to author
Forward
0 new messages