use of org.kie.api.KieServices in project drools by kiegroup.
the class IncrementalCompilationTest method testRetractLogicalAssertedObjectOnRuleRemovalWithSameObject.
@Test
public void testRetractLogicalAssertedObjectOnRuleRemovalWithSameObject() throws Exception {
// DROOLS-951
String drl1 = "rule R1 when\n" + " exists( Integer() )\n" + "then\n" + " insertLogical( \"found\" );" + "end\n";
String drl2 = "package org.drools.compiler\n" + "rule R2 when\n" + " exists( Integer() )\n" + "then\n" + " insertLogical( \"found\" );" + "end\n";
String drl3 = "package org.drools.compiler\n" + "rule R3 when\n" + " exists( Integer() )\n" + "then\n" + " insertLogical( \"found\");" + "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(1, 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(1, 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 testKJarUpgradeDRLWithSpace3.
@Test
public void testKJarUpgradeDRLWithSpace3() throws Exception {
// DROOLS-1399 ter
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"));
}
use of org.kie.api.KieServices in project drools by kiegroup.
the class IncrementalCompilationTest method testUpdateWithNewDrlAndChangeInOldOne.
@Test
public void testUpdateWithNewDrlAndChangeInOldOne() throws Exception {
// BZ-1275378
String drl1 = "package org.kie.test\n" + "global java.util.List list\n" + "rule rule1\n" + "when\n" + "then\n" + "list.add( drools.getRule().getName() );\n" + "end\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<String> list = new ArrayList<String>();
ksession.setGlobal("list", list);
ksession.fireAllRules();
assertEquals(1, list.size());
assertTrue(list.contains("rule1"));
drl1 = "package org.kie.test\n" + "global java.util.List list\n" + "rule rule1\n" + "when\n" + "Object()\n" + "then\n" + "list.add( drools.getRule().getName() );\n" + "end\n";
String drl2 = "package org.kie.test\n" + "global java.util.List list\n" + "rule rule2\n" + "when\n" + "then\n" + "list.add( drools.getRule().getName() );\n" + "end\n";
ReleaseId releaseId2 = ks.newReleaseId("org.kie", "test-upgrade", "1.1.2");
km = createAndDeployJar(ks, releaseId2, drl1, drl2);
kc.updateToVersion(releaseId2);
list = new ArrayList<String>();
ksession.setGlobal("list", list);
ksession.fireAllRules();
assertEquals(1, list.size());
assertTrue(list.contains("rule2"));
}
use of org.kie.api.KieServices in project drools by kiegroup.
the class IncrementalCompilationTest method testAddRuleWithSlidingWindows.
@Test
public void testAddRuleWithSlidingWindows() throws Exception {
// DROOLS-2292
String drl1 = "package org.drools.compiler\n" + "import " + List.class.getCanonicalName() + "\n" + "import " + BooleanEvent.class.getCanonicalName() + "\n" + "rule R1 when\n" + " $e : BooleanEvent(!enabled)\n" + " List(size >= 1) from collect ( BooleanEvent(!enabled) over window:time(1) )\n" + " $toEdit : List() from collect( BooleanEvent(!enabled) over window:time(2) )\n" + "then\n" + " modify( (BooleanEvent)$toEdit.get(0) ){ setEnabled( true ) }\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));
ReleaseId releaseId2 = ks.newReleaseId("org.kie", "test-upgrade", "2.0.0");
deployJar(ks, createKJar(ks, kproj, releaseId2, null, drl1));
KieContainer kc = ks.newKieContainer(releaseId1);
KieSession kieSession = kc.newKieSession();
kieSession.insert(new BooleanEvent());
kieSession.fireAllRules();
kc.updateToVersion(releaseId2);
kieSession.fireAllRules();
KieMarshallers marshallers = ks.getMarshallers();
Marshaller marshaller = marshallers.newMarshaller(kieSession.getKieBase());
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
marshaller.marshall(outputStream, kieSession);
}
use of org.kie.api.KieServices in project drools by kiegroup.
the class IncrementalCompilationTest method testKJarUpgradeDRLWithSpace2.
@Test
public void testKJarUpgradeDRLWithSpace2() throws Exception {
// DROOLS-1399 bis
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 is the only change in this other version.
" $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(1, 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(0, ksession.fireAllRules());
}
Aggregations