Search in sources :

Example 21 with SessionPseudoClock

use of org.kie.api.time.SessionPseudoClock in project drools by kiegroup.

the class CepEspTest method testEventOffsetExpirationOverflow.

@Test
public void testEventOffsetExpirationOverflow() {
    // DROOLS-455
    String drl = "\n" + "import java.util.*; " + "" + "declare LongLastingEvent \n" + " @role( event )" + " @timestamp( start ) " + " @duration( duration ) " + "      start : long " + "      duration : long " + "end \n" + "" + "rule Insert " + "  when " + "  then " + "      insert( new LongLastingEvent( 100, Long.MAX_VALUE ) ); " + "  end " + " " + " " + "rule Collect \n" + "when\n" + " accumulate( $x: LongLastingEvent() over window:time(1h), $num : count($x) ) \n" + "then " + "end " + "";
    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()));
    // init stateful knowledge session
    KieSession ksession = kbase.newKieSession(sessionConfig, null);
    SessionPseudoClock clock = (SessionPseudoClock) ksession.<SessionClock>getSessionClock();
    // generate the event
    ksession.fireAllRules();
    // move on..
    clock.advanceTime(10, TimeUnit.SECONDS);
    ksession.fireAllRules();
    // The event should still be there...
    assertEquals(1, ksession.getObjects().size());
    ksession.dispose();
}
Also used : KieBaseConfiguration(org.kie.api.KieBaseConfiguration) KnowledgeBuilder(org.kie.internal.builder.KnowledgeBuilder) SessionPseudoClock(org.kie.api.time.SessionPseudoClock) KieSession(org.kie.api.runtime.KieSession) InternalKnowledgeBase(org.drools.core.impl.InternalKnowledgeBase) KieSessionConfiguration(org.kie.api.runtime.KieSessionConfiguration) Test(org.junit.Test)

Example 22 with SessionPseudoClock

use of org.kie.api.time.SessionPseudoClock in project drools by kiegroup.

the class CepEspTest method testAnnotatedEventAssertion.

@Test(timeout = 10000)
public void testAnnotatedEventAssertion() throws Exception {
    KieBase kbase = loadKnowledgeBase("test_CEP_SimpleAnnotatedEventAssertion.drl");
    KieSessionConfiguration conf = KnowledgeBaseFactory.newKnowledgeSessionConfiguration();
    conf.setOption(ClockTypeOption.get("pseudo"));
    KieSession session = createKnowledgeSession(kbase, conf);
    SessionPseudoClock clock = (SessionPseudoClock) session.<SessionClock>getSessionClock();
    final List results = new ArrayList();
    session.setGlobal("results", results);
    StockTickInterface tick1 = new StockTickEvent(1, "DROO", 50, 10000);
    StockTickInterface tick2 = new StockTickEvent(2, "ACME", 10, 10010);
    StockTickInterface tick3 = new StockTickEvent(3, "ACME", 10, 10100);
    StockTickInterface tick4 = new StockTickEvent(4, "DROO", 50, 11000);
    InternalFactHandle handle1 = (InternalFactHandle) session.insert(tick1);
    clock.advanceTime(10, TimeUnit.SECONDS);
    InternalFactHandle handle2 = (InternalFactHandle) session.insert(tick2);
    clock.advanceTime(30, TimeUnit.SECONDS);
    InternalFactHandle handle3 = (InternalFactHandle) session.insert(tick3);
    clock.advanceTime(20, TimeUnit.SECONDS);
    InternalFactHandle handle4 = (InternalFactHandle) session.insert(tick4);
    clock.advanceTime(10, TimeUnit.SECONDS);
    assertNotNull(handle1);
    assertNotNull(handle2);
    assertNotNull(handle3);
    assertNotNull(handle4);
    assertTrue(handle1.isEvent());
    assertTrue(handle2.isEvent());
    assertTrue(handle3.isEvent());
    assertTrue(handle4.isEvent());
    session.fireAllRules();
    assertEquals(2, ((List) session.getGlobal("results")).size());
}
Also used : StockTickInterface(org.drools.compiler.StockTickInterface) StockTickEvent(org.drools.compiler.StockTickEvent) SessionPseudoClock(org.kie.api.time.SessionPseudoClock) KieBase(org.kie.api.KieBase) ArrayList(java.util.ArrayList) KieSession(org.kie.api.runtime.KieSession) ArrayList(java.util.ArrayList) List(java.util.List) InternalFactHandle(org.drools.core.common.InternalFactHandle) KieSessionConfiguration(org.kie.api.runtime.KieSessionConfiguration) Test(org.junit.Test)

Example 23 with SessionPseudoClock

use of org.kie.api.time.SessionPseudoClock in project drools by kiegroup.

the class TimerAndCalendarTest method testTimerRuleAfterCronReloadSession.

@Test
@Ignore("beta4 phreak")
public void testTimerRuleAfterCronReloadSession() throws Exception {
    InternalKnowledgeBase kbase = KnowledgeBaseFactory.newKnowledgeBase();
    KieSession ksession = createSession(kbase);
    // must advance time or it won't save.
    SessionPseudoClock clock = (SessionPseudoClock) ksession.<SessionClock>getSessionClock();
    clock.advanceTime(300, TimeUnit.MILLISECONDS);
    // if we do not call 'ksession.fireAllRules()', this test will run successfully.
    ksession.fireAllRules();
    clock = (SessionPseudoClock) ksession.<SessionClock>getSessionClock();
    clock.advanceTime(300, TimeUnit.MILLISECONDS);
    ksession = disposeAndReloadSession(ksession, kbase);
    clock = (SessionPseudoClock) ksession.<SessionClock>getSessionClock();
    clock.advanceTime(300, TimeUnit.MILLISECONDS);
    // build timer rule, if the rule is fired, the list size will increase every 300ms
    String timerRule = "package org.drools.test\n" + "global java.util.List list \n" + "rule TimerRule \n" + "   timer (cron: * * * * * ?) \n" + "when \n" + "then \n" + "        list.add(list.size()); \n" + " end";
    Resource resource = ResourceFactory.newByteArrayResource(timerRule.getBytes());
    Collection<KiePackage> kpackages = buildKnowledgePackage(resource, ResourceType.DRL);
    kbase.addPackages(kpackages);
    List<Integer> list = Collections.synchronizedList(new ArrayList<Integer>());
    ksession.setGlobal("list", list);
    ksession.setGlobal("list", list);
    clock.advanceTime(10, TimeUnit.MILLISECONDS);
    ksession.fireAllRules();
    clock = (SessionPseudoClock) ksession.<SessionClock>getSessionClock();
    clock.advanceTime(10, TimeUnit.MILLISECONDS);
    ksession = disposeAndReloadSession(ksession, kbase);
    ksession.setGlobal("list", list);
    clock = (SessionPseudoClock) ksession.<SessionClock>getSessionClock();
    clock.advanceTime(10, TimeUnit.MILLISECONDS);
    Assert.assertEquals(1, list.size());
    clock = (SessionPseudoClock) ksession.<SessionClock>getSessionClock();
    clock.advanceTime(3, TimeUnit.SECONDS);
    Assert.assertEquals(4, list.size());
    ksession = disposeAndReloadSession(ksession, kbase);
    ksession.setGlobal("list", list);
    clock = (SessionPseudoClock) ksession.<SessionClock>getSessionClock();
    clock.advanceTime(2, TimeUnit.SECONDS);
    // if the rule is fired, the list size will greater than one.
    Assert.assertEquals(6, list.size());
}
Also used : SessionPseudoClock(org.kie.api.time.SessionPseudoClock) KiePackage(org.kie.api.definition.KiePackage) Resource(org.kie.api.io.Resource) KieSession(org.kie.api.runtime.KieSession) SessionClock(org.kie.api.time.SessionClock) InternalKnowledgeBase(org.drools.core.impl.InternalKnowledgeBase) Ignore(org.junit.Ignore) Test(org.junit.Test)

Example 24 with SessionPseudoClock

use of org.kie.api.time.SessionPseudoClock in project drools by kiegroup.

the class CepTest method testNegatedAfter.

@Test
public void testNegatedAfter() throws Exception {
    String str = "import " + StockTick.class.getCanonicalName() + ";" + "rule R when\n" + "    $a : StockTick( company == \"DROO\" )\n" + "    $b : StockTick( company == \"ACME\", this not after[5s,8s] $a )\n" + "then\n" + "  System.out.println(\"fired\");\n" + "end\n";
    KieSession ksession = getKieSession(getCepKieModuleModel(), str);
    SessionPseudoClock clock = ksession.getSessionClock();
    ksession.insert(new StockTick("DROO"));
    clock.advanceTime(6, TimeUnit.SECONDS);
    ksession.insert(new StockTick("ACME"));
    assertEquals(0, ksession.fireAllRules());
    clock.advanceTime(4, TimeUnit.SECONDS);
    ksession.insert(new StockTick("ACME"));
    assertEquals(1, ksession.fireAllRules());
}
Also used : StockTick(org.drools.modelcompiler.domain.StockTick) SessionPseudoClock(org.kie.api.time.SessionPseudoClock) KieSession(org.kie.api.runtime.KieSession) Test(org.junit.Test)

Example 25 with SessionPseudoClock

use of org.kie.api.time.SessionPseudoClock in project drools by kiegroup.

the class CepTest method testAfter.

@Test
public void testAfter() throws Exception {
    String str = "import " + StockTick.class.getCanonicalName() + ";" + "rule R when\n" + "    $a : StockTick( company == \"DROO\" )\n" + "    $b : StockTick( company == \"ACME\", this after[5s,8s] $a )\n" + "then\n" + "  System.out.println(\"fired\");\n" + "end\n";
    KieSession ksession = getKieSession(getCepKieModuleModel(), str);
    SessionPseudoClock clock = ksession.getSessionClock();
    ksession.insert(new StockTick("DROO"));
    clock.advanceTime(6, TimeUnit.SECONDS);
    ksession.insert(new StockTick("ACME"));
    assertEquals(1, ksession.fireAllRules());
    clock.advanceTime(4, TimeUnit.SECONDS);
    ksession.insert(new StockTick("ACME"));
    assertEquals(0, ksession.fireAllRules());
}
Also used : StockTick(org.drools.modelcompiler.domain.StockTick) SessionPseudoClock(org.kie.api.time.SessionPseudoClock) KieSession(org.kie.api.runtime.KieSession) 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