Skip to content

Instantly share code, notes, and snippets.

@dacr
Last active April 2, 2023 10:13
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/1e43978a6685e67431665a914e246eed to your computer and use it in GitHub Desktop.
Save dacr/1e43978a6685e67431665a914e246eed to your computer and use it in GitHub Desktop.
Learn to use drools working memory persistence to disk. / published by https://github.com/dacr/code-examples-manager #f54e7d0b-5d98-4243-8526-a80e3116161c/7a1b5f50ea4e97289ed0e76514df5845617a5ff6
// summary : Learn to use drools working memory persistence to disk.
// keywords : scala, drools, mvel, scalatest, ai, persistence
// 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 : f54e7d0b-5d98-4243-8526-a80e3116161c
// 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.1.1"
//> using dep "fr.janalyse::drools-scripting:1.0.16"
//> using dep "org.scalatest::scalatest:3.2.10"
// ---------------------
import fr.janalyse.droolscripting._, org.scalatest._, flatspec._, matchers._, OptionValues._
import java.util.Date
import scala.jdk.CollectionConverters._
import DroolsEngineConfig._
import org.kie.api._
import org.kie.api.runtime.KieSession
import org.kie.api.builder._
import reflect.Selectable.reflectiveSelectable
object SimplePersistenceTest extends AnyFlatSpec with should.Matchers {
override val suiteName = "SimplePersistenceTest"
def using[T <: { def close():Unit }, R](resource: T)(block: T => R):R = {
try { block(resource) } finally {
if (resource != null) resource.close()
}
}
/* Inspired from this OpenNMS project change :
https://github.com/OpenNMS/opennms/pull/1447/files/493b8008c6b89933219ce0f6b70bb0eac4af4741#diff-808685f08db05ed0726ce0402470c221
*/
def marshallStateToDisk(filename:String, kieSession:KieSession):Unit = {
val kMarshallers = KieServices.Factory.get().getMarshallers()
val kieBase = kieSession.getKieBase()
val oms = kMarshallers.newSerializeMarshallingStrategy()
//val oms = kMarshallers.newIdentityMarshallingStrategy()
val marshaller = kMarshallers.newMarshaller( kieBase, Array(oms) )
using(new java.io.FileOutputStream(filename)) { fos =>
kieSession.halt()
marshaller.marshall(fos, kieSession)
kieSession.dispose()
kieSession.destroy()
}
}
def unmarshallStateFromDisk(filename:String, kieSession:KieSession):Unit = {
val kMarshallers = KieServices.Factory.get().getMarshallers()
val kieBase = kieSession.getKieBase()
val oms = kMarshallers.newSerializeMarshallingStrategy()
//val oms = kMarshallers.newIdentityMarshallingStrategy()
val marshaller = kMarshallers.newMarshaller( kieBase, Array(oms) )
using(new java.io.FileInputStream(filename)) { fos =>
marshaller.unmarshall(fos, kieSession)
}
}
val defaultKModuleContent =
"""<kmodule xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
| xmlns="http://www.drools.org/xsd/kmodule">
| <kbase name="{KBASENAME}" default="true" eventProcessingMode="stream" equalsBehavior="identity">
| <ksession name="ksession1" type="stateful" default="true" clockType="pseudo"/>
| </kbase>
|</kmodule>
|""".stripMargin
"drools" should "be able to persist its working memory" in {
val drl =
"""package testdrools
|
|global org.slf4j.Logger logger
|
|declare Arrived
| @role(event)
| @expires(2s)
|end
|
|declare Say
| that:String
|end
|
|rule "init" when
|then
| insert(new Arrived());
|end
|
|rule "hello" when
| Arrived()
|then
| insertLogical(new Say("HELLO John DOE"));
|end
|
|""".stripMargin
val engine0 = DroolsEngine(drl)
engine0.fireAllRules()
engine0.getObjects.size shouldBe(2)
engine0.getModelFirstInstanceAttribute("testdrools.Say", "that").value.toString should include regex "(?i)hello"
val filename = "drools-persistence.tmp"
marshallStateToDisk(filename, engine0.session)
val engine1 = DroolsEngine(drl)
unmarshallStateFromDisk(filename, engine1.session)
engine0.getObjects.size shouldBe(2)
engine1.getModelFirstInstanceAttribute("testdrools.Say", "that").value.toString should include regex "(?i)hello"
engine1.timeShiftInSeconds(5)
engine1.fireAllRules()
engine1.getObjects.size shouldBe(0)
engine1.dispose()
}
}
SimplePersistenceTest.execute()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment