]
Mario Fusco resolved DROOLS-3583.
---------------------------------
Resolution: Done
Fixed by
A comment creates an infinite loop
----------------------------------
Key: DROOLS-3583
URL:
https://issues.redhat.com/browse/DROOLS-3583
Project: Drools
Issue Type: Bug
Affects Versions: 7.16.0.Final
Reporter: Osira Ben
Assignee: Mario Fusco
Priority: Minor
This rule file makes the engine stuck in an infinite loop.
{code:drools}
package com.example
declare Counter
value: int
end
rule "Init" when
not Counter()
then
drools.insert(new Counter(0));
end
rule "Loop"
when
c: Counter()
then
// removing this comment line removes the loop
c.setValue(1);
update(c);
end
{code}
But the loop is not an issue.
The issue is that if the commented line is removed the loop is gone.
This code doesn't cause the loop.
{code:drools}
package com.example
declare Counter
value: int
end
rule "Init" when
not Counter()
then
drools.insert(new Counter(0));
end
rule "Loop"
when
c: Counter()
then
c.setValue(1);
update(c);
end
{code}
Java code used to run the engine.
{code:java}
public class DroolsLoopingTest {
public static class LoopError extends RuntimeException {
}
public static void main(String[] args) {
System.setProperty("drools.dump.dir", ".");
final KieServices ks = KieServices.Factory.get();
final KieRepository kr = ks.getRepository();
final KieFileSystem kfs = ks.newKieFileSystem();
kfs.write("src/main/resources/loop.drl", new
ClassPathResource("loop.drl"));
final KieBuilder kb = ks.newKieBuilder(kfs);
kb.buildAll();
if (kb.getResults().hasMessages(Message.Level.ERROR))
throw new RuntimeException(kb.getResults().toString());
final KieContainer kContainer = ks.newKieContainer(kr.getDefaultReleaseId());
KieSession kSession = kContainer.newKieSession();
kSession.addEventListener(new DefaultAgendaEventListener() {
private int i = 0;
@Override
public void afterMatchFired(AfterMatchFiredEvent event) {
System.out.println(event);
if (++i > 10) throw new LoopError();
}
});
try {
kSession.fireAllRules();
throw new RuntimeException("Expected loop error");
} catch (LoopError e) {
System.out.println("Loop detected");
}
}
}
{code}
I noticed a difference in generated code.
The rule file with the comment line generates this code
{code:java}
public static void defaultConsequence(KnowledgeHelper drools, com.example.Counter c,
org.kie.api.runtime.rule.FactHandle c__Handle__ ) throws java.lang.Exception {
org.kie.api.runtime.rule.RuleContext kcontext = drools;
// removing this comment line removes the loop
c.setValue(1);
{ drools.update( c__Handle__,
org.drools.core.util.bitmask.AllSetButLastBitMask.get(), com.example.Counter.class ); };
}
{code}
While the rule file without the comment line generates this code
{code:java}
public static void defaultConsequence(KnowledgeHelper drools, com.example.Counter c,
org.kie.api.runtime.rule.FactHandle c__Handle__ ) throws java.lang.Exception {
org.kie.api.runtime.rule.RuleContext kcontext = drools;
c.setValue(1);
{ drools.update( c__Handle__, new org.drools.core.util.bitmask.LongBitMask(2L),
com.example.Counter.class ); };
}
{code}
Expected result is that comments should not affect engine behavior.