Skip to content

Instantly share code, notes, and snippets.

@ge0ffrey
Last active August 13, 2019 11:31
Show Gist options
  • Save ge0ffrey/5909266 to your computer and use it in GitHub Desktop.
Save ge0ffrey/5909266 to your computer and use it in GitHub Desktop.
Is this progress? ... KieBase (drools 6) versus RuleBase (drools 4)
Given List<String< scoreDrlList, how do we build a RuleBase/KieBase?
Drools 4 (building a RuleBase):
PackageBuilder packageBuilder = new PackageBuilder();
for (String scoreDrl : scoreDrlList) {
InputStream scoreDrlIn = getClass().getResourceAsStream(scoreDrl);
packageBuilder.addPackageFromDrl(new InputStreamReader(scoreDrlIn, "UTF-8"));
}
RuleBaseConfiguration ruleBaseConfiguration = new RuleBaseConfiguration();
RuleBase ruleBase = RuleBaseFactory.newRuleBase(ruleBaseConfiguration);
if (packageBuilder.hasErrors()) { ... }
ruleBase.addPackage(packageBuilder.getPackage());
return ruleBase;
Drools 6 (building a KieBase):
KieServices kieServices = KieServices.Factory.get();
KieResources kieResources = kieServices.getResources();
KieFileSystem kieFileSystem = kieServices.newKieFileSystem();
for (String scoreDrl : scoreDrlList) {
InputStream scoreDrlIn = getClass().getResourceAsStream(scoreDrl);
String path = "src/main/resources/optaplanner-kie-namespace/" + scoreDrl;
kieFileSystem.write(path, kieResources.newInputStreamResource(scoreDrlIn, "UTF-8"));
}
KieBuilder kieBuilder = kieServices.newKieBuilder(kieFileSystem);
kieBuilder.buildAll();
Results results = kieBuilder.getResults();
if (results.hasMessages(Message.Level.ERROR)) { ...
} else if (results.hasMessages(Message.Level.WARNING)) { ... }
KieContainer kieContainer = kieServices.newKieContainer(kieBuilder.getKieModule().getReleaseId());
KieBaseConfiguration kieBaseConfiguration = kieServices.newKieBaseConfiguration();
KieBase kieBase = kieContainer.newKieBase(kieBaseConfiguration);
Is this progress?
Note: like 90% of the users out there, I don't care about building multiple KieBases.
And if the api would following the principle of configuration by exception, I wouldn't need to care either...
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment