在 Drools 右侧创建新的 Scala 对象

Creating new Scala object in Drools right hand side

由于我在 Drools 中使用 Scala 不可变对象,为了更新事实,我需要创建一个新对象来替换它。我已经为规则编写了一个 Scala 方法来调用 which returns 就是这样一个对象。

我的问题是,在 Drools 规则的 "then" 部分定义新的 Scala case class 对象的语法是什么?我已经尝试过类似于我在某处看到的以下语法,但它似乎也没有起到作用......(即使对于像字符串这样的标准类型)

then
    MyObject t = returnNewMyObject($a, $b)

目前对 Drools + Scala 的支持和文档似乎相当有限。有什么想法吗?

(仅供参考,我已阅读以下问题,它不是同一个查询...我的对象不是全局对象:Drools Expert output object in Scala

下面的 DRL 文件:

package resources

import function drools.RuleFunctions.*
import order.Cart
import order.CartLine
import generic.Amount

import scala.*
import scala.Option
import org.kie.api.runtime.KieRuntime
import java.math.BigDecimal


    dialect  "mvel"


    rule "Eval Cart Line"  
        agenda-group "init"
        auto-focus true
        dialect  "mvel"
        lock-on-active true
        salience 1000
        when
             $cart: Cart($line: lines(), amount == null) //If Cart found with lines, but with no cart amount set
             $o : CartLine($id : ref, $qty: quantity) from $line
        then
            Cart $newB = updateLineAmount($cart, $id, $qty, kcontext.getKieRuntime())
            update(kcontext.getKieRuntime().getFactHandle($cart),$newB) 
    end

    rule "Product 20% Discount"
        agenda-group "LineDiscount"
        auto-focus true
        dialect  "mvel"
        lock-on-active true
        salience 900
        when
            $cart: Cart($line : lines, amount == null)
            $o : CartLine(ref == "1234", amount != null ) from $line
        then
            Cart $newB = addLineDiscount($cart, $o, 20.0, kcontext.KieRuntime())
            update(kcontext.getKieRuntime().getFactHandle($cart), $newB)
        end

更新

object RuleFunctions {

  def updateLineAmount(cart: Cart, id: String, qty: Int, krt: KieRuntime): Cart= {...}

  def addLineDiscount(cart: Cart, bLine : CartLine, discPerc: Double, krt: KieRuntime): Cart= {...}
}

从 Scala 对象类型导入方法在 Drools 中可能会出现问题。原因很简单,与 Java 相比,Scala 中不存在 static 方法。这是因为 Scala 对纯面向对象语言的含义有更严格的解释。

这意味着每当您尝试使用 Drools import function 语法时,它都找不到要导入的任何静态方法。因此,Drools 编译器会抱怨任何对 Scala 单例对象类型中包含的方法的引用。

解决此问题的方法是在 Java 中编写将在您的 DRL 中使用的任何 类,您可以在其中显式定义静态方法。 Scala 编译器将很乐意将它们与您的 Scala 类.

一起编译