Creating new Scala object in Drools right hand side

醉酒当歌 提交于 2019-12-31 04:03:11

问题


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.

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)

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

(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 File below:

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= {...}
}

回答1:


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.

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.

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.



来源:https://stackoverflow.com/questions/28323507/creating-new-scala-object-in-drools-right-hand-side

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