title: Drools rule condition - check if attribute is null
body: <p>I have a fairly simple case where I want to check in my rule condition whether an attribute is not null.</p>
  
  <pre><code>rule "only do action if attribute is not null"
  when
      $fact : Fact(attribute!=null, $attribute : attribute)
  then
      rulesLogger.debug("Rule fires = " + $attribute);
  end
  </code></pre>
  
  <p>I have tracked this through in debug. A Fact is being inserted with the attribute as null but the rule still fires. The console output is as follows.</p>
  
  <pre><code>Rule fires = null
  </code></pre>
  
  <p>If I change the condition to <code>attribute==null</code> then the rule doesn't fire. So it seems to be doing the exact opposite to what I would expect.</p>
  
  <p>We do have a workaround for this using a function but it's a bit ugly and I can't figure out why it doesn't work in the first place.</p>
  
  <pre><code>function Boolean attributeExists(Fact fact)
  {
      if(fact.getAttribute() == null)
      {
          return Boolean.FALSE;
      }
      else
      {
          return Boolean.TRUE;
      }
  }
  
  rule "only do action if attribute is not null"
  when
      $fact : Fact($attribute : attribute)
      Boolean(booleanValue == true) from attributeExists($fact)
  then
      rulesLogger.debug("Rule fires = " + $attribute);
  end
  </code></pre>
  
  <p><strong>EDIT 1</strong></p>
  
  <p>The drools version is 5.3.0.</p>
  
  <p>The fact is loaded via another rule using <code>from</code> and a service method call. I can't see that the fact is invalid because it prints as I would expect to the console and also the manual workaround with a function works as expected. It's a strange one.</p>
  
  <p><strong>EDIT 2</strong></p>
  
  <p>I've found a better workaround. If I access the attribute using the getter method then the rule behaves as expected. This looks much better than having to write an extra function but it would still be nice to know why this isn't working when using the property name.</p>
  
  <pre><code>rule "only do action if attribute is not null"
  when
      $fact : Fact(getAttribute()!=null, $attribute : attribute)
  then
      rulesLogger.debug("Rule fires = " + $attribute);
  end
  </code></pre>
  
  <p>The Fact class is just a boring POJO. It has no superclass other than Object and implements no interfaces. It's not a JPA entity or anything where there might be a proxy or lazy loading.</p>