use of org.kie.api.KieServices in project drools by kiegroup.
the class IncrementalCompilationTest method testSplitAfterQuery.
@Test
public void testSplitAfterQuery() throws Exception {
String drl1 = "global java.util.List list; " + "query foo( Integer $i ) " + " $i := Integer( this < 10 ) " + "end\n" + "\n" + "rule r2 when " + " foo( $i; ) " + " Integer( this == 20 ) " + "then " + " System.out.println(\"20 \" + $i);" + " list.add( 20 + $i );\n" + "end\n" + "rule r3 when " + " $i : Integer( this == 1 ) " + "then " + " System.out.println($i);" + " update( kcontext.getKieRuntime().getFactHandle( $i ), $i + 1 );" + "end\n" + "\n";
String drl2 = "global java.util.List list; " + "query foo( Integer $i ) " + " $i := Integer( this < 10 ) " + "end\n" + "\n" + "rule r1 when " + " foo( $i ; ) " + " Integer( this == 10 ) " + "then " + " System.out.println(\"10 \" + $i);" + " list.add( 10 + $i );\n" + "end\n" + "rule r2 when " + " foo( $i; ) " + " Integer( this == 20 ) " + "then " + " System.out.println(\"20 \" + $i);" + " list.add( 20 + $i );\n" + "end\n" + "rule r3 when " + " $i : Integer( this == 1 ) " + "then " + " System.out.println($i);" + " update( kcontext.getKieRuntime().getFactHandle( $i ), $i + 1 );" + "end\n" + "\n";
KieServices ks = KieServices.Factory.get();
ReleaseId releaseId1 = ks.newReleaseId("org.kie", "test-upgrade", "1.1.1");
KieModule km = createAndDeployJar(ks, releaseId1, drl1);
KieContainer kc = ks.newKieContainer(km.getReleaseId());
KieSession ksession = kc.newKieSession();
List<Integer> list = new ArrayList<Integer>();
ksession.setGlobal("list", list);
ksession.insert(1);
ksession.insert(20);
ksession.fireAllRules();
ReleaseId releaseId2 = ks.newReleaseId("org.kie", "test-upgrade", "1.1.2");
km = createAndDeployJar(ks, releaseId2, drl2);
kc.updateToVersion(releaseId2);
ksession.fireAllRules();
assertEquals(2, list.size());
assertEquals(21, (int) list.get(0));
assertEquals(22, (int) list.get(1));
}
use of org.kie.api.KieServices in project drools by kiegroup.
the class IncrementalCompilationTest method testRetractLogicalAssertedObjectOnRuleRemoval.
@Test
public void testRetractLogicalAssertedObjectOnRuleRemoval() throws Exception {
// DROOLS-951
String drl1 = "rule R1 when\n" + " exists( Integer() )\n" + "then\n" + " insertLogical( \"found1\" );" + "end\n";
String drl2 = "package org.drools.compiler\n" + "rule R2 when\n" + " exists( Integer() )\n" + "then\n" + " insertLogical( \"found2\" );" + "end\n";
String drl3 = "package org.drools.compiler\n" + "rule R3 when\n" + " exists( Integer() )\n" + "then\n" + " insertLogical( \"found3\");" + "end\n";
KieServices ks = KieServices.Factory.get();
ReleaseId releaseId1 = ks.newReleaseId("org.kie", "test-upgrade", "1.1.1");
KieModule km = createAndDeployJar(ks, releaseId1, drl1, drl2, drl3);
KieContainer kc = ks.newKieContainer(km.getReleaseId());
KieSession ksession = kc.newKieSession();
ksession.insert(4);
ksession.fireAllRules();
assertEquals(3, ksession.getObjects(new ClassObjectFilter(String.class)).size());
ReleaseId releaseId2 = ks.newReleaseId("org.kie", "test-upgrade", "1.1.2");
km = createAndDeployJar(ks, releaseId2, drl1, drl2);
kc.updateToVersion(releaseId2);
ksession.fireAllRules();
assertEquals(2, ksession.getObjects(new ClassObjectFilter(String.class)).size());
ReleaseId releaseId3 = ks.newReleaseId("org.kie", "test-upgrade", "1.1.3");
km = createAndDeployJar(ks, releaseId3, drl1);
kc.updateToVersion(releaseId3);
ksession.fireAllRules();
assertEquals(1, ksession.getObjects(new ClassObjectFilter(String.class)).size());
ReleaseId releaseId4 = ks.newReleaseId("org.kie", "test-upgrade", "1.1.4");
km = createAndDeployJar(ks, releaseId4);
kc.updateToVersion(releaseId4);
ksession.fireAllRules();
assertEquals(0, ksession.getObjects(new ClassObjectFilter(String.class)).size());
}
use of org.kie.api.KieServices in project drools by kiegroup.
the class IncrementalCompilationTest method testIncrementalCompilationWithNewEvent.
@Test
public void testIncrementalCompilationWithNewEvent() throws Exception {
// DROOLS-1395
String drl1 = "package fr.edf.distribution.brms.common.test\n" + "import " + DummyEvent.class.getCanonicalName() + "\n" + "declare DummyEvent\n" + " @role( event )\n" + " @timestamp( eventTimestamp )\n" + "end\n" + "rule \"RG_TEST_1\"\n" + " when\n" + " $event: DummyEvent ()\n" + " then\n" + " System.out.println(\"RG_TEST_1 fired\");\n" + " retract($event);\n" + "end";
String drl2 = "package fr.edf.distribution.brms.common.test\n" + "import " + DummyEvent.class.getCanonicalName() + "\n" + "import " + OtherDummyEvent.class.getCanonicalName() + "\n" + "declare DummyEvent\n" + " @role( event )\n" + " @timestamp( eventTimestamp )\n" + "end\n" + "declare OtherDummyEvent\n" + " @role( event )\n" + " @timestamp( eventTimestamp )\n" + "end\n" + "rule \"RG_TEST_2\"\n" + " when\n" + " $event: DummyEvent ()\n" + " $other : OtherDummyEvent(id == $event.id, this after $event)\n" + " then\n" + " System.out.println(\"RG_TEST_2 fired\");\n" + " retract($other);\n" + "end\n" + "\n" + "rule \"RG_TEST_1\"\n" + " when\n" + " $event: DummyEvent ()\n" + " then\n" + " System.out.println(\"RG_TEST_1 fired\");\n" + " retract($event);\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).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));
KieContainer kc = ks.newKieContainer(releaseId1);
KieSession kieSession = kc.newKieSession();
DummyEvent evt = new DummyEvent("evt");
kieSession.insert(evt);
assertEquals(1, kieSession.fireAllRules());
ReleaseId releaseId2 = ks.newReleaseId("org.kie", "test-upgrade", "2.0.0");
deployJar(ks, createKJar(ks, kproj, releaseId2, null, drl2));
kc.updateToVersion(releaseId2);
evt = new DummyEvent("evt");
kieSession.insert(evt);
OtherDummyEvent other = new OtherDummyEvent("evt");
kieSession.insert(other);
assertEquals(1, kieSession.fireAllRules());
}
use of org.kie.api.KieServices in project drools by kiegroup.
the class IncrementalCompilationTest method buildKieModule.
private KieModule buildKieModule(String version, String declares, String rules) {
KieServices ks = KieServices.Factory.get();
KieFileSystem kfs = ks.newKieFileSystem();
ReleaseId rid = ks.newReleaseId("org.drools", "kiemodulemodel-example", version);
kfs.generateAndWritePomXML(rid);
KieModuleModel kModuleModel = ks.newKieModuleModel();
kModuleModel.newKieBaseModel("kiemodulemodel").addPackage("kiemodulemodel").addInclude("kiedeclare");
kModuleModel.newKieBaseModel("kiedeclare").addPackage("kiedeclare");
kfs.writeKModuleXML(kModuleModel.toXML());
kfs.write("src/main/resources/kiedeclare/declares.drl", declares);
kfs.write("src/main/resources/kiemodulemodel/rules.drl", rules);
KieBuilder kb = ks.newKieBuilder(kfs).buildAll();
return kb.getKieModule();
}
use of org.kie.api.KieServices in project drools by kiegroup.
the class IncrementalCompilationTest method testIncrementalCompilationWithAmbiguousRedeclares.
@Test
public void testIncrementalCompilationWithAmbiguousRedeclares() {
String drl1 = "package domestic; " + "import foreign.*; " + "declare foreign.Score " + " id : String " + "end ";
String drl2 = "\n" + "package domestic; " + "import foreign.*; " + "declare foreign.Score " + " id : String " + "end\n" + "declare Score " + " value : double " + "end " + "";
KieServices ks = KieServices.Factory.get();
KieFileSystem kfs = ks.newKieFileSystem();
ReleaseId id = ks.newReleaseId("org.test", "foo", "1.0-SNAPSHOT");
KieBuilder kieBuilder = ks.newKieBuilder(kfs);
kfs.generateAndWritePomXML(id);
kfs.write(ks.getResources().newReaderResource(new StringReader(drl1)).setResourceType(ResourceType.DRL).setSourcePath("drl1.drl"));
kieBuilder.buildAll();
KieContainer kc = ks.newKieContainer(id);
KieSession ksession = kc.newKieSession();
ksession.fireAllRules();
kfs.write(ks.getResources().newReaderResource(new StringReader(drl2)).setResourceType(ResourceType.DRL).setSourcePath("drl2.drl"));
IncrementalResults results = ((InternalKieBuilder) kieBuilder).incrementalBuild();
System.out.println(results.getAddedMessages());
assertEquals(0, results.getAddedMessages().size());
Results updateResults = kc.updateToVersion(id);
assertEquals(0, updateResults.getMessages().size());
}
Aggregations