Last active
April 2, 2023 10:12
-
-
Save dacr/035049dd76188ea81605425c4cc1aa97 to your computer and use it in GitHub Desktop.
Drools spark hello world knowledge base / published by https://github.com/dacr/code-examples-manager #4d7db219-4834-4095-82cd-f6149742d86e/77577c289dd5e80eba4e1eee1a37f18c4ea76747
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 spark hello world knowledge base | |
// keywords : scala, drools, spark, 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 : 4d7db219-4834-4095-82cd-f6149742d86e | |
// execution : scala ammonite 2.12-1.7.4-instable 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 | |
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._ | |
System.out.println( | |
"""REQUIRES 2.12-1.7.4-instable ammonite release or release >=1.7.5 when they will become available | |
| which comes with my pull-request SEE https://github.com/lihaoyi/Ammonite/pull/1011 | |
|""".stripMargin | |
) | |
object DroolsSparkHelloWorld 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 | |
} | |
} | |
DroolsSparkHelloWorld.execute() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment