Last active
February 3, 2026 20:23
-
-
Save dacr/563ff368d7f8693ff7139ad1efede13d to your computer and use it in GitHub Desktop.
Drools rules loop knowledge base / published by https://github.com/dacr/code-examples-manager #6b3ee4ec-6f57-4587-b0ea-1be4118b5a2f/6a8b74fda700b7531d721043dd3f1a7be09006f1
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // summary : Drools rules loop knowledge base | |
| // keywords : scala, drools, mvel, scalatest, ai, knowledgebase, @testable | |
| // publish : gist | |
| // authors : David Crosson | |
| // license : Apache License Version 2.0 (https://www.apache.org/licenses/LICENSE-2.0.txt) | |
| // id : 6b3ee4ec-6f57-4587-b0ea-1be4118b5a2f | |
| // created-on : 2019-10-04T09:15:21+02:00 | |
| // managed-by : https://github.com/dacr/code-examples-manager | |
| // run-with : scala-cli $file | |
| // --------------------- | |
| //> using scala "3.5.1" | |
| //> using dep "fr.janalyse::drools-scripting:1.2.0" | |
| //> using dep "org.scalatest::scalatest:3.2.19" | |
| // --------------------- | |
| import fr.janalyse.droolscripting.*, org.scalatest.*, flatspec.*, matchers.*, OptionValues.* | |
| object KbTest extends AnyFlatSpec with should.Matchers { | |
| val drl = | |
| """package test | |
| | | |
| |declare That | |
| | count:int | |
| |end | |
| | | |
| |rule "init" when then insert(new That(0)); end | |
| | | |
| |rule "count" | |
| |when | |
| | //$that:That($count:count) | |
| | $that:That($count:count , count < 100) | |
| |then | |
| | modify($that) { | |
| | setCount($count+1) | |
| | } | |
| |end | |
| |""".stripMargin | |
| "test KB" should "work" in { | |
| val engine = DroolsEngine(drl) | |
| engine.fireAllRules() | |
| engine.getModelFirstInstanceAttribute("test.That", "count").value.asInstanceOf[Int] shouldBe 100 | |
| } | |
| } | |
| KbTest.execute() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment