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:<br>
<br>rule shoot-1<br> when<br> ?h : Hunter( ?target : target )<br> ?a : Animal()<br> eval(?target == ?a)<br> then<br> ?h.shoot( ?a );<br> retract( ?a );<br>end<br><br>Avoiding eval (which is said to be inefficient), it could also be written as<br>
<br>rule shoot-2<br>
when<br> ?a: Animal()<br>
?h : Hunter( target == ?a )<br>
then<br>
?h.shoot( ?a );<br>
retract( ?a );<br>
end<br><br>This, however, places the pattern with many instances up front, which is (AFAIK) not so good in a Rete implementation.<br><br>Which one is to be preferred in Drools?<br><br>Ideally, one might want to write <br>
<br>rule shoot-3<br>
when<br>
?h : Hunter( ?target : target )<br>
?a == ?target : Animal() ### not valid DRL<br>
then<br>
?h.shoot( ?a );<br>
retract( ?a );<br>
end<br><br>This avoids eval, has the single instance fact up front, but it isn't available.<br><br>-W<br><br><br><br>