检查 Drools 列表中的特定元素
我刚刚开始使用 Drools(版本 5.1.0),所以如果这个问题已经得到解答,请耐心等待。
我有一个 java.util.List 对象,其中包含复杂类型 A 的对象,其中 A 为:
class A {
String name;
String Value;}
列表及其元素位于 Drools 引擎的工作内存中。仅当列表中元素的名称和值与特定值匹配时,是否有一种简单的方法来触发规则?
目前,我在 Drools 规则中使用自定义函数,该函数会迭代列表,如果存在与规范匹配的元素,则返回 true,但是我想知道这是否是最有效和最简单的用法。
I just have started using Drools (version 5.1.0) so please bear with me in case this question was already answered.
I have a java.util.List object which contains objects of complex type A, with A as:
class A {
String name;
String Value;}
The list as well as its elements are in the working memory of the Drools engine. Is there an easy way to fire a rule only if the name and value of an element in the list are matching specific values?
Currently, i am using a self-defined function inside the Drools rule, which iterates over the list and returns true if there is an element that matches the specification, however i wonder whether this is the most efficient and easiest use.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果 A 实例如您所说(理想情况)在工作内存中,则只需使用它编写规则:
不建议直接将集合(列表、树等)插入工作内存中,因为它们是抽象数据结构,没有任何附加内在语义。但假设您有一个 Person 类,其中包含地址列表,并且您希望对加拿大蒙特利尔的每个地址执行规则,而不将地址本身作为 facs 插入。你可以这样写:
最后,如果你真的想使用列表本身作为事实(同样,不好的做法),你可以执行以下操作,但请注意,它将匹配工作内存中的所有列表:
If the A instances are in the working memory as you say (ideal scenario), just write the rule using it:
Inserting collections (lists, trees, etc) into the working memory directly is not recommended, because they are abstract data structures without any intrinsic semantic attached. But lets say you have a Person class, that contains a list of Addresses, and you want to execute the rule for each address in Montreal, Canada, without inserting the addresses themselves as facs. You can write:
Finally, if you really want to use the list itself as a fact (again, bad practice), you can do the following, but note that it will match all lists in the working memory: