use of org.kie.api.runtime.KieSessionConfiguration in project drools by kiegroup.
the class CepEspTest method testTemporalOperatorWithGlobal.
@Test
public void testTemporalOperatorWithGlobal() {
// DROOLS-993
String drl = "import " + SimpleEvent.class.getCanonicalName() + "\n" + "global java.util.List list;\n" + "global " + SimpleEvent.class.getCanonicalName() + " baseEvent;\n" + "\n" + "declare SimpleEvent\n" + " @role( event )\n" + " @timestamp( dateEvt )\n" + "end\n" + "\n" + "rule R \n" + " when\n" + " $e : SimpleEvent( dateEvt before[10s] baseEvent.dateEvt )\n" + " then\n" + " list.add(\"1\");\n" + " end\n " + "";
KieSessionConfiguration sessionConfig = KnowledgeBaseFactory.newKnowledgeSessionConfiguration();
sessionConfig.setOption(ClockTypeOption.get(ClockType.PSEUDO_CLOCK.getId()));
KieHelper helper = new KieHelper();
helper.addContent(drl, ResourceType.DRL);
KieBase kbase = helper.build(EventProcessingOption.STREAM);
KieSession ksession = kbase.newKieSession(sessionConfig, null);
List<String> list = new ArrayList<String>();
ksession.setGlobal("list", list);
ksession.setGlobal("baseEvent", new SimpleEvent("1", 15000L));
SimpleEvent event1 = new SimpleEvent("1", 0L);
ksession.insert(event1);
ksession.fireAllRules();
assertEquals(1, list.size());
}
use of org.kie.api.runtime.KieSessionConfiguration in project drools by kiegroup.
the class CepEspTest method testEventExpirationDuringAccumulate.
@Test(timeout = 10000)
public void testEventExpirationDuringAccumulate() throws Exception {
// DROOLS-70
String str = "package org.drools.integrationtests\n" + "\n" + "import java.util.List;\n" + "\n" + "declare Stock\n" + " @role( event )\n" + " @expires( 1s ) // setting to a large value causes the test to pass\n" + " name : String\n" + " value : Double\n" + "end\n" + "\n" + "rule \"collect events\"\n" + "when\n" + " stocks := List()\n" + " from accumulate( $zeroStock : Stock( value == 0.0 );\n" + " collectList( $zeroStock ) )\n" + "then\n" + " // empty consequence\n" + "end";
KieBaseConfiguration config = KnowledgeBaseFactory.newKnowledgeBaseConfiguration();
config.setOption(EventProcessingOption.STREAM);
KieBase kbase = loadKnowledgeBaseFromString(config, str);
KieSessionConfiguration conf = KnowledgeBaseFactory.newKnowledgeSessionConfiguration();
conf.setOption(ClockTypeOption.get(ClockType.PSEUDO_CLOCK.getId()));
final KieSession ksession = kbase.newKieSession(conf, null);
final StockFactory stockFactory = new StockFactory(kbase);
final ExecutorService executor = Executors.newSingleThreadExecutor();
final Future sessionFuture = executor.submit(new Runnable() {
@Override
public void run() {
ksession.fireUntilHalt();
}
});
try {
for (int iteration = 0; iteration < 100; iteration++) {
this.populateSessionWithStocks(ksession, stockFactory);
}
// let the engine finish its job
Thread.sleep(2000);
} finally {
ksession.halt();
// not to swallow possible exception
sessionFuture.get();
}
}
use of org.kie.api.runtime.KieSessionConfiguration in project drools by kiegroup.
the class CepEspTest method testSerializationWithWindowLengthAndLiaSharing.
@Test
public void testSerializationWithWindowLengthAndLiaSharing() throws Exception {
// DROOLS-953
String drl = "import " + StockTick.class.getCanonicalName() + "\n" + "global java.util.List list\n" + "declare StockTick\n" + " @role( event )\n" + "end\n" + "\n" + "rule ReportLastEvent when\n" + " $e : StockTick() over window:length(1)\n" + "then\n" + " list.add($e.getCompany());\n" + "end\n" + "\n" + "rule ReportEventInserted when\n" + " $e : StockTick()\n" + "then\n" + " System.out.println(\"Event Insert : \" + $e);\n" + "end";
KieSessionConfiguration sessionConfig = KnowledgeBaseFactory.newKnowledgeSessionConfiguration();
sessionConfig.setOption(ClockTypeOption.get(ClockType.PSEUDO_CLOCK.getId()));
KieHelper helper = new KieHelper();
helper.addContent(drl, ResourceType.DRL);
KieBase kbase = helper.build(EventProcessingOption.STREAM);
KieSession ksession = kbase.newKieSession(sessionConfig, null);
PseudoClockScheduler clock = ksession.getSessionClock();
List<String> list = new ArrayList<String>();
ksession.setGlobal("list", list);
ksession.insert(new StockTick(1, "ACME", 50));
ksession.insert(new StockTick(2, "DROO", 50));
ksession.insert(new StockTick(3, "JBPM", 50));
ksession.fireAllRules();
assertEquals(1, list.size());
assertEquals("JBPM", list.get(0));
try {
ksession = SerializationHelper.getSerialisedStatefulKnowledgeSession(ksession, true, false);
} catch (Exception e) {
e.printStackTrace();
fail(e.getMessage());
}
list = new ArrayList<String>();
ksession.setGlobal("list", list);
ksession.fireAllRules();
System.out.println(list);
assertEquals(0, list.size());
}
use of org.kie.api.runtime.KieSessionConfiguration in project drools by kiegroup.
the class CepEspTest method testEventOffsetExpirationOverflow.
@Test
public void testEventOffsetExpirationOverflow() {
// DROOLS-455
String drl = "\n" + "import java.util.*; " + "" + "declare LongLastingEvent \n" + " @role( event )" + " @timestamp( start ) " + " @duration( duration ) " + " start : long " + " duration : long " + "end \n" + "" + "rule Insert " + " when " + " then " + " insert( new LongLastingEvent( 100, Long.MAX_VALUE ) ); " + " end " + " " + " " + "rule Collect \n" + "when\n" + " accumulate( $x: LongLastingEvent() over window:time(1h), $num : count($x) ) \n" + "then " + "end " + "";
KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder();
kbuilder.add(ResourceFactory.newByteArrayResource(drl.getBytes()), ResourceType.DRL);
if (kbuilder.hasErrors()) {
fail(kbuilder.getErrors().toString());
}
KieBaseConfiguration baseConfig = KnowledgeBaseFactory.newKnowledgeBaseConfiguration();
baseConfig.setOption(EventProcessingOption.STREAM);
InternalKnowledgeBase kbase = KnowledgeBaseFactory.newKnowledgeBase(baseConfig);
kbase.addPackages(kbuilder.getKnowledgePackages());
KieSessionConfiguration sessionConfig = KnowledgeBaseFactory.newKnowledgeSessionConfiguration();
sessionConfig.setOption(ClockTypeOption.get(ClockType.PSEUDO_CLOCK.getId()));
// init stateful knowledge session
KieSession ksession = kbase.newKieSession(sessionConfig, null);
SessionPseudoClock clock = (SessionPseudoClock) ksession.<SessionClock>getSessionClock();
// generate the event
ksession.fireAllRules();
// move on..
clock.advanceTime(10, TimeUnit.SECONDS);
ksession.fireAllRules();
// The event should still be there...
assertEquals(1, ksession.getObjects().size());
ksession.dispose();
}
use of org.kie.api.runtime.KieSessionConfiguration in project drools by kiegroup.
the class CepEspTest method testPseudoSchedulerRemoveJobTest.
@Test(timeout = 10000)
public void testPseudoSchedulerRemoveJobTest() {
String str = "import " + CepEspTest.class.getName() + ".A\n";
str += "declare A\n";
str += " @role( event )\n";
str += "end\n";
str += "rule A\n";
str += "when\n";
str += " $a : A()\n";
str += " not A(this after [1s,10s] $a)\n";
str += "then\n";
str += "end";
KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder();
kbuilder.add(ResourceFactory.newReaderResource(new StringReader(str)), ResourceType.DRL);
assertFalse(kbuilder.getErrors().toString(), kbuilder.hasErrors());
KieBaseConfiguration config = KnowledgeBaseFactory.newKnowledgeBaseConfiguration();
config.setOption(EventProcessingOption.STREAM);
KieSessionConfiguration sessionConfig = KnowledgeBaseFactory.newKnowledgeSessionConfiguration();
sessionConfig.setOption(ClockTypeOption.get("pseudo"));
InternalKnowledgeBase knowledgeBase = KnowledgeBaseFactory.newKnowledgeBase(config);
knowledgeBase.addPackages(kbuilder.getKnowledgePackages());
KieSession ksession = knowledgeBase.newKieSession(sessionConfig, KieServices.get().newEnvironment());
PseudoClockScheduler pseudoClock = ksession.<PseudoClockScheduler>getSessionClock();
FactHandle h = ksession.insert(new A());
ksession.retract(h);
}
Aggregations