Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Declare local rule variable using Drools

It's possible to create a local variable into a local rule definition using Drools rule engine?

I wrote an example code, but it's not compiling. This example is just to expose the idea about local declaration (I know that it's wrong)

/*...*/
    rule "Test local Variable"
        when
            String $departmentName = "Music";
        then
            System.out.println($departmentName);
    end
/*...*/

The error message is:

org.drools.CheckedDroolsException: There were errors in the rule source: [ERR 101] Line 25:2 no viable alternative at input 'String' in rule "Test local Variable"

The position [25,2] is defined by line:

String $departmentName = "Music";
like image 968
Andre Pastore Avatar asked May 18 '26 17:05

Andre Pastore


1 Answers

As of Drools 5, it is not possible to declare arbitrary variables in conditions. You can bind variables to facts and attributes:

when
   Person( $name : name )
then
   // $name is bound to each name of each person in the working memory
end
like image 98
Edson Tirelli Avatar answered May 21 '26 09:05

Edson Tirelli



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!