rules-users - are for loops possible in drools

by ceemh3on 2010-07-31T15:55:27+00:00

does anybody know if there is a way to do for loops in drools ?.
I am trying to loop through a list of string to see if one of the strings
matches a pattern e.g.
def listOfStrings = ['a','a.b','a.b.c']
for(String s:listOfStrings){
if(s matches "^a.b.*$"){
return true
}
}
I have written the following rule based on what documentation I could find,
but I dont think the syntax is correct
rule "Matcher"
when
TestClass : TestClass(($s matches "^a.b.*$") from listOfStrings,
count($s))
then
TestClass.setResponse( "Condition is True !!" );
end
I am finding it hard to find good documentation on the drl language
I would appreciate any help that anybody can give me

Re: rules-users - are for loops possible in drools

by Edson Tirellion 2010-07-31T16:23:17+00:00.
Try:
when
TestClass( $list : listOfStrings )
Number( $count : intValue ) from accumulate(
String( this matches "^a.b.*$" ) from $list,
count(1) )
then
// $count has number of matching strings
end
[]s
Edson
2010/7/31 ceemh3
    Try:when    TestClass( $list : listOfStrings )    Number( $count : intValue ) from accumulate(           String( this matches "^a.b.*$" ) from $list,
           count(1) )then    // $count has number of matching stringsend    []s    Edson2010/7/31 ceemh3 <martin-henderson@hotmail.com>
does anybody know if there is a way to do for loops in drools ?.
I am trying to loop through a list of string to see if one of the strings
matches a pattern e.g.
def listOfStrings = ['a','a.b','a.b.c']
for(String s:listOfStrings){
 if(s matches "^a.b.*$"){
 return true
 }
}
I have written the following rule based on what documentation I could find,
but I dont think the syntax is correct
rule "Matcher"
  when
     TestClass : TestClass(($s matches "^a.b.*$") from listOfStrings,
count($s))
  then
     TestClass.setResponse( "Condition is True !!" );
end
I am finding it hard to find good documentation on the drl language
I would appreciate any help that anybody can give me
Sent from the Drools - User mailing list archive at Nabble.com.

Re: rules-users - are for loops possible in drools

by ceemh3on 2010-07-31T16:45:50+00:00.

Thanks for the response
Since I dont actually need to keep a count of the number of strings that
match the pattern in the list, I have changed your solution to the code
below to make it simpler.
rule "Matcher"
when
TestClass( $list : listOfStrings )
String( this matches "^a.b.*$" ) from $list
then
TestClass.setResponse( "Condition is True !!" );
end
However, when I run this I get the following error because it doesnt
recognize the 'this' keyword
[43,197]: unknown:43:197 Unexpected token 'this'
Do you know what the problem could be ?

Re: rules-users - are for loops possible in drools

by Dinesh kumaron 2010-08-02T14:21:15+00:00.

Hi,
I faced a similar problem. This thread helped me to solve the issue. this
keyword is working fine for me. U can check whether it is supported in your
version. I am using 5.0.1
But even this keyword works for me, I am facing another problem.
The then block executed for every success in the block
i.e., say there are 10 items in the list and 5 of the matches the regex,
then the when block executed 5 times.
Is this the only way to iterate a loop..?
My business scenario involves nested for loops. Is it possible in drools..?
How do I write a nested for loop. Remember that my inner for loop requires
values from the outer loops and this inner loops will go up to 3 or 4
levels. Is it possible and even if it is possible, will the coding become so
complex so that the business user who is going to use it may not understand
??
Kindly advice.
Regards,
Dinesh