[rules-users] Reasoning over hierarchies.
Anstis, Michael (M.)
manstis1 at ford.com
Wed Jan 21 09:46:51 EST 2009
I wonder whether a custom accumulate function could be used to move your
logic out of the object model?
Not that accumulate was (probably) meant for this sort of function but
the "init" method could create the empty set and "accumulate" perform
your iteration below?
Just the musing of an idle mind.
________________________________
From: rules-users-bounces at lists.jboss.org
[mailto:rules-users-bounces at lists.jboss.org] On Behalf Of David Sinclair
Sent: 21 January 2009 14:28
To: Rules Users List
Subject: Re: [rules-users] Reasoning over hierarchies.
You could have something along the lines
class Person {
Person parent;
Collection getAncestors() {
Collection<Person> ancestors = new HashSet<Person>();
Person currentAncestor = parent;
while(currentAncestor != null) {
ancestors.add(currentAncestor);
currentAncestor =
currentAncestor.getParent();
}
}
}
query isAncestor(String a, String b)
p: Person(name = a)
c: Person(name = b, ancestors contains p)
end
that should do it
dave
On Wed, Jan 21, 2009 at 8:54 AM, Faron Dutton
<fgdutton at gmail.com> wrote:
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.
_______________________________________________
rules-users mailing list
rules-users at lists.jboss.org
https://lists.jboss.org/mailman/listinfo/rules-users
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.jboss.org/pipermail/rules-users/attachments/20090121/92f95733/attachment.html
More information about the rules-users
mailing list