Drools 知识库 已弃用

2022-09-04 01:46:17

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

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

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

那么,既然知识库已被弃用,我应该使用什么呢?


答案 1

这是我发现的与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


答案 2

推荐