14

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?

Jacob Schoen
  • 14,034
  • 15
  • 82
  • 102
  • Which versions are you using? Since Drools 6, a lot has changed, so this may be the culprit? – Dominik Sandjaja May 21 '14 at 13:29
  • @DaDaDom It is 6.0.1.Final. I figured that was the case, but if the drools doc (pdf file) uses KnowledgeBase and I have not been able to find the alternative. – Jacob Schoen May 21 '14 at 13:34

1 Answers1

22

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.

laune
  • 31,114
  • 3
  • 29
  • 42
  • Thanks, that makes sense I guess. I just could not find anything in the latest docs pointing me to that. – Jacob Schoen May 21 '14 at 16:51
  • 1
    should ` KieSession kieSession = kieContainer.newKieSession();` be ` KieSession kieSession = kieBase.newKieSession();` ? – Jacob Schoen May 21 '14 at 16:56
  • 1
    Both is possible. Use the shorter if you don't have to specify a configuration for the KieBase. – laune May 21 '14 at 18:49
  • This code is useful - and I wish it was in the proper documentation - but the piece that I'm still missing is how to register custom evaluators in the 6 API, without using the deprecated APIs. I found this - but the final poster didn't say how to do it... http://drools.46999.n3.nabble.com/Drools-6-x-and-Custom-Operators-Evaluators-and-Accumulators-Using-them-as-regular-functions-td4028543.html – user2163960 Sep 18 '14 at 18:43
  • 1
    Yes this is useful. Its important to note that with v6.2.0 (and perhaps others in the 6.x family) you HAVE to prepend "src/main/resources/" to the path for the kds.write(...) call. Failing to do this all my tests failed to find the DRL later when I executed the rules. – Jeff Feb 25 '15 at 23:01
  • Would recommend to enclose the `FileInputStream fis = new FileInputStream( pathToSomeDrl );` statement around a try-catch block, otherwise a compiler error is generated, since `FileInputStream` throws `FileNotFoundException`. – Anmol Singh Jaggi Sep 29 '16 at 10:24
  • @laune could you please look at my issue. https://stackoverflow.com/questions/45343809/drools-android-integration – Pankaj Jangid Jul 27 '17 at 09:39