tags:

views:

85

answers:

4

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

A: 

The Rete algorithm doesn't work this way.

I think you want to try regex in Drools.

duffymo
Thanks for the link, but the example there only searches a string to see if it matches a regex.In my example I could use"a.b.c" matches "^a.b.*$"But that isnt what I am trying to achieve, I am trying to search a list of strings to see if any of them match a regular expression.I dont think it is the regex part that is causing me problems. It is the fact that I dont know how to iterate through a list of string in drools. Do you know how I might be able to do this ? thanks
MTH
I'm not certain, but I'm guessing you want to see about applying the regex to a list. "matches at least one", "matches one or more" are the kinds of things I'd be looking for. If you aren't find loops in Drools, it's because it's not supposed to work that way.
duffymo
do you know if there is a way to embed java code into a rule?
MTH
A: 

Based on the previous answer, I have tried the following

rule "Matcher"
  when
 TestClass:TestClass(String( this matches "^a.b.*$" ) from listOfStrings)
then
       TestClass.setResponse( "Condition is True !!" );
end 

However, I now get the following error message:

[43,197]: unknown:43:197 Unexpected token 'this'
MTH
A: 

I got around this issue by converting my list to a string and checking to see if it matches the regex

MTH