问题
I am new to drools and we are currently using Drools 5.4.0 in our project. Currently we are using RuleCompiler.java and PackageBuilder.java classes of Drools 5.4.0 to compile the .xls files and create ruleSetObject. The code snippet is as given below
String drlFromFile = null;
if (Pattern.matches(regexPattern, file.getName())) {
if (file.getName().contains("csv") || file.getName().contains("CSV")) {
drlFromFile = RuleCompiler.compileCSV(file);
} else {
drlFromFile = RuleCompiler.compileSpreadSheet(file);
}
if (drlFromFile == null || drlFromFile.isEmpty()) {
logger.debug("Unable to Compile Rule Sheet: " + file.getName());
throw new DroolsParserException("Unable to Compile Rule Sheet: " + file.getName());
}
PackageBuilder builder = new PackageBuilder();
builder.addPackageFromDrl(new StringReader(drlFromFile));
Package ruleSetObject = builder.getPackage();
// Registering the compiled drl object in ruleExecutionSetRegistry
ruleExecutionSetRegistry.registerRuleSetObject(file.getName(), ruleSetObject,
getRuleEngineProviderName());
}
Now we need to upgrade to Drools 6.1.0.final, but I can not find the PackageBuilder.java class there. I tried to search for its replacement but didn't get anything.
Is any new class has been introduced in place of PackageBuilder.java? Does any one guide me how to use that class?
回答1:
The 'new' way to do things is by defining a KieModule
. Essentially, you create a Maven project which wraps your Drools rules, and then add that project as a dependency for your runtime. Generally, this expects that you will follow certain conventions in how you structure your project so that Drools can find your rules itself.
However, you may (like me) find it easier to migrate without completely restructuring your existing project. To achieve this, you can still build up a KieService
(the new KnowledgeBase
), by adding files to a KieFileSystem
. Here's a rough example of doing that:
KieServices kieServices = KieServices.Factory.get();
KieFileSystem kfs = kieServices.newKieFileSystem();
kfs.write(ResourceFactory.newFileResource(resource.getPath()));
KieBuilder kieBuilder = kieServices.newKieBuilder(kfs).buildAll();
if (kieBuilder.getResults().hasMessages(Level.ERROR)) {
// It didn't build. Do something about it...
}
KieContainer kieContainer = kieServices
.newKieContainer(kieServices.getRepository().getDefaultReleaseId());
KieSession kieSession = kieContainer.newKieSession();
And you're pretty much ready to go. In case it's useful, a recent demo project of mine contains an example of doing this for plain .drl files, and from what I understand, it should be pretty much the same if you want to add a spreadsheet to the KieFileSystem
instead of DRL.
来源:https://stackoverflow.com/questions/27036793/packagebuilder-java-not-available-in-drools-6-1-0-final