Drools 规则用于过滤数字属性低于列表总数百分比的元素

发布于 2024-10-11 09:56:37 字数 419 浏览 4 评论 0原文

我刚刚开始在一个小项目中使用 Drools,现在我需要编写一个有点复杂的规则,但我真的不知道最好的方法是什么。

我将此规则应用于相同类型的对象列表(此类有一个名为 numberOfExecutions 的字段)。对于列表中的每个元素,我需要选择 numberOfExecutions 高于 total numberOfExecutions5% 的元素(列表中的所有元素)。

到目前为止,我想不出一个很好的方法来实现这一点,有人有建议吗?

编辑1: 到目前为止,我能想到的最好的方法是在应用规则之前预先计算 numberOfExecutions 的总和,并使该值以某种方式可用于 drools 规则。

I have just started using Drools on a small project and now I need to write a rule a bit complex and I don't really know what's the best way to do it.

I am applying this rule to a list of objects of the same type (this class has a field called numberOfExecutions). For each element from the list I need to select the ones which have the numberOfExecutions above 5% of the total numberOfExecutions (the sum of numberOfExecutions of all the elements in the list).

I could not think of a nice way to implement this in drools so far, does anyone have a suggestion?

EDIT1:
The best I could think so far was to pre-compute the sum of numberOfExecutions before I apply the rules and make this value somehow available to the drools rules.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

你曾走过我的故事 2024-10-18 09:56:37

解决方案取决于您修改或创建新对象类的能力。对于您的情况,我会执行以下操作:

rule "Rule 1"
when
  $m : MyObject(counted == false)
  $c : Calculator
then
  modify($m) { setCounted(true); }
  $c.count($m);
end

rule "Rule 2"
when
  not  MyObject(counted == false)
  $m : MyObject(numberOfExecutions > ($c.totalExecutions * 0.05))
  $c : Calculator
then
  $m.markBiggerThan5();
end

我引入了新的 Calculator 类,该类可对未计数的对象进行计数(规则 1)。
如您所见,仅当所有对象都标记为已计数且总计存储在计算器中时,规则 2(您要求的规则)才会触发。

The solution depends on your ability to modify or create new object classes. Here is what I'd do in your case:

rule "Rule 1"
when
  $m : MyObject(counted == false)
  $c : Calculator
then
  modify($m) { setCounted(true); }
  $c.count($m);
end

rule "Rule 2"
when
  not  MyObject(counted == false)
  $m : MyObject(numberOfExecutions > ($c.totalExecutions * 0.05))
  $c : Calculator
then
  $m.markBiggerThan5();
end

I've introduced new Calculator class which counts uncounted objects (Rule 1).
As you can see, Rule 2 (the one you asking for) will only fire when all object are marked as counted and totals stored in Calculator.

心的位置 2024-10-18 09:56:37

您可以使用“Java”规则方言。创建一个函数来执行 numberOfExecutions 的逻辑并设置适当的标志。您可以一一传递对象列表或单个对象来检查 numberOfExecutions 标准。该方法将像任何常规 java 方法一样工作。以下是调用函数的小示例。这可能有帮助。

rule "numberOfExecutions "
    dialect "java"
    salience -1

    when

        $obj : yourObject()     
        eval(if not counted)

    then        
        checkNumberOfExecution($obj);

end

function void checkNumberOfExecution(com.test.YourObject obj) {

        //Your logic
}

这只是一个提示。您可以根据需要使用它。

谢谢。

You can use "Java" Rule Dialect. Make a function that execute your logic for numberOfExecutions and set appropriate flag. You can pass list of objects or single object one by one for checking numberOfExecutions criteria. This method will work as any regular java method. Following is a small sample for calling function. It might help.

rule "numberOfExecutions "
    dialect "java"
    salience -1

    when

        $obj : yourObject()     
        eval(if not counted)

    then        
        checkNumberOfExecution($obj);

end

function void checkNumberOfExecution(com.test.YourObject obj) {

        //Your logic
}

It is just a hint. You can use it as you want.

Thanks.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文