Drools:如何获取包含具有唯一 ID 的项目的列表

Drools: how to get a list with items with unique ids

我在 Drools 中有一个复杂的对象。 对象 A 包含对象 B 的列表。 对象 B 包含对象 C 的列表。

对象 C 有一个 属性 ID。对象 C 可以出现在具有相同 ID 的不同对象 B 上。

我需要能够计算 A 中唯一 ID 的数量。

当 id 不重复时,我的当前规则有效。但是如何获得没有重复 ID 的 listOfC?

rule CountCs
    dialect "mvel"
    when
       a : A( )

       listOfC: List( ) from accumulate (
           b : B( ) from a.bItems
           and
           c : C( ) from b.cItems;
           collectList( c )
       )
       eval( listOfC > 2)
    then
       // do whatever
    end

如果您只对出现次数感兴趣,而不对出现次数本身感兴趣,那么您可以使用 Set 来收集您的 ID。 Set 将负责为您消除重复项(假设您的 ID 实施了相应的身份和相等性检查):

rule CountCs
dialect "mvel"
when
 a : A( )
 setOfC: Set( size > 2 ) from accumulate (
   b : B( ) from a.bItems
   and
   c : C( ) from b.cItems;
   collectSet( c.id )
 )
then
  // do whatever
end

请注意,我在同一模式中包括了对集合大小的评估。您不需要为此使用额外的 eval

希望对您有所帮助,