由于我在 Drools 中使用 Scala 不可变对象,为了更新事实,我需要创建一个新对象来替换它。我为要调用的规则编写了一个 Scala 方法,它返回这样一个对象。
我的问题是,在 Drools 规则的“then”部分定义新的 Scala 案例类对象的语法是什么?我尝试过类似于我在某处看到的以下语法,但它似乎也没有达到目的...(即使对于字符串等标准类型)
then
MyObject t = returnNewMyObject($a, $b)
目前对 Drools + Scala 的支持和文档似乎相当有限。有任何想法吗?
(仅供参考,我已阅读以下问题,它不是同一个查询......我的对象不是全局的:Scala 中的 Drools Expert 输出对象)
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
Update
object RuleFunctions {
def updateLineAmount(cart: Cart, id: String, qty: Int, krt: KieRuntime): Cart= {...}
def addLineDiscount(cart: Cart, bLine : CartLine, discPerc: Double, krt: KieRuntime): Cart= {...}
}