[jboss-jira] [JBoss JIRA] Created: (JBRULES-1079) Problem with "!=" field constraint

Markus Reitz (JIRA) jira-events at lists.jboss.org
Mon Aug 13 14:33:01 EDT 2007


Problem with "!=" field constraint
----------------------------------

                 Key: JBRULES-1079
                 URL: http://jira.jboss.com/jira/browse/JBRULES-1079
             Project: JBoss Rules
          Issue Type: Bug
      Security Level: Public (Everyone can see)
    Affects Versions:  4.0.0.GA
            Reporter: Markus Reitz
         Assigned To: Mark Proctor


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 at fde8da]]
  Fact handle: [fid:3:3:42[SpecialString at e4d6ef]]
  Firing rules ...
  42[SpecialString at e4d6ef]/World[SpecialString at faa824]
  World[SpecialString at faa824]/42[SpecialString at e4d6ef]
  42[SpecialString at fde8da]/World[SpecialString at faa824]
  World[SpecialString at faa824]/42[SpecialString at fde8da]

I would expect that

  42[SpecialString at e4d6ef]/42[SpecialString at fde8da]
  42[SpecialString at fde8da]/42[SpecialString at e4d6ef]

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

-- 
This message is automatically generated by JIRA.
-
If you think it was sent incorrectly contact one of the administrators: http://jira.jboss.com/jira/secure/Administrators.jspa
-
For more information on JIRA, see: http://www.atlassian.com/software/jira

        



More information about the jboss-jira mailing list