Harlan
Harlan

Reputation: 19401

Drools Expert output object in Scala

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!

Upvotes: 3

Views: 1353

Answers (3)

salaboy
salaboy

Reputation: 4143

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

Upvotes: -1

Go Dan
Go Dan

Reputation: 15502

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

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

Upvotes: 2

jsuereth
jsuereth

Reputation: 5624

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)

Upvotes: 0

Related Questions