Search in sources :

Example 26 with SessionPseudoClock

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

the class CepTest method testExpireEventOnEndTimestamp.

@Test
public void testExpireEventOnEndTimestamp() throws Exception {
    String str = "package org.drools.compiler;\n" + "import " + StockTick.class.getCanonicalName() + ";\n" + "global java.util.List resultsAfter;\n" + "\n" + "rule \"after[60,80]\"\n" + "when\n" + "$a : StockTick( company == \"DROO\" )\n" + "$b : StockTick( company == \"ACME\", this after[60,80] $a )\n" + "then\n" + "       resultsAfter.add( $b );\n" + "end";
    KieSession ksession = getKieSession(getCepKieModuleModel(), str);
    SessionPseudoClock clock = ksession.getSessionClock();
    List<StockTick> resultsAfter = new ArrayList<StockTick>();
    ksession.setGlobal("resultsAfter", resultsAfter);
    // inserting new StockTick with duration 30 at time 0 => rule
    // after[60,80] should fire when ACME lasts at 100-120
    ksession.insert(new StockTick("DROO", 30));
    clock.advanceTime(100, TimeUnit.MILLISECONDS);
    ksession.insert(new StockTick("ACME", 20));
    ksession.fireAllRules();
    assertEquals(1, resultsAfter.size());
}
Also used : StockTick(org.drools.modelcompiler.domain.StockTick) SessionPseudoClock(org.kie.api.time.SessionPseudoClock) ArrayList(java.util.ArrayList) KieSession(org.kie.api.runtime.KieSession) Test(org.junit.Test)

Example 27 with SessionPseudoClock

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

the class CepTest method testDeclaredSlidingWindowOnEventInTypeDeclaration.

@Test
public void testDeclaredSlidingWindowOnEventInTypeDeclaration() throws Exception {
    String str = "declare String\n" + "  @role( event )\n" + "end\n" + "declare window DeclaredWindow\n" + "    String( ) over window:time( 5s )\n" + "end\n" + "rule R when\n" + "    $a : String( this == \"DROO\" ) from window DeclaredWindow\n" + "then\n" + "  System.out.println($a);\n" + "end\n";
    KieSession ksession = getKieSession(getCepKieModuleModel(), str);
    SessionPseudoClock clock = ksession.getSessionClock();
    ksession.insert("ACME");
    ksession.insert("DROO");
    assertEquals(1, ksession.fireAllRules());
}
Also used : SessionPseudoClock(org.kie.api.time.SessionPseudoClock) KieSession(org.kie.api.runtime.KieSession) Test(org.junit.Test)

Example 28 with SessionPseudoClock

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

the class CepTest method testAfterWithEntryPoints.

@Test
public void testAfterWithEntryPoints() throws Exception {
    String str = "import " + StockTick.class.getCanonicalName() + ";" + "rule R when\n" + "    $a : StockTick( company == \"DROO\" ) from entry-point ep1\n" + "    $b : StockTick( company == \"ACME\", this after[5s,8s] $a ) from entry-point ep2\n" + "then\n" + "  System.out.println(\"fired\");\n" + "end\n";
    KieSession ksession = getKieSession(getCepKieModuleModel(), str);
    SessionPseudoClock clock = ksession.getSessionClock();
    ksession.getEntryPoint("ep1").insert(new StockTick("DROO"));
    clock.advanceTime(6, TimeUnit.SECONDS);
    ksession.getEntryPoint("ep1").insert(new StockTick("ACME"));
    assertEquals(0, ksession.fireAllRules());
    clock.advanceTime(1, TimeUnit.SECONDS);
    ksession.getEntryPoint("ep2").insert(new StockTick("ACME"));
    assertEquals(1, ksession.fireAllRules());
    clock.advanceTime(4, TimeUnit.SECONDS);
    ksession.getEntryPoint("ep2").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)

Example 29 with SessionPseudoClock

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

the class OOPathCepTest method testTemporalOperatorFinishedByWithOOPath.

@Test
public void testTemporalOperatorFinishedByWithOOPath() {
    final String drl = "import org.drools.testcoverage.common.model.Message;\n" + "import org.drools.testcoverage.common.model.MessageEvent;\n" + "global java.util.List events\n" + "global java.util.List messages\n" + "\n" + "declare org.drools.testcoverage.common.model.MessageEvent\n" + "  @role( event )\n" + "  @duration( duration )\n" + "end\n" + "rule R when\n" + "  ev1: MessageEvent( /msg[ message == 'Ping' ] )\n" + "  ev2: MessageEvent( $message: /msg[ message == 'Pong' ], this finishedby ev1 )\n" + "then\n" + "  messages.add( $message );\n" + "end\n";
    final KieBase kieBase = KieBaseUtil.getKieBaseAndBuildInstallModuleFromDrl(MODULE_GROUP_ID, kieBaseTestConfiguration, drl);
    final SessionPseudoClock clock = this.initKieSessionWithPseudoClock(kieBase);
    this.insertEventAndAdvanceClock(new MessageEvent(MessageEvent.Type.sent, new Message("Ping"), DEFAULT_DURATION_IN_SECS), clock, 1);
    final Message pongMessage = this.insertEventAndAdvanceClock(new MessageEvent(MessageEvent.Type.sent, new Message("Pong"), DEFAULT_DURATION_IN_SECS), clock, 1);
    this.kieSession.fireAllRules();
    Assertions.assertThat(this.messages).as("The first sequence of events should NOT make the rule fire").isEmpty();
    this.insertEvent(new MessageEvent(MessageEvent.Type.sent, new Message("Ping"), DEFAULT_DURATION_IN_SECS - 1000));
    this.kieSession.fireAllRules();
    Assertions.assertThat(this.messages).as("The last event should make the rule fire").containsExactlyInAnyOrder(pongMessage);
}
Also used : Message(org.drools.testcoverage.common.model.Message) SessionPseudoClock(org.kie.api.time.SessionPseudoClock) KieBase(org.kie.api.KieBase) MessageEvent(org.drools.testcoverage.common.model.MessageEvent) Test(org.junit.Test)

Example 30 with SessionPseudoClock

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

the class OOPathCepTest method populateAndVerifyTimeWindowCase.

private void populateAndVerifyTimeWindowCase(final KieBase kieBase) {
    final KieSessionConfiguration sessionConfiguration = KieSessionUtil.getKieSessionConfigurationWithClock(ClockTypeOption.get("pseudo"), null);
    this.initKieSession(kieBase, sessionConfiguration);
    final SessionPseudoClock clock = this.kieSession.getSessionClock();
    final MessageEvent pingEvent = new MessageEvent(MessageEvent.Type.sent, new Message("Ping"));
    this.kieSession.insert(pingEvent);
    clock.advanceTime(1, TimeUnit.SECONDS);
    final MessageEvent ping2Event = new MessageEvent(MessageEvent.Type.received, new Message("Ping"));
    this.kieSession.insert(ping2Event);
    clock.advanceTime(1, TimeUnit.SECONDS);
    final MessageEvent ping3Event = new MessageEvent(MessageEvent.Type.received, new Message("Ping"));
    this.kieSession.insert(ping3Event);
    clock.advanceTime(1, TimeUnit.SECONDS);
    this.kieSession.fireAllRules();
    Assertions.assertThat(this.events).as("The rule should have fired for 2 events").containsExactlyInAnyOrder(ping2Event, ping3Event);
    this.events.clear();
    final MessageEvent pongEvent = new MessageEvent(MessageEvent.Type.sent, new Message("Pong"));
    this.kieSession.insert(pongEvent);
    clock.advanceTime(1, TimeUnit.SECONDS);
    final MessageEvent ping4Event = new MessageEvent(MessageEvent.Type.received, new Message("Ping"));
    this.kieSession.insert(ping4Event);
    clock.advanceTime(1, TimeUnit.SECONDS);
    this.kieSession.fireAllRules();
    Assertions.assertThat(this.events).as("The rule should have fired for ping event only").contains(ping4Event);
}
Also used : Message(org.drools.testcoverage.common.model.Message) SessionPseudoClock(org.kie.api.time.SessionPseudoClock) MessageEvent(org.drools.testcoverage.common.model.MessageEvent) KieSessionConfiguration(org.kie.api.runtime.KieSessionConfiguration)

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