我是 Scala 和 Drools 专家的新手,需要一些帮助以从 Drools session 中获取信息。我已经成功地设置了一些由 Drools 规则操作的 Scala 类。现在我想创建一个对象来存储一组输出事实,以便在 Drools 之外进行处理。这就是我所拥有的。
我有一个简单的对象,它存储一个数字结果(在规则的 RHS 中生成)以及一个注释字符串:
class TestResults {
val results = new MutableList[(Float, String)]()
def add(cost: Float, comment: String) {
results += Tuple2(cost, comment)
}
}
在 DRL 文件中,我有以下内容:
import my.domain.app.TestResults
global TestResults results
rule "always"
dialect "mvel"
when
//
then
System.out.println("75 (fixed)")
results.add(75, "fixed")
end
当我运行包含此代码的代码时,出现以下错误:
org.drools.runtime.rule.ConsequenceException: rule: always
at org.drools.runtime.rule.impl.DefaultConsequenceExceptionHandler.handleException(DefaultConsequenceExceptionHandler.java:39)
...
Caused by: [Error: null pointer or function not found: add]
[Near : {... results.add(75, "fixed"); ....}]
^
[Line: 2, Column: 9]
at org.mvel2.optimizers.impl.refl.ReflectiveAccessorOptimizer.getMethod(ReflectiveAccessorOptimizer.java:997)
在我看来,我在 Scala 中对 TestResults 对象的定义有些愚蠢,以至于 Drools 编译成的 Java 无法完全识别它。类型不匹配,也许?我想不通。有什么建议么?谢谢!
最佳答案
您需要在执行您的 session 之前初始化您的results
全局变量。您可以使用以下方法对其进行初始化:
knowledgeSession.setGlobal("results", new TestResults()))
关于java - Scala 中的 Drools Expert 输出对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7904090/