Skip to content

Instantly share code, notes, and snippets.

@dacr
Last active Jun 20, 2021
Embed
What would you like to do?
Drools pet knowledge base / published by https://github.com/dacr/code-examples-manager #d5786f29-dd7b-4635-a95d-c2cb8fc57cb4/e28645ad83587a2c82ec6d498b87f48b70f9fc6e
// summary : Drools pet knowledge base
// keywords : scala, drools, mvel, scalatest, ai, knowledgebase, @testable
// publish : gist
// authors : David Crosson
// license : Apache2
// id : d5786f29-dd7b-4635-a95d-c2cb8fc57cb4
// execution : scala ammonite script (http://ammonite.io/) - run as follow 'amm scriptname.sc'
// created-on : 2019-10-03T08:46:39+02:00
import $ivy.`fr.janalyse::drools-scripting:1.0.11`, $ivy.`org.scalatest::scalatest:3.2.6`
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