I am looking through Drools as a possibility for a rules application use case. I am able to get Drools to reference the rule-set from a .xls
file and a .drl
file independently of each other at this point in time. However, in my case there are times where the rules do not exist as a .xls
file and will be programmed within a .drl
file. Because of this, I am wondering if within the same program execution, both files could be used depending on certain input criteria? For example, if State == "GA"
then use .xls
, else if
State == "FL"
then use .drl
.
I have read on the different KieConcepts and understand them at a high-level, and I believe that I would have to create each rule file as a separate KieContainer and then load them into a single KieBase based on the definitions given to each here. However when it comes to this step I get confused as to how this is accomplished. I have two separate instances already created, would I be able to simply combine these two in one program using a KieBase?
The .xls Implementation
public static void main (String[] args) {
try {
KieServices ks = KieServices.Factory.get();
// add excel sheet to knowledge base
Resource dt = ResourceFactory.newClassPathResource("dtables/StateInterest.xls");
KieFileSystem kieFileSystem = ks.newKieFileSystem().write(dt);
KieBuilder kieBuilder = ks.newKieBuilder(kieFileSystem);
kieBuilder.buildAll();
KieRepository kieRepository = ks.getRepository();
ReleaseId krDefaultReleaseID = kieRepository.getDefaultReleaseId();
KieContainer kieContainer = ks.newKieContainer(krDefaultReleaseID);
KieSession kSession = kieContainer.newKieSession();
// go !
StateInterestRequest stateInterestRequest = new StateInterestRequest();
stateInterestRequest.setPolicyIssueState("GA");
stateInterestRequest.setPolicyIssueDate("10/2018");
kSession.insert(stateInterestRequest);
kSession.fireAllRules();
The .drl Implementation
public static final void main(String[] args) {
try {
KieServices ks = KieServices.Factory.get();
KieContainer kContainer = ks.getKieClasspathContainer();
KieSession kSession = kContainer.newKieSession("ksession-rules");
// go !
StateInterestRequest stateInterestRequest = new StateInterestRequest();
stateInterestRequest.setPolicyIssueState("FL");
stateInterestRequest.setPolicyIssueDate("2008-03-04");
kSession.insert(stateInterestRequest);
kSession.fireAllRules();
As an addendum, if possible would this be accomplished through the main()
of the final combined program, or within the rules themselves?
Yes, you can combine both types of files in one project. You can have written rules in xls format, which will match State "GA" and rules written in a DRL file which will match other states. I recommend reading the Drools documentation first, so you get the basic concepts of rules, KJars etc. [1]. I think this chapter could be helpful to you [2].
[1] https://docs.jboss.org/drools/release/7.16.0.Final/drools-docs/html_single/index.html
[2] https://docs.jboss.org/drools/release/7.16.0.Final/drools-docs/html_single/index.html#_builddeployutilizeandrunsection