TopQuadrant / shacl

SHACL API in Java based on Apache Jena
Apache License 2.0
217 stars 61 forks source link

Value related constraints #90

Closed griddigit-ci closed 4 years ago

griddigit-ci commented 4 years ago

Hello,

I am using the SHACL API and try to design shapes that would validate the following. I am wondering if there are some issues with teh API or I need to approach it differently.

1) check if a given value equals a constant e.g. 0 or not equals. I know that we have sh:equals and sh:disjoint but these seem to "compare" with another value from the data graph and not to something that is defined in the shapes graph. What should be used to define e.g. ==0 or ==30 or !=5? 2) check if the values (objects of tripples) are from an allowed list of values. There is no problem to use sh:in if the allowed values are IRIs, but it seems this does not work for literals that have non-string datatype. For instance if the following is defined in a property shape the validator does not understand that 1 and 21 are to be compared as float. If the data is 1.0 float the validator reports it as not conformity and if the value in the data graph is 1 then all is OK. It looks like the valies are treated as strings. sh:in ( "1"^^xsd:float "21"^^xsd:float] ) ; Similar effect is seem also if sh:or is used sh:or ( [sh:hasValue "1"^^xsd:float] [sh:hasValue "21"^^xsd:float] ) ;

The sh:in approach works ok if the allowed values are strings. I am looking for a solution where the allowed values are other than strings.

3) in case I need to first do domething with a value in the data graph, e.g to negate the value or have abs or it, etc, should I use SPARQL or SHACL-SPARQL to do that?

Thanks in advance.

HolgerKnublauch commented 4 years ago

I think those questions are rather general SHACL questions, not specific to the API.

  1. You can use sh:hasValue, possibly combined with sh:not, e.g. sh:not [ sh:hasValue 5 ; sh:path ex:property ] would be ex:property != 5.

  2. See the spec https://www.w3.org/TR/shacl/#InConstraintComponent which explicitly states "Note that matching of literals needs to be exact, e.g. "04"^^xsd:byte does not match "4"^^xsd:integer."

  3. Yes, SPARQL can help, also in cases where you want to do datatype-independent matching

griddigit-ci commented 4 years ago

Thanks a lot. Indeed some parts are general. on 2) I was trying to understand this note in the spec and I thought that if in the data graph we have "4.000"^^xsd:float and the shape has sh:in with "4"^^xsd:float (i.e. in case of matching types) that should not give an error. Isn't that API issue as it looks like the compare is only as strings? or the spec actually wants to state that all is treated as strings? The other confusing part is that note is informative.

Could you have a second though on this item? Do we need to address it as an issue of SHACL spec in another place?

Thanks in advance

HolgerKnublauch commented 4 years ago

No, treating the various variants of xsd:float literals the same would be a mistake, because it would mean that implementations would need to iterate over all values and compare them one by one, while otherwise they can just do a direct triple look-up similar to EXISTS { ?focusNode ?predicate ?hasValue }. The former would be very slow.