Search in sources :

Example 71 with SessionPseudoClock

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

the class CepTest method testSlidingWindow.

@Test
public void testSlidingWindow() throws Exception {
    String str = "import " + StockTick.class.getCanonicalName() + ";\n" + "rule R when\n" + "    $a : StockTick( company == \"DROO\" ) over window:length( 2 )\n" + "then\n" + "  System.out.println(\"fired\");\n" + "end\n";
    KieSession ksession = getKieSession(getCepKieModuleModel(), str);
    SessionPseudoClock clock = ksession.getSessionClock();
    clock.advanceTime(1, TimeUnit.SECONDS);
    ksession.insert(new StockTick("DROO"));
    clock.advanceTime(1, TimeUnit.SECONDS);
    ksession.insert(new StockTick("DROO"));
    clock.advanceTime(1, TimeUnit.SECONDS);
    ksession.insert(new StockTick("ACME"));
    clock.advanceTime(1, TimeUnit.SECONDS);
    ksession.insert(new StockTick("DROO"));
    assertEquals(2, 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 72 with SessionPseudoClock

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

the class CepTest method testWithDeclaredEvent.

@Test
public void testWithDeclaredEvent() throws Exception {
    String str = "import " + StockFact.class.getCanonicalName() + ";\n" + "declare StockFact @role( event ) end;\n" + "rule R when\n" + "    $a : StockFact( company == \"DROO\" )\n" + "    $b : StockFact( 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 StockFact("DROO"));
    clock.advanceTime(6, TimeUnit.SECONDS);
    ksession.insert(new StockFact("ACME"));
    assertEquals(1, ksession.fireAllRules());
    clock.advanceTime(4, TimeUnit.SECONDS);
    ksession.insert(new StockFact("ACME"));
    assertEquals(0, ksession.fireAllRules());
}
Also used : StockFact(org.drools.modelcompiler.domain.StockFact) SessionPseudoClock(org.kie.api.time.SessionPseudoClock) KieSession(org.kie.api.runtime.KieSession) Test(org.junit.Test)

Example 73 with SessionPseudoClock

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

the class CepTest method testDeclaredSlidingWindowWith2Arguments.

@Test
public void testDeclaredSlidingWindowWith2Arguments() throws Exception {
    String str = "declare String\n" + "  @role( event )\n" + "end\n" + "declare window DeclaredWindow\n" + "    String( length == 4, this.startsWith(\"D\") ) over window:time( 5s )\n" + "end\n" + "rule R when\n" + "    $a : String() 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 74 with SessionPseudoClock

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

the class CepTest method testDeclaredSlidingWindow.

@Test
public void testDeclaredSlidingWindow() throws Exception {
    String str = "import " + StockTick.class.getCanonicalName() + ";\n" + "declare window DeclaredWindow\n" + "    StockTick( company == \"DROO\" ) over window:time( 5s )\n" + "end\n" + "rule R when\n" + "    $a : StockTick() from window DeclaredWindow\n" + "then\n" + "  System.out.println($a.getCompany());\n" + "end\n";
    KieSession ksession = getKieSession(getCepKieModuleModel(), str);
    SessionPseudoClock clock = ksession.getSessionClock();
    clock.advanceTime(2, TimeUnit.SECONDS);
    ksession.insert(new StockTick("DROO"));
    clock.advanceTime(2, TimeUnit.SECONDS);
    ksession.insert(new StockTick("DROO"));
    clock.advanceTime(2, TimeUnit.SECONDS);
    ksession.insert(new StockTick("ACME"));
    clock.advanceTime(2, TimeUnit.SECONDS);
    ksession.insert(new StockTick("DROO"));
    assertEquals(2, 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