[rules-users] Problem with Collection

Wolfgang Laun wolfgang.laun at gmail.com
Sat Apr 12 00:16:12 EDT 2014


If there is no cogent reason for collecting a List and acting on it you
should match a
single Alarm and update it on the rule's right hand side:

    rule "Rule [Alarm Collection]"
    when
        $alarm: Alarm ( justInserted == true )
    then
        modify( $alarm ){ setJustInserted( false ) }
    end

Note that you still can store a reference to this Alarm in some global or
call Java
code for additional processing.

If you do need a collected List, you'll have to copy it on the rule's right
hand side
before you call your Java code.

    ArrayList copy = new ArrayList( $alarmColl );

 The update() call is causing instant reorganisation and the collect result
is some
sort of cache.




On 12 April 2014 05:56, mohanm <mohanm.eee at gmail.com> wrote:

> Hi,
>
> I am using Drools 5.5.0 Final Expert to do Alarm Co-relation. I was trying
> to collect the facts in to the ArrayList collection. My drl file will look
> like below. From the Rule I calling an Java method to access the collected
> facts.
>
> /rule "Rule [Alarm Collection]"
> no-loop
>    when
>         $alarmColl : ArrayList() from collect(Alarm
>                  (
>                         JustInserted == true;
>                  ))
>
>         then
>         // act on $alarmColl
> end/
>
> while in my Java code I am loop through the collection to set an attribute
> of the Alarm Object. But while I am trying to set this attribute in the
> collection, the collection is getting updated and indexes are changed. Due
> to which all the objects are not updated. Even I tried using the iterator
> to
> access the ArrayList collection. Still the same issue.
>
> /
> while(index < alarmColl.size()){
>                         Alarm alarmObj=alarmColl.get(index)
>
>                         if(alarmObj.isJustInserted()) {
>                                 alarmObj.setJustInserted(false);
>                                 theScenario.getSession().update(alarmObj);
>                         }
>                         index++;
> }/
>
>
> How can I update the objects in collection without changing the index or
> any
> other way to achieve this.
>
>
>
>
> --
> View this message in context:
> http://drools.46999.n3.nabble.com/Problem-with-Collection-tp4029219.html
> Sent from the Drools: User forum mailing list archive at Nabble.com.
> _______________________________________________
> 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/20140412/0ad95b22/attachment.html 


More information about the rules-users mailing list