use of org.kie.api.builder.KieModule in project drools by kiegroup.
the class IncrementalCompilationTest method testKJarUpgradeSameSessionAddingGlobal.
@Test
public void testKJarUpgradeSameSessionAddingGlobal() throws Exception {
// DROOLS-523
String drl1 = "package org.drools.compiler\n" + "global java.lang.String foo\n" + "rule R1 when\n" + " $m : Message()\n" + "then\n" + "end\n";
String drl2_1 = "package org.drools.compiler\n" + "rule R2_1 when\n" + " $m : Message( message == \"Hi Universe\" )\n" + "then\n" + "end\n";
String drl2_2 = "package org.drools.compiler\n" + "global java.lang.String foo\n" + "rule R2_2 when\n" + " $m : Message( message == foo )\n" + "then\n" + "end\n";
KieServices ks = KieServices.Factory.get();
// Create an in-memory jar for version 1.0.0
ReleaseId releaseId1 = ks.newReleaseId("org.kie", "test-upgrade", "1.0.0");
KieModule km = createAndDeployJar(ks, releaseId1, drl1, drl2_1);
// Create a session and fire rules
KieContainer kc = ks.newKieContainer(km.getReleaseId());
KieSession ksession = kc.newKieSession();
ksession.insert(new Message("Hello World"));
assertEquals(1, ksession.fireAllRules());
// Create a new jar for version 1.1.0
ReleaseId releaseId2 = ks.newReleaseId("org.kie", "test-upgrade", "1.1.0");
km = createAndDeployJar(ks, releaseId2, drl1, drl2_2);
// try to update the container to version 1.1.0
kc.updateToVersion(releaseId2);
ksession.setGlobal("foo", "Hello World");
// continue working with the session
ksession.insert(new Message("Hello World"));
assertEquals(2, ksession.fireAllRules());
}
use of org.kie.api.builder.KieModule in project drools by kiegroup.
the class IncrementalCompilationTest method testKJarUpgradeSameSession.
@Test
public void testKJarUpgradeSameSession() throws Exception {
String drl1 = "package org.drools.compiler\n" + "rule R1 when\n" + " $m : Message()\n" + "then\n" + "end\n";
String drl2_1 = "package org.drools.compiler\n" + "rule R2_1 when\n" + " $m : Message( message == \"Hi Universe\" )\n" + "then\n" + "end\n";
String drl2_2 = "package org.drools.compiler\n" + "rule R2_2 when\n" + " $m : Message( message == \"Hello World\" )\n" + "then\n" + "end\n";
KieServices ks = KieServices.Factory.get();
// Create an in-memory jar for version 1.0.0
ReleaseId releaseId1 = ks.newReleaseId("org.kie", "test-upgrade", "1.0.0");
KieModule km = createAndDeployJar(ks, releaseId1, drl1, drl2_1);
// Create a session and fire rules
KieContainer kc = ks.newKieContainer(km.getReleaseId());
KieSession ksession = kc.newKieSession();
ksession.insert(new Message("Hello World"));
assertEquals(1, ksession.fireAllRules());
// Create a new jar for version 1.1.0
ReleaseId releaseId2 = ks.newReleaseId("org.kie", "test-upgrade", "1.1.0");
km = createAndDeployJar(ks, releaseId2, drl1, drl2_2);
// try to update the container to version 1.1.0
kc.updateToVersion(releaseId2);
// continue working with the session
ksession.insert(new Message("Hello World"));
assertEquals(3, ksession.fireAllRules());
}
use of org.kie.api.builder.KieModule in project drools by kiegroup.
the class IncrementalCompilationTest method createKJarWIthPackages.
private void createKJarWIthPackages(KieServices ks, ReleaseId releaseId1, String... pkgs) {
String drl1 = "global java.util.List list;\n" + "rule R1 when\n" + " String()\n" + "then\n" + " list.add(\"R1\");" + "end\n";
String drl2 = "global java.util.List list;\n" + "rule R2 when\n" + " String()\n" + "then\n" + " list.add(\"R2\");" + "end\n";
KieModuleModel kproj = ks.newKieModuleModel();
KieBaseModel kieBaseModel1 = kproj.newKieBaseModel("kbase1").setDefault(true);
for (String pkg : pkgs) {
kieBaseModel1.addPackage(pkg);
}
KieSessionModel ksession1 = kieBaseModel1.newKieSessionModel("ksession1").setDefault(true);
KieFileSystem kfs = ks.newKieFileSystem();
kfs.writeKModuleXML(kproj.toXML());
kfs.generateAndWritePomXML(releaseId1);
kfs.write("src/main/resources/pkg1/r1.drl", drl1);
kfs.write("src/main/resources/pkg2/r2.drl", drl2);
KieModule km = deployJar(ks, buildKJar(ks, kfs, releaseId1));
}
use of org.kie.api.builder.KieModule in project drools by kiegroup.
the class IncrementalCompilationTest method testRemoveRuleAndThenFactInStreamMode.
@Test
public void testRemoveRuleAndThenFactInStreamMode() throws Exception {
// DROOLS-731
String header = "package org.some.test\n" + "import org.drools.compiler.FactA\n";
String declaration = "declare FactA\n" + "@role(event)" + "end\n";
String rule2 = "rule R when\n" + " $FactA : FactA ($FactA_field2 : field2 == 105742)\n" + " not FactA($FactA_field2 == 105742)\n" + "then\n" + "end\n";
String file2 = header + declaration + rule2;
KieServices ks = KieServices.Factory.get();
// Create an in-memory jar for version 1.0.0
ReleaseId releaseId1 = ks.newReleaseId("org.kie", "test-upgrade", "1.0.0");
KieModule km = createAndDeployJarInStreamMode(ks, releaseId1, file2);
// Create a session and fire rules
KieContainer kc = ks.newKieContainer(km.getReleaseId());
KieSession ksession = kc.newKieSession();
FactA factA = new FactA(105742);
factA.setField1("entry:" + 105742);
FactHandle fh = ksession.insert(factA);
// Create a new jar for version 1.1.0
ReleaseId releaseId2 = ks.newReleaseId("org.kie", "test-upgrade", "1.1.0");
km = createAndDeployJarInStreamMode(ks, releaseId2);
// try to update the container to version 1.1.0
kc.updateToVersion(releaseId2);
ksession.delete(fh);
}
use of org.kie.api.builder.KieModule in project drools by kiegroup.
the class IncrementalCompilationTest method testKJarUpgradeDRLWithSpace3_usingSingleQuote.
@Test
public void testKJarUpgradeDRLWithSpace3_usingSingleQuote() throws Exception {
// DROOLS-1399 ter (using single quote)
String drl_1 = "package org.drools.compiler\n" + "rule Rs when $s : String() then System.out.println($s); end\n" + "rule Rx when\n" + " $m : Message( message == 'Hello World' )\n" + "then\n" + " System.out.println($m); \n" + "end\n";
String drl_2 = "package org.drools.compiler\n" + // <<- notice rule changed
"rule Rs when $s : String( this == 'x') then System.out.println($s); end\n" + "rule Rx when\n" + // <<- notice the EXTRA SPACE is the an ADDITIONAL change in this other version.
" $m : Message( message == 'Hello World' )\n" + "then\n" + " System.out.println($m); \n" + "end\n";
KieServices ks = KieServices.Factory.get();
ReleaseId releaseId1 = ks.newReleaseId("org.kie", "test-upgrade", "1.0.0");
KieModule km = createAndDeployJar(ks, releaseId1, drl_1);
KieContainer kc = ks.newKieContainer(km.getReleaseId());
KieSession ksession = kc.newKieSession();
final List<String> fired = new ArrayList<>();
ksession.addEventListener(new DefaultAgendaEventListener() {
@Override
public void afterMatchFired(AfterMatchFiredEvent event) {
fired.add(event.getMatch().getRule().getName());
}
});
ksession.insert(new Message("Hello World"));
ksession.insert("x");
assertEquals(2, ksession.fireAllRules());
assertTrue(fired.contains("Rs"));
assertTrue(fired.contains("Rx"));
fired.clear();
ReleaseId releaseId2 = ks.newReleaseId("org.kie", "test-upgrade", "1.1.0");
km = createAndDeployJar(ks, releaseId2, drl_2);
kc.updateToVersion(releaseId2);
// rule Rx is UNchanged and should NOT fire again
// rule Rs is changed and should match again, and fire again.
assertEquals(1, ksession.fireAllRules());
assertTrue(fired.contains("Rs"));
assertFalse(fired.contains("Rx"));
}
Aggregations