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?
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...
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.
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.
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.
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 !).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With