dacr / drools-scripting   1.1.0

GitHub

Drools made easy to use for scripting or testing purposes

Scala versions: 2.12

Drools scripting License Maven

Drools made easy to use for scripting or testing purposes.

This library allows you to easily design proof of concepts based on the drools expert system. It greatly simplifies how you can quickly write drools based code examples or small experiments.

Just insert JSON facts into your drools working memory, and use the available engine methods to interact with the expert system and extract data from it. Data extraction can be done through simple accessors or through the JSON format. Check the documented methods in the DroolsEngine class or take a look to the large amount of example I've made available. (Most of them can be run directly by using the great scala-cli REPL solution)

A selection of my drools shared code examples based on drools-scripting project :

A hello world drools example runnable with scala-cli :

// ---------------------
//> 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.*, org.scalatest.matchers.*

object HelloTest extends AnyFlatSpec with should.Matchers {
  "Drools" should "say hello" in {
    val drl =
      """package test
        |rule "hello" when
        |then
        |  insert("HELLO WORLD");
        |end
        |""".stripMargin
    val engine = DroolsEngine(drl)
    engine.fireAllRules()
    engine.strings shouldBe List("HELLO WORLD")
  }
}
HelloTest.execute()

or an other one runnable with scala-cli :

// ---------------------
//> 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 HelloTest extends AnyFlatSpec with should.Matchers {
  "Drools" should "say hello" in {
    val drl =
      """package test
        |
        |declare Someone
        |  name:String
        |end
        |
        |declare Message
        |  message:String
        |end
        |
        |rule "hello" when
        |  Someone($name:name)
        |then
        |  insert(new Message("HELLO "+$name));
        |end
        |""".stripMargin
    val engine = DroolsEngine(drl)
    engine.insertJson("""{"name":"John"}""","test.Someone")
    engine.fireAllRules()
    val msgOption = engine.getModelFirstInstanceAttribute("test.Message", "message")
    msgOption.value shouldBe "HELLO John"
  }
}
HelloTest.execute()