use of org.kie.api.runtime.KieContainer in project drools by kiegroup.
the class IncrementalCompilationTest method testIncrementalCompilation.
private void testIncrementalCompilation(ReleaseId releaseId1, ReleaseId releaseId2, boolean useDeclaredType) {
String drl1 = "package org.drools.compiler\n" + "global java.util.List list\n" + "rule R0 when then list.add( \"000\" ); end \n" + "" + "rule R1 when\n" + " $s : String() " + "then\n" + " list.add( \"a\" + $s );" + "end\n";
String drl2 = useDeclaredType ? "package org.drools.compiler\n" + "global java.util.List list\n" + "declare StringWrapper\n" + " s : String\n" + "end\n" + "rule RInit when\n" + " $s : String() \n" + "then\n" + " insert( new StringWrapper( $s ) );" + "end\n" + "rule R2 when\n" + " $s : StringWrapper() \n" + "then\n" + " list.add( \"b\" + $s.getS() );" + "end\n" : "package org.drools.compiler\n" + "global java.util.List list\n" + "rule R2 when\n" + " $s : String() \n" + "then\n" + " list.add( \"b\" + $s );" + "end\n";
KieServices ks = KieServices.Factory.get();
createAndDeployJar(ks, releaseId1, drl1);
KieContainer kc = ks.newKieContainer(releaseId1);
KieSession ksession = kc.newKieSession();
List<String> list = new ArrayList<String>();
ksession.setGlobal("list", list);
ksession.insert("Foo");
ksession.fireAllRules();
assertEquals(2, list.size());
assertTrue(list.containsAll(asList("000", "aFoo")));
list.clear();
createAndDeployJar(ks, releaseId2, drl1, drl2);
Results updateResults = kc.updateToVersion(releaseId2);
assertEquals(0, updateResults.getMessages().size());
ksession.insert("Bar");
ksession.fireAllRules();
assertEquals(3, list.size());
assertTrue(list.containsAll(asList("bBar", "bFoo", "aBar")));
}
use of org.kie.api.runtime.KieContainer in project drools by kiegroup.
the class IncrementalCompilationTest method testAddPackageToKieBaseModel.
@Test
public void testAddPackageToKieBaseModel() throws Exception {
// DROOLS-1287
KieServices ks = KieServices.Factory.get();
ReleaseId releaseId1 = ks.newReleaseId("org.kie", "test-remove-pkg", "1.0");
ReleaseId releaseId2 = ks.newReleaseId("org.kie", "test-remove-pkg", "1.1");
createKJarWIthPackages(ks, releaseId1, "pkg2");
KieContainer container = ks.newKieContainer(releaseId1);
KieSession ksession = container.newKieSession();
List<String> list = new ArrayList<String>();
ksession.setGlobal("list", list);
ksession.insert("test");
ksession.fireAllRules();
assertEquals(1, list.size());
assertTrue(list.containsAll(asList("R2")));
createKJarWIthPackages(ks, releaseId2, "pkg1", "pkg2");
Results results = container.updateToVersion(releaseId2);
assertEquals(0, results.getMessages().size());
ksession = container.newKieSession();
list = new ArrayList<String>();
ksession.setGlobal("list", list);
ksession.insert("test");
ksession.fireAllRules();
assertEquals(2, list.size());
assertTrue(list.containsAll(asList("R1", "R2")));
}
use of org.kie.api.runtime.KieContainer in project drools by kiegroup.
the class IncrementalCompilationTest method testRemoveAndReaddJavaClass.
@Test
public void testRemoveAndReaddJavaClass() {
// DROOLS-1704
String javaSource = "package org.drools.test;\n" + "public class Person { }\n";
KieServices ks = KieServices.Factory.get();
ReleaseId releaseId1 = ks.newReleaseId("org.kie", "test-upgrade", "1.0.0");
ReleaseId releaseId2 = ks.newReleaseId("org.kie", "test-upgrade", "1.1.0");
ReleaseId releaseId3 = ks.newReleaseId("org.kie", "test-upgrade", "1.2.0");
KieFileSystem kfs = ks.newKieFileSystem();
kfs.generateAndWritePomXML(releaseId1);
KieModuleModel kModuleModel = ks.newKieModuleModel();
kfs.writeKModuleXML(kModuleModel.toXML());
kfs.write("src/main/java/org/drools/test/Person.java", javaSource);
ks.newKieBuilder(kfs).buildAll();
KieContainer kContainer = ks.newKieContainer(releaseId1);
try {
Class.forName("org.drools.test.Person", true, kContainer.getClassLoader());
} catch (ClassNotFoundException e) {
throw new RuntimeException(e);
}
kContainer.getKieBase();
kfs = ks.newKieFileSystem();
kfs.generateAndWritePomXML(releaseId2);
kModuleModel = ks.newKieModuleModel();
kfs.writeKModuleXML(kModuleModel.toXML());
ks.newKieBuilder(kfs).buildAll();
kContainer.updateToVersion(releaseId2);
kContainer.getKieBase();
kfs = ks.newKieFileSystem();
kfs.generateAndWritePomXML(releaseId3);
kModuleModel = ks.newKieModuleModel();
kfs.writeKModuleXML(kModuleModel.toXML());
kfs.write("src/main/java/org/drools/test/Person.java", javaSource);
ks.newKieBuilder(kfs).buildAll();
kContainer.updateToVersion(releaseId3);
}
use of org.kie.api.runtime.KieContainer in project drools by kiegroup.
the class IncrementalCompilationTest method testKJarUpgradeDRLWithSpace4_usingSingleQuote.
@Test
public void testKJarUpgradeDRLWithSpace4_usingSingleQuote() throws Exception {
// DROOLS-1399 quater (using single quote)
String drl_1 = "package org.drools.compiler\n" + "rule Rx when\n" + " $m : Message( message == 'Hello World' )\n" + "then\n" + "end\n";
String drl_2 = "package org.drools.compiler\n" + "rule Rx when\n" + // <<- notice the EXTRA SPACE typo was removed
" $m : Message( message == 'Hello World' )\n" + "then\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();
ksession.insert(new Message("Hello World"));
assertEquals(0, ksession.fireAllRules());
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
assertEquals(1, ksession.fireAllRules());
}
use of org.kie.api.runtime.KieContainer in project drools by kiegroup.
the class IncrementalCompilationTest method testRuleRemoval.
@Test
public void testRuleRemoval() throws Exception {
String drl1 = "package org.drools.compiler\n" + "rule R1 when\n" + " $m : Message()\n" + "then\n" + "end\n";
String drl2 = "rule R2 when\n" + " $m : Message( message == \"Hi Universe\" )\n" + "then\n" + "end\n";
String drl3 = "rule R3 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 + drl3);
// Create a session and fire rules
KieContainer kc = ks.newKieContainer(km.getReleaseId());
KiePackage kpkg = kc.getKieBase().getKiePackage("org.drools.compiler");
assertEquals(3, kpkg.getRules().size());
Map<String, Rule> rules = rulestoMap(kpkg.getRules());
assertNotNull(rules.get("R1"));
assertNotNull(rules.get("R2"));
assertNotNull(rules.get("R3"));
RuleTerminalNode rtn1_1 = (RuleTerminalNode) ((KnowledgeBaseImpl) kc.getKieBase()).getReteooBuilder().getTerminalNodes("org.drools.compiler.R1")[0];
RuleTerminalNode rtn2_1 = (RuleTerminalNode) ((KnowledgeBaseImpl) kc.getKieBase()).getReteooBuilder().getTerminalNodes("org.drools.compiler.R2")[0];
RuleTerminalNode rtn3_1 = (RuleTerminalNode) ((KnowledgeBaseImpl) kc.getKieBase()).getReteooBuilder().getTerminalNodes("org.drools.compiler.R3")[0];
// 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 + drl3);
// try to update the container to version 1.1.0
kc.updateToVersion(releaseId2);
KnowledgeBaseImpl rb_2 = ((KnowledgeBaseImpl) kc.getKieBase());
RuleTerminalNode rtn1_2 = (RuleTerminalNode) rb_2.getReteooBuilder().getTerminalNodes("org.drools.compiler.R1")[0];
RuleTerminalNode rtn3_2 = (RuleTerminalNode) rb_2.getReteooBuilder().getTerminalNodes("org.drools.compiler.R3")[0];
assertNull(rb_2.getReteooBuilder().getTerminalNodes("org.drools.compiler.R2"));
assertSame(rtn3_1, rtn3_2);
assertSame(rtn1_1, rtn1_2);
kpkg = kc.getKieBase().getKiePackage("org.drools.compiler");
assertEquals(2, kpkg.getRules().size());
rules = rulestoMap(kpkg.getRules());
assertNotNull(rules.get("R1"));
assertNull(rules.get("R2"));
assertNotNull(rules.get("R3"));
}
Aggregations