java Drools 规则:如何在“何时”部分使用方法?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2503321/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-29 21:31:18  来源:igfitidea点击:

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

javasyntaxrulesdrools

提问by manoelhc

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

我需要在 DSLR 文件的“何时”部分执行一个方法,我不确定是否可行。例子:

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)

(注意:isInAgeRange() 和 haveAtLeastOptIns() 是 User 的方法)

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

我尝试了 eval() 并且没有出现错误,但它没有执行。像这样:

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?

我该如何解决这个问题?

回答by Michael Neale

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 !).

请注意 eval() 顶级元素的使用 - 它也仅使用一种用户模式 ​​- 然后对其应用约束。(在未来的版本中,内联 eval 无需编写 eval 即可工作!)。