Drools Expert output object in Scala

…衆ロ難τιáo~ 提交于 2019-12-07 15:41:27

问题


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!


回答1:


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

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



回答2:


Try

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

My guess is that the types don't line up and the error message is poor. (75 is an Int, wants a Float)




回答3:


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



来源:https://stackoverflow.com/questions/7904090/drools-expert-output-object-in-scala

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!