use of org.kie.api.time.SessionPseudoClock in project drools by kiegroup.
the class AccumulateTest method testAccumulatesExpireVsCancel.
@Test
public void testAccumulatesExpireVsCancel() throws Exception {
// JBRULES-3201
String drl = "package com.sample;\n" + "\n" + "global java.util.List list; \n" + "" + "declare FactTest\n" + " @role( event ) \n" + "end\n" + " \n" + "rule \"A500 test\"\n" + "when\n" + " accumulate (\n" + " $d : FactTest() over window:time(1m), $tot : count($d); $tot > 0 )\n" + "then\n" + " System.out.println( $tot ); \n" + " list.add( $tot.intValue() ); \n " + "end\n" + "\n";
KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder();
kbuilder.add(ResourceFactory.newByteArrayResource(drl.getBytes()), ResourceType.DRL);
assertFalse(kbuilder.hasErrors());
KieBaseConfiguration kbConf = KnowledgeBaseFactory.newKnowledgeBaseConfiguration();
kbConf.setOption(EventProcessingOption.STREAM);
InternalKnowledgeBase kbase = KnowledgeBaseFactory.newKnowledgeBase(kbConf);
kbase.addPackages(kbuilder.getKnowledgePackages());
KieSessionConfiguration ksConf = KnowledgeBaseFactory.newKnowledgeSessionConfiguration();
ksConf.setOption(ClockTypeOption.get("pseudo"));
KieSession ksession = kbase.newKieSession(ksConf, null);
ArrayList list = new ArrayList();
ksession.setGlobal("list", list);
FactType ft = kbase.getFactType("com.sample", "FactTest");
ksession.insert(ft.newInstance());
ksession.fireAllRules();
ksession.insert(ft.newInstance());
ksession.fireAllRules();
ksession.insert(ft.newInstance());
ksession.fireAllRules();
SessionPseudoClock clock = ksession.getSessionClock();
clock.advanceTime(1, TimeUnit.MINUTES);
ksession.fireAllRules();
assertFalse(list.contains(0));
}
use of org.kie.api.time.SessionPseudoClock in project drools by kiegroup.
the class FireUntilHaltAccumulateTest method populateSessionWithStocks.
private void populateSessionWithStocks() {
final SessionPseudoClock clock = statefulSession.getSessionClock();
clock.advanceTime(1, TimeUnit.SECONDS);
statefulSession.insert(stockFactory.createStock("ST1", 0d));
clock.advanceTime(1, TimeUnit.SECONDS);
statefulSession.insert(stockFactory.createStock("ST2", 1d));
clock.advanceTime(1, TimeUnit.SECONDS);
statefulSession.insert(stockFactory.createStock("ST3", 0d));
clock.advanceTime(1, TimeUnit.SECONDS);
statefulSession.insert(stockFactory.createStock("ST4", 0d));
clock.advanceTime(1, TimeUnit.SECONDS);
statefulSession.insert(stockFactory.createStock("ST5", 0d));
clock.advanceTime(1, TimeUnit.SECONDS);
statefulSession.insert(stockFactory.createStock("ST6", 1d));
}
use of org.kie.api.time.SessionPseudoClock in project drools by kiegroup.
the class FirstOrderLogicTest method testForallWithSlidingWindow.
@Test
public void testForallWithSlidingWindow() throws Exception {
final KieSessionConfiguration conf = KnowledgeBaseFactory.newKnowledgeSessionConfiguration();
conf.setOption(ClockTypeOption.get(ClockType.PSEUDO_CLOCK.getId()));
KieBase kbase = loadKnowledgeBase("test_ForallSlidingWindow.drl");
KieSession ksession = createKnowledgeSession(kbase, conf);
final SessionPseudoClock clock = (SessionPseudoClock) ksession.<SessionClock>getSessionClock();
List<String> results = new ArrayList<String>();
ksession.setGlobal("results", results);
// advance time... no events, so forall should fire
clock.advanceTime(60, TimeUnit.SECONDS);
ksession.fireAllRules();
assertEquals(1, results.size());
int seq = 1;
// advance time... there are matching events now, but forall still not fire
ksession.insert(new StockTick(seq++, "RHT", 10, // 60
clock.getCurrentTime()));
clock.advanceTime(5, TimeUnit.SECONDS);
ksession.fireAllRules();
assertEquals(1, results.size());
ksession.insert(new StockTick(seq++, "RHT", 10, // 65
clock.getCurrentTime()));
clock.advanceTime(5, TimeUnit.SECONDS);
ksession.fireAllRules();
assertEquals(1, results.size());
// advance time... there are non-matching events now, so forall de-activates
ksession.insert(new StockTick(seq++, "IBM", 10, // 70
clock.getCurrentTime()));
clock.advanceTime(10, TimeUnit.SECONDS);
ksession.fireAllRules();
assertEquals(1, results.size());
// advance time... there are non-matching events now, so forall is still deactivated
ksession.insert(new StockTick(seq++, "RHT", 10, // 80
clock.getCurrentTime()));
clock.advanceTime(10, TimeUnit.SECONDS);
ksession.fireAllRules();
assertEquals(1, results.size());
// advance time... non-matching event expires now, so forall should fire
ksession.insert(new StockTick(seq++, "RHT", 10, // 90
clock.getCurrentTime()));
clock.advanceTime(10, TimeUnit.SECONDS);
ksession.fireAllRules();
assertEquals(2, results.size());
// advance time... forall still matches and should not fire
ksession.insert(new StockTick(seq++, "RHT", 10, // 100
clock.getCurrentTime()));
clock.advanceTime(10, TimeUnit.SECONDS);
ksession.fireAllRules();
assertEquals(2, results.size());
// advance time... forall still matches and should not fire
clock.advanceTime(60, TimeUnit.SECONDS);
ksession.fireAllRules();
assertEquals(2, results.size());
}
use of org.kie.api.time.SessionPseudoClock in project drools by kiegroup.
the class CepEspTest method testCollectWithWindows.
@Test(timeout = 10000)
public void testCollectWithWindows() throws Exception {
final KieBaseConfiguration kbconf = KnowledgeBaseFactory.newKnowledgeBaseConfiguration();
kbconf.setOption(EventProcessingOption.STREAM);
final KieBase kbase = loadKnowledgeBase(kbconf, "test_CEP_CollectWithWindows.drl");
KieSessionConfiguration ksconf = KnowledgeBaseFactory.newKnowledgeSessionConfiguration();
ksconf.setOption(ClockTypeOption.get(ClockType.PSEUDO_CLOCK.getId()));
KieSession ksession = createKnowledgeSession(kbase, ksconf);
WorkingMemoryFileLogger logger = new WorkingMemoryFileLogger(ksession);
File testTmpDir = new File("target/test-tmp/");
testTmpDir.mkdirs();
logger.setFileName("target/test-tmp/testCollectWithWindows-audit");
List<Number> timeResults = new ArrayList<Number>();
List<Number> lengthResults = new ArrayList<Number>();
ksession.setGlobal("timeResults", timeResults);
ksession.setGlobal("lengthResults", lengthResults);
SessionPseudoClock clock = (SessionPseudoClock) ksession.<SessionClock>getSessionClock();
try {
// First interaction
clock.advanceTime(5, // 5 seconds
TimeUnit.SECONDS);
ksession.insert(new OrderEvent("1", "customer A", 70));
ksession.fireAllRules();
assertEquals(1, timeResults.size());
assertEquals(1, timeResults.get(0).intValue());
assertEquals(1, lengthResults.size());
assertEquals(1, lengthResults.get(0).intValue());
// Second interaction: advance clock and assert new data
clock.advanceTime(10, // 10 seconds
TimeUnit.SECONDS);
ksession.insert(new OrderEvent("2", "customer A", 60));
ksession.fireAllRules();
assertEquals(2, timeResults.size());
assertEquals(2, timeResults.get(1).intValue());
assertEquals(2, lengthResults.size());
assertEquals(2, lengthResults.get(1).intValue());
// Third interaction: advance clock and assert new data
clock.advanceTime(10, // 10 seconds
TimeUnit.SECONDS);
ksession.insert(new OrderEvent("3", "customer A", 50));
ksession.fireAllRules();
assertEquals(3, timeResults.size());
assertEquals(3, timeResults.get(2).intValue());
assertEquals(3, lengthResults.size());
assertEquals(3, lengthResults.get(2).intValue());
// Fourth interaction: advance clock and assert new data
clock.advanceTime(10, // 10 seconds
TimeUnit.SECONDS);
ksession.insert(new OrderEvent("4", "customer A", 25));
ksession.fireAllRules();
// first event should have expired now
assertEquals(4, timeResults.size());
assertEquals(3, timeResults.get(3).intValue());
assertEquals(4, lengthResults.size());
assertEquals(3, lengthResults.get(3).intValue());
// Fifth interaction: advance clock and assert new data
clock.advanceTime(5, // 10 seconds
TimeUnit.SECONDS);
ksession.insert(new OrderEvent("5", "customer A", 70));
ksession.fireAllRules();
assertEquals(5, timeResults.size());
assertEquals(4, timeResults.get(4).intValue());
assertEquals(5, lengthResults.size());
assertEquals(3, lengthResults.get(4).intValue());
} finally {
logger.writeToDisk();
}
}
use of org.kie.api.time.SessionPseudoClock in project drools by kiegroup.
the class CepEspTest method testTemporalQuery.
@Test
public void testTemporalQuery() {
// BZ-967441
String drl = "package org.drools.compiler.integrationtests;\n" + "\n" + "import org.drools.compiler.integrationtests.CepEspTest.TestEvent;\n" + "\n" + "declare TestEvent\n" + " @role( event )\n" + "end\n" + "\n" + "query EventsBeforeNineSeconds\n" + " $event : TestEvent() from entry-point EStream\n" + " $result : TestEvent ( this after [0s, 9s] $event) from entry-point EventStream\n" + "end\n";
KieFileSystem kfs = KieServices.Factory.get().newKieFileSystem();
kfs.write("src/main/resources/querytest.drl", drl);
KieBuilder kbuilder = KieServices.Factory.get().newKieBuilder(kfs);
kbuilder.buildAll();
KieBase kbase = KieServices.Factory.get().newKieContainer(kbuilder.getKieModule().getReleaseId()).getKieBase();
KieSessionConfiguration ksconfig = KnowledgeBaseFactory.newKnowledgeSessionConfiguration();
ksconfig.setOption(ClockTypeOption.get("pseudo"));
KieSession ksession = kbase.newKieSession(ksconfig, null);
SessionPseudoClock clock = ksession.getSessionClock();
EntryPoint ePoint = ksession.getEntryPoint("EStream");
EntryPoint entryPoint = ksession.getEntryPoint("EventStream");
ePoint.insert(new TestEvent("zero"));
entryPoint.insert(new TestEvent("one"));
clock.advanceTime(10, TimeUnit.SECONDS);
entryPoint.insert(new TestEvent("two"));
clock.advanceTime(10, TimeUnit.SECONDS);
entryPoint.insert(new TestEvent("three"));
QueryResults results = ksession.getQueryResults("EventsBeforeNineSeconds");
assertEquals(1, results.size());
ksession.dispose();
}
Aggregations