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

Edson Tirelli (JIRA) jira-events at lists.jboss.org
Thu Aug 16 14:21:01 EDT 2007


     [ http://jira.jboss.com/jira/browse/JBRULES-1079?page=all ]

Edson Tirelli resolved JBRULES-1079.
------------------------------------

    Resolution: Done

I was able to reproduce the problem. It was a problem related to the shadow fact generated equals() method. The method was being generated like this:

   public boolean equals( Object object ) {
       if ( this == object || 
            this.delegate == object || 
            this.delegate.equals( object ) ) {
 
            return true;
       }
 
       if (( object == null ) || ( ! ( object instanceof <class> ) ) ) 
            return false;
 
       if( object instanceof ShadowProxy && 
          ( this.delegate == ((ShadowProxy)object).delegate ||
            this.delegate.equals( ((ShadowProxy)object).delegate ) ) ) {
 
            return true;
 
       }
 
       <class> other = (<class>) object;
 
       // for each field:
 
           if ( ! _fieldIsSet ) {
               __field = this.delegate.method();
                __fieldIsSet = true;
           }
 
           // for primitive types
           if ( this.field != other.field ) 
                return false;
 
           // for non primitive types
           if( ( ( this.field == null ) && ( other.field != null ) ) ||
               ( ( this.field != null ) && ( ! this.field.equals( other.field ) ) ) )
                return false;
 
       // if all fields were ok
       return true;
       // or if no fields exists
       return false;
    }

I.e, after failing on previous checks, the shadow fact tried to compare the value for each field, and in case they were all equal, it returned true, causing the unexpected behavior when fields were set to the "42" value in your test.

I removed that part from the equals() method generation and now it is like this:

   public boolean equals( Object object ) {
       if ( this == object || 
            this.delegate == object || 
            this.delegate.equals( object ) ) {
 
            return true;
       }
 
       if (( object == null ) || ( ! ( object instanceof <class> ) ) ) 
            return false;
 
       if( object instanceof ShadowProxy && 
          ( this.delegate == ((ShadowProxy)object).delegate ||
            this.delegate.equals( ((ShadowProxy)object).delegate ) ) ) {
 
            return true;
 
       }
      return false;
  }

As all tests are green, I'm giving it a go and committing.
Please let us know if it fixes your problems.

Thanks for reporting and providing test case.


> 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: Edson Tirelli
>             Fix For: 4.0.1
>
>
> 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