For loops in bundles?

Does anyone know if there is a way to make for loops in drools ?.

I'm trying to iterate over a list of strings to see if one of the strings matches a pattern, for example.

def listOfStrings = ['a','a.b','abc'] for(String s:listOfStrings){ if(s matches "^ab*$"){ return true } } 

I wrote the following rule based on documentation that I could find, but I don't think the syntax is correct.

 rule "Matcher" when TestClass : TestClass(($s matches "^ab*$") from listOfStrings, count($s)) then TestClass.setResponse( "Condition is True !!" ); end 

I find it difficult to find good documentation in drl

I would appreciate any help anyone can give.


Based on the previous answer, I tried the following

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

However, now I get the following error message:

 [43,197]: unknown:43:197 Unexpected token 'this' 
+7
drools
source share
4 answers

I think you misunderstood the basics of the rule engine; you need to think a little differently.

Instead of β€œiterating” through the list, you need to split the list into its composite lines and insert them separately as facts into the working memory.

Only lines / facts that match the when condition will trigger the rule.

You can also view global queries and queries. global will allow you to enter a service in your working memory to get the consequences for the call, and the request may be the way by which you can get matching strings from working memory.

+12
source share

I used this command when I used this drl file as the rules for my project

Hope this can be helpful for you.

package com.sample

import com.sample.HelloProcessModel;

rule "NYuser_Rule"

 no-loop true ruleflow-group "EvalLoopcondition" when m:HelloProcessModel(userlocation in ("NewYorkUser"), count < 4) then m.setLoopcondition(6);update(m); 

end

rule "ChileUser_Rule"

 no-loop true ruleflow-group "EvalLoopcondition" when m:HelloProcessModel(userlocation in ("ChileUser"), count < 3) then m.setLoopcondition(5);update(m); 

end

rule "BelgiumUser_Rule"

 no-loop true ruleflow-group "EvalLoopcondition" when m:HelloProcessModel(userlocation in ("BelgiumUser"), count < 6) then m.setLoopcondition(8);update(m); 

end

+3
source share

The Rete algorithm does not work this way.

I think you want to try regex in Drools .

+2
source share

I also iterate over String[] and using these String functions for each row from String []. This is what I use ...

String ( $vvl.indexOf( String.valueOf( charAt($idx)) ) >= 0 ) of $m.stringArray

That way, you can call different String functions for each string placed in a String Array.

0
source share

All Articles