Drool 规则指定仅针对 Parent 而非其 child 触发

Drool rule to specify only to fire for Parent not for its child

我有两个 java object(比如说 Parent 和 Child..child extends parent)。但是我需要仅针对 parent 而非其 child.

指定规则触发

例如:

when 
    $raw : Parent()
then
    System.out.println(" Parent")
end
----
when 
    $raw : Child()
then
    System.out.println(" Child")
end

如果我尝试将 child object 作为事实注入,它会为两者触发。我不能使用 not Child() bz 我有很多 child.Thanks

此致, 尼普纳

首先,让我说区分 Parent object 与其任何子 class 的必要性很可能是一个设计缺陷。如果您需要仅适用于 Parent 而不适用于其任何子 class 的规则,则表明 Parent 将被并行处理 到它的 children。要解决,不要插入 Parent object 而是创建另一个子 class

class ChildWithoutAdditionalProperties extends Parent {
}

并插入 class 的 object。

其次,not Child() 不匹配除 Child 之外任何其他 class 的 object,正如您的评论 "I am not able to use not Child()" 似乎表明的那样:它将匹配因为没有任何 Child 事实。

要测试object是否属于某个class,您可以比较object的class:

rule "parent"
when
    $e: Parent( $e.getClass == Parent.class )
then
    System.out.println( "found Parent" );
end