You could have something along the lines<br><br>class Person {<br>&nbsp;&nbsp;&nbsp;&nbsp; Person parent;<br><br>&nbsp;&nbsp;&nbsp;&nbsp; Collection getAncestors() {<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Collection&lt;Person&gt; ancestors = new HashSet&lt;Person&gt;();<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Person currentAncestor = parent;<br><br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; while(currentAncestor != null) {<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ancestors.add(currentAncestor);<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; currentAncestor = currentAncestor.getParent();<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }<br>&nbsp;&nbsp;&nbsp;&nbsp; }<br>}<br><br>query isAncestor(String a, String b)<br>
 &nbsp; &nbsp;p: Person(name = a)<br>
 &nbsp; &nbsp;c: Person(name = b, ancestors contains p)<br>
end<br><br>that should do it<br><br>dave<br><br><div class="gmail_quote">On Wed, Jan 21, 2009 at 8:54 AM, Faron Dutton <span dir="ltr">&lt;<a href="mailto:fgdutton@gmail.com">fgdutton@gmail.com</a>&gt;</span> wrote:<br><blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;">
I know this has probably been asked before but I cannot find any mention of<br>
it. How does one reason over a transitive (recursive) relation in Drools?<br>
<br>
-----------------------------------------------------------<br>
<br>
The classic example from Prolog:<br>
<br>
-- The relation parent(P,C) says that P is a parent of C.<br>
parent(P,C).<br>
<br>
-- The predicate ancestor(A,B) implies that A is an ancestor<br>
-- of B if A is a parent of B or A is a parent of C and C<br>
-- is an ancestor of B.<br>
ancestor(A,B) :- parent(A,B).<br>
ancestor(A,B) :- parent(A,C), ancestor(C,B).<br>
<br>
-- The query ancestor(bob,frank) asks if bob is an ancestor<br>
-- of frank.<br>
?- ancestor(bob,frank).<br>
<br>
-----------------------------------------------------------<br>
<br>
In Drools, I can find the parent using<br>
<br>
query isParent(String a, String b)<br>
 &nbsp; &nbsp;p: Person(name = a)<br>
 &nbsp; &nbsp;c: Person(name = b, parent = p)<br>
end<br>
<br>
likewise, I can find the grandparent using<br>
<br>
query isGrandparent(String a, String b)<br>
 &nbsp; &nbsp;g: Person(name = a)<br>
 &nbsp; &nbsp;p: Person(parent = g)<br>
 &nbsp; &nbsp;c: Person(name = b, parent = p)<br>
end<br>
<br>
I am unable to formulate the query isAncestor.<br>
<br>
_______________________________________________<br>
rules-users mailing list<br>
<a href="mailto:rules-users@lists.jboss.org">rules-users@lists.jboss.org</a><br>
<a href="https://lists.jboss.org/mailman/listinfo/rules-users" target="_blank">https://lists.jboss.org/mailman/listinfo/rules-users</a><br>
</blockquote></div><br>