我正在将Drools Rules引擎集成到我的应用程序中。我发现开始的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();
我有类似的工作,但我的问题是KnowledgeBase
被标记为已弃用,这对我来说是一个红旗,我做错了。现在KnowledgeBaseFactory.newKnowledgeBase()
未标记为已弃用,但它也会返回KnowledgeBase
类型。
那么我应该使用什么,因为不推荐使用KnowledgeBase?
答案 0 :(得分:18)
这是我发现使用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
的引用仍保留在文档中,但确实已弃用。