Search code examples
drools

Drools 5.0 - Locally... Global


I'd like to declare a global variable that is scoped to only my rules file. For example: variable $reUseMe is only declared once.

rule 1

$reUseMe : POJO(val = 1) //other conditions

rule 2

$reUseMe > val


Solution

  • There are no scoped global variables, but in some cases rule inheritance helps.

    rule "Rule 1"
      when
        $reUseMe :POJO( val == 1 )
      then
    end
    
    rule "Rule 2" extends "Rule 1"
      when
        # You can use the variables from Rule 1
        POJO( val > $reUseMe.val ) 
      then
    end
    

    Only LHS is added. RHS from Rule 1 is ignored in Rule 2.