使用 Drools 声明本地规则变量

And*_*ore 3 scope rule-engine drools

可以使用 Drools 规则引擎将局部变量创建到局部规则定义中吗?

我写了一个示例代码,但它没有编译。这个例子只是为了暴露关于局部声明的想法(我知道这是错误的)

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

错误信息是:

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"
Run Code Online (Sandbox Code Playgroud)

位置 [25,2] 由行定义:

String $departmentName = "Music";
Run Code Online (Sandbox Code Playgroud)

Eds*_*lli 6

从 Drools 5 开始,不可能在条件中声明任意变量。您可以将变量绑定到事实和属性:

when
   Person( $name : name )
then
   // $name is bound to each name of each person in the working memory
end
Run Code Online (Sandbox Code Playgroud)