use of org.kie.api.runtime.KieSessionConfiguration in project drools by kiegroup.
the class CepEspTest method testUseMapAsEvent.
@Test
public void testUseMapAsEvent() {
// DROOLS-753
String drl = "import java.util.Map\n " + "declare Map \n" + " @role(event)\n" + "end\n" + "rule \"sliding window time map\" \n" + "when \n" + " $m:Map()\n" + " accumulate(Map() over window:time( 1m ); $count:count(); $count>1 )\n" + "then \n" + " System.out.println(\"alarm!!!!\"); \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);
ksession.insert(new HashMap<String, Object>());
ksession.fireAllRules();
}
use of org.kie.api.runtime.KieSessionConfiguration in project drools by kiegroup.
the class CepEspTest method testDelayingNotWithPreEpochClock.
@Test(timeout = 10000)
public void testDelayingNotWithPreEpochClock() throws Exception {
String str = "package org.drools.compiler\n" + "declare A @role(event) symbol : String end\n" + "declare B @role(event) symbol : String end\n" + "rule Setup when\n" + "then\n" + " insert( new A() );\n" + "end\n" + "rule X\n" + "when\n" + " $a : A() and not( B( this after $a ) )\n" + "then\n" + "end\n";
KieBaseConfiguration conf = KnowledgeBaseFactory.newKnowledgeBaseConfiguration();
conf.setOption(EventProcessingOption.STREAM);
KieBase kbase = loadKnowledgeBaseFromString(conf, str);
KieSessionConfiguration ksconf = KnowledgeBaseFactory.newKnowledgeSessionConfiguration();
ksconf.setOption(ClockTypeOption.get(ClockType.PSEUDO_CLOCK.getId()));
KieSession ksession = createKnowledgeSession(kbase, ksconf);
// Getting a pre-epoch date (i.e., before 1970)
Calendar ts = Calendar.getInstance();
ts.set(1900, 1, 1);
// Initializing the clock to that date
SessionPseudoClock clock = ksession.getSessionClock();
clock.advanceTime(ts.getTimeInMillis(), TimeUnit.MILLISECONDS);
// rule X should not be delayed as the delay would be infinite
int rules = ksession.fireAllRules();
assertEquals(2, rules);
}
use of org.kie.api.runtime.KieSessionConfiguration in project drools by kiegroup.
the class CepEspTest method testCancelActivationWithExpiredEvent.
@Test
public void testCancelActivationWithExpiredEvent() {
// RHBRMS-2463
String drl = "import " + MyEvent.class.getCanonicalName() + "\n" + "import " + AtomicInteger.class.getCanonicalName() + "\n" + "declare MyEvent\n" + " @role( event )\n" + " @timestamp( timestamp )\n" + " @expires( 10ms )\n" + "end\n" + "\n" + "rule R\n" + " timer (int: 0 1; start=$startTime, repeat-limit=0 )\n" + " when\n" + " $event: MyEvent ($startTime : timestamp)\n" + " $counter : AtomicInteger(get() > 0)\n" + " then\n" + " System.out.println(\"RG_TEST_TIMER WITH \" + $event + \" AND \" + $counter);\n" + " modify($counter){\n" + " decrementAndGet()\n" + " }\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);
long now = System.currentTimeMillis();
PseudoClockScheduler sessionClock = ksession.getSessionClock();
sessionClock.setStartupTime(now - 10000);
AtomicInteger counter = new AtomicInteger(1);
MyEvent event1 = new MyEvent(now - 8000);
MyEvent event2 = new MyEvent(now - 7000);
MyEvent event3 = new MyEvent(now - 6000);
ksession.insert(counter);
ksession.insert(event1);
ksession.insert(event2);
ksession.insert(event3);
// Nothing Happens
ksession.fireAllRules();
assertEquals(1, counter.get());
sessionClock.advanceTime(10000, TimeUnit.MILLISECONDS);
ksession.fireAllRules();
assertEquals(0, counter.get());
}
use of org.kie.api.runtime.KieSessionConfiguration in project drools by kiegroup.
the class CepEspTest method testExpireLogicallyInsertedEvent.
@Test
public void testExpireLogicallyInsertedEvent() {
// RHBRMS-2515
String drl = "import " + MyEvent.class.getCanonicalName() + "\n" + "declare MyEvent\n" + " @role( event )\n" + " @timestamp( timestamp )\n" + " @expires( 10ms )\n" + "end\n" + "\n" + "rule R when\n" + " $e : MyEvent()\n" + "then\n" + " insertLogical($e.toString());\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);
ksession.insert(new MyEvent(0));
assertEquals(1L, ksession.getFactCount());
ksession.fireAllRules();
assertEquals(2L, ksession.getFactCount());
sessionClock.advanceTime(20, TimeUnit.MILLISECONDS);
ksession.fireAllRules();
assertEquals(0L, ksession.getFactCount());
}
use of org.kie.api.runtime.KieSessionConfiguration in project drools by kiegroup.
the class CepEspTest method testCEPWith2NamedConsAndEagerRule.
@Test
public void testCEPWith2NamedConsAndEagerRule() throws InterruptedException {
String drl = "package org.drools " + "global java.util.List list; " + "declare Msg " + " @role( event ) " + " sender : String @key " + "end " + "rule Init1 " + "when " + " $s : String() " + "then " + " insert( new Msg( $s ) ); " + "end " + "rule Init2 " + "when " + " Msg( 'Alice' ; )\n" + "then " + " insert( 42 ); " + "end " + "rule 'Viol' @Propagation(EAGER) when " + " $trigger : Msg( 'Alice' ; )\n" + " not Msg( 'Bob' ; this after[0, 100ms] $trigger ) do[t1]" + " Integer( ) do[t2]\n" + "then\n" + " list.add( 0 );\n" + "then[t1]\n" + " list.add( 1 );\n" + "then[t2]\n" + " list.add( 2 );\n" + "end\n";
KieSessionConfiguration sessionConfig = KnowledgeBaseFactory.newKnowledgeSessionConfiguration();
sessionConfig.setOption(ClockTypeOption.get(ClockType.PSEUDO_CLOCK.getId()));
KieHelper helper = new KieHelper();
helper.addContent(drl, ResourceType.DRL);
KieSession ksession = helper.build(EventProcessingOption.STREAM).newKieSession(sessionConfig, null);
List<Integer> list = new ArrayList<Integer>();
ksession.setGlobal("list", list);
ksession.insert("Alice");
ksession.fireAllRules();
assertTrue(list.isEmpty());
((PseudoClockScheduler) ksession.getSessionClock()).advanceTime(150, TimeUnit.MILLISECONDS);
ksession.fireAllRules();
System.out.println(list);
assertEquals(3, list.size());
assertTrue(list.contains(0));
assertTrue(list.contains(1));
assertTrue(list.contains(2));
}
Aggregations