Last active
June 24, 2023 16:26
-
-
Save dacr/1adea71ebd862de273fddba31a6bde57 to your computer and use it in GitHub Desktop.
Drools forward enhanced chaining example knowledge base with roots from wikipedia definition example / published by https://github.com/dacr/code-examples-manager #d374f076-7ea4-4e8b-9b6b-696cc8dd1120/76ea7f481d6c66ddd5ecff63ffb3b360a98bf1fd
This file contains 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 forward enhanced chaining example knowledge base with roots from wikipedia definition example | |
// 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 : d374f076-7ea4-4e8b-9b6b-696cc8dd1120 | |
// created-on : 2019-10-21T22:04:56+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._ | |
object KbTest extends AnyFlatSpec with should.Matchers { | |
override def suiteName: String = "KBTest" | |
/* https://en.wikipedia.org/wiki/Forward_chaining | |
If X croaks and X eats flies - Then X is a frog | |
If X chirps and X sings - Then X is a canary | |
If X is a frog - Then X is green | |
If X is a canary - Then X is yellow | |
*/ | |
val drl = | |
"""package test | |
|dialect "mvel" | |
| | |
|declare ItCroaks x:String end | |
|declare ItEatsFlies x:String end | |
|declare ItChirps x:String end | |
|declare ItSings x:String end | |
| | |
|declare IsGreen x:String end | |
|declare IsYellow x:String end | |
| | |
|declare IsFrog x:String end | |
|declare IsCanary x:String end | |
| | |
|rule "frog" when ItCroaks($xItCroaks:x) ItEatsFlies(x == $xItCroaks) then insert(new IsFrog($xItCroaks)) end | |
|rule "canary" when ItChirps($x:x) ItSings(x == $x) then insert(new IsCanary($x)) end | |
| | |
|rule "all frogs are green" when IsFrog($x:x) then insert(new IsGreen($x)) end | |
|rule "all canaries are yellow" when IsCanary($x:x) then insert(new IsYellow($x)) end | |
|""".stripMargin | |
"Forward chaining KB" should "work" in { | |
val engine = DroolsEngine(drl) | |
val facts = List( | |
("""{"x":"milou"}""", "test.ItCroaks"), | |
("""{"x":"milou"}""", "test.ItEatsFlies"), | |
("""{"x":"milou"}""", "test.ItSings"), | |
) | |
//facts.foreach{case (kind, json) => engine.insertJson(json, kind)} | |
for { (json, declareType) <- facts } { | |
engine.insertJson(json, declareType) | |
} | |
engine.getModelInstances("test.IsFrog").size shouldBe 0 | |
engine.fireAllRules() | |
//engine.getObjects.foreach(x => println(x)) | |
engine.getModelInstances("test.IsFrog").size shouldBe 1 | |
engine.getModelInstances("test.IsCanary").size shouldBe 0 | |
} | |
} | |
KbTest.execute() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment