具有继承性的 Drools Instanceof

Drools Instanceof with Inheritance

我有 5 种事实类型:BaseFact、AFact、BFact、CFact 和 DFact。

AFact、BFact、CFact 和 DFact 都继承自 BaseFact。

我在 BaseFacts 上有一些规则运行,我不能再在 CFacts 或 DFacts 上运行。

修改我的 BaseFact 规则的最佳方法是什么,以便它们仅 运行 BaseFacts、AFacts 和 BFacts。

是否有某种我可以检查的 instanceOf 函数,如下所示?

rule "BaseRule"
    when
        fact : BaseFact(this instanceOf AFact || this instanceOf BFact)
        ...
    then
        ...
end

或者我是否需要将此规则拆分为 2 个新规则,用于 AFact 和 BFact?

即使没有 instanceOf 运算符,也有多种方法可以实现您的需求。

这些是一些想法:

rule "BaseRule"
when
    fact : BaseFact(class == AFact.class || == BFact.class)
    ...
then
    //note that the variable fact is still of type BaseFact
    ...
end

更糟糕的版本:

rule "BaseRule"
when
    fact : BaseFact()
    not CFact(this == fact)
    not DFact(this == fact)
    ...
then
    //note that the variable fact is still of type BaseFact
    ...
end

或:

rule "BaseRule"
when
    AFact() OR
    BFact()
    ...
then
    //note you can't bind a variable to AFact or BFact
    ...
end

如果您只有 2 个要匹配的具体类型,那么使用 2 个单独的规则听起来也不是个坏主意。

希望对您有所帮助,

快速更新:我还可以使用:

rule "MyRow"
    dialect "mvel"
    when
        f1 : Wrapper( eval( nestedProperty#MyNestedClass  ), ... )
    then
        ...
end

这对于测试 类 的(嵌套)属性很方便。