use of org.drools.core.time.impl.PseudoClockScheduler in project drools by kiegroup.
the class ExpirationTest method testBetaLeftExpired2.
@Test
public void testBetaLeftExpired2() {
// DROOLS-1329
String drl = "import " + A.class.getCanonicalName() + "\n" + "import " + B.class.getCanonicalName() + "\n" + "import " + C.class.getCanonicalName() + "\n" + "declare A @role( event ) @expires(31ms) end\n" + "declare B @role( event ) @expires(11ms) end\n" + "declare C @role( event ) @expires(31ms) end\n" + "global java.util.concurrent.atomic.AtomicInteger counter;\n" + "rule R0 when\n" + " $a: A( $Aid: id > 0 )\n" + " $b: B( $Bid: id == $Aid )\n" + " $c: C( id == $Bid )\n" + "then\n" + " System.out.println(\"[\" + $a + \",\" + $b + \",\" + $c + \"]\");" + " 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();
AtomicInteger counter = new AtomicInteger(0);
ksession.setGlobal("counter", counter);
ksession.insert(new A(1));
ksession.insert(new B(1));
sessionClock.advanceTime(20, TimeUnit.MILLISECONDS);
ksession.insert(new C(1));
ksession.fireAllRules();
assertEquals(0, counter.get());
}
use of org.drools.core.time.impl.PseudoClockScheduler in project drools by kiegroup.
the class CepEspTest method test2NotsWithTemporalConstraints.
@Test
public void test2NotsWithTemporalConstraints() {
// BZ-1122738 DROOLS-479
String drl = "import " + SimpleEvent.class.getCanonicalName() + "\n" + "import java.util.Date\n" + "\n" + "declare OtherFact\n" + " @role( event )\n" + "end\n" + "\n" + "declare SimpleEvent\n" + " @role( event )\n" + " @timestamp( dateEvt )\n" + "end\n" + "\n" + "\n" + "rule R\n" + " when\n" + " $e : SimpleEvent()\n" + " not OtherFact( this after[0, 1h] $e )\n" + " not OtherFact( this after[0, 1h] $e )\n" + " then\n" + " $e.setCode(\"code2\");\n" + " end\n " + "";
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()));
final KieSession ksession = kbase.newKieSession(sessionConfig, null);
PseudoClockScheduler clock = ksession.getSessionClock();
clock.setStartupTime(System.currentTimeMillis());
SimpleEvent event = new SimpleEvent("code1");
event.setDateEvt(System.currentTimeMillis() - (2 * 60 * 60 * 1000));
ksession.insert(event);
ksession.fireAllRules();
assertEquals("code2", event.getCode());
}
use of org.drools.core.time.impl.PseudoClockScheduler in project drools by kiegroup.
the class CepEspTest method test2TimersWithNamedCons.
@Test
public void test2TimersWithNamedCons() throws InterruptedException {
String drl = "package org.drools " + "global java.util.List list; " + "declare Msg " + " @role( event ) " + " sender : String @key " + "end " + "rule Init " + "when " + " $s : String() " + "then " + " insert( new Msg( $s ) ); " + "end " + "rule 'Viol' when " + " $trigger : Msg( 'Alice' ; )\n" + " not Msg( 'Bob' ; this after[0, 100ms] $trigger ) do[t1]\n" + " not Msg( 'Charles' ; this after[0, 200ms] $trigger )\n" + "then\n" + " list.add( 0 );\n" + "then[t1]\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);
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();
assertEquals(1, list.size());
assertEquals(1, (int) list.get(0));
}
use of org.drools.core.time.impl.PseudoClockScheduler in project drools by kiegroup.
the class CepEspTest method testExpireUnusedDeclaredTypeClass.
@Test
public void testExpireUnusedDeclaredTypeClass() throws Exception {
// DROOLS-1524
String drl = "rule R when\n" + "then\n" + " System.out.println(\"fired\");\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);
PseudoClockScheduler sessionClock = ksession.getSessionClock();
ksession.insert(new EventWithoutRule());
ksession.fireAllRules();
sessionClock.advanceTime(2, TimeUnit.SECONDS);
ksession.fireAllRules();
assertEquals(0, ksession.getFactCount());
ksession.dispose();
}
use of org.drools.core.time.impl.PseudoClockScheduler in project drools by kiegroup.
the class CepEspTest method checkNoExpiration.
private void checkNoExpiration(String lhs) {
String drl = "import " + SimpleEvent.class.getCanonicalName() + "\n" + "declare SimpleEvent\n" + " @role( event )\n" + " @expires( -1 )\n" + " @timestamp( dateEvt )\n" + "end\n" + "\n" + "rule R when\n" + lhs + "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();
SimpleEvent event1 = new SimpleEvent("1", 0L);
ksession.insert(event1);
ksession.fireAllRules();
// Session should only contain the fact we just inserted.
assertEquals(1, ksession.getFactCount());
clock.advanceTime(60000, TimeUnit.MILLISECONDS);
ksession.fireAllRules();
// We've disabled expiration, so fact should still be in WorkingMemory.
assertEquals(1, ksession.getFactCount());
ksession.dispose();
}
Aggregations