Dynamically load rules in drools rule engine running on top of Apache spark

3,774 views
Skip to first unread message

kemai...@gmail.com

unread,
Oct 3, 2016, 8:18:00 PM10/3/16
to Drools Usage
Hello,

We are planning to store the rules in some sort of datastore. Whenever a rule changes or a new one gets added, we would want to reload the rule engine(spark+drools) with the changes. Looking for some suggestions to achieve the same if someone is already doing it. Any help is really appreciated.

Thanks,
Kiran

Ged Byrne

unread,
Oct 12, 2016, 3:36:23 PM10/12/16
to Drools Usage
This is easy to achieve using a virtual file system.  This is already built into the KieServices as the KieFileSystem.


And here is the source of a Drools unit test making use of the KieFileSystem to load a rule from a string.


The relevant bit looks like this:

        KieServices ks = KieServices.Factory.get();

        KieFileSystem kfs = ks.newKieFileSystem().write"src/main/resources/r1.drl", str );
        ks.newKieBuilderkfs ).buildAll();

All you have to do is pull the text of the rule definition from your DataStore (Spark or whatever) and add it to a path in your virtual filestore.

Make sure you follow the Maven conventions in your choice of path - it needs to go in a resource folder: http://maven.apache.org/plugins/maven-resources-plugin/examples/resource-directory.html

I don't think you can dynamically alter rules once a session has been built.  When your rules definitions change then you'll need to build a new session.  You can keep the same KieFileSystem and overwrite the file, but you'll need to build a new session.

If your sessions are statefull then take care to dispose of the previous session properly: https://docs.jboss.org/jbpm/v5.1/javadocs/org/drools/runtime/StatefulKnowledgeSession.html#dispose()
Reply all
Reply to author
Forward
0 new messages