Given a working memory containing a large number of rather static facts which are from several subclasses (Ape, Bear, Crocodile,...) of some base class (Animal) and a single fact of class Hunter with a field target of type Animal, the shooting of an animal might be written with a rule like this:
rule shoot-1
when
?h : Hunter( ?target : target )
?a : Animal()
eval(?target == ?a)
then
?h.shoot( ?a );
retract( ?a );
end
Avoiding eval (which is said to be inefficient), it could also be written as
rule shoot-2
when
?a: Animal()
?h : Hunter( target == ?a )
then
?h.shoot( ?a );
retract( ?a );
end
This, however, places the pattern with many instances up front, which is (AFAIK) not so good in a Rete implementation.
Which one is to be preferred in Drools?
Ideally, one might want to write
rule shoot-3
when
?h : Hunter( ?target : target )
?a == ?target : Animal() ### not valid DRL
then
?h.shoot( ?a );
retract( ?a );
end
This avoids eval, has the single instance fact up front, but it isn't available.
-W