Drools Runtime
Class Path loading
When we create a new KieContainer based on the classpath, all the available jars will be scanned. In order to create a new KieContainer, we use the KieServices (ks) to provide us with a new instance of the KieContainer, as follows:
KieContainer kContainer = ks.newKieClasspathContainer();
Drools will scan all the jars in the classpath looking for the kmodule.xml file in the META-INF/ directory. When found, this file will load the provided configurations to make them available to use in our applications
KieServices ks = KieServices.Factory.get();
KieContainer kContainer = ks.newKieClasspathContainer();
Results results = kContainer.verify();
results.getMessages().stream().forEach((message) -> {
System.out.println(">> Message ( "+message.getLevel()+" ): "+message.getText());
});
assertThat(false, is(results.hasMessages(Message.Level.ERROR)));
kContainer.getKieBaseNames().stream().map((kieBase) -> {
System.out.println(">> Loading KieBase: "+ kieBase );
return kieBase;
}).forEach((kieBase) -> {
kContainer.getKieSessionNamesInKieBase(kieBase).stream().forEach((kieSession) -> {
System.out.println("\t >> Containing KieSession: "+ kieSession );
});
});
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
the verify() method will allow us to make sure that our business assets are correct and are loaded correctly in the KieContainer instance. If the verify() method returns Results containing errors, we should stop and correct these errors before moving forward.
Loading rules using Maven artifacts (Kie-CI)
KieServices ks = KieServices.Factory.get();
KieContainer kContainer = ks.newKieContainer(ks.newReleaseId("org.drools.devguide",
"chapter-03-kjar-simple-discounts",
"0.1-SNAPSHOT"));
Results results = kContainer.verify();
results.getMessages().stream().forEach((message) -> {
System.out.println(">> Message ( "+message.getLevel()+" ): "+message.getText());
});
assertThat(false, is(results.hasMessages(Message.Level.ERROR)));
2
3
4
5
6
7
8
9
10
we are letting the KieContainer resolve an artifact that we are providing based on GroupId , ArtifactId , and Version (also referred as GAV). As you can see, the newKieContainer() method is expecting a ReleaseId object, which is also created using a helper method of the KieServices .
The Drools API allows us to not only use this ReleaseId specific version of the KieModule, but also upgrade it to a newer version, should it be necessary, through the updateToVersion() method. This method will recreate the KieContainer to become an access point to the KieBases and KieSessions of a newer version of the KieModule.
KieModule configurations (KieBases,KieSessions & StatelessKieSessions)
<?xml version="1.0" encoding="UTF-8"?>
<kmodule xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://jboss.org/kie/6.0.0/kmodule">
<kbase name="rules.cp.discount">
<ksession name="rules.cp.discount.session" type="stateful"/>
</kbase>
</kmodule>
2
3
4
5
6
7
KieScanner
The KieScanner component in Drools is nothing but a wrapper around a KieContainer that can be configured to automatically detect changes in the resources that the container depends on. There is a catch though, the resources referenced by the KieContainer being monitored must be KieJars residing in a Maven repository.
KieServices ks = KieServices.Factory.get();
KieContainer kieContainer = ks.newKieContainer(
ks.newReleaseId("group.test","artifact-test", "1.0"));
KieScanner scanner = ks.newKieScanner(kieContainer);
2
3
4
agenda-group
It's also worth mentioning that, when a rule is fired, it can also define the agenda group that is going to be activated through the implicit global variable called kcontext, as follows:
rule "Done with promotions. Onward with printing invoice"
salience -100 //last rule of group to run
agenda-group "promotions"
when
OrderLine()
then
kcontext.getKnowledgeRuntime().getAgenda().
getAgendaGroup("MAIN").setFocus();
end
2
3
4
5
6
7
8
9