[rules-users] Avoid propagation of update in a then block.

Mike Melton mike.melton at gmail.com
Tue Mar 13 16:45:41 EDT 2012


Let the rule engine do what it does best. You are fighting against the
optimizations of the engine by trying to control the flow. You can rewrite
your rule as

rule "my-rule"
 when
  $entity : Entity( closed == false )
 then
  modify($entity) { setClosed(true); }
end

The rule will fire (once) for each Entity which matches the condition. I
haven't taken the time to apply the same exercise to your more complex
rule, but a general rule you should abide by is "No looping in the
consequence unless I have a really good reason."

Mike


2012/3/13 Patrik Dufresne <ikus060 at gmail.com>

> Hi,
>
> I have some trouble to figure out how to stop / start the propagation of
> updates within a Then block.
> Here is a snippet to represent the problem I have.
>
> rule "my-rule"
>     when
>         $objects : List()
>                 from accumulate( $entity : Entity(closed==false),
> collectList($entity) )
>     then
>         for(Object obj : $objects) {
>             ((Entity) obj).setClosed(true);
>             update(obj);
>         }
> end
>
> When this rule's consequence is called first, the first enity in the list
> is 'update', but then update if propagated to immediately causing the rule
> to be evaluated with all the entities minus the updated one. So I'm
> wondering if there is a transaction like operation allowing me to update
> all the entities in the list and then fire the rules.
>
> According to the documentation no-loop should have help me for this.
>
> Here is the original rules
> rule "close-shift"
> salience -1
>  when
> $shift : Shift( )
> $assignments : List( size > 0 )
>  from accumulate (
> $assignment : PlanifEventAssignment(
> close == false,
>  shift == $shift ),
> collectList($assignment) )
> $availables : List( size >= $assignments.size )
>  from accumulate ( ( and
> ShiftAssignment(
> shift == $shift,
>  $employee : employee)
> $available : EmployeeAvailable (
> employee == $employee,
>  assignment.shift == $shift) ),
> collectList($available) )
> eval( Dfs.search($assignments, $availables) != null )
>  then
> // Recalculate the result.
> Map table = Dfs.search($assignments, $availables);
>  for(Object entry : table.entrySet()) {
> PlanifEventAssignment assignment =
> (PlanifEventAssignment)((Entry)entry).getKey();
>  EmployeeValue employee = (EmployeeValue)((Entry)entry).getValue();
> assignment.setClose(true);
>  assignment.setEmployee(employee);
> update(assignment);
> }
> end
>
>
> Patrik Dufresne
>
> _______________________________________________
> rules-users mailing list
> rules-users at lists.jboss.org
> https://lists.jboss.org/mailman/listinfo/rules-users
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.jboss.org/pipermail/rules-users/attachments/20120313/18d4203d/attachment.html 


More information about the rules-users mailing list