Arvind
Arvind

Reputation: 717

Drools: How to insert a fact after checking if it is not already present in working memory

I am using drools to validate an object. The object also has a getChildrenList() method which returns the child objects related to this object (master-detail relation).

I do some validations on the object and then I want to validate the child objects as well, so I insert all of the child objects as well into the working memory by using following rule:

 rule "Insert Children"
     when
             $parent : Parent ( eval(childrenList != empty) )
                     $ch : Child() from $p.childrenList
     then
             insert($ch);
     end

Now how can I make sure that this rule does not get fired if the children were already inserted. I mean because I modify some fact the rule gets re-fired. How can I prevent that?

Thanks

Upvotes: 5

Views: 3913

Answers (1)

Peter Hilton
Peter Hilton

Reputation: 17344

You could try adding this line to the when condition, although I suspect that this is not the 'right' idiom:

not( Child(this == $ch) )

Upvotes: 3

Related Questions