Skip to content

Instantly share code, notes, and snippets.

@dacr
Last active June 24, 2023 16:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dacr/39a769215d16359be1bbe303c51b166f to your computer and use it in GitHub Desktop.
Save dacr/39a769215d16359be1bbe303c51b166f to your computer and use it in GitHub Desktop.
Drools advanced understanding rules knowledge base / published by https://github.com/dacr/code-examples-manager #21020062-7fe7-49e3-bc21-1c3bd2599ebb/50da674b519fa9aadef803913554a93fbf59e6c5
// summary : Drools advanced understanding rules knowledge base
// keywords : scala, drools, mvel, scalatest, ai, knowledgebase, @testable
// publish : gist
// authors : David Crosson
// license : Apache NON-AI License Version 2.0 (https://raw.githubusercontent.com/non-ai-licenses/non-ai-licenses/main/NON-AI-APACHE2)
// id : 21020062-7fe7-49e3-bc21-1c3bd2599ebb
// execution : scala ammonite script (http://ammonite.io/) - run as follow 'amm scriptname.sc'
// created-on : 2019-10-14T10:19:41+02:00
// managed-by : https://github.com/dacr/code-examples-manager
// run-with : scala-cli $file
// ---------------------
//> using scala "3.3.0"
//> using dep "fr.janalyse::drools-scripting:1.1.0"
//> using dep "org.scalatest::scalatest:3.2.16"
// ---------------------
import fr.janalyse.droolscripting._, org.scalatest._, flatspec._, matchers._, OptionValues._
import scala.jdk.CollectionConverters._
object AdvancedUnderstandingRules extends AnyFlatSpec with should.Matchers {
override def suiteName: String = "AdvancedUnderstandingRules"
def checkOK(drl:String):Unit = {
val engine = DroolsEngine(drl, DroolsEngineConfig.configWithIdentity)
engine.fireAllRules()
engine.strings shouldBe List("OK")
}
def checkAllOf(that:String*)(drl:String):Unit = {
val engine = DroolsEngine(drl, DroolsEngineConfig.configWithIdentity)
engine.fireAllRules()
engine.strings.sorted shouldBe that.toList.sorted
}
def checkAllOfWithEquality(that:String*)(drl:String):Unit = {
val engine = DroolsEngine(drl, DroolsEngineConfig.configWithEquality)
engine.fireAllRules()
engine.strings.sorted shouldBe that.toList.sorted
}
val config = DroolsEngineConfig.configWithEquality
// ======================================================================
"DROOLS" should "support meta data - #AEX0" in {
val drl =
"""package test //#AEX0
|declare A @persistable
| txt:String @validateWith(\w+-\d+)
|end
|rule "that"
| @risk(0)
| @category(truc)
|when then
| insert(new A("answer-42"));
|end
|""".stripMargin
val engine = DroolsEngine(drl, DroolsEngineConfig.configWithEquality)
// list all rules
val rules = engine.session.getKieBase.getKiePackage("test").getRules
//val rule = rules.asScala.headOption.value
val rule = engine.session.getKieBase.getRule("test", "that")
// rules meta-data
rule.getMetaData.get("category") shouldBe "truc"
rule.getMetaData.get("risk").asInstanceOf[Int] shouldBe 0
// facts meta-data
val factType = engine.session.getKieBase.getFactType("test", "A")
factType.getMetaData.containsKey("persistable") shouldBe true
val txtRE = factType.getField("txt").getMetaData.get("validateWith")
txtRE shouldBe """\w+-\d+"""
val newAValue = "truc-32"
if (txtRE.toString.r.findFirstMatchIn(newAValue).isDefined ) { // just to check if every was fine with KB embedded RE
val newA = factType.newInstance()
factType.set(newA, "txt", newAValue)
engine.insert(newA)
} else fail()
}
// ======================================================================
it should "support agenda group control - #AEX1" in
checkAllOf("from first agenda", "from default agenda") {
info("""The default Agenda Group is "MAIN".""")
info("""In this example B2 is never executed as second didn't get the focus""")
"""package test //#AEX1
|declare A txt:String end
|rule "init" when then
| insert(new A("init"));
| kcontext.getKnowledgeRuntime().getAgenda().getAgendaGroup("first").setFocus();
|end
|//-----------------
|rule "B0" when A() then insert("from default agenda"); end
|rule "B1" agenda-group "first" when A() then insert("from first agenda"); end
|rule "B2" agenda-group "second" when A() then insert("from second agenda"); end
|""".stripMargin
}
// ======================================================================
it should "support agenda group stacking - #AEX2" in {
val drl =
"""package test //#AEX2
|declare A txt:String end
|//-----------------
|rule "B0" when A() then insert("from MAIN"); end
|rule "B1" agenda-group "first" when A() then insert("from first"); end
|rule "B2-1" agenda-group "second" when A() then insert("from second1"); end
|rule "B2-2" agenda-group "second" when A() then insert("from second2"); end
|rule "B3" agenda-group "somegroup" when A() then insert("from an other agenda"); end
|""".stripMargin
val engine = DroolsEngine(drl, DroolsEngineConfig.configWithEquality)
info("agenda group setFocus are stacked, last focused, first executed")
info("All fireable rule on a given agenda-group are fired up before the group is unstacked")
engine.session.getAgenda().getAgendaGroup("second").setFocus()
engine.session.getAgenda().getAgendaGroup("first").setFocus()
engine.insertJson("""{"txt":"0"}""", "test.A")
engine.fireAllRules()
engine.strings.sorted shouldBe List("from MAIN", "from first", "from second1", "from second2").sorted
}
// ======================================================================
it should "support rule flow - #AEX3" ignore { // To be continued
val drl =
"""package test //#AEX3
|
|declare CheckOrder id:String end
|declare ProcessOrder id:String end
|declare UpdateOrder id:String end
|
|rule "r1"
| ruleflow-group "group1"
|when
| CheckOrder($id:id)
|then
| insertLogical("OK");
|end
|
|""".stripMargin
val engine = DroolsEngine(drl, DroolsEngineConfig.configWithEquality)
engine.session.startProcess("group1")
}
}
AdvancedUnderstandingRules.execute()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment