# Ruleset and reasoning order

**URL:** <https://community.stardog.com/t/ruleset-and-reasoning-order/112>\
**Category:** Support\
**Created:** [February 14, 2017, 6:33pm UTC](https://community.stardog.com/t/ruleset-and-reasoning-order/112 "2017-02-14T18:33:30Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![arbazhamayun](https://avatars.discourse-cdn.com/v4/letter/a/45deac/32.png) [@arbazhamayun](https://community.stardog.com/u/arbazhamayun)\
**Post date:** [February 14, 2017, 6:33pm UTC](https://community.stardog.com/t/ruleset-and-reasoning-order/112/1 "2017-02-14T18:33:30Z")

</div>

Hi guys. Got another question.

Is there a way to control the order of rules firing? What if I create a variable in one rule and need it another rule - will Stardog be able to handle that?

---

<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:** [February 14, 2017, 7:20pm UTC](https://community.stardog.com/t/ruleset-and-reasoning-order/112/2 "2017-02-14T19:20:56Z")

</div>

Can you provide a simple example of the behavior you want to see?

---

<div class="post-metadata">

**Author:** ![arbazhamayun](https://avatars.discourse-cdn.com/v4/letter/a/45deac/32.png) [@arbazhamayun](https://community.stardog.com/u/arbazhamayun)\
**Post date:** [February 14, 2017, 8:19pm UTC](https://community.stardog.com/t/ruleset-and-reasoning-order/112/3 "2017-02-14T20:19:43Z")

</div>

I asked this question earlier and it is related to my question:

This is the behaviour I would like to see:

I have 3 rulesets, where 1 and 2 gives a literal value to two objects and no 3 needs these strings to CONCAT a new variable. This is how the rules look:

Rule 1  
[] a rule:SPARQLRule ;  
rule:content “”“  
IF {  
?window a ifcowl:IfcWindow .  
}  
THEN {  
?window ccs:ProductID “QQA” .  
}”"".

Rule 2  
[] a rule:SPARQLRule ;  
rule:content “”“  
IF {  
?wall a ifcowl:IfcWallStandardCase .  
}  
THEN {  
?wall ccs:ProductID “B” .  
}”"".

Rule 3  
[] a rule:SPARQLRule ;  
rule:content “”"  
IF {  
?ifcrelfill a ifcowl:IfcRelFillsElement ;  
ifcowl:relatingOpeningElement\_IfcRelFillsElement ?openingelement ;  
ifcowl:relatedBuilstrong textdingElement\_IfcRelFillsElement ?window .  
?window a ifcowl:IfcWindow ;  
ccs:ProductID ?windowID .

?voidelement a ifcowl:IfcRelVoidsElement ;  
ifcowl:relatedOpeningElement\_IfcRelVoidsElement ?openingelement ;  
ifcowl:relatingBuildingElement\_IfcRelVoidsElement ?wall .

?wall a ifcowl:IfcWallStandardCase .  
ccs:ProductID ?wallID .

BIND (CONCAT(-?wallID.?windowID) AS ?wallwindowID)

}  
THEN {  
?window ccs:combinedProductId ?wallwindowID .  
}""".

However when I make a SPARQL query in Stardog with:

SELECT ?window ?ID

WHERE {

?window a ifcowl:IfcWindow .

?window ccs:combinedProductId ?ID .  
}

I get no results 😕

---

<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:** [February 14, 2017, 8:22pm UTC](https://community.stardog.com/t/ruleset-and-reasoning-order/112/4 "2017-02-14T20:22:14Z")

</div>

Here is the initial silly question: Do you have reasoning enabled when running the query?

---

<div class="post-metadata">

**Author:** ![arbazhamayun](https://avatars.discourse-cdn.com/v4/letter/a/45deac/32.png) [@arbazhamayun](https://community.stardog.com/u/arbazhamayun)\
**Post date:** [February 15, 2017, 9:32am UTC](https://community.stardog.com/t/ruleset-and-reasoning-order/112/5 "2017-02-15T09:32:48Z")

</div>

Yes reasoning is enabled.

However what I am curious about is the process of reasoning in Stardog, since first it should find results for 1 and 2, and then in 3 the results from 1 and 2 will yield the results in 3. Hope this makes sense

---

<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:** [February 15, 2017, 2:28pm UTC](https://community.stardog.com/t/ruleset-and-reasoning-order/112/6 "2017-02-15T14:28:05Z")

</div>

Hi Arbaz,

I deleted your other topic as this one pretty much covers everything you asked over there.

I believe the problem here is simply a malformed Rule 3. You have right above the BIND:

```auto
?wall a ifcowl:IfcWallStandardCase .
ccs:ProductID ?wallID . # This is not a complete BGP

```

What I think you’re looking for is:

```auto
?wall a ifcowl:IfcWallStandardCase ; # Semicolon instead of period
    ccs:ProductID ?wallID .

```

Additionally, the parser did not seem to like `BIND (CONCAT(-?wallID.?windowID) AS ?wallwindowID)`, but when I changed it to `BIND (CONCAT(?wallID,?windowID) AS ?wallwindowID)` (No - sign, and comma instead of period), things seemed to work just fine. We wouldn’t have caught it when inserting the rule, since it’s all in a big string and not being parsed as SPARQL as insert time.

If I run the query on some data that I made up, I get what looks like the answer you are wanting:

```auto
Stephens-iMac:community stephen$ stardog query -r myDb "select * {?subj ccs:ProductID ?productId }"
+----------+-----------+
| subj | productId |
+----------+-----------+
| :Wall1 | "B" |
| :Wall2 | "B" |
| :Window1 | "QQA" |
| :Window2 | "QQA" |
+----------+-----------+

Query returned 4 results in 00:00:00.051
Stephens-iMac:community stephen$ stardog query -r myDb "select * {?subj ccs:combinedProductId ?combinedProductId }"
+----------+-------------------+
| subj | combinedProductId |
+----------+-------------------+
| :Window1 | "BQQA" |
| :Window2 | "BQQA" |
+----------+-------------------+

Query returned 2 results in 00:00:00.070

```

---

<div class="post-metadata">

**Author:** ![arbazhamayun](https://avatars.discourse-cdn.com/v4/letter/a/45deac/32.png) [@arbazhamayun](https://community.stardog.com/u/arbazhamayun)\
**Post date:** [February 15, 2017, 2:34pm UTC](https://community.stardog.com/t/ruleset-and-reasoning-order/112/7 "2017-02-15T14:34:15Z")

</div>

Thanks a lot Stephen! Stupid mistake on my part. What if the “-” and punctuation are necessary for my end product - is there a way to write this in a Stardog rule?

---

<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:** [February 15, 2017, 2:36pm UTC](https://community.stardog.com/t/ruleset-and-reasoning-order/112/8 "2017-02-15T14:36:35Z")

</div>

Ah, you would just have to alter your CONCAT a little bit: `CONCAT("-", ?wallID, ".", ?windowID)`

```auto
Stephens-iMac:community stephen$ stardog query -r myDb "select * {?subj ccs:combinedProductId ?combinedProductId }"
+----------+-------------------+
| subj | combinedProductId |
+----------+-------------------+
| :Window1 | "-B.QQA" |
| :Window2 | "-B.QQA" |
+----------+-------------------+

```

---

<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:** [March 1, 2017, 2:36pm UTC](https://community.stardog.com/t/ruleset-and-reasoning-order/112/9 "2017-03-01T14:36:51Z")

</div>

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