[rules-users] Reasoning over hierarchies.

Faron Dutton fgdutton at gmail.com
Wed Jan 21 08:54:46 EST 2009


I know this has probably been asked before but I cannot find any mention of
it. How does one reason over a transitive (recursive) relation in Drools?

-----------------------------------------------------------

The classic example from Prolog:

-- The relation parent(P,C) says that P is a parent of C.
parent(P,C). 

-- The predicate ancestor(A,B) implies that A is an ancestor
-- of B if A is a parent of B or A is a parent of C and C
-- is an ancestor of B.
ancestor(A,B) :- parent(A,B).
ancestor(A,B) :- parent(A,C), ancestor(C,B).

-- The query ancestor(bob,frank) asks if bob is an ancestor
-- of frank.
?- ancestor(bob,frank).

-----------------------------------------------------------

In Drools, I can find the parent using

query isParent(String a, String b)
    p: Person(name = a)
    c: Person(name = b, parent = p)
end

likewise, I can find the grandparent using

query isGrandparent(String a, String b)
    g: Person(name = a)
    p: Person(parent = g)
    c: Person(name = b, parent = p)
end

I am unable to formulate the query isAncestor.




More information about the rules-users mailing list