Skip to content

Instantly share code, notes, and snippets.

@dacr
Last active June 24, 2023 16:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dacr/6230af7afcb084dbb36b8f459a4d39c8 to your computer and use it in GitHub Desktop.
Save dacr/6230af7afcb084dbb36b8f459a4d39c8 to your computer and use it in GitHub Desktop.
Drools doctor knowledge base / published by https://github.com/dacr/code-examples-manager #ca758f2a-3bc3-482e-9c19-6470722fff8b/49e736bf0d321bb9173bac52043e916600cc1d0b
// summary : Drools doctor 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 : 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
// 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 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