use of org.kie.api.runtime.KieSessionConfiguration in project drools by kiegroup.
the class CepEspTest method testEventAssertion.
@Test(timeout = 10000)
public void testEventAssertion() throws Exception {
KieBase kbase = loadKnowledgeBase("test_CEP_SimpleEventAssertion.drl");
KieSessionConfiguration conf = KnowledgeBaseFactory.newKnowledgeSessionConfiguration();
conf.setOption(ClockTypeOption.get("pseudo"));
KieSession session = createKnowledgeSession(kbase, conf);
SessionPseudoClock clock = (SessionPseudoClock) session.<SessionClock>getSessionClock();
final List results = new ArrayList();
session.setGlobal("results", results);
StockTickInterface tick1 = new StockTick(1, "DROO", 50, 10000);
StockTickInterface tick2 = new StockTick(2, "ACME", 10, 10010);
StockTickInterface tick3 = new StockTick(3, "ACME", 10, 10100);
StockTickInterface tick4 = new StockTick(4, "DROO", 50, 11000);
InternalFactHandle handle1 = (InternalFactHandle) session.insert(tick1);
clock.advanceTime(10, TimeUnit.SECONDS);
InternalFactHandle handle2 = (InternalFactHandle) session.insert(tick2);
clock.advanceTime(30, TimeUnit.SECONDS);
InternalFactHandle handle3 = (InternalFactHandle) session.insert(tick3);
clock.advanceTime(20, TimeUnit.SECONDS);
InternalFactHandle handle4 = (InternalFactHandle) session.insert(tick4);
clock.advanceTime(10, TimeUnit.SECONDS);
assertNotNull(handle1);
assertNotNull(handle2);
assertNotNull(handle3);
assertNotNull(handle4);
assertTrue(handle1.isEvent());
assertTrue(handle2.isEvent());
assertTrue(handle3.isEvent());
assertTrue(handle4.isEvent());
session.fireAllRules();
assertEquals(2, ((List) session.getGlobal("results")).size());
}
use of org.kie.api.runtime.KieSessionConfiguration in project drools by kiegroup.
the class CepEspTest method testDeleteExpiredEventWithTimestampAndEqualityKey.
@Test
public void testDeleteExpiredEventWithTimestampAndEqualityKey() throws Exception {
// DROOLS-1017
String drl = "import " + StockTick.class.getCanonicalName() + "\n" + "declare StockTick\n" + " @role( event )\n" + " @timestamp( time )\n" + "end\n" + "\n" + "rule \"TestEventReceived\"\n" + "when\n" + " $st1 : StockTick( company == \"ACME\" )\n" + " not ( StockTick( this != $st1, this after[0s, 1s] $st1) )\n" + "then\n" + " delete($st1);\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, EqualityBehaviorOption.EQUALITY);
KieSession ksession = kbase.newKieSession(sessionConfig, null);
PseudoClockScheduler clock = ksession.getSessionClock();
clock.setStartupTime(5000L);
EventFactHandle handle1 = (EventFactHandle) ksession.insert(new StockTick(1, "ACME", 50, 0L));
clock.advanceTime(2, TimeUnit.SECONDS);
ksession.fireAllRules();
assertTrue(handle1.isExpired());
assertFalse(ksession.getFactHandles().contains(handle1));
}
use of org.kie.api.runtime.KieSessionConfiguration in project drools by kiegroup.
the class CepEspTest method testLeftTupleExpirationWithNot.
@Test
public void testLeftTupleExpirationWithNot() {
// RHBRMS-2463
String drl = "import " + MyEvent.class.getCanonicalName() + "\n" + "import " + AtomicInteger.class.getCanonicalName() + "\n" + "global AtomicInteger counter;\n" + "declare MyEvent\n" + " @role( event )\n" + " @timestamp( timestamp )\n" + " @expires( 10ms )\n" + "end\n" + "\n" + "rule R when\n" + " MyEvent()\n" + " Boolean()\n" + " not Integer()\n" + " then\n" + " counter.incrementAndGet();\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 sessionClock = ksession.getSessionClock();
sessionClock.setStartupTime(0);
AtomicInteger counter = new AtomicInteger(0);
ksession.setGlobal("counter", counter);
ksession.insert(true);
FactHandle iFh = ksession.insert(1);
ksession.insert(new MyEvent(0));
ksession.insert(new MyEvent(15));
ksession.fireAllRules();
assertEquals(0, counter.get());
sessionClock.advanceTime(20, TimeUnit.MILLISECONDS);
ksession.delete(iFh);
// MyEvent is expired
ksession.fireAllRules();
assertEquals(1, counter.get());
}
use of org.kie.api.runtime.KieSessionConfiguration in project drools by kiegroup.
the class CepEspTest method testPastEventExipration.
@Test
public void testPastEventExipration() throws InterruptedException {
// DROOLS-257
String drl = "package org.test;\n" + "import org.drools.compiler.StockTick;\n " + "" + "global java.util.List list; \n" + "" + "declare StockTick @role(event) @timestamp( time ) @expires( 200ms ) end \n" + "" + "rule \"slidingTimeCount\"\n" + "when\n" + " accumulate ( $e: StockTick() over window:length(10), $n : count($e) )\n" + "then\n" + " list.add( $n ); \n" + " System.out.println( \"Events in last X seconds: \" + $n );\n" + "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("pseudo"));
// init stateful knowledge session
KieSession ksession = kbase.newKieSession(sessionConfig, null);
SessionPseudoClock clock = ksession.getSessionClock();
ArrayList list = new ArrayList();
ksession.setGlobal("list", list);
long now = 0;
StockTick event1 = new StockTick(1, "XXX", 1.0, now);
StockTick event2 = new StockTick(2, "XXX", 1.0, now + 240);
StockTick event3 = new StockTick(2, "XXX", 1.0, now + 380);
StockTick event4 = new StockTick(2, "XXX", 1.0, now + 500);
ksession.insert(event1);
ksession.insert(event2);
ksession.insert(event3);
ksession.insert(event4);
clock.advanceTime(220, TimeUnit.MILLISECONDS);
ksession.fireAllRules();
clock.advanceTime(400, TimeUnit.MILLISECONDS);
ksession.fireAllRules();
assertEquals(Arrays.asList(3L, 1L), list);
}
use of org.kie.api.runtime.KieSessionConfiguration in project drools by kiegroup.
the class CepEspTest method testExpiredEventWithPendingActivations.
@Test
public void testExpiredEventWithPendingActivations() throws Exception {
String drl = "package org.drools.drools_usage_pZB7GRxZp64;\n" + "\n" + "declare time_Var\n" + " @role( event )\n" + " @expires( 1s )\n" + " value : Long\n" + "end\n" + "\n" + "declare ExpiringEvent_Var\n" + " @role( event )\n" + " @expires( 10s )\n" + " value : Double\n" + "end\n" + "\n" + "declare window ExpiringEvent_Window1 ExpiringEvent_Var() over window:length(1) end\n" + "\n" + "rule \"Expring variable - Init\"\n" + "activation-group \"ExpiringEvent\"\n" + " when\n" + " $t : time_Var($now : Value != null) over window:length(1)\n" + "\n" + " not ExpiringEvent_Var()\n" + "\n" + " then\n" + " System.out.println($now + \" : Init\");\n" + " insert(new ExpiringEvent_Var(0.0));\n" + "end\n" + "\n" + "rule \"Expiring variable - Rule 1\"\n" + "activation-group \"ExpiringEvent\"\n" + " when\n" + " $t : time_Var($now : Value != null) over window:length(1)\n" + "\n" + " ExpiringEvent_Var(this before $t, $previousValue : Value < 1.0) from window ExpiringEvent_Window1\n" + "\n" + " then\n" + " System.out.println($now + \" : Rule 1\");\n" + " insert(new ExpiringEvent_Var(1.0));\n" + "\n" + "end\n" + "\n" + "rule \"Expiring variable - Rule 2\"\n" + "activation-group \"ExpiringEvent\"\n" + " when\n" + " $t : time_Var($now : Value != null) over window:length(1)\n" + "\n" + " ExpiringEvent_Var(this before $t, $previousValue : Value) from window ExpiringEvent_Window1\n" + "\n" + " then\n" + " System.out.println($now + \" : Rule 2\");\n" + " insert(new ExpiringEvent_Var($previousValue));\n" + "\n" + "end";
KieServices kieServices = KieServices.Factory.get();
KieBaseConfiguration kieBaseConf = KieServices.Factory.get().newKieBaseConfiguration();
kieBaseConf.setOption(EventProcessingOption.STREAM);
KieBase kieBase = new KieHelper().addContent(drl, ResourceType.DRL).build(kieBaseConf);
KieSessionConfiguration config = kieServices.newKieSessionConfiguration();
config.setOption(ClockTypeOption.get("pseudo"));
KieSession session = kieBase.newKieSession(config, null);
SessionPseudoClock clock = session.getSessionClock();
FactType time_VarType = kieBase.getFactType("org.drools.drools_usage_pZB7GRxZp64", "time_Var");
clock.advanceTime(1472057509000L, TimeUnit.MILLISECONDS);
Object time_Var = time_VarType.newInstance();
time_VarType.set(time_Var, "value", 1472057509000L);
session.insert(time_Var);
session.fireAllRules();
for (int i = 0; i < 10; i++) {
clock.advanceTime(1, TimeUnit.SECONDS);
Object time_VarP1 = time_VarType.newInstance();
time_VarType.set(time_VarP1, "value", clock.getCurrentTime());
session.insert(time_VarP1);
session.fireAllRules();
}
clock.advanceTime(1, TimeUnit.SECONDS);
session.fireAllRules();
clock.advanceTime(1, TimeUnit.HOURS);
Object time_VarP1 = time_VarType.newInstance();
time_VarType.set(time_VarP1, "value", clock.getCurrentTime());
session.insert(time_VarP1);
session.fireAllRules();
clock.advanceTime(1, TimeUnit.HOURS);
Object time_VarP2 = time_VarType.newInstance();
time_VarType.set(time_VarP2, "value", clock.getCurrentTime());
session.insert(time_VarP2);
session.fireAllRules();
clock.advanceTime(1, TimeUnit.HOURS);
session.fireAllRules();
// actually should be empty..
for (Object o : session.getFactHandles()) {
if (o instanceof EventFactHandle) {
EventFactHandle eventFactHandle = (EventFactHandle) o;
assertFalse(eventFactHandle.isExpired());
}
}
}
Aggregations