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/bae5897d75efe32426516d77ca6dbf9c to your computer and use it in GitHub Desktop.
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/4fc9d2a572447eefae1d9d600dd9e87732f1c904
// 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.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._
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