Last active
October 6, 2024 10:54
-
-
Save dacr/bae5897d75efe32426516d77ca6dbf9c to your computer and use it in GitHub Desktop.
Basic drools json usage examples through unit test cases. / published by https://github.com/dacr/code-examples-manager #ea0953fa-188a-4ce3-b18d-65082c777577/478fd6f130c65b9088ebf4dcca3f8afbb822ecee
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 : Basic drools json usage examples through unit test cases. | |
// keywords : scala, drools, mvel, scalatest, ai, json, @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 : ea0953fa-188a-4ce3-b18d-65082c777577 | |
// execution : scala ammonite script (http://ammonite.io/) - run as follow 'amm scriptname.sc' | |
// created-on : 2019-10-01T10:52: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.* | |
import DroolsEngineConfig.* | |
object BasicsJsonWithDroolsTest extends AnyFlatSpec with should.Matchers { | |
override val suiteName = "BasicsJsonWithDroolsTest" | |
"Drools" should "manage json inputs associated to simple mvel declarations" in { | |
val drl = | |
"""package testdrools | |
| | |
|declare Someone | |
| name: String | |
|end | |
| | |
|rule "hello" when | |
| Someone($name:name) | |
|then | |
| insert("Hello "+$name); | |
|end | |
|""".stripMargin | |
val engine = DroolsEngine(drl) | |
engine.insertJson("""{"name":"John"}""", "testdrools.Someone") | |
engine.fireAllRules() | |
engine.getModelFirstInstance("testdrools.Someone") shouldBe defined | |
engine.getModelFirstInstance("java.lang.String").value shouldBe "Hello John" | |
engine.dispose() | |
} | |
it should "manage json inputs associated to more complex mvel declarations" in { | |
val drl = | |
"""package testdrools | |
| | |
|declare Address | |
| street:String | |
| town:String | |
| country:String | |
|end | |
| | |
|declare Someone | |
| name: String | |
| age: int | |
| surnames: String[] | |
| address: Address | |
|end | |
| | |
|rule "hello" when | |
| Someone($name:name, $address:address, $surnames:surnames, $country:address.country) | |
|then | |
| insert("Hello "+$name+" "+$surnames.length+" "+$country); | |
|end | |
|""".stripMargin | |
val engine = DroolsEngine(drl) | |
val json = | |
""" | |
|{ | |
| "name": "John", | |
| "age": 42, | |
| "surnames": ["joe", "junior"], | |
| "address": { | |
| "street":"Somewhere", | |
| "town":"NoTown", | |
| "country":"France" | |
| } | |
|} | |
|""".stripMargin | |
engine.insertJson(json, "testdrools.Someone") | |
engine.fireAllRules() | |
engine.getModelFirstInstance("java.lang.String").value shouldBe "Hello John 2 France" | |
engine.dispose() | |
} | |
it should "be able to deal with ISO8601 json dates" in { | |
val drl= | |
"""package test | |
|import java.util.Date | |
|declare Someone | |
| name:String | |
| birth:Date | |
|end | |
|""".stripMargin | |
val engine = DroolsEngine(drl) | |
engine.insertJson("""{"name":"joe", "birth":"2019-01-01T14:00:00Z"}""", "test.Someone") | |
engine.fireAllRules() | |
val people = engine.getModelInstances("test.Someone") | |
info(people.mkString(",")) | |
people should have size 1 | |
} | |
it should "be able to use enumerations" in { | |
val drl= | |
"""package test | |
| | |
|declare enum Priority LOW(0), MEDIUM(1), HIGH(2); | |
| value: int | |
|end | |
| | |
|declare enum Color RED("red"), GREEN("green"), BLUE("blue"); | |
| name: String | |
|end | |
| | |
|declare Combo | |
| priority:Priority | |
| color:Color | |
|end | |
|""".stripMargin | |
val engine = DroolsEngine(drl) | |
engine.insertJson(""""LOW"""", "test.Priority") | |
engine.insertJson(""""RED"""", "test.Color") | |
engine.insertJson("""{"priority":"LOW", "color":"GREEN"}""", "test.Combo") | |
engine.fireAllRules() | |
engine.getObjects should have size 3 | |
val combo = engine.getModelInstances("test.Combo").headOption.value | |
combo.toString should include regex "LOW.*GREEN" | |
} | |
} | |
BasicsJsonWithDroolsTest.execute() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment