Skip to content

Instantly share code, notes, and snippets.

@dacr
Last active Jun 20, 2021
Embed
What would you like to do?
Drools doctor knowledge base / published by https://github.com/dacr/code-examples-manager #ca758f2a-3bc3-482e-9c19-6470722fff8b/1d486c77154240c13a3dbbfeae8a4886f14f0d63
// summary : Drools doctor knowledge base
// keywords : scala, drools, mvel, scalatest, ai, knowledgebase, @testable
// publish : gist
// authors : David Crosson
// license : Apache2
// id : ca758f2a-3bc3-482e-9c19-6470722fff8b
// execution : scala ammonite script (http://ammonite.io/) - run as follow 'amm scriptname.sc'
// created-on : 2019-10-02T17:04:57+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 DockorKbTest extends AnyFlatSpec with should.Matchers {
val drl =
"""package diagnosis
|dialect "mvel"
|// ------------------------------------------
|declare enum Strength NONE(0),LOW(1),MEDIUM(2),HIGH(3);
| intensity: int
|end
|declare enum CoughingKind NONE(0),DRY(1), OILY(2);
| kind: int
|end
|
|// ------------------------------------------
|declare Sickness end
|declare Flu extends Sickness end
|
|// ------------------------------------------
|declare Symptom end
|
|declare Fever extends Symptom // --- Fièvre
| strength:Strength
|end
|declare Coughing extends Symptom // --- Toux
| strength:Strength
| kind:CoughingKind
|end
|declare MuscleAche extends Symptom // --- Courbature
| strength:Strength
|end
|
|// ------------------------------------------
|declare PatientTemperature
| temperature: float
|end
|
|rule "no fever" when PatientTemperature(temperature < 38) then insert(new Fever(Strength.NONE)); end
|rule "low fever" when PatientTemperature(temperature >= 38 && < 39) then insert(new Fever(Strength.LOW)); end
|rule "medium fever" when PatientTemperature(temperature >= 39 && < 40) then insert(new Fever(Strength.MEDIUM)); end
|rule "high fever" when PatientTemperature(temperature >= 40) then insert(new Fever(Strength.HIGH)); end
|
|// ------------------------------------------
|rule "flu diagnostic"
|when
| Fever(strength.intensity >= Strength.MEDIUM.intensity)
| MuscleAche(strength != Strength.NONE)
|then
| insert(new Flu());
|end
|""".stripMargin
"Doctor KB" should "deduce if patient has a flu" in {
val engine = DroolsEngine(drl, DroolsEngineConfig.configWithEquality)
val facts = List(
"diagnosis.PatientTemperature" -> """{"temperature":39.5}""",
"diagnosis.Coughing" -> """{"strength":"MEDIUM", "kind":"OILY"}""",
"diagnosis.MuscleAche" -> """{"strength":"HIGH"}"""
)
facts.foreach{case (kind, json) => engine.insertJson(json, kind)}
engine.fireAllRules()
engine.getObjects.foreach(ob => info(ob.toString))
engine.getModelInstances("diagnosis.Symptom").toList.size should be > 0
engine.getModelInstances("diagnosis.Sickness").toList.size should be > 0
}
}
DockorKbTest.execute()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment