use of org.kie.api.builder.KieModule in project drools by kiegroup.
the class IncrementalCompilationTest method testEventDeclarationInSeparatedDRL.
@Test
public void testEventDeclarationInSeparatedDRL() throws Exception {
// DROOLS-1241
String drl1 = "import " + SimpleEvent.class.getCanonicalName() + ";\n" + "declare SimpleEvent\n" + " @role( event )\n" + " @timestamp( timestamp )\n" + " @expires( 2d )\n" + "end\n";
String drl2 = "import " + SimpleEvent.class.getCanonicalName() + ";\n" + "global java.util.List list;\n" + "rule R1 when\n" + " $s:SimpleEvent(code==\"MY_CODE\") over window:time( 1s )\n" + "then\n" + " list.add(\"MY_CODE\");\n" + "end\n";
String drl3 = "import " + SimpleEvent.class.getCanonicalName() + ";\n" + "global java.util.List list;\n" + "rule R2 when\n" + " $s:SimpleEvent(code==\"YOUR_CODE\") over window:time( 1s )\n" + "then\n" + " list.add(\"YOUR_CODE\");\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-cep-upgrade", "1.1.1");
KieModule km = deployJar(ks, createKJar(ks, kproj, releaseId1, null, drl1, drl2));
KieContainer kc = ks.newKieContainer(km.getReleaseId());
KieSession ksession = kc.newKieSession();
List<String> list = new ArrayList<String>();
ksession.setGlobal("list", list);
ksession.insert(new SimpleEvent("1", "MY_CODE", 0));
ksession.insert(new SimpleEvent("2", "YOUR_CODE", 0));
ksession.fireAllRules();
assertEquals(1, list.size());
assertEquals("MY_CODE", list.get(0));
list.clear();
ReleaseId releaseId2 = ks.newReleaseId("org.kie", "test-cep-upgrade", "1.1.2");
// the null drl placeholder is used to have the same drl with a different file name
// this causes the removal and readdition of both rules
km = deployJar(ks, createKJar(ks, kproj, releaseId2, null, drl1, drl2, drl3));
Results results = kc.updateToVersion(releaseId2);
assertEquals(0, results.getMessages().size());
ksession.fireAllRules();
assertEquals(1, list.size());
assertEquals("YOUR_CODE", list.get(0));
}
use of org.kie.api.builder.KieModule in project drools by kiegroup.
the class IncrementalCompilationTest method testKeepBuilderConfAfterIncrementalUpdate.
@Test
public void testKeepBuilderConfAfterIncrementalUpdate() throws Exception {
// DROOLS-1282
String drl1 = "import " + DummyEvent.class.getCanonicalName() + "\n" + "rule R1 when\n" + " DummyEvent() @watch(id)\n" + "then end\n";
String drl2 = "import " + DummyEvent.class.getCanonicalName() + "\n" + "rule R1 when\n" + " DummyEvent() @watch(*)\n" + "then end\n";
KieServices ks = KieServices.Factory.get();
KieModuleModel kproj = ks.newKieModuleModel().setConfigurationProperty(PropertySpecificOption.PROPERTY_NAME, PropertySpecificOption.ALWAYS.toString());
ReleaseId releaseId1 = ks.newReleaseId("org.kie", "test-property-reactive-upgrade", "1");
KieModule km = deployJar(ks, createKJar(ks, kproj, releaseId1, null, drl1));
KieContainer container = ks.newKieContainer(releaseId1);
container.newKieSession();
ReleaseId releaseId2 = ks.newReleaseId("org.kie", "test-property-reactive-upgrade", "2");
km = deployJar(ks, createKJar(ks, kproj, releaseId2, null, drl2));
Results results = container.updateToVersion(releaseId2);
assertEquals(0, results.getMessages().size());
}
use of org.kie.api.builder.KieModule in project drools by kiegroup.
the class IncrementalCompilationTest method testUpdateVersionWithKSessionLogger.
@Test
public void testUpdateVersionWithKSessionLogger() {
// DROOLS-790
String drl1 = "import java.util.List\n" + "import java.util.ArrayList\n" + "\n" + "rule \"Test1\"\n" + "\n" + "when\n" + " $a : Integer()\n" + "then\n" + " insert(new ArrayList());\n" + "end\n";
String drl2 = "rule \"Test2\"\n" + "when\n" + " $b : List()\n" + " then\n" + " $b.isEmpty();\n" + "end";
KieServices ks = KieServices.Factory.get();
ReleaseId releaseId1 = ks.newReleaseId("org.kie", "test-upgrade", "1.0.0");
KieModule km = createAndDeployJar(ks, releaseId1, drl1);
KieContainer kc = ks.newKieContainer(km.getReleaseId());
StatelessKieSession statelessKieSession = kc.newStatelessKieSession();
KieRuntimeLogger kieRuntimeLogger = ks.getLoggers().newConsoleLogger(statelessKieSession);
List<Command> cmds = new ArrayList<Command>();
cmds.add(CommandFactory.newInsertElements(new ArrayList()));
FireAllRulesCommand fireAllRulesCommand = (FireAllRulesCommand) CommandFactory.newFireAllRules();
cmds.add(fireAllRulesCommand);
cmds.add(CommandFactory.newGetObjects("returnedObjects"));
BatchExecutionCommand batchExecutionCommand = CommandFactory.newBatchExecution(cmds);
statelessKieSession.execute(batchExecutionCommand);
kieRuntimeLogger.close();
ReleaseId releaseId2 = ks.newReleaseId("org.kie", "test-upgrade", "1.1.0");
km = createAndDeployJar(ks, releaseId2, drl1 + drl2);
kc.updateToVersion(km.getReleaseId());
}
use of org.kie.api.builder.KieModule 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.builder.KieModule 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());
}
Aggregations