Last active
October 6, 2024 10:54
-
-
Save dacr/819826154cab02918563816002381245 to your computer and use it in GitHub Desktop.
Understanding insertLogical, aggregates in the context of drools events reasoning, keeping up to date a computed value / published by https://github.com/dacr/code-examples-manager #a0e2caf5-ca1b-4d9a-91cf-62c6aa7623be/8b7138a3c54a8d8fef736892b7f9e1db5bf54cc1
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 : Understanding insertLogical, aggregates in the context of drools events reasoning, keeping up to date a computed value | |
// keywords : scala, drools, mvel, scalatest, ai, @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 : a0e2caf5-ca1b-4d9a-91cf-62c6aa7623be | |
// created-on : 2018-09-12T21:11:39+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 SimpleTest extends AnyFlatSpec with should.Matchers { | |
override val suiteName = "SimpleTest" | |
"drools" should "propagate and expires" in { | |
val drl = | |
"""package testdrools | |
| | |
|global org.slf4j.Logger logger | |
|// -------------------------------------------------------------------- | |
|declare Hello | |
| name:String | |
| age:int | |
|end | |
|// -------------------------------------------------------------------- | |
|declare HelloEvent | |
| @role(event) | |
| @expires(20s) | |
| hello:Hello | |
|end | |
|// -------------------------------------------------------------------- | |
|declare Metric | |
| name:String | |
| value:double | |
|end | |
|// -------------------------------------------------------------------- | |
|rule "INIT-1" | |
|when | |
|then | |
| insert(new HelloEvent(new Hello("John", 24))); | |
|end | |
|// -------------------------------------------------------------------- | |
|rule "INIT-2" | |
|duration 10000 | |
|when | |
|then | |
| insert(new HelloEvent(new Hello("Sarah", 42))); | |
|end | |
|// -------------------------------------------------------------------- | |
|rule "TEST-1: hello event to fact" | |
|when | |
| HelloEvent($hello:hello) | |
|then | |
| insertLogical($hello); | |
|end | |
|// -------------------------------------------------------------------- | |
|rule "TEST-1A : init metric container" | |
|@Propagation(EAGER) | |
|enabled true | |
|when | |
| not Metric(name == "averageAge") | |
|then | |
| insert(new Metric("averageAge",0.0)); | |
|end | |
|// -------------------------------------------------------------------- | |
|rule "TEST-1B : hello average age" | |
|no-loop | |
|enabled true | |
|when | |
| exists Hello() | |
| accumulate(Hello($age:age); $avg: average($age)) | |
| $metric: Metric(name == "averageAge") | |
|then | |
| logger.info("****************** TEST-1B new average age "+ $avg); | |
| modify($metric) { | |
| setValue($avg); | |
| } | |
|end | |
|// -------------------------------------------------------------------- | |
|rule "TEST-1C : talk when average age evolves" | |
|enabled true | |
|when | |
| $metric: Metric(name == "averageAge", $avg:value) | |
|then | |
| logger.info("****************** TEST-1C new average age AGAIN"+ $avg); | |
|end | |
|// -------------------------------------------------------------------- | |
|rule "TEST-1D : reset average age" | |
|no-loop | |
|enabled true | |
|when | |
| $metric: Metric(name == "averageAge") | |
| not Hello() | |
|then | |
| logger.info("****************** TEST-1D average age RESET"); | |
| modify($metric) { | |
| setValue(0.0); | |
| } | |
|end | |
|""".stripMargin | |
val config = DroolsEngineConfig(withDroolsLogging = true) | |
val engine = DroolsEngine(drl, config) | |
engine.fireAllRules() // Only John 24 | |
engine.getModelFirstInstanceAttribute("testdrools.Metric", "value").value.asInstanceOf[Double] shouldBe 24 | |
engine.timeShiftInSeconds(12) | |
engine.fireAllRules() // John 24 & Sarah 42 | |
engine.getModelFirstInstanceAttribute("testdrools.Metric", "value").value.asInstanceOf[Double] shouldBe 33 | |
engine.timeShiftInSeconds(10) | |
engine.fireAllRules() // Only Sarah 42 | |
engine.getModelFirstInstanceAttribute("testdrools.Metric", "value").value.asInstanceOf[Double] shouldBe 42 | |
engine.timeShiftInSeconds(30) | |
engine.fireAllRules() // Nobody | |
engine.getModelFirstInstanceAttribute("testdrools.Metric", "value").value.asInstanceOf[Double] shouldBe 0 | |
engine.getObjects.size shouldBe 1 // The metric container | |
} | |
} | |
SimpleTest.execute() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment