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

pentarious cristiano.giuffrida at gmail.com
Wed Aug 22 09:19:27 EDT 2007


Actually, before opening a JIRA I would like to show you my actual code...
Here it is:

rule "My Rule"
     no-loop true
    when
	$list : ArrayList() from collect( Item( type == "MY TYPE" ) )
    then
	Item item = (Item) $list.get(0);
	ItemOption option = new ItemOption( item, "MY OPTION" );
	item.setType("MY NEW TYPE");
	item.getOptions().add( option );
	insert(option);
	update(item);
end

Basically we have some items (Item) and we need to take into considerations
just items of type "MY TYPE", adding a new option "MY OPTION" to the list of
the options of the first item in $list. Item is a bean with a field as
follows:
     private Collection<ItemOption > options = new ArrayList<ItemOption>();
I thought it was worth writing again before opening a JIRA, as I've looked
further into the problem and I found out another interesting thing. As you
may have noticed I put two lines in my code:
item.setType("MY NEW TYPE");
item.getOptions().add( option );
It's interesting to notice that the first line works as expected (i.e. after
executing the rule the item has a new type called "MY NEW TYPE"), while the
second line doesn't (i.e. after executing the rule the item doesn't have a
new option in its option list). So, do you think I should definitively open
a JIRA (I've never opened one before, can you give a link just in case?) or
I'm just missing something and there is a trivial solution to this issue?

Regards,

CG


Edson Tirelli-3 wrote:
> 
>     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
> 
> _______________________________________________
> 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#a12274411
Sent from the drools - user mailing list archive at Nabble.com.




More information about the rules-users mailing list