use of org.kie.api.builder.KieBuilder in project drools by kiegroup.
the class CepEspTest method testUpdateEventThroughEntryPoint.
@Test
public void testUpdateEventThroughEntryPoint() throws Exception {
String drl = "import org.drools.compiler.integrationtests.CepEspTest.TestEvent\n" + "\n" + "declare TestEvent\n" + " @role( event )\n" + " @expires( 4s )\n" + "end\n" + "\n" + "rule \"TestEventReceived\"\n" + " no-loop\n" + " when\n" + " $event : TestEvent ( name != null ) over window:time( 4s ) from entry-point EventStream\n" + " then\n" + " // insert( new Message( $event.getValue().toString() ) );\n" + "end\n";
KieServices ks = KieServices.Factory.get();
KieFileSystem kfs = ks.newKieFileSystem();
KieModuleModel kieModule = ks.newKieModuleModel();
kieModule.newKieBaseModel("KBase").setDefault(true).setEventProcessingMode(EventProcessingOption.STREAM).newKieSessionModel("KSession").setDefault(true);
kfs.writeKModuleXML(kieModule.toXML());
kfs.write("src/main/resources/lifecycle.drl", drl);
KieBuilder builder = ks.newKieBuilder(kfs).buildAll();
assertEquals(0, builder.getResults().getMessages().size());
KieSession kieSession = ks.newKieContainer(ks.getRepository().getDefaultReleaseId()).newKieSession();
EntryPoint entryPoint = kieSession.getEntryPoint("EventStream");
TestEvent event = new TestEvent("testEvent1");
FactHandle handle = entryPoint.insert(event);
TestEvent event2 = new TestEvent("testEvent2");
entryPoint.update(handle, event2);
// make sure the event is in the entry-point
assertFalse(entryPoint.getObjects().contains(event));
assertTrue(entryPoint.getObjects().contains(event2));
assertEquals(entryPoint.getObject(handle), event2);
kieSession.dispose();
}
use of org.kie.api.builder.KieBuilder in project drools by kiegroup.
the class CepEspTest method testTemporalEvaluatorsUsingRawDateFields.
@Test
public void testTemporalEvaluatorsUsingRawDateFields() throws InterruptedException {
// DROOLS-421
String drl = "\n" + "import java.util.*; " + "global List list; " + "declare Delivery " + " effectiveDate : Date " + "end " + "declare Config " + " todate : Date " + "end " + "rule Control " + "when " + " $dpo: Delivery( $eff : effectiveDate ) " + " $gCfg: Config( todate == null || todate after[ 0d ] $eff ) " + "then " + " list.add( 0 ); " + "end " + "rule Init " + "when " + "then " + " Delivery dpo = new Delivery( new Date( 1000 ) ); " + " Config gCfg = new Config(); " + " gCfg.setTodate( new Date( 2000 ) ); " + " " + " insert( dpo ); " + " insert( gCfg ); " + "end " + "";
KieServices ks = KieServices.Factory.get();
KieFileSystem kfs = ks.newKieFileSystem();
kfs.write(ResourceFactory.newByteArrayResource(drl.getBytes()).setTargetPath("rules.drl"));
KieBuilder kbuilder = KieServices.Factory.get().newKieBuilder(kfs);
kbuilder.buildAll();
assertEquals(0, kbuilder.getResults().getMessages().size());
KieSession ksession = ks.newKieContainer(kbuilder.getKieModule().getReleaseId()).newKieSession();
assertNotNull(ksession);
List list = new ArrayList();
ksession.setGlobal("list", list);
ksession.fireAllRules();
assertEquals(1, list.size());
ksession.dispose();
}
use of org.kie.api.builder.KieBuilder in project drools by kiegroup.
the class CepEspTest method testTemporalEvaluatorsUsingSelfDates.
@Test
public void testTemporalEvaluatorsUsingSelfDates() throws InterruptedException {
// DROOLS-421
String drl = "\n" + "import java.util.*; " + "global List list; " + "declare Delivery " + " thisDate : Date " + " thatDate : Date " + "end " + "rule Init when then insert( new Delivery( new Date( 1000 ), new Date( 200 ) ) ); end " + "rule Control " + "when " + " Delivery( thisDate == null || thisDate after[ 0d ] thatDate ) " + "then " + " list.add( 0 ); " + "end " + "";
KieServices ks = KieServices.Factory.get();
KieFileSystem kfs = ks.newKieFileSystem();
kfs.write(ResourceFactory.newByteArrayResource(drl.getBytes()).setTargetPath("rules.drl"));
KieBuilder kbuilder = KieServices.Factory.get().newKieBuilder(kfs);
kbuilder.buildAll();
assertEquals(0, kbuilder.getResults().getMessages().size());
KieSession ksession = ks.newKieContainer(kbuilder.getKieModule().getReleaseId()).newKieSession();
assertNotNull(ksession);
List list = new ArrayList();
ksession.setGlobal("list", list);
ksession.fireAllRules();
assertEquals(1, list.size());
ksession.dispose();
}
use of org.kie.api.builder.KieBuilder in project drools by kiegroup.
the class CepEspTest method testTemporalEvaluatorsUsingRawDateFieldsFromFrom.
@Test
public void testTemporalEvaluatorsUsingRawDateFieldsFromFrom() throws InterruptedException {
// DROOLS-421
String drl = "\n" + "import java.util.*; " + "global List list; " + "declare Delivery " + " effectiveDate : Date " + "end " + "declare Config " + " todate : Date " + "end " + "rule Control " + "when " + " $dpo: Delivery( $eff : effectiveDate ) from new Delivery( new Date( 1000 ) ) " + " $gCfg: Config( todate == null || todate after[ 0d ] $eff ) from new Config( new Date( 2000 ) ) " + "then " + " list.add( 0 ); " + "end " + "";
KieServices ks = KieServices.Factory.get();
KieFileSystem kfs = ks.newKieFileSystem();
kfs.write(ResourceFactory.newByteArrayResource(drl.getBytes()).setTargetPath("rules.drl"));
KieBuilder kbuilder = KieServices.Factory.get().newKieBuilder(kfs);
kbuilder.buildAll();
assertEquals(0, kbuilder.getResults().getMessages().size());
KieSession ksession = ks.newKieContainer(kbuilder.getKieModule().getReleaseId()).newKieSession();
assertNotNull(ksession);
List list = new ArrayList();
ksession.setGlobal("list", list);
ksession.fireAllRules();
assertEquals(1, list.size());
ksession.dispose();
}
use of org.kie.api.builder.KieBuilder in project drools by kiegroup.
the class CepJavaTypeTest method testJavaTypeAnnotatedWithRole_WindowLength.
@Test
public void testJavaTypeAnnotatedWithRole_WindowLength() {
String drl = "package org.drools.compiler.integrationtests\n" + "\n" + "import org.drools.compiler.integrationtests.CepJavaTypeTest.Event;\n" + "\n" + "rule \"CEP Window Length\"\n" + "when\n" + " Event() over window:length (10)\n" + "then\n" + "end\n";
KieServices ks = KieServices.Factory.get();
KieModuleModel module = ks.newKieModuleModel();
KieBaseModel defaultBase = module.newKieBaseModel("defaultKBase").setDefault(true).addPackage("*");
defaultBase.newKieSessionModel("defaultKSession").setDefault(true);
KieFileSystem kfs = ks.newKieFileSystem().write("src/main/resources/r1.drl", drl);
kfs.writeKModuleXML(module.toXML());
KieBuilder builder = ks.newKieBuilder(kfs).buildAll();
assertTrue(builder.getResults().getMessages().isEmpty());
}
Aggregations