Drools KnowledgeBase Deprecated

Jacob Schoen picture Jacob Schoen · May 21, 2014 · Viewed 13.6k times · Source

I am integrating the Drools Rules engine into my application. 99% of the examples I have found to get started look like:

KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder();
kbuilder.add( ResourceFactory.newUrlResource( url ),
                      ResourceType.DRL );
if ( kbuilder.hasErrors() ) {
    System.err.println( builder.getErrors().toString() );
}                     

KnowledgeBase kbase = KnowledgeBaseFactory.newKnowledgeBase();
kbase.addKnowledgePackages( builder.getKnowledgePackages() );

StatefulKnowledgeSession ksession = knowledgeBase.newStatefulKnowledgeSession();
ksession.insert( new Fibonacci( 10 ) );
ksession.fireAllRules();

ksession.dispose();

I have something similar working, but my question is that KnowledgeBase is marked as deprecated, which is a red flag to me that I am doing it wrong. Now KnowledgeBaseFactory.newKnowledgeBase() is not marked deprecated, but it also returns a KnowledgeBase type.

So what should I be using since KnowledgeBase is deprecated?

Answer

laune picture laune · May 21, 2014

This is the code I've found to work with 6.x:

    KieServices kieServices = KieServices.Factory.get();
    KieFileSystem kfs = kieServices.newKieFileSystem();
    FileInputStream fis = new FileInputStream( pathToSomeDrl );
    kfs.write( "src/main/resources/simple.drl",
                kieServices.getResources().newInputStreamResource( fis ) );
    KieBuilder kieBuilder = kieServices.newKieBuilder( kfs ).buildAll();
    Results results = kieBuilder.getResults();
    if( results.hasMessages( Message.Level.ERROR ) ){
        System.out.println( results.getMessages() );
        throw new IllegalStateException( "### errors ###" );
    }
    KieContainer kieContainer =
        kieServices.newKieContainer( kieServices.getRepository().getDefaultReleaseId() );
    KieBase kieBase = kieContainer.getKieBase();
    KieSession kieSession = kieContainer.newKieSession();

References to KnowledgeBase have remained in the documentation, but it is indeed deprecated.