如何使用 drools-spring 在 drools 中设置全局变量并在 DRL 文件中使用相同的变量

How can I set global variable in drools using drools-spring and use same in DRL file

基本上我想将 class 引用存储为来自 spring 服务 class 的全局变量,并在 DRL 文件中使用相同的变量来访问全局引用的方法和属性。

在您的服务中 class 使用以下代码:

StatelessKnowledgeSession knowledgeSession;
CustomClass reference;
knowledgeSession.setGlobal("global1", reference);
knowledgeSession.execute(fact);

内部 DRL 文件

import CustomClass;

global CustomClass global1

在任何规则中使用引用。

您必须将对象引用设置到 kieSession 中,如下所示

kieSession.setGlobal("serviceName", serviceRef);

并在您的 drools 文件中导入全局引用

global com.example.Service serviceName