Search in sources :

Example 41 with SessionPseudoClock

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));
}
Also used : KieBaseConfiguration(org.kie.api.KieBaseConfiguration) KnowledgeBuilder(org.kie.internal.builder.KnowledgeBuilder) SessionPseudoClock(org.kie.api.time.SessionPseudoClock) ArrayList(java.util.ArrayList) KieSession(org.kie.api.runtime.KieSession) InternalKnowledgeBase(org.drools.core.impl.InternalKnowledgeBase) KieSessionConfiguration(org.kie.api.runtime.KieSessionConfiguration) FactType(org.kie.api.definition.type.FactType) Test(org.junit.Test)

Example 42 with SessionPseudoClock

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));
}
Also used : SessionPseudoClock(org.kie.api.time.SessionPseudoClock)

Example 43 with SessionPseudoClock

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());
}
Also used : StockTick(org.drools.compiler.StockTick) SessionPseudoClock(org.kie.api.time.SessionPseudoClock) KieBase(org.kie.api.KieBase) ArrayList(java.util.ArrayList) KieSession(org.kie.api.runtime.KieSession) SpecialString(org.drools.compiler.SpecialString) KieSessionConfiguration(org.kie.api.runtime.KieSessionConfiguration) Test(org.junit.Test)

Example 44 with SessionPseudoClock

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();
    }
}
Also used : KieBaseConfiguration(org.kie.api.KieBaseConfiguration) WorkingMemoryFileLogger(org.drools.core.audit.WorkingMemoryFileLogger) SessionPseudoClock(org.kie.api.time.SessionPseudoClock) KieBase(org.kie.api.KieBase) OrderEvent(org.drools.compiler.OrderEvent) ArrayList(java.util.ArrayList) KieSession(org.kie.api.runtime.KieSession) File(java.io.File) KieSessionConfiguration(org.kie.api.runtime.KieSessionConfiguration) Test(org.junit.Test)

Example 45 with SessionPseudoClock

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();
}
Also used : KieFileSystem(org.kie.api.builder.KieFileSystem) SessionPseudoClock(org.kie.api.time.SessionPseudoClock) KieBase(org.kie.api.KieBase) NamedEntryPoint(org.drools.core.common.NamedEntryPoint) EntryPoint(org.kie.api.runtime.rule.EntryPoint) KieSession(org.kie.api.runtime.KieSession) KieBuilder(org.kie.api.builder.KieBuilder) KieSessionConfiguration(org.kie.api.runtime.KieSessionConfiguration) QueryResults(org.kie.api.runtime.rule.QueryResults) Test(org.junit.Test)

Aggregations

SessionPseudoClock (org.kie.api.time.SessionPseudoClock)74 Test (org.junit.Test)66 KieSession (org.kie.api.runtime.KieSession)52 KieSessionConfiguration (org.kie.api.runtime.KieSessionConfiguration)39 KieBase (org.kie.api.KieBase)37 ArrayList (java.util.ArrayList)30 KieBaseConfiguration (org.kie.api.KieBaseConfiguration)20 MessageEvent (org.drools.testcoverage.common.model.MessageEvent)15 EntryPoint (org.kie.api.runtime.rule.EntryPoint)15 StockTick (org.drools.compiler.StockTick)14 Message (org.drools.testcoverage.common.model.Message)14 List (java.util.List)13 InternalKnowledgeBase (org.drools.core.impl.InternalKnowledgeBase)13 NamedEntryPoint (org.drools.core.common.NamedEntryPoint)12 KnowledgeBuilder (org.kie.internal.builder.KnowledgeBuilder)11 StockTick (org.drools.modelcompiler.domain.StockTick)10 OrderEvent (org.drools.compiler.OrderEvent)6 QueryResults (org.kie.api.runtime.rule.QueryResults)4 KieHelper (org.kie.internal.utils.KieHelper)4 StockTickInterface (org.drools.compiler.StockTickInterface)3