[rules-users] Problem with "!=" field constraint

Edson Tirelli tirelli at post.com
Mon Aug 13 12:39:14 EDT 2007


   Markus,

   Can you plz open a JIRA for this?

   Thank you,
      Edson

2007/8/13, Markus Reitz <marreitz at yahoo.de>:
>
> Hi,
>
> I have a problem with a rule which uses a "!=" field
> constraint. 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();
>     }
>   }
> }
>
> I'm inserting three different objects in the working
> memory of the Drools 4 Engine. Two of the three
> objects have the same text - "42". When I'm using the
> following rule
>
>   package test
>
>   rule "A test"
>
>     when
>       x : SpecialString()
>       y : SpecialString(this!=x)
>
>     then
>       System.out.println(x+"/"+y);
>   end
>
> I get the output
>
>   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]
>
> which is not what I expect. Why are combinations of x
> and y with the two "42"-instances not listed, i.e.
>
> Why doesn't the output contain
>
>   42[SpecialString at e4d6ef]/42[SpecialString at fde8da]
>   42[SpecialString at fde8da]/42[SpecialString at e4d6ef]
>
> The class SpecialString uses the standard equals(...)
> and hashCode(...) implementation, so these should be
> based on the object references, not the content:
>
> 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()+"]");
>   }
> }
>
> Best regards
>
> Markus
>
>
>       Wissenswertes für Bastler und Hobby Handwerker. BE A BETTER
> HEIMWERKER! www.yahoo.de/clever
> _______________________________________________
> rules-users mailing list
> rules-users at lists.jboss.org
> https://lists.jboss.org/mailman/listinfo/rules-users
>



-- 
  Edson Tirelli
  Software Engineer - JBoss Rules Core Developer
  Office: +55 11 3529-6000
  Mobile: +55 11 9287-5646
  JBoss, a division of Red Hat @ www.jboss.com
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.jboss.org/pipermail/rules-users/attachments/20070813/72ced8c8/attachment.html 


More information about the rules-users mailing list