Search in sources :

Example 1 with StockFact

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

the class CepTest method testExpires.

@Test
public void testExpires() throws Exception {
    String str = "package org.drools.compiler;\n" + "import " + StockFact.class.getCanonicalName() + ";\n" + "\n" + "declare StockFact\n" + "    @role( value = event )\n" + "    @expires( value = 2s, policy = TIME_SOFT )\n" + "end\n" + "\n" + "rule \"expiration\"\n" + "when\n" + "   StockFact( company == \"DROO\" )\n" + "then\n" + "end";
    KieSession ksession = getKieSession(getCepKieModuleModel(), str);
    SessionPseudoClock clock = ksession.getSessionClock();
    ksession.insert(new StockFact("DROO"));
    clock.advanceTime(1, TimeUnit.SECONDS);
    ksession.fireAllRules();
    assertEquals(1, ksession.getObjects().size());
    clock.advanceTime(2, TimeUnit.SECONDS);
    ksession.fireAllRules();
    assertEquals(0, ksession.getObjects().size());
}
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 2 with StockFact

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

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

Aggregations

StockFact (org.drools.modelcompiler.domain.StockFact)3 Test (org.junit.Test)3 KieSession (org.kie.api.runtime.KieSession)3 SessionPseudoClock (org.kie.api.time.SessionPseudoClock)3 ArrayList (java.util.ArrayList)1 StockTick (org.drools.modelcompiler.domain.StockTick)1