Skip to content

Instantly share code, notes, and snippets.

@dacr
Last active Jun 20, 2021
Embed
What would you like to do?
Learn to use drools working memory persistence to disk. / published by https://github.com/dacr/code-examples-manager #f54e7d0b-5d98-4243-8526-a80e3116161c/c9875548498236b52f3ef3182a988e2c3942d308
// summary : Learn to use drools working memory persistence to disk.
// keywords : scala, drools, mvel, scalatest, ai, persistence
// publish : gist
// authors : David Crosson
// license : Apache2
// id : f54e7d0b-5d98-4243-8526-a80e3116161c
// execution : scala ammonite script (http://ammonite.io/) - run as follow 'amm scriptname.sc'
// created-on : 2018-09-12T21:11:39+02:00
import $ivy.`fr.janalyse::drools-scripting:1.0.11`, $ivy.`org.scalatest::scalatest:3.2.6`
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._
object SimplePersistenceTest extends AnyFlatSpec with should.Matchers {
override val suiteName = "SimplePersistenceTest"
def using[T <: { def close() }, 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