Drools 规则用于过滤数字属性低于列表总数百分比的元素
我刚刚开始在一个小项目中使用 Drools,现在我需要编写一个有点复杂的规则,但我真的不知道最好的方法是什么。
我将此规则应用于相同类型的对象列表(此类有一个名为 numberOfExecutions 的字段)。对于列表中的每个元素,我需要选择 numberOfExecutions 高于 total numberOfExecutions 的 5% 的元素(列表中的所有元素)。
到目前为止,我想不出一个很好的方法来实现这一点,有人有建议吗?
编辑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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
解决方案取决于您修改或创建新对象类的能力。对于您的情况,我会执行以下操作:
我引入了新的
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:
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 inCalculator
.您可以使用“Java”规则方言。创建一个函数来执行 numberOfExecutions 的逻辑并设置适当的标志。您可以一一传递对象列表或单个对象来检查 numberOfExecutions 标准。该方法将像任何常规 java 方法一样工作。以下是调用函数的小示例。这可能有帮助。
这只是一个提示。您可以根据需要使用它。
谢谢。
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.
It is just a hint. You can use it as you want.
Thanks.