drools - 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插入事实。
解决方案
您需要在发送执行规则的请求时初始化全局变量,例如:
<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>
试试这种方法
推荐阅读
- web-applications - 将徽章添加到 Chrome 而不是安装的 PWA
- python - 尽管使用了正确的 CSS 选择器/XPATH,但我无法找到元素,并且我正在抓取的 html 中没有 iframe。我如何获得元素?
- android - Firebase 功能在生产中的升级过程
- vue.js - Vee-validate 和 Vue-I18n 本地化不起作用
- redux - 如何避免 Expo App 中的内存不足问题?
- mysql - Mysql中某些值的平均值
- angular - 您是否应该始终为每个可观察的连接使用订阅?
- javascript - Google Sheets Apps 脚本 - 从具有多行的单元格中选择值
- azure-devops - Yaml 触发器不起作用,但在管道中覆盖确实
- python - /en/admin/cms/page/add-plugin/ 处的 DjangoCMS 插件 OperationalError - 没有这样的表:cmsplugin_soundcloud_soundcloud