6.2 版本的 drools 弃用代码

Deprecated code of drools to version 6.2

我有一个旧的和弃用的 drools 代码,这个代码可以读取容器外的规则文件 (drl) 并且可以使用任何值对象和任何 class.

            CommandExecutor ksession;
            KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder();
            String file = System.getenv("DOMAIN_HOME")+"/mydrls/business.drl";
            kbuilder.add(ResourceFactory.newFileResource(file), ResourceType.DRL);
            KnowledgeBase kbase = kbuilder.newKnowledgeBase();
            ksession = kbase.newStatelessKnowledgeSession();
            DataTest data = new DataTest();
            data.setAge(25);
            List cmds = new ArrayList();
            cmds.add( CommandFactory.newInsert( data ) );
            ksession.execute(CommandFactory.newBatchExecution( cmds ));

我如何使用 drools 6.2 版做同样的事情?

试试这个:

KieServices kieServices = KieServices.Factory.get();
KieFileSystem kfs = kieServices.newKieFileSystem();
FileInputStream fis = new FileInputStream( "simple/simple.drl" );
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();
StatelessKieSession kieSession =
    kieContainer.newStatelessKieSession();
}

您可以使用 KieHelper 来简化 Laune 的代码 class:

KieHelper kieHelper = new KieHelper();         
kieHelper.addResource(ResourceFactory.newInputStreamResource(new FileInputStream( "simple/simple.drl" )), ResourceType.DRL);

Results results = kieHelper.verify();
if( results.hasMessages( Message.Level.ERROR ) ){
    System.out.println( results.getMessages() );
    throw new IllegalStateException( "### errors ###" );
}

StatelessKieSession kieSession = kieHelper.build().newStatelessKieSession();

希望对您有所帮助。