Search code examples
drools

"is not relevant to this pattern" warning since 8.40.0


This warning was added in https://github.com/kiegroup/drools/pull/5270 but we currently fail to understand what exactly is wrong.

Simplified rule:

rule "Sample-Denied"
no-loop
when
  $req: Request()
  $si: SessionInfo()
  $settings: Settings()
  DataAccessor (
    $si.score < $settings.requiredScore(getProperty($req.key()), $si.sessionType)
  )
  $response: Response()
then
  modify($response){
    denied()
  };
end

Produces the warning:

|o.d.mvel.MVELConstraint|| $si is not relevant to this pattern, so it causes class reactivity. Consider placing this constraint in the original pattern if possible : $si.score < $settings.requiredScore(getProperty($req.key()), $si.sessionType)

The rule somewhat matches examples in the drools documentation and we couldn't find any reference to this warning.

Rewriting it like this removes the warning:

  $req: Request()
  $settings: Settings()
  $da: DataAccessor()
  SessionInfo(
    score < $settings.requiredScore($da.getProperty($req.key()), sessionType)
  )
  $response: Response()

But I'd like to understand why?


Solution

  • This warning was introduced for a case where a condition is not relevant to the current pattern. It's not an error, but it could be error-prone, so a warning is raised.

    In reality, it checks the first property in a condition --- In your condition, the first property is $si.score and consider that the constraint should fit in SessionInfo pattern, saying Consider placing this constraint in the original pattern if possible.

    However, the condition analysis may not be perfect. I see that you use getProperty method of DataAccessor. If you have a strong reason to write the constraint in DataAccessor pattern, it's okay to ignore the warning.

    From my point of view, your rewrite version looks better, because it starts with score which is a SessionInfo's property.

    This part of the document is slightly related.

    https://docs.drools.org/latest/drools-docs/drools/language-reference/index.html#performance-tuning-drl-ref_drl-rules

    Define the property and value of pattern constraints from left to right
    
    In DRL pattern constraints, ensure that the fact property name is on the left side of the operator and that the value (constant or a variable) is on the right side. The property name must always be the key in the index and not the value. For example, write Person( firstName == "John" ) instead of Person( "John" == firstName ). Defining the constraint property and value from right to left can hinder Drools rule engine performance.
    

    This is just about performance, but you may think that "Starting with Fact's property" is the standard way of Drools constraint writing.

    I hope it makes some sense.

    Regards, Toshiya