0 Replies Latest reply on Jun 21, 2007 8:32 AM by natraj.gudla

    Recursive Loop with no-loop true

    natraj.gudla

      Hi, We are trying to test a simple DRL which has 2 rules. The first rule modifies the object attribute which the second rules checks for. But it is observed that the execution is going into an infinite loop even after setting the "no-loop true" for both the rules.
      We are using the Drools 3.0.6. Check the codes below....Any help?? Seems like more than one modify call on the object is forcing it into a loop. A single modification does not seem to have this problem.

      DRL File:
      #created on: Jun 21, 2007
      package com.sample
      import com.sample.LoanApplication;
      #list any import classes here.
      #declare any global variables here
      rule "Your First Rule"
      no-loop true;
      when
      lA: LoanApplication(ageBracket == 10)
      then
      System.out.println(" Cond Success ");
      lA.setProductType("Product10");
      modify(lA);

      end

      rule "Your Second Rule"
      no-loop true;
      #include attributes such as "salience" here...
      when
      lA: LoanApplication( productType == "Product10" )
      then
      System.out.println(" Base is 10 ");
      lA.setProductType("ProductType20");
      modify(lA);
      end


      JAVA CLASS FILE:

      package com.sample;

      public class LoanApplication {

      public Person person;
      public String risk;
      public int priorClaims;
      public String productType;
      public int base;
      public String recordReason;
      public int discount;
      public int ageBracket;

      public int callageBracket(){
      //System.out.println(" Custom method called ");
      return ageBracket;
      }
      public int getAgeBracket() {
      //return person.age;
      return ageBracket;
      }

      public void setAgeBracket(int ageBracket) {
      this.ageBracket = ageBracket;
      }

      public static void main(String[] args) {
      // TODO Auto-generated method stub
      }

      public int getBase() {
      return base;
      }

      public void setBase(int base) {
      this.base = base;
      }

      public int getDiscount() {
      return discount;
      }

      public void setDiscount(int discount) {
      this.discount = discount;
      }

      public Person getPerson() {
      return person;
      }

      public void setPerson(Person person) {
      this.person = person;
      }

      public int getPriorClaims() {
      return priorClaims;
      }

      public void setPriorClaims(int priorClaims) {
      this.priorClaims = priorClaims;
      }

      public String getProductType() {
      return productType;
      }

      public void setProductType(String productType) {
      this.productType = productType;
      }

      public String getRecordReason() {
      return recordReason;
      }

      public void setRecordReason(String recordReason) {
      this.recordReason = recordReason;
      }

      public String getRisk() {
      return risk;
      }

      public void setRisk(String risk) {
      this.risk = risk;
      }

      }


      THE JAVA TEST CLASS:

      package com.sample;

      import java.io.InputStreamReader;
      import java.io.Reader;
      import java.util.Iterator;

      import org.drools.QueryResult;
      import org.drools.QueryResults;
      import org.drools.RuleBase;
      import org.drools.RuleBaseFactory;
      import org.drools.WorkingMemory;
      import org.drools.compiler.PackageBuilder;
      import org.drools.compiler.PackageBuilderConfiguration;
      import org.drools.rule.Package;
      import org.drools.spi.AgendaGroup;

      /**
      * This is a sample file to launch a rule package from a rule source file.
      */
      public class DroolsTest {

      public static final void main(String[] args) {
      try {

      //load up the rulebase
      RuleBase ruleBase = readRule();
      WorkingMemory workingMemory = ruleBase.newWorkingMemory();

      LoanApplication loanApp = new LoanApplication();
      loanApp.setAgeBracket(10);
      loanApp.setDiscount(10);
      loanApp.setProductType("Product11");

      workingMemory.assertObject(loanApp);
      workingMemory.fireAllRules();

      System.out.println(" Loan App Base "+loanApp.getProductType());


      } catch (Throwable t) {
      t.printStackTrace();
      }
      }

      /**
      * Please note that this is the "low level" rule assembly API.
      */
      public static RuleBase readRule() throws Exception {
      Reader source = new InputStreamReader( DroolsTest.class.getResourceAsStream( "/TestLooping.drl" ) );
      PackageBuilderConfiguration config = new PackageBuilderConfiguration();
      config.setCompiler(PackageBuilderConfiguration.JANINO);
      PackageBuilder builder = new PackageBuilder(config);
      builder.addPackageFromDrl( source );
      Package pkg = builder.getPackage();
      RuleBase ruleBase = RuleBaseFactory.newRuleBase();
      ruleBase.addPackage( pkg );
      return ruleBase;
      }