Uploaded image for project: 'Drools'
  1. Drools
  2. DROOLS-7321

Long comparisons are not null-safe anymore

XMLWordPrintable

    • Icon: Bug Bug
    • Resolution: Duplicate
    • Icon: Major Major
    • 8.36.0.Final
    • 7.73.0.Final
    • None
    • None
    • 2023 Week 03-05 (from Jan 16), 2023 Week 06-08 (from Feb 6), 2023 Week 09-11 (from Feb 27)
    • Hide

      The following rules using '!=' will fail with ConstraintEvaluationException caused by a NullPointerException, when executed with two 'SomeEvent' instances inserted, one of them with 'null' in 'nr':

      package testpackage
      
      declare SomeEvent
      @role(event)
        nr : Long
      end
      
      rule "R1 Not equal nr, left null"
      when
        $e1 : SomeEvent( nr == null ) 
        $e2 : SomeEvent( this != $e1, nr != $e1.nr ) 
      then
        System.out.println("R1 not equal nr, $e1: " + $e1 + ", $e2: " + $e2);
      end
      
      rule "R2 Not equal nr, left not null"
      when
        $e1 : SomeEvent( nr != null ) 
        $e2 : SomeEvent( this != $e1, nr != $e1.nr ) 
      then
        System.out.println("R2 not equal nr, $e1: " + $e1 + ", $e2: " + $e2);
      end 

      Will produce:

      org.drools.mvel.ConstraintEvaluationException: Error evaluating constraint 'nr != $e1.nr' in [Rule "R1 Not equal nr, left null" in testpackage/testNullSafeEquals.drl]
      
      	at org.drools.mvel.MVELConstraint.evaluate(MVELConstraint.java:286)
      	at org.drools.mvel.MVELConstraint.isAllowedCachedLeft(MVELConstraint.java:246)
      	at org.drools.core.common.DoubleBetaConstraints.isAllowedCachedLeft(DoubleBetaConstraints.java:108)
      	at org.drools.core.phreak.PhreakJoinNode.doLeftInserts(PhreakJoinNode.java:108)
      	at org.drools.core.phreak.PhreakJoinNode.doNode(PhreakJoinNode.java:71)
              ...
      Caused by: java.lang.NullPointerException
      	at org.mvel2.math.MathProcessor.toLong(MathProcessor.java:653)
      	at org.mvel2.math.MathProcessor.doOperationsSameType(MathProcessor.java:496)
      	at org.mvel2.math.MathProcessor._doOperations(MathProcessor.java:210)
      	at org.mvel2.math.MathProcessor.doOperations(MathProcessor.java:78)
      	at org.mvel2.ast.BinaryOperation.getReducedValueAccelerated(BinaryOperation.java:145)
      	at org.mvel2.MVELRuntime.execute(MVELRuntime.java:85)
              ...
      end 
      Show
      The following rules using '!=' will fail with ConstraintEvaluationException caused by a NullPointerException, when executed with two 'SomeEvent' instances inserted, one of them with 'null' in 'nr': package testpackage declare SomeEvent @role(event)   nr : Long end rule "R1 Not equal nr, left null " when   $e1 : SomeEvent( nr == null )    $e2 : SomeEvent( this != $e1, nr != $e1.nr )  then   System .out.println( "R1 not equal nr, $e1: " + $e1 + ", $e2: " + $e2); end rule "R2 Not equal nr, left not null " when   $e1 : SomeEvent( nr != null )    $e2 : SomeEvent( this != $e1, nr != $e1.nr )  then   System .out.println( "R2 not equal nr, $e1: " + $e1 + ", $e2: " + $e2); end Will produce: org.drools.mvel.ConstraintEvaluationException: Error evaluating constraint 'nr != $e1.nr' in [Rule "R1 Not equal nr, left null " in testpackage/testNullSafeEquals.drl] at org.drools.mvel.MVELConstraint.evaluate(MVELConstraint.java:286) at org.drools.mvel.MVELConstraint.isAllowedCachedLeft(MVELConstraint.java:246) at org.drools.core.common.DoubleBetaConstraints.isAllowedCachedLeft(DoubleBetaConstraints.java:108) at org.drools.core.phreak.PhreakJoinNode.doLeftInserts(PhreakJoinNode.java:108) at org.drools.core.phreak.PhreakJoinNode.doNode(PhreakJoinNode.java:71) ... Caused by: java.lang.NullPointerException at org.mvel2.math.MathProcessor.toLong(MathProcessor.java:653) at org.mvel2.math.MathProcessor.doOperationsSameType(MathProcessor.java:496) at org.mvel2.math.MathProcessor._doOperations(MathProcessor.java:210) at org.mvel2.math.MathProcessor.doOperations(MathProcessor.java:78) at org.mvel2.ast.BinaryOperation.getReducedValueAccelerated(BinaryOperation.java:145) at org.mvel2.MVELRuntime.execute(MVELRuntime.java:85) ... end
    • NEW
    • NEW
    • ---
    • ---

      When I compare two Long fields in a drl rule, the comparison is not null-safe anymore. The documentation for == (equals()) and != (!equals()) states, it should be null safe (https://docs.drools.org/7.73.0.Final/drools-docs/html_single/index.html#_patterns_and_constraints).

      Using Drools 7.62.0 the comparison works as expected. (I have not tested all versions in between.)

      The change in behavior may be related to the upgrade of MVEL to 2.4.14

        1. NullSafeEqualsTest.java
          4 kB
          Jochen Welle

              rhn-support-tkobayas Toshiya Kobayashi
              jochen-1 Jochen Welle (Inactive)
              Votes:
              0 Vote for this issue
              Watchers:
              2 Start watching this issue

                Created:
                Updated:
                Resolved: