[rules-users] How to limit results from a collect statement

Edson Tirelli tirelli at post.com
Wed Aug 22 08:19:06 EDT 2007


    Well, it must work. Can you open a JIRA with a self contained test case
please? I will investigate.

   []s
   Edson

2007/8/22, pentarious <cristiano.giuffrida at gmail.com>:
>
>
> I know. Actually I didn't put any update or insert statement in the code
> just
> to make it simpler. The actual code looks like:
> when
>       $itemList : ArrayList() from collect( Item( category == "VIDEO" ) )
> then
>       ItemDiscount disc = new ItemDiscount(10);
>       insert(disc);
>       $myFirstItem = (Item)$itemList.get(0);
>       $mySecondItem = (Item)$itemList.get(1);
>       $myFirstItem.addDiscount(disc);
>       $mySecondItem.addDiscount(disc);
>       update($myFirstItem);
>       update($mySecondItem);
> Now, why this doesn't work at all? It does work if I write a rule for each
> item using in the when clause $item  : Item from $itemList, but as I said
> I
> need to write a list-oriented rule.
>
> Regards,
>
> CG
>
>
> Edson Tirelli-3 wrote:
> >
> >    If you want the "engine to see" your changes, in a way you affect
> other
> > rules, you must call update( object ) for the object you are changing.
> > Otherwise, the change will happen to the original object, but not to the
> > shadow proxy that the engine uses to ensure consistency.
> >
> >    []s
> >    Edson
> >
> > 2007/8/22, pentarious <cristiano.giuffrida at gmail.com>:
> >>
> >>
> >> Ok, I'm still working on that, I'll publish my solution asap. In the
> >> meantime
> >> it turns out our requirements need something more than that. We have an
> >> ordered list of beans (items) and we have to be able to modify a
> specific
> >> subset of the list (adding a discount). Now I tried to solve this
> problem
> >> by
> >> working directly on the list of items. But I can't understand why this
> >> doesn't work:
> >> when
> >>      $itemList : ArrayList() from collect( Item( category == "VIDEO" )
> )
> >> then
> >>      ItemDiscount disc = new ItemDiscount(10);
> >>      $myFirstItem = (Item)$itemList.get(0);
> >>      $mySecondItem = (Item)$itemList.get(1);
> >>      $myFirstItem.addDiscount(disc);
> >>      $mySecondItem.addDiscount(disc);
> >>
> >> After executing this rule it's like nothing happened! I mean no
> discount
> >> has
> >> been added to the two items! The problem seems to be related to
> >> extracting
> >> items from the list. Apparently, every time we get an item from the
> list,
> >> it
> >> looses its link with the original item (I don't know if it's a copy of
> >> the
> >> original item or whatever), so that every update doesn't affect the
> >> original
> >> item any more. But of course that's what we're interested in! We want
> to
> >> modify original items in the rule session. Am I missing something? Or
> Is
> >> it
> >> a bug?
> >>
> >> Regards,
> >>
> >> CG
> >>
> >>
> >>
> >> Edson Tirelli-3 wrote:
> >> >
> >> >     Please, share with us! Send to the list!
> >> >
> >> >     Edson
> >> >
> >> > 2007/8/21, pentarious <cristiano.giuffrida at gmail.com>:
> >> >>
> >> >>
> >> >> I found an interesting implementation to do that. If anyone is
> >> interested
> >> >> I
> >> >> could share my solution.
> >> >>
> >> >> Regards,
> >> >>
> >> >> CG
> >> >>
> >> >>
> >> >> Mark Proctor wrote:
> >> >> >
> >> >> > There is no supported way to do this. You could use an inline-eval
> >> >> > against a global, but that is very clunkey. However it does seem
> >> like
> >> a
> >> >> > good feature idea for a future release, open a jira and I'll look
> >> into
> >> >> > getting it included.
> >> >> >
> >> >> > Mark
> >> >> > pentarious wrote:
> >> >> >> Does anyone know a method to limit the number of results from a
> >> >> collect
> >> >> >> statement? I was just wondering how to do something like this:
> >> >> >> $myList : List() from collect( MyObject(myField == "something") )
> >> >> limit
> >> >> 3
> >> >> >> //it should put in $myList just the first 3 objects retrieved
> >> >> >> I'm quite sure there is nothing in Drools which can natively
> >> support
> >> >> >> something like that. So, how to do that, if needed? I tried this,
> >> but
> >> >> it
> >> >> >> seems not to be working (I can't understand why, though):
> >> >> >> $myList : List() from collect( MyObject(myField == "something") )
> >> >> >> $myObject : MyObject() from $myList
> >> >> >> eval( $myList.indexOf($myObject) < 3 )
> >> >> >> Indeed, it doesn't work because the link beetween $myList and
> >> >> $myObject
> >> >> >> seems to be released during any execution of the rule (In fact
> >> >> >> $myList.contains($myObject) returns false! Again, I can't
> >> understand
> >> >> >> why...)
> >> >> >>
> >> >> >> Any advice would be really appreciated!
> >> >> >>
> >> >> >> Regards,
> >> >> >>
> >> >> >> CG
> >> >> >>
> >> >> >
> >> >> > _______________________________________________
> >> >> > rules-users mailing list
> >> >> > rules-users at lists.jboss.org
> >> >> > https://lists.jboss.org/mailman/listinfo/rules-users
> >> >> >
> >> >> >
> >> >>
> >> >> --
> >> >> View this message in context:
> >> >>
> >>
> http://www.nabble.com/How-to-limit-results-from-a-collect-statement-tf4235669.html#a12249025
> >> >> Sent from the drools - user mailing list archive at Nabble.com.
> >> >>
> >> >> _______________________________________________
> >> >> rules-users mailing list
> >> >> rules-users at lists.jboss.org
> >> >> https://lists.jboss.org/mailman/listinfo/rules-users
> >> >>
> >> >
> >> >
> >> >
> >> > --
> >> >   Edson Tirelli
> >> >   Software Engineer - JBoss Rules Core Developer
> >> >   Office: +55 11 3529-6000
> >> >   Mobile: +55 11 9287-5646
> >> >   JBoss, a division of Red Hat @ www.jboss.com
> >> >
> >> > _______________________________________________
> >> > rules-users mailing list
> >> > rules-users at lists.jboss.org
> >> > https://lists.jboss.org/mailman/listinfo/rules-users
> >> >
> >> >
> >>
> >> --
> >> View this message in context:
> >>
> http://www.nabble.com/How-to-limit-results-from-a-collect-statement-tf4235669.html#a12270505
> >> Sent from the drools - user mailing list archive at Nabble.com.
> >>
> >> _______________________________________________
> >> rules-users mailing list
> >> rules-users at lists.jboss.org
> >> https://lists.jboss.org/mailman/listinfo/rules-users
> >>
> >
> >
> >
> > --
> >   Edson Tirelli
> >   Software Engineer - JBoss Rules Core Developer
> >   Office: +55 11 3529-6000
> >   Mobile: +55 11 9287-5646
> >   JBoss, a division of Red Hat @ www.jboss.com
> >
> > _______________________________________________
> > rules-users mailing list
> > rules-users at lists.jboss.org
> > https://lists.jboss.org/mailman/listinfo/rules-users
> >
> >
>
> --
> View this message in context:
> http://www.nabble.com/How-to-limit-results-from-a-collect-statement-tf4235669.html#a12272536
> Sent from the drools - user mailing list archive at Nabble.com.
>
> _______________________________________________
> rules-users mailing list
> rules-users at lists.jboss.org
> https://lists.jboss.org/mailman/listinfo/rules-users
>



-- 
  Edson Tirelli
  Software Engineer - JBoss Rules Core Developer
  Office: +55 11 3529-6000
  Mobile: +55 11 9287-5646
  JBoss, a division of Red Hat @ www.jboss.com
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.jboss.org/pipermail/rules-users/attachments/20070822/a352ffed/attachment.html 


More information about the rules-users mailing list