Bind not work in the sparql rule

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#
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.

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 .
 }""" .

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
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

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:
[] 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.

I did that as you pointed out see here 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?

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:

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 .
}""" .

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.

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