4 Replies Latest reply on Apr 3, 2007 2:58 AM by kranthi kumar dalai

    Recursion in rules execution

    tarandeep sawhney Newbie

      Hi,

      I've below rules in my rule file, when i execute this drl, it leads to recursion when rules are fired. It'll be really great if somebody can suggest what can be done to avoid recursion during execution of dynamic rules given below.

      package com.globallogic.rules
      import com.thirdpillar.QuoteUpdater
      import java.lang.String
      import java.math.BigDecimal
      
      global BigDecimal TEMP
      global BigDecimal GL_SD_Amt
      
      rule "gst_rate_ol"
       no-loop true
       salience 50
       when
       QuoteUpdater:QuoteUpdater()
       eval(QuoteUpdater.getCustomerTypeCountry().equals("New Zealand") &&
       QuoteUpdater.isFinanceLease() == true ||
       QuoteUpdater.isRental() == true ||
       QuoteUpdater.isOperatingLease() == true ||
       QuoteUpdater.isHirePurchase() == true)
       then
       System.out.println("1");
       TEMP = BigDecimal . valueOf ( 0.125 ) ;
       QuoteUpdater . setQuoteLevelGSTRate ( BigDecimal . valueOf ( TEMP . doubleValue ( ) ) ) ;
       modify(QuoteUpdater);
      end
      rule "NZ_SD"
       no-loop true
       salience 45
       when
       QuoteUpdater:QuoteUpdater()
       eval(QuoteUpdater . getCustomerTypeCountry ( ).equals("New Zealand"))
       then
       System.out.println("2");
       QuoteUpdater . setQuoteLevelGSTStreamRate ( BigDecimal . valueOf ( 0 ) ) ;
       QuoteUpdater . setQuoteLevelStampDutyRate ( BigDecimal . valueOf ( 0 ) ) ;
       GL_SD_Amt = BigDecimal . valueOf ( 0 ) ;
       QuoteUpdater . setQuoteLevelStampDutyAmount ( BigDecimal . valueOf ( 0 ) ) ;
       modify(QuoteUpdater);
      end
      rule "gst_stream_rate_ol"
       salience 40
       no-loop true
       when
       QuoteUpdater:QuoteUpdater()
       eval(QuoteUpdater.getCustomerTypeCountry().equals("New Zealand") &&
       QuoteUpdater.isRental() == true ||
       QuoteUpdater.isOperatingLease() == true)
       then
       System.out.println("3");
       TEMP = BigDecimal . valueOf ( 0.125 ) ;
       QuoteUpdater . setQuoteLevelGSTStreamRate ( BigDecimal . valueOf ( TEMP . doubleValue ( ) ) ) ;
       modify(QuoteUpdater);
      end
      


      code in my test class is given below.
       /**
       *
       */
      package com.thirdpillar;
      
      import java.io.InputStreamReader;
      import java.io.Reader;
      
      import org.drools.RuleBase;
      import org.drools.RuleBaseFactory;
      import org.drools.WorkingMemory;
      import org.drools.compiler.PackageBuilder;
      import org.drools.rule.Package;
      
      /**
       * @author tarun.singh
       *
       */
      public class BasedOnDynamic {
      
       /**
       * @param args
       */
       public static void main(String[] args) {
       try {
       QuoteUpdater quoteUpdater = new QuoteUpdater();
       quoteUpdater.setCustomerTypeCountry("New Zealand");
       quoteUpdater.setFinanceLease(true);
       quoteUpdater.setRental(true);
       quoteUpdater.setOperatingLease(true);
       quoteUpdater.setHirePurchase(true);
      
       final Reader source = new InputStreamReader( QuoteUpdater.class.getResourceAsStream( "BasedOnDynamic.drl" ) );
       final PackageBuilder builder = new PackageBuilder();
       builder.addPackageFromDrl( source );
       final Package pkg = builder.getPackage();
       final RuleBase ruleBase = RuleBaseFactory.newRuleBase();
       ruleBase.addPackage( pkg );
      
       final WorkingMemory workingMemory = ruleBase.newWorkingMemory();
       workingMemory.assertObject( quoteUpdater );
       workingMemory.fireAllRules();
      
       } catch (Exception e) {
       e.printStackTrace();
       }
       }
      
      }
      


        • 1. Re: Recursion in rules execution
          Vivon Pereira Newbie

          You have to be very careful while using the 'OR' condition (||) in the JBoss rules.
          In the documentation that the JBoss have provided, they do mention that, every OR will be treated as a seperate sub-rule by itself.
          Hence, if u have a rule like this: condition1 || condition2, then the result is going to be executed twice for the two specified conditions.

          Hence, i recommend u judiciously use the or condition.
          Bcoz, if u have a condition like the one below, then the result would be totally wrong:
          rule test
          when
          t:Test(age > 25 || skill == 'IT')
          then
          t.setWage(t.getWage() * 1000);
          end

          The result should have been 1000 but it would give 1000000.

          There is one more point I have noticed in ur rule file is that, there are more than one rule containing modify method. When I wrote my rule file, it did not allow me to have more than one non-exclusive rule to have a modify method.
          Could u tell me, didnt it throw u any error. Since it hangs my code when it finds two rules having modify method in the same drl file.
          The java code is the same as urs.

          • 2. Re: Recursion in rules execution
            kranthi kumar dalai Newbie

            i am new to drools, i gone through drools documentation, now we are drools in our project as semantic module frame work as java. so can i have the details of how to write new drl file and java file in eclipse IDE(drools IDE) and what are the required JAR file for that.

            • 3. Re: Recursion in rules execution
              kranthi kumar dalai Newbie

              i am new to drools, i gone through drools documentation, now we are drools in our project as semantic module frame work as java. so can i have the details of how to write new drl file and java file in eclipse IDE(drools IDE) and what are the required JAR file for that.

              • 4. Re: Recursion in rules execution
                kranthi kumar dalai Newbie

                i am new to drools, i gone through drools documentation, now we are drools in our project as semantic module frame work as java. so can i have the details of how to write new drl file and java file in eclipse IDE(drools IDE) and what are the required JAR file for that.