You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@jena.apache.org by Olivier Torres <to...@intactile.com> on 2013/12/31 16:35:34 UTC

Jena Rule with inheritance

Hi,
i have the following type hierarchy, from the more general to the more specific :
A1, A2, A3,

and the rules :
if A1 then B= "value1"
if A2 then B= "value2"
if A3 then B= "value3"

When a triple matches with A3 then the rules infer the triples : B= "value1", B= "value2", B= "value3".
In my use case i want only the deduced triple B= "value3",
Is someone know how to implement this behavior ?
Thank you.


Olivier.

Re: Jena Rule with inheritance

Posted by Dave Reynolds <da...@gmail.com>.
On 31/12/13 16:12, Olivier Torres wrote:
> Thank you Dave,
>
> in my use case i have implemented the brute force version. The problem is that all the rules infer. I want only the more specific deduction b="value3",

Ah, sorry I miss read the question.

That is much harder, you are asking for something non-monotonic which is 
outside the comfort zone for JenaRules.

The easy way would be to simply not do the subClassOf closure and assume 
the data is asserting the direct type but I guess you have some reason 
to want the type closure.

In which case the next easiest option is to do the type closure using 
forward rules then do the value assignment rules backwards (so you know 
that the type inference has completed):

   (?a rdfs:subClassOf ?b)  (?b rdfs:subClassOf ?c)
         -> (?a rdfs:subClassOf ?c) .

   (?a rdf:type ?c)  (?c rdfs:subClass ?d)
         -> (?a rdf:type ?d) .

   (?a eg:B "value3") <- (?a rdf:type eg:A3) .
   (?a eg:B "value2") <- (?a rdf:type eg:A2) noValue(?a rdf:type eg:A3) .
   (?a eg:B "value1") <- (?a rdf:type eg:A1) noValue(?a rdf:type eg:A2) .

Having the type hierarchy explicitly encoded in the rules is pretty 
unsatisfying but the easiest thing to do in this case.

Dave

> Olivier
>
> Le 31 déc. 2013 à 16:54, Dave Reynolds <da...@gmail.com> a écrit :
>
>> On 31/12/13 15:35, Olivier Torres wrote:
>>> Hi,
>>> i have the following type hierarchy, from the more general to the more specific :
>>> A1, A2, A3,
>>>
>>> and the rules :
>>> if A1 then B= "value1"
>>> if A2 then B= "value2"
>>> if A3 then B= "value3"
>>>
>>> When a triple matches with A3 then the rules infer the triples : B= "value1", B= "value2", B= "value3".
>>> In my use case i want only the deduced triple B= "value3",
>>> Is someone know how to implement this behavior ?
>>
>> You have a couple of options. Either include the RDFS closure rules and write your rules as backward rules [1] or include the type inference directly in your own rules.
>>
>> A simple brute force version without need for transitive caching would be something like:
>>
>>   (?a rdfs:subClassOf ?b)  (?b rdfs:subClassOf ?c)
>>        -> (?a rdfs:subClassOf ?c) .
>>
>>   (?a rdf:type ?c)  (?c rdfs:subClass ?d)
>>        -> (?a rdf:type ?d) .
>>
>>   (?a rdf:type eg:A1) -> (?a eg:B "value1") .
>>   (?a rdf:type eg:A2) -> (?a eg:B "value2") .
>>   (?a rdf:type eg:A3) -> (?a eg:B "value3") .
>>
>> Dave
>>
>> [1] http://jena.apache.org/documentation/inference/#RDFSPlusRules
>>
>


Re: Jena Rule with inheritance

Posted by Olivier Torres <to...@intactile.com>.
Thank you Dave,

in my use case i have implemented the brute force version. The problem is that all the rules infer. I want only the more specific deduction b="value3",

Olivier

Le 31 déc. 2013 à 16:54, Dave Reynolds <da...@gmail.com> a écrit :

> On 31/12/13 15:35, Olivier Torres wrote:
>> Hi,
>> i have the following type hierarchy, from the more general to the more specific :
>> A1, A2, A3,
>> 
>> and the rules :
>> if A1 then B= "value1"
>> if A2 then B= "value2"
>> if A3 then B= "value3"
>> 
>> When a triple matches with A3 then the rules infer the triples : B= "value1", B= "value2", B= "value3".
>> In my use case i want only the deduced triple B= "value3",
>> Is someone know how to implement this behavior ?
> 
> You have a couple of options. Either include the RDFS closure rules and write your rules as backward rules [1] or include the type inference directly in your own rules.
> 
> A simple brute force version without need for transitive caching would be something like:
> 
>  (?a rdfs:subClassOf ?b)  (?b rdfs:subClassOf ?c)
>       -> (?a rdfs:subClassOf ?c) .
> 
>  (?a rdf:type ?c)  (?c rdfs:subClass ?d)
>       -> (?a rdf:type ?d) .
> 
>  (?a rdf:type eg:A1) -> (?a eg:B "value1") .
>  (?a rdf:type eg:A2) -> (?a eg:B "value2") .
>  (?a rdf:type eg:A3) -> (?a eg:B "value3") .
> 
> Dave
> 
> [1] http://jena.apache.org/documentation/inference/#RDFSPlusRules
> 


Re: Jena Rule with inheritance

Posted by Dave Reynolds <da...@gmail.com>.
On 31/12/13 15:35, Olivier Torres wrote:
> Hi,
> i have the following type hierarchy, from the more general to the more specific :
> A1, A2, A3,
>
> and the rules :
> if A1 then B= "value1"
> if A2 then B= "value2"
> if A3 then B= "value3"
>
> When a triple matches with A3 then the rules infer the triples : B= "value1", B= "value2", B= "value3".
> In my use case i want only the deduced triple B= "value3",
> Is someone know how to implement this behavior ?

You have a couple of options. Either include the RDFS closure rules and 
write your rules as backward rules [1] or include the type inference 
directly in your own rules.

A simple brute force version without need for transitive caching would 
be something like:

   (?a rdfs:subClassOf ?b)  (?b rdfs:subClassOf ?c)
        -> (?a rdfs:subClassOf ?c) .

   (?a rdf:type ?c)  (?c rdfs:subClass ?d)
        -> (?a rdf:type ?d) .

   (?a rdf:type eg:A1) -> (?a eg:B "value1") .
   (?a rdf:type eg:A2) -> (?a eg:B "value2") .
   (?a rdf:type eg:A3) -> (?a eg:B "value3") .

Dave

[1] http://jena.apache.org/documentation/inference/#RDFSPlusRules


Re: Jena Rule with inheritance

Posted by nanooq <na...@cassiopeia.uberspace.de>.
Hi Oliver, 

unfortunately, I did not fully understand your question, so I consider
my answer to not fully satisfy your needs. Maybe it is useful to you to
further describe your situation?

Excerpts from Olivier Torres's message of 2013-12-31 16:35:34 +0100:
> i have the following type hierarchy, from the more general to the more specific :
> A1, A2, A3,
> 
> and the rules :
> if A1 then B= "value1"
> if A2 then B= "value2"
> if A3 then B= "value3"

This is how I understand your structure:

Subject/Resource: B, Predicate/Property: equals, Object/RDFNode: A[1-3];

> When a triple matches with A3 then the rules infer the triples : B= "value1", B= "value2", B= "value3".
> In my use case i want only the deduced triple B= "value3",
> Is someone know how to implement this behavior ?

This is how I would gather all statements containing value3:

Property equals = yourModel.createPropery(yourEqualsNamespace, yourEqualsLocalName);
RDFNode value3 = yourModel.createLiteral("value3");
StmtIterator statements = yourModel.listStatements(null, equals, value3);

while (statements.hasNext() ) {
  System.out.println(statement.next())  
}


I haven't tested the code but I hope, you get the idea.

Cheers, 

Heinz