orit1992 opened a new issue, #6243:
URL: https://github.com/apache/incubator-kie-drools/issues/6243

   I've encountered an issue in Drools **8.44.0.Final** where rule execution 
changes **depending on whether a subclass overrides a getter from its 
superclass**. Even more concerning, in some cases, **a property that is 
actually non-null gets evaluated as null in WHEN condition**.
   
   I was able to verify that the issue occurs for sure in Drools versions from 
**7.74.1** up to **10.0.0** (latest available version) using both Java11 and 
Java17, the version **6.5.0.Final** doesn't have the issue.
   
   **Steps to Reproduce**
   Consider the following examples rules:
   
   ``` drl
   rule "Set Address"
   salience 1
   when
           $fact : Person( address == null)
   then
           System.out.println("Person Hash " + $fact.hashCode());
           modify($fact) {
                   setAddress("ROME")
           }
   end
   
   rule "Set worker name"
   when
   $fact : Worker(
           $address: address != null
   )
   then
           System.out.println("Worker Hash " + $fact.hashCode());
           System.out.println("(address != null) evaluated and address is " + 
$address );
           modify($fact) {
                   setName("PIPPO")
           }
   end
   ```
   
   And the following Java classes:
   ``` java
   public class Person {
       private String name;
       private int age;
       private String address;
   
       public String getName() {
           return name;
       }
   
       public void setName(String name) {
           this.name = name;
       }
   
       public int getAge() {
           return age;
       }
   
       public void setAge(int age) {
           this.age = age;
       }
   
       public String getAddress() {
           return address;
       }
   
       public void setAddress(String address) {
           this.address = address;
       }
   }
   
   public class Worker extends Person {
       // If this override is removed, the rule "Set worker name" works fine.
       @Override
       public String getAddress() {
           return super.getAddress();
       }
   }
   
   public class Student extends Person {}
   ```
   
   **Unexpected Behavior**
   - If `Worker` **does NOT override** `getAddress()`, the rule `"Set worker 
name"` **executes**, and `name` is set to "PIPPO".
   - If `Worker` **overrides** `getAddress()`, the rule `"Set worker name"` 
**is never triggered**.
   - The only difference between the two scenarios is the overridden method in 
Worker, which should not affect rule execution logic.
   
   **Even More Concerning**: **Evaluation of** `null` **When the Value is 
Non-Null**
   To illustrate a more severe case, consider the following additional rule:
   
   ``` drl
   /**
    * When Worker class overrides getAddress(), then `address == null` 
evaluates to true,
    * but it will print a non-null value.
    * Uncomment the following rule and comment the previous ones to test.
    */
   rule "Check worker address"
   when
       $fact : Worker( $address: address == null )
   then
       System.out.println("Worker Hash " + $fact.hashCode());
       System.out.println("(address == null) evaluated but address is " + 
$address);
       modify($fact) {
           setName("PIPPO");
       }
   end
   ```
   With this setup:
   - Drools evaluates `address == null` as `true`, even when `getAddress()` 
actually returns a **non-null** value.
   - The rule fires, but the printed value of $address is unexpectedly **not 
null**.
   
   ---
   To help reproduce this issue, I've created a 
[repo](https://github.com/orit1992/droolsbehavior/tree/main) containing Maven 
project (Java17) with unit tests that demonstrate the behavior:
   **Student** class: extends Person and **Does not override** `getAddress()`, 
and the rule execution works as expected 
(`com.example.DroolsRuleTest#testStudentSessionAddress`).
   **Worker** class: extends Person and  **Overrides** `getAddress()`, causing 
the rule execution to **fail** 
(`com.example.DroolsRuleTest#testWorkerSessionAddress`).
   
   
   Thanks for your time


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to