Uploaded image for project: 'Drools'
  1. Drools
  2. DROOLS-3583

A comment creates an infinite loop

XMLWordPrintable

    • Icon: Bug Bug
    • Resolution: Done
    • Icon: Minor Minor
    • None
    • 7.16.0.Final
    • None
    • None
    • 2019 Week 50-52 (from Dec 9)
    • NEW
    • NEW

      This rule file makes the engine stuck in an infinite loop.

      Unable to find source-code formatter for language: drools. Available languages are: actionscript, ada, applescript, bash, c, c#, c++, cpp, css, erlang, go, groovy, haskell, html, java, javascript, js, json, lua, none, nyan, objc, perl, php, python, r, rainbow, ruby, scala, sh, sql, swift, visualbasic, xml, yaml
      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
      

      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.

      Unable to find source-code formatter for language: drools. Available languages are: actionscript, ada, applescript, bash, c, c#, c++, cpp, css, erlang, go, groovy, haskell, html, java, javascript, js, json, lua, none, nyan, objc, perl, php, python, r, rainbow, ruby, scala, sh, sql, swift, visualbasic, xml, yaml
      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
      

      Java code used to run the engine.

      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");
              }
          }
      }
      

      I noticed a difference in generated code.
      The rule file with the comment line generates this code

      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 ); };
      }
      

      While the rule file without the comment line generates this code

      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 ); };
      }
      

      Expected result is that comments should not affect engine behavior.

              mfusco@redhat.com Mario Fusco
              osiraben Osira Ben (Inactive)
              Votes:
              0 Vote for this issue
              Watchers:
              2 Start watching this issue

                Created:
                Updated:
                Resolved: