Scala中的Drools Expert输出对象 [英] Drools Expert output object in Scala

查看:193
本文介绍了Scala中的Drools Expert输出对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是Scala和Drools Expert的新手,需要一些帮助来获取Drools会话中的信息。我已经成功设置了一些由Drools规则操纵的Scala类。现在,我想创建一个对象来存储一组输出事实,以在Drools之外进行处理。这就是我所拥有的。

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.

我有一个简单的对象,用于存储数值结果(在规则的RHS中生成)以及注释字符串:

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)
    }
}

在DRL文件中,我有以下内容:

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)

在我看来,我在Scala中对TestResults对象的定义有些愚蠢,例如Java Drools编译到看不到它。类型不匹配,也许吗?我不知道。有什么建议么?谢谢!

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!

推荐答案

您需要初始化结果全局变量< 之前,先执行strong>会话。您可以使用以下命令对其进行初始化:

You need to initialize your results global variable before executing your session. You can initialize it using:

knowledgeSession.setGlobal("results", new TestResults()))

这篇关于Scala中的Drools Expert输出对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆