Drools scripting
Drools made easy to use for scripting or testing purposes.
This library allows you to easily design proof of concepts based on the drools expert system. It greatly simplifies how you can quickly write drools based code examples or small experiments.
Just insert JSON facts into your drools working memory, and use the available engine methods to interact with the expert system and extract data from it. Data extraction can be done through simple accessors or through the JSON format. Check the documented methods in the DroolsEngine class or take a look to the large amount of example I've made available. (Most of them can be run directly by using the great ammonite REPL solution from Li Haoyi)
A selection of my drools shared code examples based on drools-scripting project :
- drools-hello-world-revisited : Drools Hello world revisited
- drools-hello-world : Drools Hello world
- drools-basics : Basic drools usage examples through unit test cases.
- drools-basics-json : Basic drools usage examples through unit test cases.
- drools-kb-understanding : Drools understanding rules knowledge base
- drools-kb-events-understanding : Drools understanding events base knowledge base
- drools-kb-understanding-constraints-limitations : Drools constraints analysis
- drools-backward-simple : Simple drools backward chaining example.
- drools-interactions : Drools web based user interactions with a performance diagnostic knowledge base
- drools-interactions-enhanced : Drools web based user interactions with a performance diagnostic knowledge base
- drools-interactions-gatling : drools interactions performance test script
- drools-equality : Drools equality behavior and customization thanks to the
key
annotation. - drools-insertLogical-aggregate-expires : Understanding insertLogical, aggregates in the context of drools events reasoning, keeping up to date a computed value
- drools-kb-banking-advanced : Enhanced drools example banking application wired to a kafka topic
- drools-kb-advanced-understanding : Drools advanced understanding rules knowledge base
- drools-kb-banking-skeleton : Drools banking skeleton application wired to a kafka topic
- drools-kb-banking-events-stream-generator : Start an embedded kafka and provide simple way to generate simple banking events
- drools-kb-banking : Drools banking example application wired to a kafka topic
- drools-kb-doctor : Drools family knowledge base
- drools-kb-family : Drools family knowledge base
- drools-kb-forever-loop-on-modify : Drools rules loop knowledge base
- drools-kb-forever-loop : Drools forever loop knowledge base
- drools-kb-official-examples-games : Drools official examples launcher examples
- drools-kb-official-examples-simple-with-pauses : Drools step by step FireKB example
- drools-kb-official-examples-simple : Drools official minimalist FireKB example
- drools-kb-official-examples : Drools official examples unit tests, it list all example knowledge base names
- drools-kb-pets : Drools family knowledge base
- drools-kb-reasoning-forward-chaining-enhanced : Drools forward chaining example knowledge base with roots from wikipedia definition example
- drools-kb-reasoning-forward-chaining : Drools forward chaining example knowledge base with roots from wikipedia definition example
- drools-parsing-issues : Drools parsing issues
- drools-persistence : Learn to use drools working memory persistence to disk.
A hello world drools example runnable with ammonite :
import $ivy.`fr.janalyse::drools-scripting:1.0.11`, $ivy.`org.scalatest::scalatest:3.2.2`
import fr.janalyse.droolscripting._, org.scalatest.flatspec._, org.scalatest.matchers._
object HelloTest extends AnyFlatSpec with should.Matchers {
"Drools" should "say hello" in {
val drl =
"""package test
|rule "hello" when
|then
| insert("HELLO WORLD");
|end
|""".stripMargin
val engine = DroolsEngine(drl)
engine.fireAllRules()
engine.strings shouldBe List("HELLO WORLD")
}
}
HelloTest.execute()
or an other one runnable with ammonite :
import $ivy.`fr.janalyse::drools-scripting:1.0.11`, $ivy.`org.scalatest::scalatest:3.2.2`
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()
Ammonite notes
Ammonite resolves by default both binary and source artifacts, with recent release of drools it
generates a runtime error as drools can't deal with several kie.conf file for a given module,
so you'll have to change ammonite default start up behavior in order to avoid sources resolutions.
Just add those following lines at the beginning of all your drools scripts (@
separates
bootstrapping code from regular one, this is required as we change the behavior of the import $ivy.
instruction):
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...
import scala.jdk.CollectionConverters._
fetch.withClassifiers(fetch.getClassifiers.asScala.filter(_ != "sources").asJava)
}
@
import $ivy.`fr.janalyse::drools-scripting:1.0.13`
The interp.resolutionHooks
instruction can also instead be added into your default ammonite configuration, in
the file $HOME/.ammonite/predefShared.sc
which is probably a better workaround to add those lines into all your
scripts.