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