Kamlesh
Kamlesh

Reputation: 21

Load multiple rules file in Drool 7.5

I am working on to upgrade Drools for my Spring based application from 6.5 to 7.50. The application has two different .DRL files against which the incoming payload will be conditionally validated. For e.g. if the incoming xml is related to life insurance, only the rules present in life.drl would run. Similarly, if the incoming xml is for annuities, only the rules present in annuity.drl would run. This runs perfectly fine on Drools 6.5 but not able to do so on Drools 7.50

Basically I want to grab two different KieBuilder instances (each for life and annuities) and then use the same to build a KieContainer, grab a session and trigger the rules. Below is a sample of how I am doing -

  KieServices ks = KieServices.Factory.get();
  KieRepository kr = ks.getRepository();

  // Loading Life rules
  KieBuilder kbLife = ks.newKieBuilder(getKieFS("life.drl"));
  kbLife.buildAll();

  // Loading Annuity rules
  KieBuilder kbAnnuity = ks.newKieBuilder(getKieFS("annuity.drl"));
  kbAnnuity.buildAll();


  KieModule kmLife = kr.getKieModule(kr.getDefaultReleaseId());
  KieContainer kcLife = ks.newKieContainer(kmLife.getReleaseId());
  KieSession ksLife = kcLife.newKieSession();
  ksLife.insert(some_fact);
  ksLife.fireAllRules();


  KieModule kmAnnuity = kr.getKieModule(kr.getDefaultReleaseId());
  KieContainer kcAnnuity = ks.newKieContainer(kmAnnuity.getReleaseId());
  KieSession ksAnnuity = kcAnnuity.newKieSession();
  ksAnnuity.insert(some_fact);
  ksAnnuity.fireAllRules();

The issue I am facing here is that only the Annuity rules are retained in the memory as it seems that the annuity rules are simply overwriting the life rules. I have verified the same by commenting out the line "kbAnnuity.buildAll();" and found that life rules are triggering correctly. If I reposition the line "kbAnnuity.buildAll();" after "ksLife.fireAllRules();", both the rules work properly. Since I am new to Drools 7.5, not sure where I am going wrong. Kindly advise

Upvotes: 2

Views: 1640

Answers (1)

Roddy of the Frozen Peas
Roddy of the Frozen Peas

Reputation: 15219

I'm a little confused about what you're actually doing since you're missing a great deal of information in your question.

Let's assume you have a directory structure like this:

src/main/resources/
├── META-INF/
│   └── kmodule.xml
├── annuity/
│   └── annuity.drl
└── life/
    └── life.drl

Your kmodule.xml should define two rule bases like this:

<kmodule xmlns="http://www.drools.org/xsd/kmodule">>
  <kbase name="annuity" packages="rules.annuity">
        <!-- define sessions, listeners as needed -->
  </kbase>
  <kbase name="life" packages="rules.life">
    <!-- define sessions, listeners as needed -->
  </kbase>
</kmodule>

Then when you want to fire your rules ...

public void fireRules(String kbaseName, Object fact) {
    KieServices kieServices = KieServices.Factory.get();
    KieContainer kContainer = kieServices.getKieClasspathContainer();
    KieBase kBase = kContainer.getKieBase(kbaseName);
    KieSession session = kBase.newKieSession();
    session.insert(fact)
    session.fireAllRules();
}

So:

fireRules("annuity", fact);
fireRules("life", fact);

Also recommend you use something a little newer than 7.5. 7.52 is perfectly lovely.

Upvotes: 1

Related Questions