Skip to content

Instantly share code, notes, and snippets.

@dacr
Last active October 6, 2024 10:54
Show Gist options
  • Save dacr/f029b4c19a1631c9120a78eff4e3c4f6 to your computer and use it in GitHub Desktop.
Save dacr/f029b4c19a1631c9120a78eff4e3c4f6 to your computer and use it in GitHub Desktop.
Drools pet knowledge base / published by https://github.com/dacr/code-examples-manager #d5786f29-dd7b-4635-a95d-c2cb8fc57cb4/f9a685152b1a520664467f8cde0f055850dc2106
// summary : Drools pet 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 : d5786f29-dd7b-4635-a95d-c2cb8fc57cb4
// created-on : 2019-10-03T08:46:39+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 animals
|
|import java.util.Date
|
|declare Pet
| name: String
| birth: Date
|end
|
|declare Cat extends Pet
| lifeCount:int
|end
|
|""".stripMargin
"Animals KB" should "work" in {
val facts = List(
"animals.Cat"->"""{"name":"minou", "birth":"2019-01-01T14:00:00Z", "lifeCount":7}""",
)
val engine = DroolsEngine(drl)
facts.foreach{case (kind, json) => engine.insertJson(json, kind)}
engine.fireAllRules()
engine.getObjects should have size(1)
val cats = engine.getModelInstances("animals.Cat")
cats.size shouldBe 1
}
}
KbTest.execute()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment