Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Drools Rules: How can I use a method on "when" section?

I need to execute a method on "when" section of a DSLR file and I´m not sure if it´s possible. Example:

rule "WNPRules_10"
  when
    $reminder:Reminder(source == "HMI")
    $user:User(isInAgeRange("30-100")==true)
    Reminder(clickPercentual >= 10)
    User(haveAtLeastOptIns("1,2,3,4") == true)
  then
    $reminder.setPriority(1);update($reminder);
end

(note: isInAgeRange() and haveAtLeastOptIns() are methods of User)

I tried with eval() and no errors appeared, but it didn´t execute. Like this:

rule "WNPRules_10"
 when
  $reminder:Reminder(source == "HMI")
  $user:User(eval($user.isInAgeRange("30-100")==true))
  Reminder(clickPercentual >= 10)
  User(eval($user.haveAtLeastOptIns("1,2,3,4") == true))
 then
  $reminder.setPriority(1);update($reminder);
end

How can I resolve this problem?

like image 840
manoelhc Avatar asked Mar 23 '10 20:03

manoelhc


People also ask

How do you fire a specific rule in Drools?

In any case, the Customer has to be identified by a fact. To associcate a Customer with rules you need: class Customer { private String name; private List<String> rules; //... } So you are saying to create agenda for each customer and fire the rules based on agenda...

How rules are executed in Drools?

Let's quickly summarize how Drools works. Rules are written on . drl files, facts or POJOs are inserted in the Knowledge Base, then rules are applied on each fact. If a "When" part of a rule satisfies a fact, the "Then" part will execute.

How can you improve Drools performance of rule execution?

Literal Restrictions using the operator '==' provide for faster execution as we can index using hashing to improve performance. One can bind variables to facts and their fields and then use them in subsequent field constraints. A bound variable is called a declaration.

What is accumulate function in DRL Drools?

Drools accumulate CE supports inline custom code in its init/action/reverse/result blocks of code, but that is not declarative, nor is reusable among multiple rules and it is good only for a one-time need. Accumulate Functions to the rescue: implementing an accumulate function is a 20 minutes task.


1 Answers

Your second attempt looks fairly confused - also - do you have so User patterns - do you want them to refer to the same instance of user? or can they be separate instances (or must they be separate?) - that will change things a bit in some cases depending on your intent.

In terms of the simplest rewrite I can think of:

  rule "WNPRules_10"
  when
    $reminder:Reminder(source == "HMI")
    $user:User()
    eval($user.isInAgeRange("30-100") && $user.haveAtLeastOptIns("1,2,3,4"))
    Reminder(clickPercentual >= 10)
  then
    $reminder.setPriority(1);update($reminder);
  end

Note the use of the eval() top level element - it also uses only one user pattern - and then applies the constraints to it. (In a future version inline evals will work without having to write eval !).

like image 85
Michael Neale Avatar answered Oct 20 '22 02:10

Michael Neale