Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Drools Hello world revisited / published by https://github.com/dacr/code-examples-manager #ceb03394-2537-43f5-96c4-85f702e68ac6/62be04cdc1c85f6cdd11f8c9023eb7a1420f1ade
// summary : Drools Hello world revisited
// keywords : scala, drools, mvel, scalatest, ai, helloworld, @testable
// publish : gist
// authors : David Crosson
// license : Apache2
// id : ceb03394-2537-43f5-96c4-85f702e68ac6
// execution : scala ammonite script (http://ammonite.io/) - run as follow 'amm scriptname.sc'
// created-on : 2019-10-01T09:16:40+02:00
interp.resolutionHooks += { fetch =>
// -- This is mandatory with drools >= 7.0.46 because drools sources artifacts also brings kie.conf
// -- (it generates resources conflict at KIE init) and because by default ammonite also load sources artifact...
// -- WARN : YOU MAY HAVE TO CLEANUP YOUR LOCAL COURSIER CACHE ~/.cache/coursier to remove sources artifact
// -- WARN : find ~/.cache/coursier/ -name "*drools*-sources.jar" | xargs rm -f
import scala.jdk.CollectionConverters._
fetch.withClassifiers(fetch.getClassifiers.asScala.filter(_ != "sources").asJava)
}
@
import $ivy.`fr.janalyse::drools-scripting:1.0.15`, $ivy.`org.scalatest::scalatest:3.2.6`
import fr.janalyse.droolscripting._, org.scalatest._, flatspec._, matchers._, OptionValues._
object HelloTest extends AnyFlatSpec with should.Matchers {
"Drools" should "say hello" in {
val drl =
"""package test
|
|declare Someone
| name:String
|end
|
|declare Message
| message:String
|end
|
|rule "hello" when
| Someone($name:name)
|then
| insert(new Message("HELLO "+$name));
|end
|""".stripMargin
val engine = DroolsEngine(drl)
engine.insertJson("""{"name":"John"}""","test.Someone")
engine.fireAllRules()
val msgOption = engine.getModelFirstInstanceAttribute("test.Message", "message")
msgOption.value shouldBe "HELLO John"
}
}
HelloTest.execute()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment