Search in sources :

Example 96 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 = RuleBaseFactory.newKnowledgeSessionConfiguration();
    ksconf.setOption(ClockTypeOption.get(ClockType.PSEUDO_CLOCK.getId()));
    ksconf.setOption(TimedRuleExecutionOption.YES);
    ksconf.setOption(TimerJobFactoryOption.get("trackable"));
    ksconf.setOption(ClockTypeOption.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) IteratorToList(org.drools.mvel.integrationtests.IteratorToList) List(java.util.List) ArrayList(java.util.ArrayList) KieSessionConfiguration(org.kie.api.runtime.KieSessionConfiguration) PseudoClockScheduler(org.drools.core.time.impl.PseudoClockScheduler) EntryPoint(org.kie.api.runtime.rule.EntryPoint) Test(org.junit.Test)

Example 97 with PseudoClockScheduler

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

the class ParallelEvaluationTest method testFireUntilHaltWithExpiration2.

@Test(timeout = 40000L)
@Ignore("this test is failing on Jenkins but not locally, we need to figure out why")
public void testFireUntilHaltWithExpiration2() throws InterruptedException {
    String drl = "import " + A.class.getCanonicalName() + "\n" + "import " + B.class.getCanonicalName() + "\n" + "declare A @role( event ) @expires(11ms) end\n" + "declare B @role( event ) @expires(11ms) end\n" + "global java.util.concurrent.atomic.AtomicInteger counter;\n" + "global java.util.concurrent.CountDownLatch fireLatch;\n" + "rule R0 when\n" + "  $A: A( $Aid : value > 0 )\n" + "  $B: B( ($Bid: value <= $Aid) && (value > ($Aid - 1 )))\n" + "then\n" + "  counter.incrementAndGet();\n" + "  fireLatch.countDown();" + "end\n" + "rule R1 when\n" + "  $A: A( $Aid: value > 1 )\n" + "  $B: B( ($Bid: value <= $Aid) && (value > ($Aid - 1 )))\n" + "then\n" + "  counter.incrementAndGet();\n" + "  fireLatch.countDown();" + "end\n" + "rule R2 when\n" + "  $A: A( $Aid: value > 2 )\n" + "  $B: B( ($Bid: value <= $Aid) && (value > ($Aid - 1 )))\n" + "then\n" + "  counter.incrementAndGet();\n" + "  fireLatch.countDown();" + "end\n" + "rule R3 when\n" + "  $A: A( $Aid: value > 3 )\n" + "  $B: B( ($Bid: value <= $Aid) && (value > ($Aid - 1 )))\n" + "then\n" + "  counter.incrementAndGet();\n" + "  fireLatch.countDown();" + "end";
    KieSessionConfiguration sessionConfig = RuleBaseFactory.newKnowledgeSessionConfiguration();
    sessionConfig.setOption(ClockTypeOption.get(ClockType.PSEUDO_CLOCK.getId()));
    KieBaseTestConfiguration streamConfig = TestParametersUtil.getStreamInstanceOf(kieBaseTestConfiguration);
    final KieModule kieModule = KieUtil.getKieModuleFromDrls("test", streamConfig, drl);
    final KieBase kbase = KieBaseUtil.newKieBaseFromKieModuleWithAdditionalOptions(kieModule, streamConfig, MultithreadEvaluationOption.YES);
    KieSession ksession = kbase.newKieSession(sessionConfig, null);
    try {
        assertTrue(((InternalWorkingMemory) ksession).getAgenda().isParallelAgenda());
        PseudoClockScheduler sessionClock = ksession.getSessionClock();
        sessionClock.setStartupTime(0);
        AtomicInteger counter = new AtomicInteger(0);
        ksession.setGlobal("counter", counter);
        new Thread(() -> ksession.fireUntilHalt()).start();
        int eventsNr = 5;
        final CountDownLatch fireLatch = new CountDownLatch(eventsNr * 4);
        ksession.setGlobal("fireLatch", fireLatch);
        for (int i = 0; i < eventsNr; i++) {
            ksession.insert(new A(i + 4));
            ksession.insert(new B(i + 4));
            sessionClock.advanceTime(10, TimeUnit.MILLISECONDS);
        }
        fireLatch.await();
        assertEquals(eventsNr * 4, counter.get());
    } finally {
        ksession.halt();
        ksession.dispose();
    }
}
Also used : CountDownLatch(java.util.concurrent.CountDownLatch) PseudoClockScheduler(org.drools.core.time.impl.PseudoClockScheduler) InternalWorkingMemory(org.drools.core.common.InternalWorkingMemory) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) KieBase(org.kie.api.KieBase) KieBaseTestConfiguration(org.drools.testcoverage.common.util.KieBaseTestConfiguration) KieSession(org.kie.api.runtime.KieSession) KieSessionConfiguration(org.kie.api.runtime.KieSessionConfiguration) KieModule(org.kie.api.builder.KieModule) Ignore(org.junit.Ignore) Test(org.junit.Test)

Example 98 with PseudoClockScheduler

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

the class ParallelEvaluationTest method testEventsExpiration.

@Test(timeout = 40000L)
public void testEventsExpiration() {
    StringBuilder sb = new StringBuilder(400);
    sb.append("global java.util.List list;\n");
    sb.append("import " + MyEvent.class.getCanonicalName() + ";\n");
    sb.append("declare MyEvent @role( event ) @expires( 20ms ) @timestamp( timestamp ) end\n");
    for (int i = 0; i < 10; i++) {
        sb.append(getRuleWithEvent(i));
    }
    KieSessionConfiguration sessionConfig = RuleBaseFactory.newKnowledgeSessionConfiguration();
    sessionConfig.setOption(ClockTypeOption.get(ClockType.PSEUDO_CLOCK.getId()));
    KieBaseTestConfiguration streamConfig = TestParametersUtil.getStreamInstanceOf(kieBaseTestConfiguration);
    final KieModule kieModule = KieUtil.getKieModuleFromDrls("test", streamConfig, sb.toString());
    final KieBase kbase = KieBaseUtil.newKieBaseFromKieModuleWithAdditionalOptions(kieModule, streamConfig, MultithreadEvaluationOption.YES);
    KieSession ksession = kbase.newKieSession(sessionConfig, null);
    assertTrue(((InternalWorkingMemory) ksession).getAgenda().isParallelAgenda());
    PseudoClockScheduler sessionClock = ksession.getSessionClock();
    sessionClock.setStartupTime(0);
    List<Integer> list = new DebugList<Integer>();
    ksession.setGlobal("list", list);
    for (int i = 0; i < 10; i++) {
        ksession.insert(new MyEvent(i, i * 2L));
    }
    ksession.fireAllRules();
    assertEquals(10, list.size());
    assertEquals(10L, ksession.getFactCount());
    sessionClock.advanceTime(29, TimeUnit.MILLISECONDS);
    ksession.fireAllRules();
    assertEquals(5L, ksession.getFactCount());
    sessionClock.advanceTime(12, TimeUnit.MILLISECONDS);
    ksession.fireAllRules();
    assertEquals(0L, ksession.getFactCount());
}
Also used : DebugList(org.drools.mvel.compiler.util.debug.DebugList) PseudoClockScheduler(org.drools.core.time.impl.PseudoClockScheduler) InternalWorkingMemory(org.drools.core.common.InternalWorkingMemory) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) KieBase(org.kie.api.KieBase) KieBaseTestConfiguration(org.drools.testcoverage.common.util.KieBaseTestConfiguration) KieSession(org.kie.api.runtime.KieSession) KieSessionConfiguration(org.kie.api.runtime.KieSessionConfiguration) KieModule(org.kie.api.builder.KieModule) Test(org.junit.Test)

Example 99 with PseudoClockScheduler

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

the class IncrementalCompilationCepTest method testChangeWindowTime.

@Test
public void testChangeWindowTime() {
    // DROOLS-853
    final 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";
    final 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";
    final KieServices ks = KieServices.Factory.get();
    final ReleaseId releaseId1 = ks.newReleaseId("org.kie", "test-upgrade", "1.0.0");
    KieUtil.getKieModuleFromDrls(releaseId1, kieBaseTestConfiguration, KieSessionTestConfiguration.STATEFUL_PSEUDO, new HashMap<>(), drl1);
    final KieContainer kc = ks.newKieContainer(releaseId1);
    final KieSession ksession = kc.newKieSession();
    final PseudoClockScheduler clock = ksession.getSessionClock();
    final 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());
    final ReleaseId releaseId2 = ks.newReleaseId("org.kie", "test-upgrade", "1.1.0");
    KieUtil.getKieModuleFromDrls(releaseId2, kieBaseTestConfiguration, KieSessionTestConfiguration.STATEFUL_PSEUDO, new HashMap<>(), 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 : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) KieServices(org.kie.api.KieServices) KieSession(org.kie.api.runtime.KieSession) ReleaseId(org.kie.api.builder.ReleaseId) PseudoClockScheduler(org.drools.core.time.impl.PseudoClockScheduler) KieContainer(org.kie.api.runtime.KieContainer) Test(org.junit.Test)

Example 100 with PseudoClockScheduler

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

the class IncrementalCompilationCepTest method testIncrementalCompilationWithTimerNode.

@Test
public void testIncrementalCompilationWithTimerNode() {
    // DROOLS-1195
    final String drl1 = "package org.drools.test\n" + "import " + DummyEvent.class.getCanonicalName() + "\n" + "declare DummyEvent\n" + "    @role( event )\n" + "    @timestamp( eventTimestamp )\n" + "end\n" + "rule \"RG_TEST_TIMER\"\n" + "timer (int: 0 1; start=$expirationTimestamp , repeat-limit=0 )\n" + "    when\n" + "       $dummy: DummyEvent (id == 'timer', $expirationTimestamp : systemTimestamp )\n" + "    then\n " + "System.out.println(\"1\");\n" + "end\n";
    final String drl2 = "package org.drools.test\n" + "import " + DummyEvent.class.getCanonicalName() + "\n" + "declare DummyEvent\n" + "    @role( event )\n" + "    @timestamp( eventTimestamp )\n" + "end\n" + "rule \"RG_TEST_TIMER_NEW\"\n" + "timer (int: 0 1; start=$expirationTimestamp , repeat-limit=0 )\n" + "    when\n" + "       $dummy: DummyEvent (id == 'timer', $expirationTimestamp : systemTimestamp )\n" + "		DummyEvent (id == 'timer_match')\n" + "    then\n " + "System.out.println(\"1\");\n" + "end\n" + "rule \"RG_OTHER_RULE\"\n" + "    when\n" + "       $dummy: DummyEvent ( id == 'timer' )\n" + "    then\n " + "System.out.println(\"2\");\n" + "end\n";
    final long now = System.currentTimeMillis();
    final KieServices ks = KieServices.Factory.get();
    final ReleaseId releaseId1 = ks.newReleaseId("org.kie", "test-upgrade", "1.0.0");
    KieUtil.getKieModuleFromDrls(releaseId1, kieBaseTestConfiguration, KieSessionTestConfiguration.STATEFUL_PSEUDO, new HashMap<>(), drl1);
    final KieContainer kc = ks.newKieContainer(releaseId1);
    final KieSession kieSession = kc.newKieSession();
    final DummyEvent dummyEvent = new DummyEvent();
    dummyEvent.setId("timer");
    dummyEvent.setEventTimestamp(now);
    dummyEvent.setSystemTimestamp(now + TimeUnit.HOURS.toMillis(1));
    final DummyEvent other = new DummyEvent();
    other.setId("timer_match");
    other.setEventTimestamp(now);
    kieSession.insert(dummyEvent);
    kieSession.insert(other);
    final ReleaseId releaseId2 = ks.newReleaseId("org.kie", "test-upgrade", "2.0.0");
    KieUtil.getKieModuleFromDrls(releaseId2, kieBaseTestConfiguration, KieSessionTestConfiguration.STATEFUL_PSEUDO, new HashMap<>(), drl2);
    kc.updateToVersion(releaseId2);
    final PseudoClockScheduler scheduler = kieSession.getSessionClock();
    scheduler.setStartupTime(now);
    scheduler.advanceTime(1, TimeUnit.DAYS);
    assertEquals(2, kieSession.fireAllRules());
}
Also used : KieServices(org.kie.api.KieServices) KieSession(org.kie.api.runtime.KieSession) ReleaseId(org.kie.api.builder.ReleaseId) PseudoClockScheduler(org.drools.core.time.impl.PseudoClockScheduler) KieContainer(org.kie.api.runtime.KieContainer) Test(org.junit.Test)

Aggregations

PseudoClockScheduler (org.drools.core.time.impl.PseudoClockScheduler)134 KieSession (org.kie.api.runtime.KieSession)126 Test (org.junit.Test)116 KieBase (org.kie.api.KieBase)105 KieSessionConfiguration (org.kie.api.runtime.KieSessionConfiguration)71 ArrayList (java.util.ArrayList)67 List (java.util.List)57 Date (java.util.Date)40 Arrays.asList (java.util.Arrays.asList)36 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)33 EntryPoint (org.kie.api.runtime.rule.EntryPoint)23 DateFormat (java.text.DateFormat)18 SimpleDateFormat (java.text.SimpleDateFormat)18 Calendar (org.kie.api.time.Calendar)18 StockTick (org.drools.testcoverage.common.model.StockTick)17 FactHandle (org.kie.api.runtime.rule.FactHandle)17 KieHelper (org.kie.internal.utils.KieHelper)16 Ignore (org.junit.Ignore)14 KieBaseTestConfiguration (org.drools.testcoverage.common.util.KieBaseTestConfiguration)13 Locale (java.util.Locale)12