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 无法完全看到它。也许类型不匹配?我想不通。有什么建议吗?谢谢你!
I'm a novice in both Scala and Drools Expert, and need some help getting information out of a Drools session. I've successfully set up some Scala classes that get manipulated by Drools rules. Now I want to create an object to store a set of output facts for processing outside of Drools. Here's what I've got.
I've got a simple object that stores a numeric result (generated in the RHS of a rule), along with a comment string:
class TestResults {
val results = new MutableList[(Float, String)]()
def add(cost: Float, comment: String) {
results += Tuple2(cost, comment)
}
}
In the DRL file, I have the following:
import my.domain.app.TestResults
global TestResults results
rule "always"
dialect "mvel"
when
//
then
System.out.println("75 (fixed)")
results.add(75, "fixed")
end
When I run the code that includes this, I get the following error:
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)
This looks to me like there's something goofy with my definition of the TestResults object in Scala, such that the Java that Drools compiles down to can't quite see it. Type mismatch, perhaps? I can't figure it out. Any suggestions? Thank you!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您需要在执行会话之前初始化
results
全局变量。您可以使用以下方法初始化它:You need to initialize your
results
global variable before executing your session. You can initialize it using:尝试一下
我的猜测是类型不对齐并且错误消息很差。 (75 是一个 Int,需要一个 Float)
Try
My guess is that the types don't line up and the error message is poor. (75 is an Int, wants a Float)
是的..并尝试在您的规则中添加一个条件,这样它就更有意义(当部分)。
条件评估是规则引擎最重要的功能,编写没有条件的规则没有太大意义。
干杯
That's right.. and try to add a condition to your rule, so it make more sense (the when part).
The condition evaluation is the most important feature of rule engines, writing rules without conditions doesn't make too much senses.
Cheers