Drools模式流和容器

时间:2014-11-21 15:45:53

标签: stream drools mode

这是我的代码。

public static KieContainer createKieContainerForProject() {
        KieServices ks = KieServices.Factory.get();
        // Create a module model
        KieModuleModel kieModuleModel = ks.newKieModuleModel();

        // Base Model from the module model
        KieBaseModel kieBaseModel = kieModuleModel.newKieBaseModel( "KBase" )
                .setDefault( true )
                .setEqualsBehavior( EqualityBehaviorOption.EQUALITY)
                .setEventProcessingMode( EventProcessingOption.STREAM );                

        // Create session model for the Base Model
        KieSessionModel ksessionModel = kieBaseModel.newKieSessionModel( "KSession" )
                .setDefault( true )
                .setType( KieSessionModel.KieSessionType.STATEFUL )
                .setClockType( ClockTypeOption.get("realtime") );

        // Create File System services
        KieFileSystem kFileSystem = ks.newKieFileSystem();

        File file = new File("src/main/resources/rules/Sample.drl");
        Resource resource = ks.getResources().newFileSystemResource(file).setResourceType(ResourceType.DRL);
    kFileSystem.write( resource );       

        KieBuilder kbuilder = ks.newKieBuilder( kFileSystem );
        // kieModule is automatically deployed to KieRepository if successfully built.
        kbuilder.buildAll();

        if (kbuilder.getResults().hasMessages(org.kie.api.builder.Message.Level.ERROR)) {
            throw new RuntimeException("Build time Errors: " + kbuilder.getResults().toString());
        }    
        KieContainer kContainer = ks.newKieContainer(ks.getRepository().getDefaultReleaseId());
        return kContainer;
    }

}

当我打电话给它时,它不起作用,我的规则也没有用。

我的规则是

    rule "Sound the alarm in case temperature rises above threshold"
        when
          TemperatureThreshold( $max : max )
          Number( doubleValue > $max ) from accumulate(
              SensorReading( $temp : temperature ) over window:time( 10m ),
              average( $temp ) )
    then
        // sound the alarm
    end

当我运行程序时,他说它有错误,模式不是流,代码不起作用。

如何将程序置于流模式?

2 个答案:

答案 0 :(得分:4)

引出代码,添加-KieBase和KieSession创建:

    KieServices ks = KieServices.Factory.get();
    KieFileSystem kFileSystem = ks.newKieFileSystem();
    FileInputStream fis = new FileInputStream( "...drl" );

    kFileSystem.write("src/main/resources/somename.drl",
              ks.getResources().newInputStreamResource( fis ) ); //XXX

    KieBuilder kbuilder = ks.newKieBuilder( kFileSystem );
    kbuilder.buildAll();
    if (kbuilder.getResults().hasMessages(org.kie.api.builder.Message.Level.ERROR)) {
        throw new RuntimeException("Build time Errors: " + kbuilder.getResults().toString());
    }    
    KieContainer kContainer = ks.newKieContainer(ks.getRepository().getDefaultReleaseId());

    KieBaseConfiguration config = ks.newKieBaseConfiguration();
    config.setOption(EventProcessingOption.STREAM);

    KieBase kieBase = kContainer.newKieBase( config );
    KieSession kieSession = kieBase.newKieSession();

这应该会为您提供一个能够运行您的规则的会话。 (

答案 1 :(得分:0)

根据the docs,如下:

KieBaseConfiguration config = KieServices.Factory.get().newKieBaseConfiguration();
config.setOption( EventProcessingOption.STREAM );

但是,对于一个有效的例子,可能值得一看this Drools test