检查 Drools 列表中的特定元素

发布于 2025-01-08 11:35:54 字数 331 浏览 6 评论 0原文

我刚刚开始使用 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 技术交流群。

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

发布评论

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

评论(1

蓝海 2025-01-15 11:35:54

如果 A 实例如您所说(理想情况)在工作内存中,则只需使用它编写规则:

rule X
when
    A( name == "bob", value == 10 )
...

不建议直接将集合(列表、树等)插入工作内存中,因为它们是抽象数据结构,没有任何附加内在语义。但假设您有一个 Person 类,其中包含地址列表,并且您希望对加拿大蒙特利尔的每个地址执行规则,而不将地址本身作为 facs 插入。你可以这样写:

rule X
when
    Person( $addresses : addresses )
    Address( city == "Montreal", country == "CA" ) from $addresses
...

最后,如果你真的想使用列表本身作为事实(同样,不好的做法),你可以执行以下操作,但请注意,它将匹配工作内存中的所有列表:

rule X
when
    $list : List()
    A( name == "bob", value == 10 ) from $list
...

If the A instances are in the working memory as you say (ideal scenario), just write the rule using it:

rule X
when
    A( name == "bob", value == 10 )
...

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:

rule X
when
    Person( $addresses : addresses )
    Address( city == "Montreal", country == "CA" ) from $addresses
...

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:

rule X
when
    $list : List()
    A( name == "bob", value == 10 ) from $list
...
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文