[rules-users] Delete a List of Facts

Wolfgang Laun wolfgang.laun at gmail.com
Sat May 25 05:05:14 EDT 2013


In this rule, $facts is bound to a "live" ArrayList, i.e., a retract
has an immediate effect on the collection. Iterate a copy for
retracting all in a single firing.

That said, it might be better to write a simple rule retracting each
matching Fact individually. Collecting the list and retracting from a
copy does not avoid updating the agenda with this rule, 9 times for 10
matching Fact facts.

-W


On 25/05/2013, Weiss, Wolfgang <Wolfgang.Weiss at joanneum.at> wrote:
> Hi,
>
> I want to delete a list of facts from the working memory and therefore
> created following rule:
>
> rule "delete rule"
>   agenda-group "evaluation"
>   salience 50
>   no-loop true
>
>   when
>     $facts : ArrayList(size > 0) from collect (Fact())
>   then
>     logger.info("delete rule, number of facts in list: " + $facts.size());
>
>     for (int i = 0; i < $facts.size(); i++) {
>        Fact f = (Fact)$facts.get(i);
>        logger.info("retract fact id: " + f.getId());
>        retract(f);
>     }
> end
>
> When inserting 10 facts with IDs 1 to 10 and then fire all rules, I get
> following result:
> delete rule, number of facts in list: 10
> retract fact id: 1
> retract fact id: 3
> retract fact id: 5
> retract fact id: 7
> retract fact id: 9
>
> but, the facts 2, 4, 6, 8 and 10 are not deleted. I would have expected to
> set "no-loop" to true, as I do some modification of facts and don't want
> that this rule is fired again with the same facts. When I set "no-loop" to
> false, I get following result:
> delete rule, number of facts in list: 10
> retract fact id: 1
> retract fact id: 3
> retract fact id: 5
> retract fact id: 7
> retract fact id: 9
> delete rule, number of facts in list: 5
> retract fact id: 2
> retract fact id: 6
> retract fact id: 10
> delete rule, number of facts in list: 2
> retract fact id: 4
> delete rule, number of facts in list: 1
> retract fact id: 8
>
> now, all facts are deleted but the "delete rule" was fired 4 times. Is this
> the intended behavior of Drools? Is there a way to fire a rule only once and
> delete a list of facts?
> I can reproduce this behavior with Drools 5.5.0.Final and 6.0.0.Beta2 but
> did not try any other version.
>
> Best Regards,
> Wolfgang
>
>


More information about the rules-users mailing list