在Drools右侧创建新的Scala对象 [英] Creating new Scala object in Drools right hand side

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

问题描述

由于我正在Drools中使用Scala不可变对象,因此为了更新事实,我需要创建一个新对象来替换它。我为要调用的规则编写了一个Scala方法,该方法只返回这样的对象。

Since I'm working with Scala immutable objects within Drools, in order to update a fact I would need to create a new object to replace it. I've written a Scala method for the rule to call which returns just such an object.

我的查询是,在其中定义新的Scala case类对象的语法是什么Drools规则的 then部分?我尝试了类似于以下在某处看到的语法,但它似乎也没有奏效...(即使对于像字符串这样的标准类型)

My query is, what's the syntax for defining a new Scala case class object in the "then" section of a Drools rule? I've tried syntax similar to the following which I saw somewhere but it doesn't seem to be doing the trick either...(even for standard types like Strings)

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

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

Support and documentation for Drools + Scala seems to be rather limited at the moment. Any ideas?

(仅供参考,我读过以下问题,而且查询不相同...我的对象不是全局对象:Scala中的Drools Expert输出对象

(FYI I've read the following question and it's not the same query...my object is not a global: 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对成为纯面向对象的语言的含义进行了更严格的解释。

Importing methods from Scala Object types can be problematic in Drools. The simple reason is that static methods do not exist in Scala in contrast with Java. This is because Scala takes a more stricter interpretation of what it means to be a pure object orientated language.

这意味着,无论何时您尝试使用Drools import函数语法,它找不到要导入的任何静态方法。结果,Drools编译器抱怨对Scala单例对象类型中包含的方法的任何引用。

What this means is that whenever you try to use the Drools import function syntax, it cannot find any static methods to import. As a result, the Drools compiler complains about any references to methods contained in Scala singleton object types.

为此的一种解决方法是编写将要编写的所有类。在Java的DRL中使用,可以在其中显式定义静态方法。 Scala编译器将很高兴将它们与您的Scala类一起编译。

A work around for this is to write any classes which are going to be used in your DRLs in Java where you can explicitly define static methods. The Scala compiler will happily compile these along with your Scala classes.

这篇关于在Drools右侧创建新的Scala对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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