Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Drools KnowledgeBase Deprecated

Tags:

java

drools

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?

like image 880
Jacob Schoen Avatar asked May 21 '14 13:05

Jacob Schoen


1 Answers

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.

like image 131
laune Avatar answered Oct 20 '22 08:10

laune