将规则添加到现有的KiePackage和KieBase中

时间:2016-02-22 20:21:15

标签: java drools kie

我正在尝试将规则添加到已使用KieBuilder定义和构建的KiePackage中。

我知道你可以使用以下方法删除规则:

base.removeRule("com.somepackage", "ruleName");

但有没有相当于添加规则?我已经尝试将新的.drl文件添加到KieFileSystem中并使用KieScanner来获取更改,但它似乎不起作用:

KieBaseConfiguration config = service.newKieBaseConfiguration();
KieBase base = kContainer.newKieBase(config);

KieFileSystem fileSystem = service.newKieFileSystem(); 
KieBuilder builder = service.newKieBuilder(fileSystem); 

fileSystem.write(originalDrlFile, someFile)    
builder.buildAll()                               // first drl is loaded

fileSystem.write(newlDrlFile, someFile)    

KieScanner scanner = service.newKieScanner(kContainer);
scanner.scanNow();
KieSession ksession1 = kContainer.newKieSession();    

builder.buildAll()                               // reloading with new drl.

当我评估反对ksession1的事实时,新规则在KieBase中不存在。我错过了什么吗?

2 个答案:

答案 0 :(得分:2)

private KieSession kieSession;
public void build() throws Exception {
    KieServices kieServices = KieServices.Factory.get();

    KieFileSystem kfs = kieServices.newKieFileSystem();
    FileInputStream fis2 = new FileInputStream( "override/r2.drl" );
    kfs.write( "src/main/resources/r2.drl",
                kieServices.getResources().newInputStreamResource( fis2 ) );
    FileInputStream fis1 = new FileInputStream( "override/r1.drl" );
    kfs.write( "src/main/resources/r1.drl",
                kieServices.getResources().newInputStreamResource( fis1 ) );

    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 = kieContainer.newKieSession();
}

build方法说明了增量构建。要添加规则,您可以再次使用带有新规则的其他DRL文件来执行整个过程。或者,如果您保留KieFileSystem,您可以稍后添加DRL文件,再次调用buildAll,重新创建KieBase等。

答案 1 :(得分:0)

我们在哪里建立kieBuilder和kieBase之间的关系?

我尝试过这样的事情......

kieContainer= kieService.newKieContainer(kiebuilder.getKieModule().getReleaseId());
kieSession = kieContainer.newKieSession();