Yes, the "from collect" is new to 4.0.
If you are using 3.0.6, you must use 2 rules and a helper class to do the
count:
rule "counting"
salience 10
when
$counter : Counter()
Student( name == "Drools" )
then
$counter.setValue( $counter.getValue() + 1 );
end
rule "Total instances"
when
$counter : Counter( )
then
System.out.println( "Total instances = "+$counter.getValue() );
end
Please not the higher salience value in the first rule. Also, remember to
assert all your objects into the working memory, not only the list.
The Counter class is a simple java bean to help you do the count.
Hope it helps.
[]s
Edson
2007/6/1, rajesh_sachin10(a)yahoo.co.in <rajesh_sachin10(a)yahoo.co.in>:
Hi
I'm working on Drools 3.06. I am not able to decipher the drl snippet you
have posted.
I'am not clear about the <b>'from collect'</b> Keywords. My
problem
statement is to get the number of
occurance of a Student(Name =="Drools") in a collection.
Please find below a small java snippet which is equivalent to what I'm
tring to do.
int count = 0;
List<Student> nameList = getListofNames();
while(nameList.iterator().hasNext())
{
Student student = (Student)nameList.iterator().next();
if(Student.getName.equals("Drools"))
{
count++;
}
}
System.out.println("Occurance Count" + count);
Edson Tirelli-3 wrote:
>
> Nope, if you want actual instances you must use "collect":
>
> when
> context : Context(state == Context:START_UP)
> list : ArrayList( size > 0 ) from collect( ClassName(varName
=="value"
)
> )
> then
> // list has your matching ClassName objects
> end
>
> []s
> Edson
>
>
>
> 2007/6/1, Rajesh.Sachin10 <rajesh_sachin10(a)yahoo.co.in>:
>>
>>
>> Its possible to use 'exists' keyword to get the number of occurance?
>>
>> Eg:
>>
>> context : Context(state == Context:START_UP) exists ClassName(varName
==
>> "value")
>>
>>
>> The ClassName is an Object array..... So to get the number of occurance
>> of
>> the "value" in varName, we have any method or keywords?
>> --
>> View this message in context:
>>
http://www.nabble.com/Usage-of-%27exists%27-keyword-to-get-the-number-of-...
>> Sent from the drools - user mailing list archive at
Nabble.com.
>>
>> _______________________________________________
>> rules-users mailing list
>> rules-users(a)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(a)lists.jboss.org
>
https://lists.jboss.org/mailman/listinfo/rules-users
>
>
Quoted from:
http://www.nabble.com/Usage-of-%27exists%27-keyword-to-get-the-number-of-...
--
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