将(标签)列表与另一个列表进行匹配并检测公共元素的存在

发布于 2025-01-05 02:45:49 字数 1028 浏览 8 评论 0原文

我的要求是匹配标签。在示例中,这个特定的 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 有 memberOfcontains,但它们检查的是标量与集合,而不是集合与集合。

我看到两种可能的方法:

  1. 引入一个函数 boolean isIntersecting(list,list) 并告诉 Drools 使用它进行真实性检查
  2. 将 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:

  1. introduce a function boolean isIntersecting(list,list) and tell Drools to use that for truth checking
  2. Implement TeacherHour.attributes[] as a string instead of a list and HourConstraint.valueslist as a regular expression that can match that list

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

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

发布评论

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

评论(1

誰認得朕 2025-01-12 02:45:49

有几种选择。最直接的方法是使用 Collections 类来为您完成此操作:

rule X
when
    $t: TeacherHour( )
    HourConstraint( Collections.disjoint( $t.attributes["tags"], values_list ) == false )
...

如果这是您在规则中经常使用的内容,那么我建议将该函数包装在由 Drools 支持的可插入运算符中。假设您将运算符命名为“intersect”,则可以像这样编写规则:

rule X
when
    $t: TeacherHour( )
    HourConstraint( values_list intersect $t.attributes["tags"] )
...

第三种选择是使用“from”,但这在运行时效率较低,因为它会导致第一个列表上的迭代:

rule X
when
    $t: TeacherHour( )
    $tag : String() from $t.attributes["tags"]
    exists( HourConstraint( values_list contains $tag ) )
...

There are a few options. Most straight forward is to use the Collections class to do that for you:

rule X
when
    $t: TeacherHour( )
    HourConstraint( Collections.disjoint( $t.attributes["tags"], values_list ) == false )
...

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:

rule X
when
    $t: TeacherHour( )
    HourConstraint( values_list intersect $t.attributes["tags"] )
...

A third option, is to use "from", but that is less efficient in runtime as it causes iterations on the first list:

rule X
when
    $t: TeacherHour( )
    $tag : String() from $t.attributes["tags"]
    exists( HourConstraint( values_list contains $tag ) )
...
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文