本文介绍了不推荐使用Drools KnowledgeBase的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在将Drools Rules引擎集成到我的应用程序中。我发现入门的示例中有99%如下:

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();

我有类似的工作方式,但是我的问题是 KnowledgeBase 被标记为已弃用,这对我来说是一个危险信号,表示我做错了。现在 KnowledgeBaseFactory.newKnowledgeBase()未标记为已弃用,但它还会返回 KnowledgeBase 类型。

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.

那么自KnowledgeBase被弃用以来,我应该使用什么?

So what should I be using since KnowledgeBase is deprecated?

推荐答案

这是代码我发现可以使用6.x:

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();

KnowledgeBase 的引用仍然保留在文档中,但确实不推荐使用。

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

这篇关于不推荐使用Drools KnowledgeBase的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-11 02:01