I have recently started working with Drools and while writing a drl file I am facing this issue. I am creating a list of "Person" class objects and passing this list to a .drl file. Now I want to iterate the list to check for the persons (objects of class Person) having "salary" greater than some value. But since putting collections in the working memory is a bad practice, is iteration possible without doing so?

1 Answer
1

Iteration is inherent to Drools rules. This is one of the differences between a rule and an if statement in an imperative language. The rule is going to be evaluated for each of the object (this is not 100% accurate; Drools is more intelligent than that) present in your working memory.

So, if you insert each of your persons objects as a fact in your session, you only need to have 1 rule to do what you need:

Each time you insert a Person object, the rule is going to be evaluated for it. If the object passes the evaluation, a new activation of that rule will be generated.
When you call fireAllRules() all the activations are going to be executed: the RHS of your activated rules is going to be executed. Of course, this a roughly explanation of how Drools and the RETE algorithm work. For more precise information please refer to Drools documentation.

Thanks a lot Esteban. Now suppose I insert a list of Person objects instead of a single Person object at a time (as you explained). Is it possible to iterate this list for individual elements and apply rules on the elements without placing the list in the working memory?
–
user2323379Apr 26 '13 at 11:55

If you don't want to place the list in the working memory, then you must iterate in your java code and do an insert() call for each of its elements.
–
Esteban AlivertiApr 26 '13 at 12:46