Understanding how Drools 6 API help to load a rule asset in KieBuilder and build KieModule
Issue
- The following
Drools 6API code help users to load rule asset (likeDRL) inKieBuilderso thatKieModulecould be built and deployed toKieRepository.
public static KieContainer createKieContainerForProject() {
KieServices kieServices = KieServices.Factory.get();
// Create a module model
KieModuleModel kieModuleModel = kieServices.newKieModuleModel();
// Base Model from the module model
KieBaseModel kieBaseModel = kieModuleModel.newKieBaseModel( "KBase" )
.setDefault( true )
.setEqualsBehavior( EqualityBehaviorOption.EQUALITY)
.setEventProcessingMode( EventProcessingOption.STREAM );
// Create session model for the Base Model
KieSessionModel ksessionModel = kieBaseModel.newKieSessionModel( "KSession" )
.setDefault( true )
.setType( KieSessionModel.KieSessionType.STATEFUL )
.setClockType( ClockTypeOption.get("realtime") );
// Create File System services
KieFileSystem kFileSystem = kieServices.newKieFileSystem();
File file = new File("/opt/brmsrepo/src/main/resources/rules/Test.drl");
Resource resource = kieServices.getResources().newFileSystemResource(file).setResourceType(ResourceType.DRL);
kFileSystem.write( resource );
KieBuilder kbuilder = kieServices.newKieBuilder( kFileSystem );
// kieModule is automatically deployed to KieRepository if successfully built.
kbuilder.buildAll();
if (kbuilder.getResults().hasMessages(org.kie.api.builder.Message.Level.ERROR)) {
throw new RuntimeException("Build time Errors: " + kbuilder.getResults().toString());
}
KieContainer kContainer = kieServices.newKieContainer(kieServices.getRepository().getDefaultReleaseId());
return kContainer;
}
However there are some questions regarding the code flow.
1) The ksessionModel that is created is never used. Where and how to use it?
2) Where else the KBase name will be used after creating kieModuleModel.newKieBaseModel( "KBase") ?
3) The kFileSystem.write( resource ); is used in the aforementioned example, but the following piece of code seems to be also there to do the same job.
KieFileSystem kFileSystem = ...
kFileSystem.write( "src/main/resources/TestDRL.txt", kieServices.getResources().newInputStream Resource( drlStream )
.setResourceType(ResourceType.DRL) );
In Drools 5 it was required to type the DRL file whereas here there are 2 parameters available, src/m ain/resources/TestDRL.txt and a Resource to the file as shown above. Which scenario is appropriate for using the src/main/resources/TestDRL.txt way ?
4) What is the equivalent in Drools 6 for the old KnowledgeBase API used in Drools 5?
Environment
- Red Hat JBoss BRMS (BRMS)
- 6.0.x
- Drools
- 6.0.0
Subscriber exclusive content
A Red Hat subscription provides unlimited access to our knowledgebase of over 48,000 articles and solutions.
Welcome! Check out the Getting Started with Red Hat page for quick tours and guides for common tasks.
