Drools 6:为正在运行的KieSession添加规则 [英] Drools 6: add rules to a running KieSession
问题描述
你们有个主意吗?
问候
是的,支持用例,但重要的是要了解Drools 6引入了版本化可部署工件的概念( mavenized kjars)。换句话说,一旦你用版本X创建一个kjar,它应该是不可变的。如果要向kjar中定义的kbases添加/删除规则,则应创建另一个kjar版本X + 1。这个kjar可以在磁盘中作为真正的jar或在内存中创建。
理解kjar是不可变源工件和kcontainer的概念也很重要是实例化kjar并允许使用其kbases和ksessions的容器。
如果可以理解,那么您需要做的就是为X版实例化容器,当您想要更改kbase时,请调用容器updateToVersion(.. 。)将其更新为新版本的方法。 KBase和KSessions会像在Drools 5中一样逐步更新和保存。
代码段:
//使用版本1.0.0
ReleaseId releaseId1 = ks.newReleaseId(org.kie,test-upgrade,1.0.0 );
...
//创建会话和消防规则
KieContainer kc = ks.newKieContainer(releaseId1);
KieSession ksession = kc.newKieSession();
ksession.insert(新消息(Hello World));
...
//升级到版本1.1.0
ReleaseId releaseId2 = ks.newReleaseId(org.kie,test-upgrade,1.1.0 );
kc.updateToVersion(releaseId2);
//继续使用会话
ksession.insert(新消息(Hello World));
...
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
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.
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"));
...
这篇关于Drools 6:为正在运行的KieSession添加规则的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!