Search in sources :

Example 26 with StockTick

use of org.drools.modelcompiler.domain.StockTick in project drools by kiegroup.

the class CepTest method testNotAfter.

@Test
public void testNotAfter() throws Exception {
    String str = "import " + StockTick.class.getCanonicalName() + ";" + "rule R when\n" + "    $a : StockTick( company == \"DROO\" )\n" + "    not( 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"));
    clock.advanceTime(10, TimeUnit.SECONDS);
    assertEquals(0, ksession.fireAllRules());
    ksession.insert(new StockTick("DROO"));
    clock.advanceTime(3, TimeUnit.SECONDS);
    ksession.insert(new StockTick("ACME"));
    clock.advanceTime(10, TimeUnit.SECONDS);
    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 27 with StockTick

use of org.drools.modelcompiler.domain.StockTick in project drools by kiegroup.

the class CepTest method testExpireEventOnEndTimestampWithDeclaredEvent.

@Test
public void testExpireEventOnEndTimestampWithDeclaredEvent() throws Exception {
    String str = "package org.drools.compiler;\n" + "import " + StockFact.class.getCanonicalName() + ";\n" + "global java.util.List resultsAfter;\n" + "\n" + "declare StockFact\n" + "    @role( event )\n" + "    @duration( duration )\n" + "end\n" + "\n" + "rule \"after[60,80]\"\n" + "when\n" + "$a : StockFact( company == \"DROO\" )\n" + "$b : StockFact( 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 StockFact("DROO", 30));
    clock.advanceTime(100, TimeUnit.MILLISECONDS);
    ksession.insert(new StockFact("ACME", 20));
    ksession.fireAllRules();
    assertEquals(1, resultsAfter.size());
}
Also used : StockFact(org.drools.modelcompiler.domain.StockFact) 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 28 with StockTick

use of org.drools.modelcompiler.domain.StockTick 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)

Example 29 with StockTick

use of org.drools.modelcompiler.domain.StockTick 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 30 with StockTick

use of org.drools.modelcompiler.domain.StockTick in project drools by kiegroup.

the class AccumulateTest method testAccumulateWithMaxCalendarAndConstraint.

@Test
public void testAccumulateWithMaxCalendarAndConstraint() {
    String str = "import " + Customer.class.getCanonicalName() + ";\n" + "import " + StockTick.class.getCanonicalName() + ";\n" + "rule AccumulateMaxDate\n" + "  dialect \"java\"\n" + "  when\n" + "  $customer : Customer( code == \"RHT\" )\n" + "  $max1 : Number() from accumulate(\n" + "    StockTick( company == $customer.code\n" + "    , $time : dueDate);\n" + "    max($time.getTime().getTime()))\n" + "then\n" + "end\n";
    KieSession ksession = getKieSession(str);
    StockTick st = new StockTick("RHT");
    st.setDueDate(Calendar.getInstance());
    Customer c = new Customer();
    c.setCode("RHT");
    ksession.insert(st);
    ksession.insert(c);
    Assertions.assertThat(ksession.fireAllRules()).isEqualTo(1);
}
Also used : StockTick(org.drools.modelcompiler.domain.StockTick) Customer(org.drools.modelcompiler.domain.Customer) KieSession(org.kie.api.runtime.KieSession) Test(org.junit.Test)

Aggregations

StockTick (org.drools.modelcompiler.domain.StockTick)35 Test (org.junit.Test)35 KieSession (org.kie.api.runtime.KieSession)35 SessionPseudoClock (org.kie.api.time.SessionPseudoClock)29 ArrayList (java.util.ArrayList)6 IOException (java.io.IOException)4 Arrays (java.util.Arrays)4 Collection (java.util.Collection)4 List (java.util.List)4 TimeUnit (java.util.concurrent.TimeUnit)4 Assertions (org.assertj.core.api.Assertions)4 ClockType (org.drools.core.ClockType)4 Global (org.drools.model.Global)4 Model (org.drools.model.Model)4 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)3 Query (org.drools.model.Query)3 Query2Def (org.drools.model.Query2Def)3 Rule (org.drools.model.Rule)3 Variable (org.drools.model.Variable)3 ModelImpl (org.drools.model.impl.ModelImpl)3