Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

are for loops possible in drools?

Tags:

drools

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


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'
like image 371
mh377 Avatar asked Jul 31 '10 13:07

mh377


2 Answers

I think you've misunderstood the fundamentals of a rules engine; you need to think a bit differently.

Instead of 'iterating' over the list, you need to break the list into its component strings and insert them individually as facts into working memory.

Only the strings/facts which match the 'when' condition will fire the rule.

You might also want to look into globals and queries. global will allow you to inject a service into your working memory for your consequences to call, and the query might be a way by which you can obtain the matched strings out of working memory.

like image 89
Steven Herod Avatar answered Sep 19 '22 07:09

Steven Herod


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

Hope this may be helpful to 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
like image 34
Dhaval Shah Avatar answered Sep 22 '22 07:09

Dhaval Shah