use of org.drools.core.time.impl.PseudoClockScheduler 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.drools.core.time.impl.PseudoClockScheduler 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));
}
use of org.drools.core.time.impl.PseudoClockScheduler in project drools by kiegroup.
the class CepEspTest method testSerializationBeforeFireWithWindowLength.
@Test
public void testSerializationBeforeFireWithWindowLength() 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";
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));
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(1, list.size());
assertEquals("JBPM", list.get(0));
}
use of org.drools.core.time.impl.PseudoClockScheduler in project drools by kiegroup.
the class CepEspTest method testSerializationWithWindowLength.
@Test
public void testSerializationWithWindowLength() 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";
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.drools.core.time.impl.PseudoClockScheduler in project drools by kiegroup.
the class CepEspTest method testSubclassWithLongerExpirationThanSuperWithSerialization.
@Test
public void testSubclassWithLongerExpirationThanSuperWithSerialization() throws Exception {
// DROOLS-983
String drl = "import " + SuperClass.class.getCanonicalName() + "\n" + "import " + SubClass.class.getCanonicalName() + "\n" + "\n" + "rule R1 when\n" + " $e : SuperClass()\n" + "then\n" + "end\n" + "rule R2 when\n" + " $e : SubClass()\n" + " not SubClass(this != $e)\n" + "then\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();
EventFactHandle handle1 = (EventFactHandle) ksession.insert(new SubClass());
ksession.fireAllRules();
clock.advanceTime(15, TimeUnit.SECONDS);
ksession.fireAllRules();
assertFalse(handle1.isExpired());
assertEquals(1, ksession.getObjects().size());
try {
ksession = SerializationHelper.getSerialisedStatefulKnowledgeSession(ksession, true, false);
} catch (Exception e) {
e.printStackTrace();
fail(e.getMessage());
}
clock = ksession.getSessionClock();
clock.advanceTime(10, TimeUnit.SECONDS);
ksession.fireAllRules();
assertEquals(0, ksession.getObjects().size());
}
Aggregations