use of org.kie.api.builder.model.KieModuleModel 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.model.KieModuleModel in project drools by kiegroup.
the class IncrementalCompilationTest method testRemoveRuleWithNonInitializedPath.
@Test
public void testRemoveRuleWithNonInitializedPath() {
// DROOLS-1177
String drl1 = "import " + MyEvent.class.getCanonicalName() + "\n" + "declare MyEvent @role( event ) end\n" + "rule \"RG_TEST_1\"\n" + " when\n" + " $dummy: MyEvent (id == 1)\n" + " $other: MyEvent (this != $dummy)\n" + " then\n" + " retract($other);\n" + "end\n" + "rule \"RG_TEST_2\"\n" + " when\n" + " $dummy: MyEvent (id == 1)\n" + " then\n" + " retract($dummy);\n" + "end\n";
String drl2 = "import " + MyEvent.class.getCanonicalName() + "\n" + "declare IMyEvent @role( event ) end\n" + "rule \"RG_TEST_2\"\n" + " when\n" + " $dummy: MyEvent (id == 1)\n" + " then\n" + " retract($dummy);\n" + "end\n";
KieServices ks = KieServices.Factory.get();
KieModuleModel kproj = ks.newKieModuleModel();
KieBaseModel kieBaseModel1 = kproj.newKieBaseModel("KBase1").setDefault(true).setEventProcessingMode(EventProcessingOption.STREAM);
KieSessionModel ksession1 = kieBaseModel1.newKieSessionModel("KSession1").setDefault(true).setType(KieSessionModel.KieSessionType.STATEFUL);
ReleaseId releaseId1 = ks.newReleaseId("org.kie", "test-upgrade", "1.0.0");
deployJar(ks, createKJar(ks, kproj, releaseId1, null, drl1));
KieContainer kc = ks.newKieContainer(releaseId1);
KieSession ksession = kc.newKieSession();
ksession.insert(new MyEvent(0));
ReleaseId releaseId2 = ks.newReleaseId("org.kie", "test-upgrade", "1.1.0");
deployJar(ks, createKJar(ks, kproj, releaseId2, null, drl2));
kc.updateToVersion(releaseId2);
}
use of org.kie.api.builder.model.KieModuleModel in project drools by kiegroup.
the class IncrementalCompilationTest method testIncrementalCompilationWithIncludes.
@Test
public void testIncrementalCompilationWithIncludes() throws Exception {
// DROOLS-462
String drl1 = "global java.util.List list\n" + "rule R1 when\n" + " $s : String() " + "then\n" + " list.add( \"a\" + $s );" + "end\n";
String drl2 = "global java.util.List list\n" + "rule R1 when\n" + " $s : String() " + "then\n" + " list.add( \"b\" + $s );" + "end\n";
ReleaseId releaseId = KieServices.Factory.get().newReleaseId("org.test", "test", "1.0.0-SNAPSHOT");
KieServices ks = KieServices.Factory.get();
KieModuleModel kproj = ks.newKieModuleModel();
KieBaseModel kieBaseModel1 = kproj.newKieBaseModel("KBase1").addPackage("org.pkg1");
kieBaseModel1.newKieSessionModel("KSession1");
KieBaseModel kieBaseModel2 = kproj.newKieBaseModel("KBase2").addPackage("org.pkg2").addInclude("KBase1");
kieBaseModel2.newKieSessionModel("KSession2");
KieFileSystem kfs = ks.newKieFileSystem().generateAndWritePomXML(releaseId).write("src/main/resources/KBase1/org/pkg1/r1.drl", drl1).writeKModuleXML(kproj.toXML());
KieBuilder kieBuilder = ks.newKieBuilder(kfs);
kieBuilder.buildAll();
assertEquals(0, kieBuilder.getResults().getMessages().size());
KieContainer kc = ks.newKieContainer(releaseId);
KieSession ksession = kc.newKieSession("KSession2");
List<String> list = new ArrayList<String>();
ksession.setGlobal("list", list);
ksession.insert("Foo");
ksession.fireAllRules();
assertEquals(1, list.size());
assertEquals("aFoo", list.get(0));
list.clear();
kfs.delete("src/main/resources/KBase1/org/pkg1/r1.drl");
kfs.write("src/main/resources/KBase1/org/pkg1/r2.drl", drl2);
IncrementalResults results = ((InternalKieBuilder) kieBuilder).incrementalBuild();
assertEquals(0, results.getAddedMessages().size());
Results updateResults = kc.updateToVersion(releaseId);
assertEquals(0, updateResults.getMessages().size());
ksession.insert("Bar");
ksession.fireAllRules();
assertEquals(2, list.size());
assertTrue(list.containsAll(asList("bBar", "bFoo")));
}
use of org.kie.api.builder.model.KieModuleModel in project drools by kiegroup.
the class IncrementalCompilationTest method testIncrementalCompilationWithSlidingWindow.
@Test
public void testIncrementalCompilationWithSlidingWindow() {
// DROOLS-881
String drl1 = "import " + MyEvent.class.getCanonicalName() + "\n" + "declare MyEvent @role( event ) end\n" + "rule A when\n" + " Number($number : intValue)\n" + " from accumulate( MyEvent($id : id) over window:time(10s), sum($id) )\n" + "then\n" + " System.out.println(\"1. SUM : \" + $number);\n" + "end\n" + "\n" + "rule B when\n" + " Number($number : intValue)\n" + " from accumulate( MyEvent($id : id) over window:time(10s), count($id) )\n" + "then\n" + " System.out.println(\"1. CNT : \" + $number);\n" + "end";
String drl2 = "import " + MyEvent.class.getCanonicalName() + "\n" + "declare MyEvent @role( event ) end\n" + "rule A when\n" + " Number($number : intValue)\n" + " from accumulate( MyEvent($id : id) over window:time(10s), sum($id) )\n" + "then\n" + " System.out.println(\"2. SUM : \" + $number);\n" + "end\n" + "\n" + "rule B when\n" + " Number($number : intValue)\n" + " from accumulate( MyEvent($id : id) over window:time(10s), count($id) )\n" + "then\n" + " System.out.println(\"2. CNT : \" + $number);\n" + "end";
KieServices ks = KieServices.Factory.get();
KieModuleModel kproj = ks.newKieModuleModel();
KieBaseModel kieBaseModel1 = kproj.newKieBaseModel("KBase1").setDefault(true).setEventProcessingMode(EventProcessingOption.STREAM);
KieSessionModel ksession1 = kieBaseModel1.newKieSessionModel("KSession1").setDefault(true).setType(KieSessionModel.KieSessionType.STATEFUL).setClockType(ClockTypeOption.get(ClockType.PSEUDO_CLOCK.getId()));
ReleaseId releaseId1 = ks.newReleaseId("org.kie", "test-upgrade", "1.0.0");
deployJar(ks, createKJar(ks, kproj, releaseId1, null, drl1));
ReleaseId releaseId2 = ks.newReleaseId("org.kie", "test-upgrade", "1.1.0");
deployJar(ks, createKJar(ks, kproj, releaseId2, null, drl2));
KieContainer kc = ks.newKieContainer(releaseId1);
KieSession ksession = kc.newKieSession();
PseudoClockScheduler clock = ksession.getSessionClock();
ksession.insert(new MyEvent(1));
ksession.fireAllRules();
clock.advanceTime(7, TimeUnit.SECONDS);
kc.updateToVersion(releaseId2);
ksession.fireAllRules();
clock.advanceTime(7, TimeUnit.SECONDS);
kc.updateToVersion(releaseId1);
ksession.fireAllRules();
}
use of org.kie.api.builder.model.KieModuleModel in project drools by kiegroup.
the class SeveralKieSessionsTest method prepareKieModule.
/**
* Inserts a new KieModule containing single KieBase and two KieSession
* instances into KieRepository.
*
* @return created module ReleaseId
*/
private ReleaseId prepareKieModule() {
final KieServices ks = KieServices.Factory.get();
final ReleaseId releaseId = ks.newReleaseId("org.drools.compiler", "severalKieSessionsTest", "1.0.0");
KieModuleModel module = ks.newKieModuleModel();
KieBaseModel baseModel = module.newKieBaseModel("defaultKBase");
baseModel.setDefault(true);
baseModel.addPackage("*");
baseModel.newKieSessionModel("defaultKSession").setDefault(true);
KieFileSystem kfs = ks.newKieFileSystem();
kfs.writeKModuleXML(module.toXML());
kfs.generateAndWritePomXML(releaseId);
kfs.write("src/main/resources/" + PACKAGE_PATH + "/" + DRL_FILE_NAME, ResourceFactory.newClassPathResource(DRL_FILE_NAME, this.getClass()));
KieBuilder builder = ks.newKieBuilder(kfs).buildAll();
assertEquals(0, builder.getResults().getMessages().size());
ks.getRepository().addKieModule(builder.getKieModule());
return releaseId;
}
Aggregations