在Scala中Drools Expert输出对象
我是Scala和Drools Expert的新手,需要一些帮助才能从Drools会话中获取信息。 我已经成功设置了一些被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无法完全看到它。 类型不匹配,也许? 我无法弄明白。 有什么建议么? 谢谢!
您需要在执行会话之前初始化results
全局变量。 您可以使用以下命令初始化它:
knowledgeSession.setGlobal("results", new TestResults()))
尝试
import my.domain.app.TestResults global TestResults results rule "always" dialect "mvel" when // then System.out.println("75 (fixed)") results().add(75.0f, "fixed") end
我的猜测是类型不排队,错误信息很差。 (75是Int,想要Float)
这是正确的..并试图为你的规则添加一个条件,所以它更有意义(时间部分)。 条件评估是规则引擎最重要的特征,没有条件的写规则不会产生太多的意义。
干杯