首页 > 解决方案 > Drools 工作台:如何在规则文件中使用全局变量

问题描述

我正在使用与 Kie 执行服务器集成的 Drools 工作台 7.17。我使用工作台创建了项目,该项目由数据模型、规则文件和全局定义组成。

如果执行规则并检索全局变量值,我想使用全局变量在其中设置一些值。我可以使用 Spring Boot 应用程序来实现这一点,我们在会话中添加全局变量kieSession.setGlobal("response", response); 并使用kieSession.getGlobal("response"). 我尝试使用工作台进行复制,但是当我尝试在全局变量中设置值时出现空指针异常。以下是我的规则文件:

package com.myspace.drools_ruleengine;
import com.myspace.drools_ruleengine.Person;
global com.myspace.drools_ruleengine.Response response;
dialect "mvel"
rule "If person age >= 18 then person is adult"
no-loop
when
    $p: Person(age >= 18)
then
    response.setMessage("Adult");  // throwing error- null pointer exception
end

我创建了全局定义并将响应添加为 Response 类的别名。除此之外还有什么要求吗?我正在使用Kie Server Rest API插入事实。

标签: droolskie-workbench

解决方案


您需要在发送执行规则的请求时初始化全局变量,例如:

<batch-execution>
<set-global identifier="obj">
  <com.sample.Test/>
</set-global>
<insert>
  <com.Person>
     <name>abc</name>
   </com.Person>
</insert>
<fire-all-rules/>
</batch-execution>

试试这种方法


推荐阅读