Last active
October 6, 2024 10:54
-
-
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/a48d2e5b422e2f8cf908b4908ac0bb7f1ac1d052
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 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.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 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