当我编写规则并尝试“或”姐妹类时,我遇到了一个小问题。一些背景,这都是一个小模型来演示,这是在 6.4 中。我有一个父类 Fact,它有两个女儿:ChildAFact 和 ChildBFact,我有以下规则:
rule "Rule 1"
when
f: ( ChildAFact() or ChildBFact() )
then
System.out.println(f);
end
当我运行测试时,出现此错误:
java.lang.RuntimeException: Error while creating KieBase[Message [id=1, level=ERROR, path=Sample.drl, line=12, column=0
text=Duplicate declaration for variable 'f' in the rule 'Rule 1'], Message [id=2, level=ERROR, path=Sample.drl, line=10, column=0
text=Unable to Analyse Expression System.out.println(f);:
[Error: unable to resolve method using strict-mode: org.drools.core.spi.KnowledgeHelper.f()]
[Near : {... System.out.println(f); ....}]
^
[Line: 10, Column: 0]]]
at org.drools.compiler.kie.builder.impl.KieContainerImpl.getKieBase(KieContainerImpl.java:450)
at org.drools.compiler.kie.builder.impl.KieContainerImpl.newKieSession(KieContainerImpl.java:604)
at org.drools.compiler.kie.builder.impl.KieContainerImpl.newKieSession(KieContainerImpl.java:575)
at com.sample.DroolsTest.main(DroolsTest.java:20)
我希望有人知道解决这个问题的方法。我希望匹配条件的对象将被强制转换为“ored”对象的最接近的共同祖先
最佳答案
将一个变量绑定(bind)到两种不同类型是不可能的。但你可能会过得去
rule "a or b"
when
f: Fact( class == ChildAFact.class || == ChildBFact.class )
then
System.out.println(f);
end
此类规则的用途受到限制。一般来说,您应该编写单独的规则,针对每种事实类型一个规则。您可以使用“扩展”来分解公共(public)部分,这样您就不必重复所有约束。
关于java - 将 'or' 与姐妹类一起使用时出现 Drools 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39231026/