Drools 6: add rules to a running KieSession

删除回忆录丶 提交于 2019-12-18 03:46:52

问题


till now I couldn't figure out the best way (meaning with minimal overhead) to add rules to a running KieSession in Drools 6.0.0 AND still keeping my facts in KieSession. In Drools 5 the KSession was updated when KBase was changed, but the same seems not be true for Drools 6 since my rules are not created in the KieBase. Is there a way to do it without replacing whole KieModules or Jars in the KieFileSystem. I think there should be an easy way to it.

Do you guys have an idea?

Regards


回答1:


Yes, the use case is supported, but it is important to understand that Drools 6 introduces the concept of versioned deployable artifacts (the mavenized kjars). In other words, once you create a kjar with version X, it is supposed to be immutable. If you want to add/remove rules to the kbases defined in kjar, you should create another kjar version X+1. This kjar can be created either physically in the disk as a real jar or in memory.

Also important to understand the concept that the kjar is the immutable source artifact and the kcontainer is the container that instantiates the kjar and allows the use of its kbases and ksessions.

If that is understood, then all you need to do is instantiate the container for version X, and when you want to change the kbase, call the container updateToVersion(...) method to update it to the new version. KBases and KSessions are incrementally updated and preserved like they were in Drools 5.

Unit test here: https://github.com/droolsjbpm/drools/blob/master/drools-compiler/src/test/java/org/drools/compiler/integrationtests/IncrementalCompilationTest.java#L158

Code snippet:

    // work with version 1.0.0
    ReleaseId releaseId1 = ks.newReleaseId("org.kie", "test-upgrade", "1.0.0");
    ...

    // Create a session and fire rules
    KieContainer kc = ks.newKieContainer( releaseId1 );
    KieSession ksession = kc.newKieSession();
    ksession.insert(new Message("Hello World"));
    ...

    // upgrade to version 1.1.0
    ReleaseId releaseId2 = ks.newReleaseId("org.kie", "test-upgrade", "1.1.0");
    kc.updateToVersion(releaseId2);

    // continue working with the session
    ksession.insert(new Message("Hello World"));
    ...


来源:https://stackoverflow.com/questions/22205945/drools-6-add-rules-to-a-running-kiesession

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!