title: comparing String valued fields in Drool 5.5
body: <p>I have some puzzling issues when use Drools 5.5 final to compare String valued fields.</p>
  
  <p>Essentially, I am trying to find if there are a pair of persons sharing the same name. The Person class looks like below:</p>
  
  <pre><code>public class Person {
    private String name;
  
    public String getName()      { return name; }
    public void setName(String n) { this.name = n; }
    public Person(String name)    { this.name = name;}
  }
  </code></pre>
  
  <p>The rule that I try to trigger is :</p>
  
  <pre><code>rule "uniquePersonName"
  when
      $p1: Person($n1: name)
      $p2: Person(this != $p1, name == $n1)
  then
      System.out.println("Duplicated person name found : " + $n1 + " " + $p2.getName());
  end
  </code></pre>
  
  <p>But it never got triggered. However, if I change it to :</p>
  
  <pre><code>when
      $p1: Person($n1: name)
      $p2: Person(this != $p1, name != $n1)
  </code></pre>
  
  <p>The system works as expected, in other words, it finds all the pairs where the persons have different names.</p>
  
  <p>After digging deeper, I found if I changed the name field to be of type Integer, the original rule worked fine. That made me think it was because of some bug with String comparison. So with the name field defined as the String type, I tried:</p>
  
  <ol>
  <li><code>not (name == $n1)</code></li>
  <li><code>(name == $n1)</code> </li>
  <li><code>name.toString() = $n1.toString()</code>  </li>
  <li><code>name == $p1.getName()</code></li>
  </ol>
  
  <p>Unfortunately, none of them worked.</p>
  
  <p>Finally, the only way I could get it behave is to write the rule as:</p>
  
  <pre><code>when
      $p1: Person($n1: name)
      $p2: Person(name == $n1)
      eval($p1!=$p2)
  </code></pre>
  
  <p>This made me think perhaps the problem is caused by a combination of how <code>this</code>  works and how String comparison is done.</p>
  
  <p>This is very basic feature and I would be surprised this is caused by a bug in Drools 5.5 final. But again, I could not see a way through. Can any of you help?</p>
  
  <p>Thanks.</p>
  
  <p>GW </p>