Uploaded image for project: 'JBRULES'
  1. JBRULES
  2. JBRULES-1602

The order of RHS statements shouldn't affect behavior (insertLogical)

This issue belongs to an archived project. You can view it, but you can't modify it. Learn more

    XMLWordPrintable

Details

    • Bug
    • Resolution: Done
    • Major
    • 5.4.0.Final
    • 4.0.7
    • drools-core
    • None

    Description

      I have a class 'Cat' that is dynamic (JavaBean with PropertyChangeSupport ) with only a 'name' attribute.

      Then I have the following rule:

      rule "test"
      when
      $cat:Cat( name == "tom" )
      then
      $cat.setName("cat");
      insertLogical(new String("test"));

      end

      When I test the rule against a Cat named tom, the logical inserted fact is not retracted. (So the fact is inserted because the rule matches, but is not retracted even if the rule is no more matching at the end.)

      If I change the order of the RHS, it works fine. The fact is inserted and then retracted.

      rule "test"
      when
      $cat:Cat( name == "tom" )
      then
      insertLogical(new String("test"));
      $cat.setName("cat");
      end

      So the behavior depends on the ordering of the RHS statements.

      Here is a complete example to show the problem:

      public class Cat {
      private String name;
      protected PropertyChangeSupport changes = new PropertyChangeSupport(this);

      public Cat(String name)

      { super(); this.name = name; }

      public String getName()

      { return this.name; }

      public void setName(String name)

      { final String oldName = this.name; this.name = name; this.changes.firePropertyChange("name", oldName, name); }

      public void addPropertyChangeListener(final PropertyChangeListener listener)

      { this.changes.addPropertyChangeListener(listener); }

      public void removePropertyChangeListener(
      final PropertyChangeListener listener)

      { this.changes.removePropertyChangeListener(listener); }

      }

      public class RHSOrderingTest {

      private static final String PACKAGE = "package ch.generali.pgr.rule ";
      private static final String IMPORT = "import ch.generali.pgr.rules.Cat ";
      private static final String WHEN = "rule \"test ok\" when $cat:Cat( name == \"tom\" ) ";

      private static final String THEN_OK = "then insertLogical(Integer.valueOf(1)); $cat.setName(\"cat\"); end";
      private static final String THEN_KO = "then $cat.setName(\"cat\"); insertLogical(Integer.valueOf(1)); end";

      private static final String RULE_OK = PACKAGE + IMPORT + WHEN + THEN_OK;

      private static final String RULE_KO = PACKAGE + IMPORT + WHEN + THEN_KO;

      private static final String QUERY = "query \"My test Integer\" integer : Integer( intValue == 1 ) end";

      public static void main(String[] args)

      { boolean ok_1 = testWithRule(RULE_OK); boolean ok_2 = testWithRule(RULE_KO); System.out.println((ok_1 ? "SUCCESS: " : "FAILED: ") + THEN_OK); System.out.println((ok_2 ? "SUCCESS: " : "FAILED: ") + THEN_KO); }

      private static boolean testWithRule(String rule)

      { Cat tom = new Cat("tom"); RuleBase rb = createRuleBase(rule + "\n" + QUERY); WorkingMemory wm = rb.newStatefulSession(); wm.insert(tom, true); wm.fireAllRules(); QueryResults results = wm.getQueryResults("My test Integer"); return results.size() == 0; }

      private static RuleBase createRuleBase(String rules) {

      try

      { RuleBase ruleBase = RuleBaseFactory.newRuleBase(); PackageBuilderConfiguration conf = new PackageBuilderConfiguration(); PackageBuilder packageBuilder = new PackageBuilder(conf); Reader source = new StringReader(rules); packageBuilder.addPackageFromDrl(source); Package pkg = packageBuilder.getPackage(); ruleBase.addPackage(pkg); return ruleBase; }

      catch (Exception e)

      { throw new RuntimeException(e); }

      }

      }

      Attachments

        Activity

          People

            etirelli@redhat.com Edson Tirelli
            pgras_jira Patrick Gras (Inactive)
            Archiver:
            rhn-support-ceverson Clark Everson

            Dates

              Created:
              Updated:
              Resolved:
              Archived:

              PagerDuty