use of org.drools.core.time.impl.PseudoClockScheduler in project drools by kiegroup.
the class CepEspTest method testExpireLogicalEvent.
@Test
public void testExpireLogicalEvent() {
String drl = "package org.drools; " + "declare Foo " + " @role(event) " + " @expires(10ms) " + "end " + "rule In " + "when " + "then " + " insertLogical( new Foo() ); " + "end ";
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);
ksession.fireAllRules();
((PseudoClockScheduler) ksession.getSessionClock()).advanceTime(1, TimeUnit.SECONDS);
ksession.fireAllRules();
assertEquals(0, ksession.getObjects().size());
assertEquals(0, ((NamedEntryPoint) ksession.getEntryPoint(EntryPointId.DEFAULT.getEntryPointId())).getTruthMaintenanceSystem().getEqualityKeyMap().size());
}
use of org.drools.core.time.impl.PseudoClockScheduler in project drools by kiegroup.
the class CepEspTest method testBeforeOperator.
@Test(timeout = 10000)
public void testBeforeOperator() throws Exception {
// read in the source
KieBaseConfiguration conf = KnowledgeBaseFactory.newKnowledgeBaseConfiguration();
conf.setOption(EventProcessingOption.STREAM);
final KieBase kbase = loadKnowledgeBase(conf, "test_CEP_BeforeOperator.drl");
KieSessionConfiguration sconf = KnowledgeBaseFactory.newKnowledgeSessionConfiguration();
sconf.setOption(ClockTypeOption.get(ClockType.PSEUDO_CLOCK.getId()));
KieSession ksession = createKnowledgeSession(kbase, sconf);
final PseudoClockScheduler clock = (PseudoClockScheduler) ksession.<SessionClock>getSessionClock();
clock.setStartupTime(1000);
List list = new ArrayList();
ksession.setGlobal("list", list);
StockTickInterface tick1 = new StockTick(1, "DROO", 50, System.currentTimeMillis(), 3);
StockTickInterface tick2 = new StockTick(2, "ACME", 10, System.currentTimeMillis(), 3);
StockTickInterface tick3 = new StockTick(3, "ACME", 10, System.currentTimeMillis(), 3);
StockTickInterface tick4 = new StockTick(4, "DROO", 50, System.currentTimeMillis(), 5);
StockTickInterface tick5 = new StockTick(5, "ACME", 10, System.currentTimeMillis(), 5);
StockTickInterface tick6 = new StockTick(6, "ACME", 10, System.currentTimeMillis(), 3);
StockTickInterface tick7 = new StockTick(7, "ACME", 10, System.currentTimeMillis(), 5);
StockTickInterface tick8 = new StockTick(8, "ACME", 10, System.currentTimeMillis(), 3);
ksession.insert(tick1);
clock.advanceTime(4, TimeUnit.MILLISECONDS);
ksession.insert(tick2);
clock.advanceTime(4, TimeUnit.MILLISECONDS);
ksession.insert(tick3);
clock.advanceTime(4, TimeUnit.MILLISECONDS);
ksession.insert(tick4);
ksession.insert(tick5);
clock.advanceTime(1, TimeUnit.MILLISECONDS);
ksession.insert(tick6);
ksession.insert(tick7);
clock.advanceTime(2, TimeUnit.MILLISECONDS);
ksession.insert(tick8);
ksession.fireAllRules();
assertEquals(1, list.size());
StockTick[] stocks = (StockTick[]) list.get(0);
assertSame(tick4, stocks[0]);
assertSame(tick2, stocks[1]);
}
use of org.drools.core.time.impl.PseudoClockScheduler in project drools by kiegroup.
the class CepEspTest method testMetByOperator.
@Test(timeout = 10000)
public void testMetByOperator() throws Exception {
// read in the source
KieBaseConfiguration conf = KnowledgeBaseFactory.newKnowledgeBaseConfiguration();
conf.setOption(EventProcessingOption.STREAM);
final KieBase kbase = loadKnowledgeBase(conf, "test_CEP_MetByOperator.drl");
KieSessionConfiguration sconf = KnowledgeBaseFactory.newKnowledgeSessionConfiguration();
sconf.setOption(ClockTypeOption.get(ClockType.PSEUDO_CLOCK.getId()));
KieSession ksession = createKnowledgeSession(kbase, sconf);
final PseudoClockScheduler clock = (PseudoClockScheduler) ksession.<PseudoClockScheduler>getSessionClock();
clock.setStartupTime(1000);
List list = new ArrayList();
ksession.setGlobal("list", list);
StockTickInterface tick1 = new StockTick(1, "DROO", 50, System.currentTimeMillis(), 3);
StockTickInterface tick2 = new StockTick(2, "ACME", 10, System.currentTimeMillis(), 3);
StockTickInterface tick3 = new StockTick(3, "ACME", 10, System.currentTimeMillis(), 3);
StockTickInterface tick4 = new StockTick(4, "DROO", 50, System.currentTimeMillis(), 5);
StockTickInterface tick5 = new StockTick(5, "ACME", 10, System.currentTimeMillis(), 5);
StockTickInterface tick6 = new StockTick(6, "ACME", 10, System.currentTimeMillis(), 3);
StockTickInterface tick7 = new StockTick(7, "ACME", 10, System.currentTimeMillis(), 5);
StockTickInterface tick8 = new StockTick(8, "ACME", 10, System.currentTimeMillis(), 3);
InternalFactHandle fh1 = (InternalFactHandle) ksession.insert(tick1);
clock.advanceTime(4, TimeUnit.MILLISECONDS);
InternalFactHandle fh2 = (InternalFactHandle) ksession.insert(tick2);
clock.advanceTime(4, TimeUnit.MILLISECONDS);
ksession.insert(tick3);
clock.advanceTime(4, TimeUnit.MILLISECONDS);
ksession.insert(tick4);
ksession.insert(tick5);
clock.advanceTime(1, TimeUnit.MILLISECONDS);
ksession.insert(tick6);
ksession.insert(tick7);
clock.advanceTime(2, TimeUnit.MILLISECONDS);
ksession.insert(tick8);
ksession.fireAllRules();
assertEquals(1, list.size());
StockTick[] stocks = (StockTick[]) list.get(0);
assertSame(tick1, stocks[0]);
assertSame(tick2, stocks[1]);
}
use of org.drools.core.time.impl.PseudoClockScheduler in project drools by kiegroup.
the class CepEspTest method testLeftTupleExpiration.
@Test
public void testLeftTupleExpiration() {
// 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" + " 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);
ksession.insert(new MyEvent(0));
ksession.insert(new MyEvent(15));
ksession.fireAllRules();
assertEquals(0, counter.get());
sessionClock.advanceTime(20, TimeUnit.MILLISECONDS);
ksession.insert(1);
// MyEvent is expired
ksession.fireAllRules();
assertEquals(1, counter.get());
}
use of org.drools.core.time.impl.PseudoClockScheduler in project drools by kiegroup.
the class CepEspTest method testExpirationOnBefore.
@Test
public void testExpirationOnBefore() {
// DROOLS-1227
String drl = "declare String @role( event ) end\n" + "declare Integer @role( event ) end\n" + "\n" + "rule R when\n" + " $s: String()\n" + " $i: Integer(this before[0,10s] $s)\n" + "then\n" + " System.out.println(\"fired\");\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();
ksession.insert(1);
assertEquals(1, ksession.getFactCount());
ksession.fireAllRules();
sessionClock.advanceTime(11, TimeUnit.SECONDS);
ksession.fireAllRules();
assertEquals(0, ksession.getFactCount());
}
Aggregations