# SHACL-SPARQL not reporting constraint violation

**URL:** <https://community.stardog.com/t/shacl-sparql-not-reporting-constraint-violation/1795>\
**Category:** Support\
**Created:** [July 24, 2019, 2:58pm UTC](https://community.stardog.com/t/shacl-sparql-not-reporting-constraint-violation/1795 "2019-07-24T14:58:06Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![NovasTaylor](https://yyz2.discourse-cdn.com/flex030/user_avatar/community.stardog.com/novastaylor/32/344_2.png) [@NovasTaylor](https://community.stardog.com/u/NovasTaylor)\
**Post date:** [July 24, 2019, 2:58pm UTC](https://community.stardog.com/t/shacl-sparql-not-reporting-constraint-violation/1795/1 "2019-07-24T14:58:06Z")

</div>

This is likely a syntax error on my part, but I am hoping someone can help me out because it will serve as an example in a larger project.

Referring to the attached data file _minEx-Data.ttl_, :Animal\_00M02 should violate the constraint where End Date must be greater than or equal to Begin Date as written in the SHACL file _minEx-SHACL.TTL_. The query itself is correct as proven by executing _minEx-RawQueryThis.rq_ which returns the correct observation that contains the error (Animal\_00M02).

Suggestions greatly appreciated!  
Tim

PS: Would love to see more documentation, and examples, or a webinar for advanced SHACL-SPARQL in Stardog. 🙂

[minEx-Data.TTL](https://community.stardog.com/uploads/short-url/fOnrKyNPkiHpQqKhPpqVKoQY0xB.TTL) (940 Bytes) [minEx-RawQueryThis.rq](https://community.stardog.com/uploads/short-url/9YLfkL01oq6Fsbgu4VUHC7mzTEQ.rq) (535 Bytes) [minEx-SHACL.TTL](https://community.stardog.com/uploads/short-url/fCoRJ0itRvVdXlcVSN3Qqoa8z2e.TTL) (991 Bytes)

---

<div class="post-metadata">

**Author:** ![NovasTaylor](https://yyz2.discourse-cdn.com/flex030/user_avatar/community.stardog.com/novastaylor/32/344_2.png) [@NovasTaylor](https://community.stardog.com/u/NovasTaylor)\
**Post date:** [July 25, 2019, 2:40pm UTC](https://community.stardog.com/t/shacl-sparql-not-reporting-constraint-violation/1795/2 "2019-07-25T14:40:27Z")

</div>

I got to thinking, maybe this is an issue with the date comparison, even though it works in SPARQL. So I wrote this (rather pathetic) kludge and once again it works as a SPARQL query, but not as SHACL-SPARQL. Is anyone able to reproduce this problem?

```
SELECT $this ?beginDate ?endDate
WHERE {
  $this :hasReferenceInterval ?interval .
  ?interval :ReferenceBegin ?beginIRI ;
            :ReferenceEnd ?endIRI .
  ?beginIRI time:inXSDDate ?beginDate .
  ?endIRI time:inXSDDate ?endDate .
  
  BIND (year(?beginDate)*10000 + 
        month(?beginDate)*100 +
        day(?beginDate) AS ?beginDateInt )
  BIND (year(?endDate)*10000 + 
        month(?endDate)*100 +
        day(?endDate) AS ?endDateInt )
 FILTER (! (xsd:integer(?endDateInt) >= xsd:integer(?beginDateInt) ))
}
```

---

<div class="post-metadata">

**Author:** ![stephen](https://yyz2.discourse-cdn.com/flex030/user_avatar/community.stardog.com/stephen/32/27_2.png) [@stephen](https://community.stardog.com/u/stephen)\
**Post date:** [July 25, 2019, 7:20pm UTC](https://community.stardog.com/t/shacl-sparql-not-reporting-constraint-violation/1795/3 "2019-07-25T19:20:14Z")

</div>

Hi Tim,

It looks like your constraint has two issues:

1. You declare the `time` prefix in your SHACL, but not the base prefix! You want:

```auto
sh:prefixes [
    sh:declare [
      sh:prefix "time" ;
      sh:namespace "http://www.w3.org/2006/time#"^^xsd:anyURI ;
    ], [
      sh:prefix "" ;
      sh:namespace "http://foo.bar.org/"^^xsd:anyURI ;
    ]
  ] ;

```

1. Your focus node is `:ReferenceInterval`, however the way your query is written, `$this` will only ever be bound to `:AnimalSubject` entities. I rewrote the query so that `?interval` is now `$this` as follows:

```auto
sh:select
  """SELECT $this (?beginDate AS ?intervalStart) (?endDate AS ?intervalEnd)
    WHERE {
      $this :ReferenceBegin ?beginIRI ;
                :ReferenceEnd ?endIRI .
      ?beginIRI time:inXSDDate ?beginDate .
      ?endIRI time:inXSDDate ?endDate .
      FILTER (! (?endDate >= ?beginDate ))
    }""" ;

```

Adding this constraint, I was able to see a violation. I'm assuming this doesn't rewrite the constraint in a way that makes it not what you're looking for, but if it does, let me know and we can look more into it.

---

<div class="post-metadata">

**Author:** ![NovasTaylor](https://yyz2.discourse-cdn.com/flex030/user_avatar/community.stardog.com/novastaylor/32/344_2.png) [@NovasTaylor](https://community.stardog.com/u/NovasTaylor)\
**Post date:** [July 25, 2019, 8:31pm UTC](https://community.stardog.com/t/shacl-sparql-not-reporting-constraint-violation/1795/4 "2019-07-25T20:31:47Z")

</div>

Thank you! This works, and makes sense. I wish there were more detailed SHACL-SPARQL examples available.

Other than executing a follow-up query, is there a way to to identify in the report which AnimalSubject has the interval that trips the constraint? I expect this is outside of the scope of SHACL-SPARQL.

Thanks again. Case closed (and it was my syntax all along)  
Tim

---

<div class="post-metadata">

**Author:** ![NovasTaylor](https://yyz2.discourse-cdn.com/flex030/user_avatar/community.stardog.com/novastaylor/32/344_2.png) [@NovasTaylor](https://community.stardog.com/u/NovasTaylor)\
**Post date:** [July 25, 2019, 8:34pm UTC](https://community.stardog.com/t/shacl-sparql-not-reporting-constraint-violation/1795/5 "2019-07-25T20:34:22Z")

</div>

Stephen, If you want to grab some easy Stackoverflow reputation karma, you can copypasta to my question on StackOverflow. I just put a bounty on it this afternoon. Enjoy a sweet +50 . 🙂

> <https://stackoverflow.com/questions/57169183/shacl-to-compare-values-on-two-different-nodes>

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex030/uploads/stardog/original/2X/e/ed66b48a616f505106a4acde3c9cee7e6da9bc67.svg) [@system](https://community.stardog.com/u/system)\
**Post date:** [August 8, 2019, 8:34pm UTC](https://community.stardog.com/t/shacl-sparql-not-reporting-constraint-violation/1795/6 "2019-08-08T20:34:24Z")

</div>

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.
