# Bind not work in the sparql rule

**URL:** <https://community.stardog.com/t/bind-not-work-in-the-sparql-rule/547>\
**Category:** Support\
**Created:** [September 8, 2017, 9:44am UTC](https://community.stardog.com/t/bind-not-work-in-the-sparql-rule/547 "2017-09-08T09:44:55Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![ali](https://avatars.discourse-cdn.com/v4/letter/a/8c91f0/32.png) [@ali](https://community.stardog.com/u/ali)\
**Post date:** [September 8, 2017, 9:44am UTC](https://community.stardog.com/t/bind-not-work-in-the-sparql-rule/547/1 "2017-09-08T09:44:55Z")

</div>

I want to assign person with age greater then 18 as Adult as a rule:

```
[] a rule:SPARQLRule ;
    rule:content """
	PREFIX reasoning: <http://www.semanticweb.org/stardog/reasoning#>
        IF {
               ?indiv a ? :Person .
	       ?indiv :age ?age .
	       FILTER (?age >=18) .
	       BIND (UUID() AS ?myInstance).
          }
          
        THEN { ?myInstance a :Adult .
          }""" .

```

I ran the query as:

prefix reasoning:[http://www.semanticweb.org/stardog/reasoning#](http://www.semanticweb.org/stardog/reasoning#)  
SELECT DISTINCT ?myInstance WHERE {

```
     ?myInstance a ? reasoning:Adult .

```

}

But there are no Adults as rule is not triggered, can you guide me why ? I have tested IF part is working, but when I bind it will not work. May be we dont need BIND here. I just want to assign person having age \>18 as Adult, and later on use Adult in some query.

---

<div class="post-metadata">

**Author:** ![zachary.whitley](https://yyz2.discourse-cdn.com/flex030/user_avatar/community.stardog.com/zachary.whitley/32/94_2.png) [@zachary.whitley](https://community.stardog.com/u/zachary.whitley)\
**Post date:** [September 8, 2017, 10:30am UTC](https://community.stardog.com/t/bind-not-work-in-the-sparql-rule/547/2 "2017-09-08T10:30:53Z")

</div>

It looks like you have some prefix issues. In the rule you used the default prefix and in your query you’re using your reasoning: prefix.

Try this for your rule

```
[] a rule:SPARQLRule ;
rule:content """
PREFIX reasoning: http://www.semanticweb.org/stardog/reasoning#
IF {
  ?indiv a ? :Person .
  ?indiv :age ?age .
  FILTER (?age >=18) .
  BIND (UUID() AS ?myInstance).
}
THEN {
  ?myInstance a reasoning:Adult .
 }""" .
```

---

<div class="post-metadata">

**Author:** ![ali](https://avatars.discourse-cdn.com/v4/letter/a/8c91f0/32.png) [@ali](https://community.stardog.com/u/ali)\
**Post date:** [September 9, 2017, 10:22am UTC](https://community.stardog.com/t/bind-not-work-in-the-sparql-rule/547/3 "2017-09-09T10:22:40Z")

</div>

I tried but it has not worked. Can you try, its just a very small example at [https://sites.google.com/site/mhdfahad/plugins/plugins-dkp-aom/query.zip](https://sites.google.com/site/mhdfahad/plugins/plugins-dkp-aom/query.zip)  
onlyOnt.owl is an ontology of two concepts person and adult  
data.owl has data about instances of person  
rule3.ttl has rule for adult  
sparql\_query.txt has the query I want to execute

It will not display the result for the adult concept

---

<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:** [September 9, 2017, 11:41am UTC](https://community.stardog.com/t/bind-not-work-in-the-sparql-rule/547/4 "2017-09-09T11:41:24Z")

</div>

You have still several issues:

1. in onlyOnt.owl the base URI is `http://www.semanticweb.org/stardog/reasoning`, please consider the missing `/` or `#` at the end. As a result, URI of the class `Person` will be `http://www.semanticweb.org/stardog/reasoningPerson` and for the property `age` it will be `http://www.semanticweb.org/stardog/reasoningage`
2. in data.owl, again, the base URI is `http://www.semanticweb.org/stardog/reasoning` and the namespace for the prefix `reasoning` is `http://www.semanticweb.org/stardog/reasoning#`. That is totally confusing and I don’t understand why you did this … Then you used the class `#Person` which resolves to `http://www.semanticweb.org/stardog/reasoning#Person` And for the properties, also for `age`, you used `reasoning:age` - this resolves to `http://www.semanticweb.org/stardog/reasoning#age`
3. In your rule you use a totally different namespace `<urn:test:>` for the prefix `:` , thus, `:Person` will be resolved to `urn:test:Person` and `:age` to `urn:test:data` - indeed that doesn’t match the data.

Solution:

- Fix the namespace issue in onlyOnt.owl
- fix the namespace issue in the IF part of the rule:

```auto
[] a rule:SPARQLRule ;
rule:content """
PREFIX reasoning: http://www.semanticweb.org/stardog/reasoning#
IF {
  ?indiv a reasoning:Person .
  ?indiv reasoning:age ?age .
  FILTER (?age >=18) .
  BIND (UUID() AS ?myInstance).
}
THEN {
  ?myInstance a reasoning:Adult .
 }""" .

```

As a sidenote: What is the idea of the rule? Why don’t you assign the class `Adult` to the existing individuals? Note, this could even achieved with a plain OWL axiom.

---

<div class="post-metadata">

**Author:** ![ali](https://avatars.discourse-cdn.com/v4/letter/a/8c91f0/32.png) [@ali](https://community.stardog.com/u/ali)\
**Post date:** [September 9, 2017, 3:20pm UTC](https://community.stardog.com/t/bind-not-work-in-the-sparql-rule/547/5 "2017-09-09T15:20:23Z")

</div>

I did that as you pointed out see here [https://sites.google.com/site/mhdfahad/plugins/plugins-dkp-aom/query.zip](https://sites.google.com/site/mhdfahad/plugins/plugins-dkp-aom/query.zip)

But still the rule is not fired . I have to apply rules rather than axioms. Can you give it a try on stardog please?

---

<div class="post-metadata">

**Author:** ![evren](https://yyz2.discourse-cdn.com/flex030/user_avatar/community.stardog.com/evren/32/1515_2.png) [@evren](https://community.stardog.com/u/evren)\
**Post date:** [September 9, 2017, 11:55pm UTC](https://community.stardog.com/t/bind-not-work-in-the-sparql-rule/547/6 "2017-09-09T23:55:26Z")

</div>

The prefix in the rule is still broken (missing angle brackets) and as Lorenz pointed out you should use the existing individual in the THEN part of your rule. Note that, we have a rule example in our docs that is almost same as this one. Here is a minimal example that works:

```auto
PREFIX reasoning: <http://www.semanticweb.org/stardog/reasoning#>
PREFIX rule: <tag:stardog:api:rule:>

:tony a reasoning:Person;
   reasoning:age 61 .

[] a rule:SPARQLRule ;
rule:content """
PREFIX reasoning: <http://www.semanticweb.org/stardog/reasoning#>
IF {
  ?indiv a reasoning:Person .
  ?indiv reasoning:age ?age .
  FILTER (?age >= 18) .
}
THEN {
  ?indiv a reasoning:Adult .
}""" .

```

---

<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:** [September 10, 2017, 10:23am UTC](https://community.stardog.com/t/bind-not-work-in-the-sparql-rule/547/7 "2017-09-10T10:23:59Z")

</div>

In addition to the answer from Evren, you should really be careful with your prefix declarations. It wasn’t the cause that your rule isn’t working (Evren solved your problem with the rule) - but now you have

- `http://www.semanticweb.org/stardog/reasoning/` in your ontology and
- `http://www.semanticweb.org/stardog/reasoning#` in your data

This will lead to issues at latest once you’re defining some more complex definitions for the classes in your ontology which won’t be used for data reasoning then.

---

<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 24, 2017, 10:24am UTC](https://community.stardog.com/t/bind-not-work-in-the-sparql-rule/547/8 "2017-09-24T10:24:05Z")

</div>

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