Last active
October 6, 2024 10:54
-
-
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/2115f636862183f1a0d5016fa410c8b234823402
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 : 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.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 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