# SPARQL Rule with reasoning does not work

**URL:** <https://community.stardog.com/t/sparql-rule-with-reasoning-does-not-work/519>\
**Category:** Support\
**Created:** [August 21, 2017, 4:27pm UTC](https://community.stardog.com/t/sparql-rule-with-reasoning-does-not-work/519 "2017-08-21T16:27:28Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![arsham](https://avatars.discourse-cdn.com/v4/letter/a/77aa72/32.png) [@arsham](https://community.stardog.com/u/arsham)\
**Post date:** [August 21, 2017, 4:27pm UTC](https://community.stardog.com/t/sparql-rule-with-reasoning-does-not-work/519/1 "2017-08-21T16:27:29Z")

</div>

Hello All,

I am a newbie to Stardog DB and try to use the Rule and the reasoning feature of Stardog. I expect from enabling the reasoning and using the following simple rule to update values of individuals matching to the IF condition. It does happening. I will truly appreciate if you can help me.  
Here is the simple rule that tries to update the state of a ?x based on the state of another object ?p.  
I added the rule to the database and the web interface for database browsing shows it as a SPARQLRule. I also enabled reasoning and chose the SL reasoning algorithm.

```auto
IF {	?x a :Path.
        ?x :hasActive ?p.
	?p :hasState ?s.
	BIND ( ?s AS ?state).

    }
    THEN {
        ?x :hasState ?state .
    }

```

Thanks in advance!

---

<div class="post-metadata">

**Author:** ![jess](https://yyz2.discourse-cdn.com/flex030/user_avatar/community.stardog.com/jess/32/10_2.png) [@jess](https://community.stardog.com/u/jess)\
**Post date:** [August 21, 2017, 4:45pm UTC](https://community.stardog.com/t/sparql-rule-with-reasoning-does-not-work/519/2 "2017-08-21T16:45:08Z")

</div>

Hi,

Please share the query you are executing, the expected results and the actual results.

Jess

---

<div class="post-metadata">

**Author:** ![arsham](https://avatars.discourse-cdn.com/v4/letter/a/77aa72/32.png) [@arsham](https://community.stardog.com/u/arsham)\
**Post date:** [August 21, 2017, 7:17pm UTC](https://community.stardog.com/t/sparql-rule-with-reasoning-does-not-work/519/3 "2017-08-21T19:17:29Z")

</div>

Hi Jess,  
Thanks for your reply,  
I am not running any query, I am under the impression of this rule will fire whenever any changes in the db are happening and I expect it updates the `?X` state with the state of `?P`. To test if it works, I simply change the state of the `?P` to “Inactive” and expect `?X :hasState "active"` changes to `?X :hasState "Inactive"` automaticlly.

BR,

---

<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:** [August 21, 2017, 7:24pm UTC](https://community.stardog.com/t/sparql-rule-with-reasoning-does-not-work/519/4 "2017-08-21T19:24:17Z")

</div>

Hi,

It sounds like you are expecting the rules to materialize triples and act as sort of a "trigger." Stardog does not perform materialization; we perform [Query Rewriting](https://www.stardog.com/docs/#_query_rewriting) at query runtime.

In short, If you run the query `select * where {?x :hasState "active"}`, it will give you all of the entities that either explicitly have that triple in the db, or for which the rule applies. Instead of looking for `?X :hasState "Inactive"` in the database, try running the query above, changing a state, and running it again. You should see the results you're looking for.

---

<div class="post-metadata">

**Author:** ![arsham](https://avatars.discourse-cdn.com/v4/letter/a/77aa72/32.png) [@arsham](https://community.stardog.com/u/arsham)\
**Post date:** [August 22, 2017, 1:11pm UTC](https://community.stardog.com/t/sparql-rule-with-reasoning-does-not-work/519/5 "2017-08-22T13:11:58Z")

</div>

Hi,  
Thanks for your reply, I can see the expected results by running the query (1): select \* where {?x :hasState “Inactive”}, however, if I run the select \* where {?x :hasState “active”} query it also returns the triples supposed to be “Inactive” ! I guess this time it is because of it has explicit value of “active” . It is a preplex behaviour to me, is there anyway that I can get more consistent outputs?  
Thanks !

---

<div class="post-metadata">

**Author:** ![lorenz\_b](https://avatars.discourse-cdn.com/v4/letter/l/d78d45/32.png) [@lorenz\_b](https://community.stardog.com/u/lorenz_b)\
**Post date:** [August 23, 2017, 7:58am UTC](https://community.stardog.com/t/sparql-rule-with-reasoning-does-not-work/519/6 "2017-08-23T07:58:37Z")

</div>

The rule does exactly what’s stated there. It matches the data and produces new data. If a path has both states, indeed this will be inferred.

But to be honest, without seeing the data it’s impossible to help you further. Can you share the sample data please?

---

<div class="post-metadata">

**Author:** ![arsham](https://avatars.discourse-cdn.com/v4/letter/a/77aa72/32.png) [@arsham](https://community.stardog.com/u/arsham)\
**Post date:** [August 23, 2017, 2:22pm UTC](https://community.stardog.com/t/sparql-rule-with-reasoning-does-not-work/519/7 "2017-08-23T14:22:55Z")

</div>

Thanks for your reply,  
You can find a simplified data set from [test-arsham-w-const-2-1.ttl](https://canada1.discourse-cdn.com/flex030/uploads/stardog/original/1X/0d81c018bc8405d5ba5159d32e821fc1f05e23cb.ttl) (2.3 KB)  
For this data set, if I run the query: `SELECT * WHERE {?p a of:Path; of:hasState 'Inactive'}` returns Nil if the reasoning is OFF and return the three paths if the reasoning is ON as it should be. However, If I run `SELECT * WHERE {?p a of:Path; of:hasState 'active'}` it always (with/without reasoning) return the same three defined paths.

If I change the rule to the one mentioned in the previous post, it did not work at all. I have to specify the value for the Dataproperty :hasState to make it work as described.

Many Thanks for help!

---

<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:** [August 24, 2017, 1:31pm UTC](https://community.stardog.com/t/sparql-rule-with-reasoning-does-not-work/519/8 "2017-08-24T13:31:05Z")

</div>

Hi,

With your dataset I was able to play around with the rules/queries and _sort of_ get what I think you’re looking for.

I removed ALL of the `of:hasState` triples from the `of:Path`s, keeping them just on the `of:AvailPath`s. I then added the following rule:

```auto
[] a <tag:stardog:api:rule:SPARQLRule> ;
	<tag:stardog:api:rule:content> """IF {	
            ?x a of:Path.    
            ?x of:hasActive/of:hasState \"active\".
          }    
          THEN {        
            ?x of:hasState \"active\".    
          }""" .

```

This allows me to query the “active” Paths like so:

```auto
> stardog query -r myDb "select ?p {?p a of:Path ; of:hasState \"active\"}"
+---------------------------------------------+
| p |
+---------------------------------------------+
| of:path_00:00:00:00:00:01_00:00:00:00:00:03 |
+---------------------------------------------+

```

And the “Inactive” Paths like so:

```auto
> stardog query -r myDb "select ?p {?p a of:Path . MINUS {?p of:hasState \"active\"}}"
+---------------------------------------------+
| p |
+---------------------------------------------+
| of:path_00:00:00:00:00:01_00:00:00:00:00:04 |
| of:path_00:00:00:00:00:01_00:00:00:00:00:02 |
+---------------------------------------------+

```

While it doesn’t reason out an “Inactive” triple, it does honor the logic of showing you `of:Path`s that are NOT “active”

---

<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:** [September 7, 2017, 1:31pm UTC](https://community.stardog.com/t/sparql-rule-with-reasoning-does-not-work/519/9 "2017-09-07T13:31:36Z")

</div>

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