You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@jena.apache.org by Miguel Bento Alves <mb...@gmail.com> on 2014/03/13 20:05:50 UTC
jena rule: rule in the head of a forward rule
I¹m doing some tests with jena reasoner and I concluded, that I cannot have
a rule in the head of a forward rule. In a forward rule I only can have
assert data, not deducted data.
I¹m right? Why the reason?
In the example that I give below, we can rewrite the rule well to get the
same result. However, I have a situation in a project where I have:
c1, c2 -> c3, c4 - where c1 is a rule. I cannot rewrite in this form, c3, c4
<- c1, c2 because backward rules only can have one clause on the head. To
overcome this, I only see one solution, materialize c1.
Any other idea?
In the follow example, r2, r3, and r4 works fine but r5 doesn¹t work.
[r1: (?x exa:sportPartner ?y) <-
(?x exa:playSport ?z),
(?y exa:playSport ?z),
notEqual(?x, ?y)]
[r2: (?x exa:seaPartner ?y) <-
(?x exa:playSport ?s1),
(?y exa:playSport ?s2),
notEqual(?x, ?y),
(?s1 rdf:type exa:SeaSport),
(?s2 rdf:type exa:SeaSport)]
[r3: (?x exa:seaPartner1 ?y) <-
(?x exa:sportPartner ?y),
(?x rdf:type exa:ManOfSea),
(?y rdf:type exa:ManOfSea)
]
[r4: (?x exa:playSport ?s1),
(?y exa:playSport ?s2),
notEqual(?x, ?y),
(?s1 rdf:type exa:SeaSport),
(?s2 rdf:type exa:SeaSport) ->
(?x exa:seaPartner2 ?y)]
[r5: (?x exa:sportPartner ?y),
(?x rdf:type exa:SeaMan),
(?y rdf:type exa:SeaMan) ->
(?x exa:seaPartner3 ?y)]
Re: jena rule: rule in the head of a forward rule
Posted by Dave Reynolds <da...@gmail.com>.
Hi Miguel,
The difference between forward and backward rules, in the case of Jena
Rules at least, is simply to do with performance tradeoffs. Generally
whatever you can do with one you can do with the other. There's no
connection between backward rules and particular shapes of triple
pattern. None of your examples have rules within rules, and don't need them.
I would strongly advise you to use either forward rules or backward
rules but not to mix them unless you are really really sure that's what
you need to do and are clear on the consequences.
On 15/03/14 11:43, Miguel Bento Alves wrote:
> Hi Joshua,
>
> I wasn't very clear in my example. I have my rule r1 which defines ?s p
> ?o. However, I can't define that the property p of ?s is ?o with the
> traditional mechanisms of inference of OWL or OWL2. Thus, I have a rule
> that defines:
> (?s p ?o) <- c1, ... .
Which could equally well be a forward rule:
c1, ... -> (?s p ?o) .
> However, I need to produce a reified triple that has an associated
> certainty factor:
>
> (?f p ?e),
> (?f p1 ?p),
> (?e p2 ?p),
> makeTemp(?bn) ->
> (?bn rdf:subject ?f),
> (?bn rdf:predicate p3),
> (?bn rdf:object ?e),
> (?bn exa:certainty "0.9"^^xsd:decimal).
>
>
> I'm not having a good understanding what rules are for?
There's certainly some underlying confusion here somewhere.
> Taking your example, why not:
> (?s rdf:type ?superclass),
> c1 ->
> t1, t2.
> (and the previous example doesn't work as a rule, only works as OWL
> inference mechanisms).
?? That would work fine as a rule (depending on what precisely c1, t1
and t2 are).
Though your use of the variable name ?superclass is worrying, you might
have been trying to create an example more like:
(?s rdf:type ?type) (?type rdfs:subClassOf ?superclass) ... -> ... .
Don't know what you mean by "only works as OWL inference mechanism".
The built in Jena OWL reasoners are just rule systems (which implement
incomplete but useful fragments of OWL full).
> Or why not:
> (?f rdf:type ?superclass),
>
> (?f p1 ?p),
> (?e p2 ?p),
> makeTemp(?bn) ->
> (?bn rdf:subject ?f),
> (?bn rdf:predicate p3),
> (?bn rdf:object ?e),
> (?bn exa:certainty "0.9"^^xsd:decimal).
Sure.
One thing common in your examples which may or may not be part of the
problem is that you seem to assert only the reification. Just because
there is a reified triple in a graph doesn't mean that the triple itself
is there. If you want to also assert the triple (so that it can trigger
other rules or be queries) then you would have a pattern more like:
(?f p ?e),
(?f p1 ?p),
(?e p2 ?p),
makeTemp(?bn) ->
(?f p3 ?e),
(?bn rdf:subject ?f),
(?bn rdf:predicate p3),
(?bn rdf:object ?e),
(?bn exa:certainty "0.9"^^xsd:decimal).
Dave
> Miguel
>
> On 15/03/14 01:45, "Joshua TAYLOR" <jo...@gmail.com> wrote:
>
>> On Fri, Mar 14, 2014 at 4:23 PM, Miguel Bento Alves
>> <mb...@gmail.com> wrote:
>>> Hi Joshua,
>>>
>>> r1 is a backward rule [r1: Š <- Š]. In my understanding, backward rules
>>> are things that relate two resources, overcoming the limitations of OWL
>>> and OWL2.
>>
>> A backward rule doesn't relate two resources. A backward rule
>> provides a pattern for a triple as the head, and some conditions under
>> which the triple can be inferred. E.g.,
>>
>> [r: (?s rdf:type ?superclass) <-
>> (?s rdf:type ?subclass)
>> (?subclass rdfs:subClassOf ?superclass)]
>>
>> When the reasoner tries to infer a triple of the form (?s rdf:type
>> ?superclass), it will try to use this rule by checking whether matches
>> for the body can be found. It doesn't make sense to say that the rule
>> r relates two resources; that's not what a rule is. A rule is a
>> basis for for inferring triples from existing data and other
>> conditions.
>>
>>> Thus, I think that the example that I gave make sense:
>>> (r1 is a backward rule)
>>>
>>> (?f r1 ?e),
>>> (?f p1 ?p),
>>> (?e p2 ?p),
>>> makeTemp(?bn) ->
>>> (?bn rdf:subject ?f),
>>> (?bn rdf:predicate p3),
>>> (?bn rdf:object ?e),
>>> (?bn exa:certainty "0.9"^^xsd:decimal).
>>
>> Again, I'm not sure what (?f r1 ?e) is supposed to mean. In the rule
>> syntax, it's just a triple with variables as the subject and object,
>> and a URI r1 as the predicate. In the example rule r that I gave,
>> what would you expect (?s r ?o) to mean?
>>
>>> Moreover, this rule works well if I materialize r1.
>>> Construct {
>>> (?f r1 ?e)
>>> }
>>> Where {
>>> (?f r1 ?e)
>>> }
>>> (of course, if I materialize a rule I remove from ruleset).
>>
>> I'm not sure what this is supposed to mean. If you construct the data
>> that matched a triple pattern, then you'll get the same data that
>> matched the triple pattern. I'm not sure how this is "a
>> materialization of r1".
>>
>>> Do you think that this understanding of what is a (backward) rule is not
>>> correct?
>>
>> I think it's not correct, but I'm still not sure what you think it
>> would mean to have a backward rule [r: ... <- ...] and then write a
>> triple pattern (?s r ?o). Rules aren't predicates, so it doesn't
>> make sense to me to try to use them as predicates.
>>
>> //JT
>> --
>> Joshua Taylor, http://www.cs.rpi.edu/~tayloj/
>
>
Re: jena rule: rule in the head of a forward rule
Posted by Miguel Bento Alves <mb...@gmail.com>.
Hi Joshua,
I wasn't very clear in my example. I have my rule r1 which defines ?s p
?o. However, I can't define that the property p of ?s is ?o with the
traditional mechanisms of inference of OWL or OWL2. Thus, I have a rule
that defines:
(?s p ?o) <- c1, ... .
However, I need to produce a reified triple that has an associated
certainty factor:
(?f p ?e),
(?f p1 ?p),
(?e p2 ?p),
makeTemp(?bn) ->
(?bn rdf:subject ?f),
(?bn rdf:predicate p3),
(?bn rdf:object ?e),
(?bn exa:certainty "0.9"^^xsd:decimal).
I'm not having a good understanding what rules are for?
Taking your example, why not:
(?s rdf:type ?superclass),
c1 ->
t1, t2.
(and the previous example doesn't work as a rule, only works as OWL
inference mechanisms).
Or why not:
(?f rdf:type ?superclass),
(?f p1 ?p),
(?e p2 ?p),
makeTemp(?bn) ->
(?bn rdf:subject ?f),
(?bn rdf:predicate p3),
(?bn rdf:object ?e),
(?bn exa:certainty "0.9"^^xsd:decimal).
Miguel
On 15/03/14 01:45, "Joshua TAYLOR" <jo...@gmail.com> wrote:
>On Fri, Mar 14, 2014 at 4:23 PM, Miguel Bento Alves
><mb...@gmail.com> wrote:
>> Hi Joshua,
>>
>> r1 is a backward rule [r1: Š <- Š]. In my understanding, backward rules
>> are things that relate two resources, overcoming the limitations of OWL
>> and OWL2.
>
>A backward rule doesn't relate two resources. A backward rule
>provides a pattern for a triple as the head, and some conditions under
>which the triple can be inferred. E.g.,
>
>[r: (?s rdf:type ?superclass) <-
> (?s rdf:type ?subclass)
> (?subclass rdfs:subClassOf ?superclass)]
>
>When the reasoner tries to infer a triple of the form (?s rdf:type
>?superclass), it will try to use this rule by checking whether matches
>for the body can be found. It doesn't make sense to say that the rule
>r relates two resources; that's not what a rule is. A rule is a
>basis for for inferring triples from existing data and other
>conditions.
>
>> Thus, I think that the example that I gave make sense:
>> (r1 is a backward rule)
>>
>> (?f r1 ?e),
>> (?f p1 ?p),
>> (?e p2 ?p),
>> makeTemp(?bn) ->
>> (?bn rdf:subject ?f),
>> (?bn rdf:predicate p3),
>> (?bn rdf:object ?e),
>> (?bn exa:certainty "0.9"^^xsd:decimal).
>
>Again, I'm not sure what (?f r1 ?e) is supposed to mean. In the rule
>syntax, it's just a triple with variables as the subject and object,
>and a URI r1 as the predicate. In the example rule r that I gave,
>what would you expect (?s r ?o) to mean?
>
>> Moreover, this rule works well if I materialize r1.
>> Construct {
>> (?f r1 ?e)
>> }
>> Where {
>> (?f r1 ?e)
>> }
>> (of course, if I materialize a rule I remove from ruleset).
>
>I'm not sure what this is supposed to mean. If you construct the data
>that matched a triple pattern, then you'll get the same data that
>matched the triple pattern. I'm not sure how this is "a
>materialization of r1".
>
>> Do you think that this understanding of what is a (backward) rule is not
>> correct?
>
>I think it's not correct, but I'm still not sure what you think it
>would mean to have a backward rule [r: ... <- ...] and then write a
>triple pattern (?s r ?o). Rules aren't predicates, so it doesn't
>make sense to me to try to use them as predicates.
>
>//JT
>--
>Joshua Taylor, http://www.cs.rpi.edu/~tayloj/
Re: jena rule: rule in the head of a forward rule
Posted by Joshua TAYLOR <jo...@gmail.com>.
On Fri, Mar 14, 2014 at 4:23 PM, Miguel Bento Alves
<mb...@gmail.com> wrote:
> Hi Joshua,
>
> r1 is a backward rule [r1: Š <- Š]. In my understanding, backward rules
> are things that relate two resources, overcoming the limitations of OWL
> and OWL2.
A backward rule doesn't relate two resources. A backward rule
provides a pattern for a triple as the head, and some conditions under
which the triple can be inferred. E.g.,
[r: (?s rdf:type ?superclass) <-
(?s rdf:type ?subclass)
(?subclass rdfs:subClassOf ?superclass)]
When the reasoner tries to infer a triple of the form (?s rdf:type
?superclass), it will try to use this rule by checking whether matches
for the body can be found. It doesn't make sense to say that the rule
r relates two resources; that's not what a rule is. A rule is a
basis for for inferring triples from existing data and other
conditions.
> Thus, I think that the example that I gave make sense:
> (r1 is a backward rule)
>
> (?f r1 ?e),
> (?f p1 ?p),
> (?e p2 ?p),
> makeTemp(?bn) ->
> (?bn rdf:subject ?f),
> (?bn rdf:predicate p3),
> (?bn rdf:object ?e),
> (?bn exa:certainty "0.9"^^xsd:decimal).
Again, I'm not sure what (?f r1 ?e) is supposed to mean. In the rule
syntax, it's just a triple with variables as the subject and object,
and a URI r1 as the predicate. In the example rule r that I gave,
what would you expect (?s r ?o) to mean?
> Moreover, this rule works well if I materialize r1.
> Construct {
> (?f r1 ?e)
> }
> Where {
> (?f r1 ?e)
> }
> (of course, if I materialize a rule I remove from ruleset).
I'm not sure what this is supposed to mean. If you construct the data
that matched a triple pattern, then you'll get the same data that
matched the triple pattern. I'm not sure how this is "a
materialization of r1".
> Do you think that this understanding of what is a (backward) rule is not
> correct?
I think it's not correct, but I'm still not sure what you think it
would mean to have a backward rule [r: ... <- ...] and then write a
triple pattern (?s r ?o). Rules aren't predicates, so it doesn't
make sense to me to try to use them as predicates.
//JT
--
Joshua Taylor, http://www.cs.rpi.edu/~tayloj/
Re: jena rule: rule in the head of a forward rule
Posted by Miguel Bento Alves <mb...@gmail.com>.
Hi Joshua,
r1 is a backward rule [r1: <- ]. In my understanding, backward rules
are things that relate two resources, overcoming the limitations of OWL
and OWL2. Thus, I think that the example that I gave make sense:
(r1 is a backward rule)
(?f r1 ?e),
(?f p1 ?p),
(?e p2 ?p),
makeTemp(?bn) ->
(?bn rdf:subject ?f),
(?bn rdf:predicate p3),
(?bn rdf:object ?e),
(?bn exa:certainty "0.9"^^xsd:decimal).
Moreover, this rule works well if I materialize r1.
Construct {
(?f r1 ?e)
}
Where {
(?f r1 ?e)
}
(of course, if I materialize a rule I remove from ruleset).
Do you think that this understanding of what is a (backward) rule is not
correct?
Miguel
On 14/03/14 13:07, "Joshua TAYLOR" <jo...@gmail.com> wrote:
>On Fri, Mar 14, 2014 at 5:11 AM, Miguel Bento Alves
><mb...@gmail.com> wrote:
>>
>> Yes, you are right I made a mistake with the grammar. The rule is on the
>> body, not in the head, sorry.
>>
>> ³...body? I also don't see what the problem would be with your r5:
>>
>> [r5: (?x exa:sportPartner ?y),
>> (?x rdf:type exa:SeaMan),
>> (?y rdf:type exa:SeaMan) ->
>> (?x exa:seaPartner3 ?y)]
>> ³
>>
>> The problem is that doesn´t work, because the rule is in the body of the
>> forward rule.
>
>My point here is that I don't see where in r5 you're claiming that
>there's a rule in the body. The rule r5 has three terms in the body:
>(?x exa:sportPartner ?y), and (?x rdf:type exa:SeaMan), and (?y
>rdf:type exa:SeaMan). It has one term in the head: (?x
>exa:seaPartner3 ?y). I understand that rules aren't allowed as body
>terms, but even if they were, it would look something like
>
>[ (x y z), # body term that's a triple
> [(a b c) -> (d e f)], # body term that's a rule
> (e f g) # another triple body term
> ->
> (t u v)] # head term
>
>That won't work, of course, but I can see how it "has a rule as a body
>term". I don't see anything about r5 that is an attempt to have a
>rule as a body term.
>
>> My problem is: I want to associate a certainty factor to a
>> conclusion. So, my approach was to create a blank node with the triple
>> plus the certainty factor:
>>
>> (r1 is a rule)
>>
>> (?f r1 ?e),
>> (?f p1 ?p),
>> (?e p2 ?p),
>> makeTemp(?bn) ->
>> (?bn rdf:subject ?f),
>> (?bn rdf:predicate p3),
>> (?bn rdf:object ?e),
>> (?bn exa:certainty "0.9"^^xsd:decimal).
>>
>> This rule doesn¹t work because I have a rule in the body. However, if I
>> materialize r1 then everything is ok.
>
>I'm still not sure what this would mean. I presume that by "(r1 is a
>rule)" you mean that there's a rule [r1: ... -> ...] somewhere. What
>would a pattern (?f r1 ?e) mean, even if the r1 used in a predicate
>position did refer to the rule somehow? Rules aren't thing that
>relate two resources; they are things that get fired when certain
>triples exist and other conditions are met, and that can produce some
>more triples.
>
>If you're trying to produce a reified triple that has an associated
>certainty factor, be aware that there's nothing the matter with
>creating this data:
>
>_:x rdf:subject <subject>
>_:x rdf:predicate <predicate>
>_:x rdf:object <object>
>_:x ex:certainty <certainty>
>
>However, be aware that is is four triples in the graph. Most
>importantly, the triple
>
><subject> <predicate> <object>
>
>is _not_ in the graph, so other rules that are expecting it won't find
>it. If you want to match this data and get the certainty, you'll need
>to use a pattern like
>
>(?x rdf:subject ?s)
>(?x rdf:predicate ?p)
>(?x rdf:object ?o)
>(?x ex:certainty ?c)
>
>As a working example to demonstrate that you can create these kinds of
>triples, suppose you have this data:
>
>"""
>@prefix : <urn:ex:> .
>
>:individual1 a :someType .
>:individual2 a :someType .
>"""
>
>and this rule:
>
>"""
>[r1:
> (?x rdf:type ?z)
> (?y rdf:type ?z)
> notEqual(?x,?y)
> makeTemp(?b)
> ->
> (?b rdf:subject ?x)
> (?b rdf:predicate <urn:ex:similarTo>)
> (?b rdf:object ?y)
> (?b <urn:ex:certainty> 0.9)]
>"""
>
>then this is what you get out of an inference graph:
>
>"""
>@prefix : <urn:ex:> .
>
>[] <http://www.w3.org/1999/02/22-rdf-syntax-ns#object>
> <urn:ex:individual1> ;
> <http://www.w3.org/1999/02/22-rdf-syntax-ns#predicate>
> <urn:ex:similarTo> ;
> <http://www.w3.org/1999/02/22-rdf-syntax-ns#subject>
> <urn:ex:individual2> ;
> <urn:ex:certainty> "0.9"^^<http://www.w3.org/2001/XMLSchema#float> .
>
><urn:ex:individual1>
> a <urn:ex:someType> .
>
><urn:ex:individual2>
> a <urn:ex:someType> .
>
>[] <http://www.w3.org/1999/02/22-rdf-syntax-ns#object>
> <urn:ex:individual2> ;
> <http://www.w3.org/1999/02/22-rdf-syntax-ns#predicate>
> <urn:ex:similarTo> ;
> <http://www.w3.org/1999/02/22-rdf-syntax-ns#subject>
> <urn:ex:individual1> ;
> <urn:ex:certainty> "0.9"^^<http://www.w3.org/2001/XMLSchema#float> .
>"""
>
>> But I can¹t get the values outside the rules, with the Sparql for
>> instance.
>
>With this approach, you'll be able to get values out with SPARQL. A
>query like this:
>
>prefix : <urn:ex:>
>prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
>
>select ?s ?p ?o ?c where {
> [ rdf:subject ?s ;
> rdf:predicate ?p ;
> rdf:object ?o ;
> :certainty ?c ]
>}
>
>produces these results:
>
>| <urn:ex:individual2> | <urn:ex:similarTo> | <urn:ex:individual1> |
>"0.9"^^xsd:float |
>| <urn:ex:individual1> | <urn:ex:similarTo> | <urn:ex:individual2> |
>"0.9"^^xsd:float |
>
>Here's the code:
>
>import java.io.ByteArrayInputStream;
>
>import com.hp.hpl.jena.query.QueryExecutionFactory;
>import com.hp.hpl.jena.query.ResultSet;
>import com.hp.hpl.jena.query.ResultSetFormatter;
>import com.hp.hpl.jena.rdf.model.InfModel;
>import com.hp.hpl.jena.rdf.model.Model;
>import com.hp.hpl.jena.rdf.model.ModelFactory;
>import com.hp.hpl.jena.reasoner.Reasoner;
>import com.hp.hpl.jena.reasoner.rulesys.GenericRuleReasoner;
>import com.hp.hpl.jena.reasoner.rulesys.Rule;
>import com.hp.hpl.jena.vocabulary.RDF;
>
>
>public class RuleExample {
>
> public final static String DATA = "" +
> "@prefix : <urn:ex:> .\n" +
> "\n" +
> ":individual1 a :someType .\n" +
> ":individual2 a :someType .\n" +
> "";
>
> public final static String RULES = "" +
> "[r1:\n" +
> " (?x rdf:type ?z)\n" +
> " (?y rdf:type ?z)\n" +
> " notEqual(?x,?y)\n" +
> " makeTemp(?b)\n" +
> " ->\n" +
> " (?b rdf:subject ?x)\n" +
> " (?b rdf:predicate <urn:ex:similarTo>)\n" +
> " (?b rdf:object ?y)\n" +
> " (?b <urn:ex:certainty> 0.9)]\n" +
> "";
>
> public final static String QUERY = "" +
> "prefix : <urn:ex:>\n" +
> "prefix rdf: <"+RDF.getURI()+">\n" +
> "\n" +
> "select ?s ?p ?o ?c where {\n" +
> " [ rdf:subject ?s ;\n" +
> " rdf:predicate ?p ;\n" +
> " rdf:object ?o ;\n" +
> " :certainty ?c ]\n" +
> "}\n" +
> "";
>
> public static void main(String[] args) {
> Model model = ModelFactory.createDefaultModel();
> model.read( new ByteArrayInputStream( DATA.getBytes() ), null, "TTL"
>);
> Reasoner reasoner = new GenericRuleReasoner( Rule.parseRules( RULES
>));
> InfModel iModel = ModelFactory.createInfModel( reasoner, model );
> System.out.println( DATA );
> System.out.println( RULES );
> iModel.write( System.out, "TTL" );
> System.out.println( QUERY );
> ResultSet results = QueryExecutionFactory.create( QUERY, iModel
>).execSelect();
> ResultSetFormatter.out( results );
> }
>}
>
>
>
>
>--
>Joshua Taylor, http://www.cs.rpi.edu/~tayloj/
Re: jena rule: rule in the head of a forward rule
Posted by Joshua TAYLOR <jo...@gmail.com>.
On Fri, Mar 14, 2014 at 5:11 AM, Miguel Bento Alves
<mb...@gmail.com> wrote:
>
> Yes, you are right I made a mistake with the grammar. The rule is on the
> body, not in the head, sorry.
>
> “...body? I also don't see what the problem would be with your r5:
>
> [r5: (?x exa:sportPartner ?y),
> (?x rdf:type exa:SeaMan),
> (?y rdf:type exa:SeaMan) ->
> (?x exa:seaPartner3 ?y)]
> “
>
> The problem is that doesn´t work, because the rule is in the body of the
> forward rule.
My point here is that I don't see where in r5 you're claiming that
there's a rule in the body. The rule r5 has three terms in the body:
(?x exa:sportPartner ?y), and (?x rdf:type exa:SeaMan), and (?y
rdf:type exa:SeaMan). It has one term in the head: (?x
exa:seaPartner3 ?y). I understand that rules aren't allowed as body
terms, but even if they were, it would look something like
[ (x y z), # body term that's a triple
[(a b c) -> (d e f)], # body term that's a rule
(e f g) # another triple body term
->
(t u v)] # head term
That won't work, of course, but I can see how it "has a rule as a body
term". I don't see anything about r5 that is an attempt to have a
rule as a body term.
> My problem is: I want to associate a certainty factor to a
> conclusion. So, my approach was to create a blank node with the triple
> plus the certainty factor:
>
> (r1 is a rule)
>
> (?f r1 ?e),
> (?f p1 ?p),
> (?e p2 ?p),
> makeTemp(?bn) ->
> (?bn rdf:subject ?f),
> (?bn rdf:predicate p3),
> (?bn rdf:object ?e),
> (?bn exa:certainty "0.9"^^xsd:decimal).
>
> This rule doesn’t work because I have a rule in the body. However, if I
> materialize r1 then everything is ok.
I'm still not sure what this would mean. I presume that by "(r1 is a
rule)" you mean that there's a rule [r1: ... -> ...] somewhere. What
would a pattern (?f r1 ?e) mean, even if the r1 used in a predicate
position did refer to the rule somehow? Rules aren't thing that
relate two resources; they are things that get fired when certain
triples exist and other conditions are met, and that can produce some
more triples.
If you're trying to produce a reified triple that has an associated
certainty factor, be aware that there's nothing the matter with
creating this data:
_:x rdf:subject <subject>
_:x rdf:predicate <predicate>
_:x rdf:object <object>
_:x ex:certainty <certainty>
However, be aware that is is four triples in the graph. Most
importantly, the triple
<subject> <predicate> <object>
is _not_ in the graph, so other rules that are expecting it won't find
it. If you want to match this data and get the certainty, you'll need
to use a pattern like
(?x rdf:subject ?s)
(?x rdf:predicate ?p)
(?x rdf:object ?o)
(?x ex:certainty ?c)
As a working example to demonstrate that you can create these kinds of
triples, suppose you have this data:
"""
@prefix : <urn:ex:> .
:individual1 a :someType .
:individual2 a :someType .
"""
and this rule:
"""
[r1:
(?x rdf:type ?z)
(?y rdf:type ?z)
notEqual(?x,?y)
makeTemp(?b)
->
(?b rdf:subject ?x)
(?b rdf:predicate <urn:ex:similarTo>)
(?b rdf:object ?y)
(?b <urn:ex:certainty> 0.9)]
"""
then this is what you get out of an inference graph:
"""
@prefix : <urn:ex:> .
[] <http://www.w3.org/1999/02/22-rdf-syntax-ns#object>
<urn:ex:individual1> ;
<http://www.w3.org/1999/02/22-rdf-syntax-ns#predicate>
<urn:ex:similarTo> ;
<http://www.w3.org/1999/02/22-rdf-syntax-ns#subject>
<urn:ex:individual2> ;
<urn:ex:certainty> "0.9"^^<http://www.w3.org/2001/XMLSchema#float> .
<urn:ex:individual1>
a <urn:ex:someType> .
<urn:ex:individual2>
a <urn:ex:someType> .
[] <http://www.w3.org/1999/02/22-rdf-syntax-ns#object>
<urn:ex:individual2> ;
<http://www.w3.org/1999/02/22-rdf-syntax-ns#predicate>
<urn:ex:similarTo> ;
<http://www.w3.org/1999/02/22-rdf-syntax-ns#subject>
<urn:ex:individual1> ;
<urn:ex:certainty> "0.9"^^<http://www.w3.org/2001/XMLSchema#float> .
"""
> But I can’t get the values outside the rules, with the Sparql for
> instance.
With this approach, you'll be able to get values out with SPARQL. A
query like this:
prefix : <urn:ex:>
prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
select ?s ?p ?o ?c where {
[ rdf:subject ?s ;
rdf:predicate ?p ;
rdf:object ?o ;
:certainty ?c ]
}
produces these results:
| <urn:ex:individual2> | <urn:ex:similarTo> | <urn:ex:individual1> |
"0.9"^^xsd:float |
| <urn:ex:individual1> | <urn:ex:similarTo> | <urn:ex:individual2> |
"0.9"^^xsd:float |
Here's the code:
import java.io.ByteArrayInputStream;
import com.hp.hpl.jena.query.QueryExecutionFactory;
import com.hp.hpl.jena.query.ResultSet;
import com.hp.hpl.jena.query.ResultSetFormatter;
import com.hp.hpl.jena.rdf.model.InfModel;
import com.hp.hpl.jena.rdf.model.Model;
import com.hp.hpl.jena.rdf.model.ModelFactory;
import com.hp.hpl.jena.reasoner.Reasoner;
import com.hp.hpl.jena.reasoner.rulesys.GenericRuleReasoner;
import com.hp.hpl.jena.reasoner.rulesys.Rule;
import com.hp.hpl.jena.vocabulary.RDF;
public class RuleExample {
public final static String DATA = "" +
"@prefix : <urn:ex:> .\n" +
"\n" +
":individual1 a :someType .\n" +
":individual2 a :someType .\n" +
"";
public final static String RULES = "" +
"[r1:\n" +
" (?x rdf:type ?z)\n" +
" (?y rdf:type ?z)\n" +
" notEqual(?x,?y)\n" +
" makeTemp(?b)\n" +
" ->\n" +
" (?b rdf:subject ?x)\n" +
" (?b rdf:predicate <urn:ex:similarTo>)\n" +
" (?b rdf:object ?y)\n" +
" (?b <urn:ex:certainty> 0.9)]\n" +
"";
public final static String QUERY = "" +
"prefix : <urn:ex:>\n" +
"prefix rdf: <"+RDF.getURI()+">\n" +
"\n" +
"select ?s ?p ?o ?c where {\n" +
" [ rdf:subject ?s ;\n" +
" rdf:predicate ?p ;\n" +
" rdf:object ?o ;\n" +
" :certainty ?c ]\n" +
"}\n" +
"";
public static void main(String[] args) {
Model model = ModelFactory.createDefaultModel();
model.read( new ByteArrayInputStream( DATA.getBytes() ), null, "TTL" );
Reasoner reasoner = new GenericRuleReasoner( Rule.parseRules( RULES ));
InfModel iModel = ModelFactory.createInfModel( reasoner, model );
System.out.println( DATA );
System.out.println( RULES );
iModel.write( System.out, "TTL" );
System.out.println( QUERY );
ResultSet results = QueryExecutionFactory.create( QUERY, iModel
).execSelect();
ResultSetFormatter.out( results );
}
}
--
Joshua Taylor, http://www.cs.rpi.edu/~tayloj/
Re: jena rule: rule in the head of a forward rule
Posted by Miguel Bento Alves <mb...@gmail.com>.
Hi Joshua,
Yes, you are right I made a mistake with the grammar. The rule is on the
body, not in the head, sorry.
“...body? I also don't see what the problem would be with your r5:
[r5: (?x exa:sportPartner ?y),
(?x rdf:type exa:SeaMan),
(?y rdf:type exa:SeaMan) ->
(?x exa:seaPartner3 ?y)]
“
The problem is that doesn´t work, because the rule is in the body of the
forward rule. My problem is: I want to associate a certainty factor to a
conclusion. So, my approach was to create a blank node with the triple
plus the certainty factor:
(r1 is a rule)
(?f r1 ?e),
(?f p1 ?p),
(?e p2 ?p),
makeTemp(?bn) ->
(?bn rdf:subject ?f),
(?bn rdf:predicate p3),
(?bn rdf:object ?e),
(?bn exa:certainty "0.9"^^xsd:decimal).
This rule doesn’t work because I have a rule in the body. However, if I
materialize r1 then everything is ok.
I also tried with a functor, like:
(?bn p3 t(?f, ?e, "0.9"^^xsd:decimal)) <-
(?f r1 ?e),
(?f p1 ?p),
(?e p2 ?p),
makeTemp(?bn).
But I can’t get the values outside the rules, with the Sparql for
instance.
Miguel
On 14/03/14 01:21, "Joshua TAYLOR" <jo...@gmail.com> wrote:
>On Thu, Mar 13, 2014 at 3:05 PM, Miguel Bento Alves
><mb...@gmail.com> wrote:
>> I¹m doing some tests with jena reasoner and I concluded, that I cannot
>>have
>> a rule in the head of a forward rule. In a forward rule I only can have
>> assert data, not deducted data.
>>
>> I¹m right? Why the reason?
>
>
>You most certainly can have rules in the heads of forward rules.
>E.g., from Jena's RDFS forward backward reasoner, there's this rule:
>
> [rdfs2: (?p rdfs:domain ?c) -> [(?x rdf:type ?c) <- (?x ?p ?y)] ]
>
>You mention a situation like
>
> c1, c2 -> c3, c4 - where c1 is a rule.
>
>First, that would be a rule in the body, not in the head, since the
>grammar [1] is
>
>bare-rule
> := term, ... term -> hterm, ... hterm // forward rule
> or bhterm <- term, ... term // backward rule
>
>and hterm means "head term". So you're actually asking about whether a
>rule can appear as a body in a forward chaining rule. According to
>the grammar, it cannot, since the production for term is
>
>term
> := (node, node, node) // triple pattern
> or (node, node, functor) // extended triple pattern
> or builtin(node, ... node) // invoke procedural primitive
>
>Can you give an example of what such a thing would be useful for? It
>doesn't make much sense to me to have a rule as a precondition in a
>rule. What would it mean to use a rule as a precondition? None of
>your examples show a rule used a as a precondition or a post condition
>in a rule. Can you give an example where you'd want a rule in the
>body? I also don't see what the problem would be with your r5:
>
> [r5: (?x exa:sportPartner ?y),
> (?x rdf:type exa:SeaMan),
> (?y rdf:type exa:SeaMan) ->
> (?x exa:seaPartner3 ?y)]
>
>If ?x has a ?y as a sportPartner, and both and ?x and ?y are instances
>of SeaMan, then ?x has ?y as a seaPartner3. What's the matter with
>that?
>
>//JT
>
>[1] http://jena.apache.org/documentation/inference/#RULEsyntax
>--
>Joshua Taylor, http://www.cs.rpi.edu/~tayloj/
Re: jena rule: rule in the head of a forward rule
Posted by Joshua TAYLOR <jo...@gmail.com>.
On Thu, Mar 13, 2014 at 3:05 PM, Miguel Bento Alves
<mb...@gmail.com> wrote:
> I¹m doing some tests with jena reasoner and I concluded, that I cannot have
> a rule in the head of a forward rule. In a forward rule I only can have
> assert data, not deducted data.
>
> I¹m right? Why the reason?
You most certainly can have rules in the heads of forward rules.
E.g., from Jena's RDFS forward backward reasoner, there's this rule:
[rdfs2: (?p rdfs:domain ?c) -> [(?x rdf:type ?c) <- (?x ?p ?y)] ]
You mention a situation like
c1, c2 -> c3, c4 - where c1 is a rule.
First, that would be a rule in the body, not in the head, since the
grammar [1] is
bare-rule
:= term, ... term -> hterm, ... hterm // forward rule
or bhterm <- term, ... term // backward rule
and hterm means "head term". So you're actually asking about whether a
rule can appear as a body in a forward chaining rule. According to
the grammar, it cannot, since the production for term is
term
:= (node, node, node) // triple pattern
or (node, node, functor) // extended triple pattern
or builtin(node, ... node) // invoke procedural primitive
Can you give an example of what such a thing would be useful for? It
doesn't make much sense to me to have a rule as a precondition in a
rule. What would it mean to use a rule as a precondition? None of
your examples show a rule used a as a precondition or a post condition
in a rule. Can you give an example where you'd want a rule in the
body? I also don't see what the problem would be with your r5:
[r5: (?x exa:sportPartner ?y),
(?x rdf:type exa:SeaMan),
(?y rdf:type exa:SeaMan) ->
(?x exa:seaPartner3 ?y)]
If ?x has a ?y as a sportPartner, and both and ?x and ?y are instances
of SeaMan, then ?x has ?y as a seaPartner3. What's the matter with
that?
//JT
[1] http://jena.apache.org/documentation/inference/#RULEsyntax
--
Joshua Taylor, http://www.cs.rpi.edu/~tayloj/