I have a slight problem I am running into when I am writing rules and trying to 'or' sister classes. Some background, this is all a small mockup to demonstrate, this is in 6.4. I have a Parent class Fact which has two daughters: ChildAFact and ChildBFact and I have this Rule:
rule "Rule 1"
when
f: ( ChildAFact() or ChildBFact() )
then
System.out.println(f);
end
When I run my test, I get this Error:
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)
I was hoping someone knew a way to fix this. I was hoping that the object that matches the condition would just be cast to the closest common ancestor of the objects that are 'ored'
Binding one variable to two different types isn't possible. But you might get by with
rule "a or b"
when
f: Fact( class == ChildAFact.class || == ChildBFact.class )
then
System.out.println(f);
end
The usefulness of such rules is restricted. Generally you should write separate rules, one for each fact type. You might use "extend" to factor out common parts so that you don't have to repeat all of the constraints.