Last active
October 6, 2024 10:54
-
-
Save dacr/e1f605addfedef45604f2c587dfe083c to your computer and use it in GitHub Desktop.
Drools forward chaining example knowledge base with roots from wikipedia definition example / published by https://github.com/dacr/code-examples-manager #d0bf6eb4-acb7-49b9-bb54-edd6d63f3572/8856e083650042683bcb5b7f353ad497db55b1c3
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 forward 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 : d0bf6eb4-acb7-49b9-bb54-edd6d63f3572 | |
// created-on : 2019-10-04T16:36:54+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 { | |
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 end | |
|declare ItEatsFlies end | |
|declare ItChirps end | |
|declare ItSings end | |
| | |
|declare IsGreen end | |
|declare IsYellow end | |
| | |
|declare IsFrog end | |
|declare IsCanary end | |
| | |
|rule "frog" when ItCroaks() ItEatsFlies() then insert(new IsFrog()) end | |
|rule "canary" when ItChirps() ItSings() then insert(new IsCanary()) end | |
| | |
|rule "all frogs are green" when IsFrog() then insert(new IsGreen()) end | |
|rule "all canaries are yellow" when IsCanary() then insert(new IsYellow()) end | |
|""".stripMargin | |
"Forward chaining KB" should "work" in { | |
val engine = DroolsEngine(drl) | |
val facts = List( ("{}", "test.ItCroaks"), ("{}", "test.ItEatsFlies") ) | |
//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.getModelInstances("test.IsFrog").size shouldBe 1 | |
} | |
} | |
KbTest.execute() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment