将(标签)列表与另一个列表进行匹配并检测公共元素的存在
我的要求是匹配标签。在示例中,这个特定的 HourConstraint
检查分配给 Hour(23)
的 TeacherHour
。
具体来说,它会检查 TeacherHour.attributes["tags"]
的值 ["asst_ct","teacher_john_smith"]
并检测至少一个匹配项 ,本例中为两个("asst_ct"
和 "teacher_john_smith"
)。
TeacherHour:
id: 47
assigned_hour: Null
attributes:Map<List<String>>
"tags":["asst_ct","no_strenuous_duties","kinda_boring","teacher_john_smith"]
"another_attribute":[...]
HourConstraint:
hour: Hour(23)
attribute: "tags"
values_list: ["asst_ct","teacher_john_smith"]
问题:如何检测两个列表之间是否存在共同元素(正确或错误)?
Drools Expert 有 memberOf
和 contains
,但它们检查的是标量与集合,而不是集合与集合。
我看到两种可能的方法:
- 引入一个函数 boolean isIntersecting(list,list) 并告诉 Drools 使用它进行真实性检查
- 将 TeacherHour.attributes[] 作为字符串而不是列表和
HourConstraint.valueslist
作为可以匹配该列表的正则表达式
My requirement is to match tags. In the example, this particular HourConstraint
checks the TeacherHour
assigned to Hour(23)
.
Specifically, it checks TeacherHour.attributes["tags"]
for the values ["asst_ct","teacher_john_smith"]
and detects atleast one match, two in this case (both "asst_ct"
and "teacher_john_smith"
) .
TeacherHour:
id: 47
assigned_hour: Null
attributes:Map<List<String>>
"tags":["asst_ct","no_strenuous_duties","kinda_boring","teacher_john_smith"]
"another_attribute":[...]
HourConstraint:
hour: Hour(23)
attribute: "tags"
values_list: ["asst_ct","teacher_john_smith"]
Question: How do I detect the presence (true or false) of common elements between two lists?
Drools Expert has memberOf
and contains
, but they check a scalar vs a collection, never a collection vs a collection.
I see two potential ways:
- introduce a function
boolean isIntersecting(list,list)
and tell Drools to use that for truth checking - Implement
TeacherHour.attributes[]
as a string instead of a list andHourConstraint.valueslist
as a regular expression that can match that list
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
有几种选择。最直接的方法是使用 Collections 类来为您完成此操作:
如果这是您在规则中经常使用的内容,那么我建议将该函数包装在由 Drools 支持的可插入运算符中。假设您将运算符命名为“intersect”,则可以像这样编写规则:
第三种选择是使用“from”,但这在运行时效率较低,因为它会导致第一个列表上的迭代:
There are a few options. Most straight forward is to use the Collections class to do that for you:
If this is something you would use often in your rules, then I recommend wrapping that function in a pluggable operator, supported by Drools. Lets say you name the operator "intersect", you can then write your rules like this:
A third option, is to use "from", but that is less efficient in runtime as it causes iterations on the first list: