Search in sources :

Example 61 with PseudoClockScheduler

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

the class CepEspTest method testExpiredEventModification.

@Test
public void testExpiredEventModification() {
    // BZ-1082990
    String drl = "import " + SimpleEvent.class.getCanonicalName() + "\n" + "import java.util.Date\n" + "\n" + "declare OtherFact\n" + "    @role( event )\n" + "end\n" + "\n" + "declare SimpleEvent\n" + "    @role( event )\n" + "    @expires( 1h )\n" + "    @timestamp( dateEvt )\n" + "end\n" + "\n" + "\n" + "rule R no-loop\n" + "    when\n" + "        $e : SimpleEvent()\n" + "        not OtherFact( this after[1ms, 1h] $e )\n" + "    then\n" + "        modify($e) {setCode(\"code2\")};\n" + "    end\n " + "";
    KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder();
    kbuilder.add(ResourceFactory.newByteArrayResource(drl.getBytes()), ResourceType.DRL);
    if (kbuilder.hasErrors()) {
        fail(kbuilder.getErrors().toString());
    }
    KieBaseConfiguration baseConfig = KnowledgeBaseFactory.newKnowledgeBaseConfiguration();
    baseConfig.setOption(EventProcessingOption.STREAM);
    InternalKnowledgeBase kbase = KnowledgeBaseFactory.newKnowledgeBase(baseConfig);
    kbase.addPackages(kbuilder.getKnowledgePackages());
    KieSessionConfiguration sessionConfig = KnowledgeBaseFactory.newKnowledgeSessionConfiguration();
    sessionConfig.setOption(ClockTypeOption.get(ClockType.PSEUDO_CLOCK.getId()));
    final KieSession ksession = kbase.newKieSession(sessionConfig, null);
    PseudoClockScheduler clock = ksession.getSessionClock();
    clock.setStartupTime(System.currentTimeMillis());
    SimpleEvent event = new SimpleEvent("code1");
    event.setDateEvt(System.currentTimeMillis() - (2 * 60 * 60 * 1000));
    ksession.insert(event);
    ksession.fireAllRules();
}
Also used : KieBaseConfiguration(org.kie.api.KieBaseConfiguration) KnowledgeBuilder(org.kie.internal.builder.KnowledgeBuilder) KieSession(org.kie.api.runtime.KieSession) InternalKnowledgeBase(org.drools.core.impl.InternalKnowledgeBase) KieSessionConfiguration(org.kie.api.runtime.KieSessionConfiguration) PseudoClockScheduler(org.drools.core.time.impl.PseudoClockScheduler) Test(org.junit.Test)

Example 62 with PseudoClockScheduler

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

the class IncrementalCompilationTest method testChangeWindowTime.

@Test
public void testChangeWindowTime() {
    // DROOLS-853
    String drl1 = "import " + MyEvent.class.getCanonicalName() + "\n" + "global java.util.concurrent.atomic.AtomicInteger result\n" + "declare MyEvent @expires(5m) @role( event ) end\n" + "rule A when\n" + "    accumulate( $e : MyEvent() over window:time(10s), $result : count($e) )\n" + "then" + "    System.out.println(\"Result-1: \" + $result);\n" + "    result.set( $result.intValue() );\n" + "end";
    String drl2 = "import " + MyEvent.class.getCanonicalName() + "\n" + "global java.util.concurrent.atomic.AtomicInteger result\n" + "declare MyEvent @expires(5m) @role( event ) end\n" + "rule A when\n" + "    accumulate( $e : MyEvent() over window:time(5s), $result : count($e) )\n" + "then" + "    System.out.println(\"Result-2: \" + $result);\n" + "    result.set( $result.intValue() );\n" + "end";
    KieServices ks = KieServices.Factory.get();
    KieModuleModel kproj = ks.newKieModuleModel();
    KieBaseModel kieBaseModel1 = kproj.newKieBaseModel("KBase1").setDefault(true).setEventProcessingMode(EventProcessingOption.STREAM);
    KieSessionModel ksession1 = kieBaseModel1.newKieSessionModel("KSession1").setDefault(true).setType(KieSessionModel.KieSessionType.STATEFUL).setClockType(ClockTypeOption.get(ClockType.PSEUDO_CLOCK.getId()));
    ReleaseId releaseId1 = ks.newReleaseId("org.kie", "test-upgrade", "1.0.0");
    deployJar(ks, createKJar(ks, kproj, releaseId1, null, drl1));
    KieContainer kc = ks.newKieContainer(releaseId1);
    KieSession ksession = kc.newKieSession();
    PseudoClockScheduler clock = ksession.getSessionClock();
    AtomicInteger result = new AtomicInteger(0);
    ksession.setGlobal("result", result);
    ksession.insert(new MyEvent(1));
    clock.advanceTime(4, TimeUnit.SECONDS);
    ksession.insert(new MyEvent(2));
    clock.advanceTime(4, TimeUnit.SECONDS);
    ksession.insert(new MyEvent(3));
    ksession.fireAllRules();
    assertEquals(3, result.get());
    // expires 1
    clock.advanceTime(3, TimeUnit.SECONDS);
    ksession.fireAllRules();
    assertEquals(2, result.get());
    ReleaseId releaseId2 = ks.newReleaseId("org.kie", "test-upgrade", "1.1.0");
    deployJar(ks, createKJar(ks, kproj, releaseId2, null, drl2));
    kc.updateToVersion(releaseId2);
    // shorter window: 2 is out
    ksession.fireAllRules();
    assertEquals(1, result.get());
    ksession.insert(new MyEvent(4));
    ksession.insert(new MyEvent(5));
    ksession.fireAllRules();
    assertEquals(3, result.get());
    // expires 3
    clock.advanceTime(3, TimeUnit.SECONDS);
    ksession.fireAllRules();
    assertEquals(2, result.get());
    // expires 4 & 5
    clock.advanceTime(3, TimeUnit.SECONDS);
    ksession.fireAllRules();
    assertEquals(0, result.get());
}
Also used : KieBaseModel(org.kie.api.builder.model.KieBaseModel) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) KieModuleModel(org.kie.api.builder.model.KieModuleModel) KieServices(org.kie.api.KieServices) StatelessKieSession(org.kie.api.runtime.StatelessKieSession) KieSession(org.kie.api.runtime.KieSession) ReleaseId(org.kie.api.builder.ReleaseId) PseudoClockScheduler(org.drools.core.time.impl.PseudoClockScheduler) KieSessionModel(org.kie.api.builder.model.KieSessionModel) KieContainer(org.kie.api.runtime.KieContainer) Test(org.junit.Test)

Example 63 with PseudoClockScheduler

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

the class IncrementalCompilationTest method testIncrementalCompilationWithSlidingWindow.

@Test
public void testIncrementalCompilationWithSlidingWindow() {
    // DROOLS-881
    String drl1 = "import " + MyEvent.class.getCanonicalName() + "\n" + "declare MyEvent @role( event ) end\n" + "rule A when\n" + "    Number($number : intValue)\n" + "              from accumulate( MyEvent($id : id) over window:time(10s), sum($id) )\n" + "then\n" + "    System.out.println(\"1. SUM : \" + $number);\n" + "end\n" + "\n" + "rule B when\n" + "    Number($number : intValue)\n" + "              from accumulate( MyEvent($id : id) over window:time(10s), count($id) )\n" + "then\n" + "    System.out.println(\"1. CNT : \" + $number);\n" + "end";
    String drl2 = "import " + MyEvent.class.getCanonicalName() + "\n" + "declare MyEvent @role( event ) end\n" + "rule A when\n" + "    Number($number : intValue)\n" + "              from accumulate( MyEvent($id : id) over window:time(10s), sum($id) )\n" + "then\n" + "    System.out.println(\"2. SUM : \" + $number);\n" + "end\n" + "\n" + "rule B when\n" + "    Number($number : intValue)\n" + "              from accumulate( MyEvent($id : id) over window:time(10s), count($id) )\n" + "then\n" + "    System.out.println(\"2. CNT : \" + $number);\n" + "end";
    KieServices ks = KieServices.Factory.get();
    KieModuleModel kproj = ks.newKieModuleModel();
    KieBaseModel kieBaseModel1 = kproj.newKieBaseModel("KBase1").setDefault(true).setEventProcessingMode(EventProcessingOption.STREAM);
    KieSessionModel ksession1 = kieBaseModel1.newKieSessionModel("KSession1").setDefault(true).setType(KieSessionModel.KieSessionType.STATEFUL).setClockType(ClockTypeOption.get(ClockType.PSEUDO_CLOCK.getId()));
    ReleaseId releaseId1 = ks.newReleaseId("org.kie", "test-upgrade", "1.0.0");
    deployJar(ks, createKJar(ks, kproj, releaseId1, null, drl1));
    ReleaseId releaseId2 = ks.newReleaseId("org.kie", "test-upgrade", "1.1.0");
    deployJar(ks, createKJar(ks, kproj, releaseId2, null, drl2));
    KieContainer kc = ks.newKieContainer(releaseId1);
    KieSession ksession = kc.newKieSession();
    PseudoClockScheduler clock = ksession.getSessionClock();
    ksession.insert(new MyEvent(1));
    ksession.fireAllRules();
    clock.advanceTime(7, TimeUnit.SECONDS);
    kc.updateToVersion(releaseId2);
    ksession.fireAllRules();
    clock.advanceTime(7, TimeUnit.SECONDS);
    kc.updateToVersion(releaseId1);
    ksession.fireAllRules();
}
Also used : KieBaseModel(org.kie.api.builder.model.KieBaseModel) KieModuleModel(org.kie.api.builder.model.KieModuleModel) KieServices(org.kie.api.KieServices) StatelessKieSession(org.kie.api.runtime.StatelessKieSession) KieSession(org.kie.api.runtime.KieSession) ReleaseId(org.kie.api.builder.ReleaseId) PseudoClockScheduler(org.drools.core.time.impl.PseudoClockScheduler) KieSessionModel(org.kie.api.builder.model.KieSessionModel) KieContainer(org.kie.api.runtime.KieContainer) Test(org.junit.Test)

Example 64 with PseudoClockScheduler

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

the class MarshallingTest method testMarshallEntryPointsWithExpires.

@Test
public void testMarshallEntryPointsWithExpires() throws Exception {
    String str = "package org.domain.test \n" + "import " + getClass().getCanonicalName() + ".*\n" + "global java.util.List list\n" + "declare A\n" + " @role( event )\n" + " @expires( 10s )\n" + "end\n" + "declare B\n" + "" + " @role( event )\n" + " @expires( 10s )\n" + "end\n" + "" + "declare C\n" + " @role( event )\n" + " @expires( 15s )\n" + "end\n" + "" + "rule a1\n" + "when\n" + "   $a : A() from entry-point 'a-ep'\n" + "then\n" + "list.add( $a );" + "end\n" + "" + "rule b1\n" + "when\n" + "   $b : B() from entry-point 'b-ep'\n" + "then\n" + "list.add( $b );" + "end\n" + "" + "rule c1\n" + "when\n" + "   $c : C() from entry-point 'c-ep'\n" + "then\n" + "list.add( $c );" + "end\n";
    KieBaseConfiguration config = KnowledgeBaseFactory.newKnowledgeBaseConfiguration();
    config.setOption(EventProcessingOption.STREAM);
    KieBase kBase = loadKnowledgeBaseFromString(config, str);
    KieSessionConfiguration ksconf = KnowledgeBaseFactory.newKnowledgeSessionConfiguration();
    ksconf.setOption(ClockTypeOption.get("pseudo"));
    ksconf.setOption(TimerJobFactoryOption.get("trackable"));
    KieSession ksession = kBase.newKieSession(ksconf, null);
    List list = new ArrayList();
    ksession.setGlobal("list", list);
    EntryPoint aep = ksession.getEntryPoint("a-ep");
    aep.insert(new A());
    ksession = marsallStatefulKnowledgeSession(ksession);
    EntryPoint bep = ksession.getEntryPoint("b-ep");
    bep.insert(new B());
    ksession = marsallStatefulKnowledgeSession(ksession);
    EntryPoint cep = ksession.getEntryPoint("c-ep");
    cep.insert(new C());
    ksession = marsallStatefulKnowledgeSession(ksession);
    ksession.fireAllRules();
    ksession = marsallStatefulKnowledgeSession(ksession);
    assertEquals(3, list.size());
    aep = ksession.getEntryPoint("a-ep");
    assertEquals(1, aep.getFactHandles().size());
    bep = ksession.getEntryPoint("b-ep");
    assertEquals(1, bep.getFactHandles().size());
    cep = ksession.getEntryPoint("c-ep");
    assertEquals(1, cep.getFactHandles().size());
    PseudoClockScheduler timeService = (PseudoClockScheduler) ksession.<SessionClock>getSessionClock();
    timeService.advanceTime(11, TimeUnit.SECONDS);
    ksession = marsallStatefulKnowledgeSession(ksession);
    ksession.fireAllRules();
    ksession = marsallStatefulKnowledgeSession(ksession);
    aep = ksession.getEntryPoint("a-ep");
    assertEquals(0, aep.getFactHandles().size());
    bep = ksession.getEntryPoint("b-ep");
    assertEquals(0, bep.getFactHandles().size());
    cep = ksession.getEntryPoint("c-ep");
    assertEquals(1, cep.getFactHandles().size());
}
Also used : KieBaseConfiguration(org.kie.api.KieBaseConfiguration) FactA(org.drools.compiler.FactA) FactB(org.drools.compiler.FactB) FactC(org.drools.compiler.FactC) KieBase(org.kie.api.KieBase) ArrayList(java.util.ArrayList) EntryPoint(org.kie.api.runtime.rule.EntryPoint) KieSession(org.kie.api.runtime.KieSession) List(java.util.List) ArrayList(java.util.ArrayList) IteratorToList(org.drools.compiler.integrationtests.IteratorToList) KieSessionConfiguration(org.kie.api.runtime.KieSessionConfiguration) PseudoClockScheduler(org.drools.core.time.impl.PseudoClockScheduler) Test(org.junit.Test)

Example 65 with PseudoClockScheduler

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

the class MarshallingTest method testSnapshotRecoveryScheduledRulesPlain.

@Test
public void testSnapshotRecoveryScheduledRulesPlain() throws Exception {
    // DROOLS-1537
    String drl = "package com.drools.restore.reproducer\n" + "global java.util.List list;\n" + "global java.util.List list2;\n" + "rule R1\n" + " timer (int: 20s)\n" + " when\n" + "   $m : String( this == \"Hello World1\" )\n" + " then\n" + "   list.add( $m );\n" + "   retract( $m );\n" + "end\n" + "rule R2\n" + " timer (int: 30s)\n" + " when\n" + "   $m : String( this == \"Hello World2\" )\n" + " then\n" + "   list2.add( $m );\n" + "   retract( $m );\n" + "end\n";
    KieSessionConfiguration ksconf = KnowledgeBaseFactory.newKnowledgeSessionConfiguration();
    ksconf.setOption(ClockTypeOption.get(ClockType.PSEUDO_CLOCK.getId()));
    ksconf.setOption(TimedRuleExecutionOption.YES);
    ksconf.setOption(TimerJobFactoryOption.get("trackable"));
    ksconf.setOption(ClockTypeOption.get("pseudo"));
    KieBase kbase1 = new KieHelper().addContent(drl, ResourceType.DRL).build(EventProcessingOption.STREAM);
    KieSession ksession = kbase1.newKieSession(ksconf, null);
    PseudoClockScheduler timeService = (PseudoClockScheduler) ksession.<SessionClock>getSessionClock();
    List list = new ArrayList();
    ksession.setGlobal("list", list);
    List list2 = new ArrayList();
    ksession.setGlobal("list2", list2);
    ksession.insert("Hello World1");
    ksession.insert("Hello World2");
    ksession.fireAllRules();
    timeService.advanceTime(10500, TimeUnit.MILLISECONDS);
    KieBase kbase2 = new KieHelper().addContent(drl, ResourceType.DRL).build(EventProcessingOption.STREAM);
    ksession = marshallAndUnmarshall(kbase1, kbase2, ksession, ksconf);
    ksession.setGlobal("list", list);
    ksession.setGlobal("list2", list2);
    PseudoClockScheduler timeService2 = (PseudoClockScheduler) ksession.<SessionClock>getSessionClock();
    ksession.fireAllRules();
    long accumulatedSleepTime = 0;
    for (int i = 0; i < 6; i++) {
        timeService2.advanceTime(5050, TimeUnit.MILLISECONDS);
        accumulatedSleepTime += 5050;
        assertEquals(i < 1 ? 0 : 1, list.size());
        assertEquals(i < 3 ? 0 : 1, list2.size());
    }
}
Also used : KieBase(org.kie.api.KieBase) ArrayList(java.util.ArrayList) KieHelper(org.kie.internal.utils.KieHelper) KieSession(org.kie.api.runtime.KieSession) List(java.util.List) ArrayList(java.util.ArrayList) IteratorToList(org.drools.compiler.integrationtests.IteratorToList) KieSessionConfiguration(org.kie.api.runtime.KieSessionConfiguration) PseudoClockScheduler(org.drools.core.time.impl.PseudoClockScheduler) EntryPoint(org.kie.api.runtime.rule.EntryPoint) 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