Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
521 views
in Technique[技术] by (71.8m points)

java - Drools 6: add rules to a running KieSession

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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"));
    ...

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...