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

Problem with "!=" field constraint

    Details

    • Type: Bug
    • Status: Resolved (View Workflow)
    • Priority: Major
    • Resolution: Done
    • Affects Version/s: 4.0.0.GA
    • Fix Version/s: 4.0.1
    • Component/s: None
    • Labels:
      None

      Description

      The following code illustrates the problem.

      import org.drools.*;
      import org.drools.compiler.*;
      import org.drools.rule.Package;

      import java.io.*;

      public class DroolsTest {
      private RuleBase rules;
      private StatefulSession memory;

      public DroolsTest(File file) throws Exception

      { rules =RuleBaseFactory.newRuleBase(); memory=rules.newStatefulSession(); rules.addPackage(loadPackage(file)); }

      protected Package loadPackage(File file) throws IOException {
      FileInputStream stream=null;

      try

      { stream=new FileInputStream(file); return(loadPackage(stream)); }

      finally

      { if (stream!=null) stream.close(); }

      }

      protected Package loadPackage(InputStream stream) throws IOException {
      try

      { PackageBuilder builder=new PackageBuilder(); builder.addPackageFromDrl(new InputStreamReader(stream)); return(builder.getPackage()); }

      catch(Exception ex)

      { throw new IOException(); }

      }

      public StatefulSession getSession()

      { return(memory); }

      public static void main(String ... args) {
      try

      { DroolsTest test=new DroolsTest(new File(args[0])); SpecialString first42 =new SpecialString("42"); SpecialString second42=new SpecialString("42"); test.getSession().insert(new SpecialString("World")); test.getSession().insert(first42); test.getSession().insert(second42); System.out.println("Fact handle: "+test.getSession().getFactHandle(first42)); System.out.println("Fact handle: "+test.getSession().getFactHandle(second42)); System.out.println("Firing rules ..."); test.getSession().fireAllRules(); }

      catch (Exception ex)

      { ex.printStackTrace(); }

      }
      }

      In the above example, three different objects are created and inserted into the working memory of the Drools Engine. Two of the three objects have the same text - "42".

      The class SpecialString uses the standard equals(...) and hashCode(...) implementation:

      public class SpecialString {
      private String text;

      public SpecialString(String text)

      { this.text=text; }

      public String getText()

      { return(text); }

      @Override public String toString()

      { return(getText()+"["+super.toString()+"]"); }

      }

      For the rule

      package test

      rule "A test"

      when
      x : SpecialString()
      y : SpecialString(this!=x)

      then
      System.out.println(x+"/"+y);
      end

      The output is

      Fact handle: [fid:2:2:42[SpecialString@fde8da]]
      Fact handle: [fid:3:3:42[SpecialString@e4d6ef]]
      Firing rules ...
      42[SpecialString@e4d6ef]/World[SpecialString@faa824]
      World[SpecialString@faa824]/42[SpecialString@e4d6ef]
      42[SpecialString@fde8da]/World[SpecialString@faa824]
      World[SpecialString@faa824]/42[SpecialString@fde8da]

      I would expect that

      42[SpecialString@e4d6ef]/42[SpecialString@fde8da]
      42[SpecialString@fde8da]/42[SpecialString@e4d6ef]

      is also contained in the output, but this is not the case.

        Gliffy Diagrams

          Attachments

            Activity

              People

              • Assignee:
                tirelli Edson Tirelli
                Reporter:
                marreitz Markus Reitz
              • Votes:
                0 Vote for this issue
                Watchers:
                1 Start watching this issue

                Dates

                • Created:
                  Updated:
                  Resolved: