Search in sources :

Example 51 with PseudoClockScheduler

use of org.drools.core.time.impl.PseudoClockScheduler in project drools by kiegroup.

the class CepEspTest method testSubclassWithLongerExpirationThanSuper.

@Test
public void testSubclassWithLongerExpirationThanSuper() throws Exception {
    // DROOLS-983
    String drl = "import " + SuperClass.class.getCanonicalName() + "\n" + "import " + SubClass.class.getCanonicalName() + "\n" + "\n" + "rule R1 when\n" + "    $e : SuperClass()\n" + "then\n" + "end\n" + "rule R2 when\n" + "    $e : SubClass()\n" + "    not SubClass(this != $e)\n" + "then\n" + "end";
    KieSessionConfiguration sessionConfig = KnowledgeBaseFactory.newKnowledgeSessionConfiguration();
    sessionConfig.setOption(ClockTypeOption.get(ClockType.PSEUDO_CLOCK.getId()));
    KieHelper helper = new KieHelper();
    helper.addContent(drl, ResourceType.DRL);
    KieBase kbase = helper.build(EventProcessingOption.STREAM);
    KieSession ksession = kbase.newKieSession(sessionConfig, null);
    PseudoClockScheduler clock = ksession.getSessionClock();
    EventFactHandle handle1 = (EventFactHandle) ksession.insert(new SubClass());
    ksession.fireAllRules();
    clock.advanceTime(15, TimeUnit.SECONDS);
    ksession.fireAllRules();
    assertFalse(handle1.isExpired());
    assertEquals(1, ksession.getObjects().size());
    clock.advanceTime(10, TimeUnit.SECONDS);
    ksession.fireAllRules();
    assertTrue(handle1.isExpired());
    assertEquals(0, ksession.getObjects().size());
}
Also used : KieBase(org.kie.api.KieBase) KieHelper(org.kie.internal.utils.KieHelper) EventFactHandle(org.drools.core.common.EventFactHandle) KieSession(org.kie.api.runtime.KieSession) KieSessionConfiguration(org.kie.api.runtime.KieSessionConfiguration) PseudoClockScheduler(org.drools.core.time.impl.PseudoClockScheduler) Test(org.junit.Test)

Example 52 with PseudoClockScheduler

use of org.drools.core.time.impl.PseudoClockScheduler in project drools by kiegroup.

the class CepEspTest method testFireExpiredEventOnInactiveGroup.

@Test
public void testFireExpiredEventOnInactiveGroup() throws Exception {
    // DROOLS-1523
    String DRL = "global java.util.List list;\n" + "declare String  @role(event) @expires( 6d ) end\n" + "declare Integer @role(event) @expires( 3d ) end\n" + "\n" + "rule \"RG_1\"\n" + "    agenda-group \"rf-grp1\"\n" + "    when\n" + "        $event: Integer()\n" + "        not String(this after [1ms, 48h] $event)\n" + "    then\n" + "      System.out.println(\"RG_1 fired\");\n" + "      retract($event);\n" + "      list.add(\"RG_1\");\n" + "end\n" + "\n" + "rule \"RG_2\"\n" + "    agenda-group \"rf-grp1\"\n" + "    when\n" + "        $event: String()\n" + "        not Integer(this after [1ms, 144h] $event)\n" + "    then\n" + "      System.out.println(\"RG_2 fired\");\n" + "      list.add(\"RG_2\");\n" + "end\n";
    KieSessionConfiguration sessionConfig = KnowledgeBaseFactory.newKnowledgeSessionConfiguration();
    sessionConfig.setOption(ClockTypeOption.get(ClockType.PSEUDO_CLOCK.getId()));
    KieHelper helper = new KieHelper();
    helper.addContent(DRL, ResourceType.DRL);
    KieBase kbase = helper.build(EventProcessingOption.STREAM);
    KieSession kieSession = kbase.newKieSession(sessionConfig, null);
    kieSession.addEventListener(new DefaultAgendaEventListener() {

        public void agendaGroupPopped(AgendaGroupPoppedEvent event) {
            if (event.getAgendaGroup().getName().equals("rf-grp0")) {
                event.getKieRuntime().getAgenda().getAgendaGroup("rf-grp1").setFocus();
            }
        }
    });
    List<String> list = new ArrayList<>();
    kieSession.setGlobal("list", list);
    PseudoClockScheduler sessionClock = kieSession.getSessionClock();
    kieSession.insert("DummyEvent");
    // <- OtherDummyEvent
    kieSession.insert(1);
    kieSession.getAgenda().getAgendaGroup("rf-grp0").setFocus();
    // OK nothing happens
    kieSession.fireAllRules();
    assertEquals(2, kieSession.getFactCount());
    sessionClock.advanceTime(145, TimeUnit.HOURS);
    kieSession.getAgenda().getAgendaGroup("rf-grp0").setFocus();
    kieSession.fireAllRules();
    assertEquals("Expiration occured => no more fact in WM", 0, kieSession.getFactCount());
    assertEquals("RG_1 should fire once", 1, list.stream().filter(r -> r.equals("RG_1")).count());
    assertEquals("RG_2 should fire once", 1, list.stream().filter(r -> r.equals("RG_2")).count());
}
Also used : AgendaGroupPoppedEvent(org.kie.api.event.rule.AgendaGroupPoppedEvent) KieBase(org.kie.api.KieBase) ArrayList(java.util.ArrayList) KieHelper(org.kie.internal.utils.KieHelper) DefaultAgendaEventListener(org.kie.api.event.rule.DefaultAgendaEventListener) KieSession(org.kie.api.runtime.KieSession) KieSessionConfiguration(org.kie.api.runtime.KieSessionConfiguration) PseudoClockScheduler(org.drools.core.time.impl.PseudoClockScheduler) Test(org.junit.Test)

Example 53 with PseudoClockScheduler

use of org.drools.core.time.impl.PseudoClockScheduler in project drools by kiegroup.

the class CepEspTest method testExpireEventOnEndTimestamp.

@Test(timeout = 10000)
public void testExpireEventOnEndTimestamp() throws Exception {
    // DROOLS-40
    String str = "package org.drools.compiler;\n" + "\n" + "import org.drools.compiler.StockTick;\n" + "\n" + "global java.util.List resultsAfter;\n" + "\n" + "declare StockTick\n" + "    @role( event )\n" + "    @duration( duration )\n" + "end\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";
    KieBaseConfiguration config = KnowledgeBaseFactory.newKnowledgeBaseConfiguration();
    config.setOption(EventProcessingOption.STREAM);
    KieBase kbase = loadKnowledgeBaseFromString(config, str);
    KieSessionConfiguration conf = KnowledgeBaseFactory.newKnowledgeSessionConfiguration();
    conf.setOption(ClockTypeOption.get(ClockType.PSEUDO_CLOCK.getId()));
    KieSession ksession = kbase.newKieSession(conf, null);
    PseudoClockScheduler clock = (PseudoClockScheduler) 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(1, "DROO", 0, 0, 30));
    clock.advanceTime(100, TimeUnit.MILLISECONDS);
    ksession.insert(new StockTick(2, "ACME", 0, 0, 20));
    ksession.fireAllRules();
    assertEquals(1, resultsAfter.size());
}
Also used : KieBaseConfiguration(org.kie.api.KieBaseConfiguration) StockTick(org.drools.compiler.StockTick) KieBase(org.kie.api.KieBase) ArrayList(java.util.ArrayList) KieSession(org.kie.api.runtime.KieSession) KieSessionConfiguration(org.kie.api.runtime.KieSessionConfiguration) PseudoClockScheduler(org.drools.core.time.impl.PseudoClockScheduler) Test(org.junit.Test)

Example 54 with PseudoClockScheduler

use of org.drools.core.time.impl.PseudoClockScheduler in project drools by kiegroup.

the class CepEspTest method testExpirationOnAfter.

@Test
public void testExpirationOnAfter() {
    // DROOLS-1227
    String drl = "declare String @role( event ) end\n" + "declare Integer @role( event ) end\n" + "\n" + "rule R when\n" + "    $s: String()\n" + "    $i: Integer(this after[0,10s] $s)\n" + "then\n" + "    System.out.println(\"fired\");\n" + "end";
    KieSessionConfiguration sessionConfig = KnowledgeBaseFactory.newKnowledgeSessionConfiguration();
    sessionConfig.setOption(ClockTypeOption.get(ClockType.PSEUDO_CLOCK.getId()));
    KieHelper helper = new KieHelper();
    helper.addContent(drl, ResourceType.DRL);
    KieBase kbase = helper.build(EventProcessingOption.STREAM);
    KieSession ksession = kbase.newKieSession(sessionConfig, null);
    PseudoClockScheduler sessionClock = ksession.getSessionClock();
    ksession.insert("test");
    ksession.insert(1);
    assertEquals(2, ksession.getFactCount());
    ksession.fireAllRules();
    sessionClock.advanceTime(11, TimeUnit.SECONDS);
    ksession.fireAllRules();
    assertEquals(0, ksession.getFactCount());
}
Also used : KieBase(org.kie.api.KieBase) KieHelper(org.kie.internal.utils.KieHelper) KieSession(org.kie.api.runtime.KieSession) KieSessionConfiguration(org.kie.api.runtime.KieSessionConfiguration) PseudoClockScheduler(org.drools.core.time.impl.PseudoClockScheduler) Test(org.junit.Test)

Example 55 with PseudoClockScheduler

use of org.drools.core.time.impl.PseudoClockScheduler in project drools by kiegroup.

the class CepEspTest method testCEPNamedCons.

@Test
public void testCEPNamedCons() throws InterruptedException {
    String drl = "package org.drools " + "global java.util.List list; " + "declare  Msg " + "    @role( event )" + "    sender : String  @key " + "end " + "rule Init " + "when " + "  $s : String() " + "then " + "  System.out.println( 'Msg ' + $s ); " + "  insert( new Msg( $s ) ); " + "end " + "rule 'Expect_Test_Rule Fulfill' " + "when " + "    $trigger : Msg( 'John' ; ) " + "    Msg( 'Peter' ; this after[0,100000ms] $trigger ) " + "    do[fulfill] " + "then " + "  System.out.println( 'Expectation fulfilled' ); " + "  list.add( 1 ); " + "then[fulfill] " + "  System.out.println( 'insert fulf fact' ); " + "  list.add( 2 ); " + "end " + "rule 'Expect_Test_Rule Violation' " + "when " + "    $trigger : Msg( 'John' ; ) do[asap]" + "    not Msg( 'Peter' ; this after[0,100000ms] $trigger ) " + "    do[viol]  \n" + "then " + "  System.out.println( 'Expectation violated' ); " + "  list.add( -1 ); " + "then[viol] " + "  System.out.println( 'insert viol fact' ); " + "  list.add( -2 ); " + "then[asap] " + "  System.out.println( 'Did it anyway' ); " + "  list.add( 0 ); " + "end " + "";
    KieSessionConfiguration sessionConfig = KnowledgeBaseFactory.newKnowledgeSessionConfiguration();
    sessionConfig.setOption(ClockTypeOption.get(ClockType.PSEUDO_CLOCK.getId()));
    KieHelper helper = new KieHelper();
    helper.addContent(drl, ResourceType.DRL);
    KieSession ksession = helper.build(EventProcessingOption.STREAM).newKieSession(sessionConfig, null);
    List list = new ArrayList();
    ksession.setGlobal("list", list);
    ksession.insert("John");
    ksession.fireAllRules();
    System.out.println("--------------------");
    ((PseudoClockScheduler) ksession.getSessionClock()).advanceTime(10, TimeUnit.MILLISECONDS);
    ksession.insert("Peter");
    ksession.fireAllRules();
    System.out.println(list);
    assertTrue(list.contains(0));
    assertTrue(list.contains(1));
    assertTrue(list.contains(2));
    assertFalse(list.contains(-1));
    assertFalse(list.contains(-2));
}
Also used : ArrayList(java.util.ArrayList) KieHelper(org.kie.internal.utils.KieHelper) KieSession(org.kie.api.runtime.KieSession) ArrayList(java.util.ArrayList) List(java.util.List) KieSessionConfiguration(org.kie.api.runtime.KieSessionConfiguration) PseudoClockScheduler(org.drools.core.time.impl.PseudoClockScheduler) Test(org.junit.Test)

Aggregations

PseudoClockScheduler (org.drools.core.time.impl.PseudoClockScheduler)81 KieSession (org.kie.api.runtime.KieSession)76 Test (org.junit.Test)75 KieSessionConfiguration (org.kie.api.runtime.KieSessionConfiguration)72 KieBase (org.kie.api.KieBase)60 ArrayList (java.util.ArrayList)39 KieHelper (org.kie.internal.utils.KieHelper)39 List (java.util.List)29 Date (java.util.Date)19 Arrays.asList (java.util.Arrays.asList)18 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)18 KieBaseConfiguration (org.kie.api.KieBaseConfiguration)14 StockTick (org.drools.compiler.StockTick)13 DateFormat (java.text.DateFormat)9 SimpleDateFormat (java.text.SimpleDateFormat)9 Calendar (org.kie.api.time.Calendar)8 StockTickInterface (org.drools.compiler.StockTickInterface)7 EntryPoint (org.kie.api.runtime.rule.EntryPoint)7 IOException (java.io.IOException)5 FactA (org.drools.compiler.FactA)5