Search in sources :

Example 66 with PseudoClockScheduler

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

the class ParallelEvaluationTest method testEventsExpiration.

@Test(timeout = 10000L)
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 = KnowledgeBaseFactory.newKnowledgeSessionConfiguration();
    sessionConfig.setOption(ClockTypeOption.get(ClockType.PSEUDO_CLOCK.getId()));
    KieSession ksession = new KieHelper().addContent(sb.toString(), ResourceType.DRL).build(EventProcessingOption.STREAM, MultithreadEvaluationOption.YES).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 : InternalWorkingMemory(org.drools.core.common.InternalWorkingMemory) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) KieHelper(org.kie.internal.utils.KieHelper) KieSession(org.kie.api.runtime.KieSession) DebugList(org.drools.compiler.util.debug.DebugList) KieSessionConfiguration(org.kie.api.runtime.KieSessionConfiguration) PseudoClockScheduler(org.drools.core.time.impl.PseudoClockScheduler) Test(org.junit.Test)

Example 67 with PseudoClockScheduler

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

the class ParallelEvaluationTest method testFireUntilHaltWithExpiration.

@Test(timeout = 10000L)
public void testFireUntilHaltWithExpiration() {
    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(getRuleWithEventForExpiration(i));
    }
    KieSessionConfiguration sessionConfig = KnowledgeBaseFactory.newKnowledgeSessionConfiguration();
    sessionConfig.setOption(ClockTypeOption.get(ClockType.PSEUDO_CLOCK.getId()));
    KieSession ksession = new KieHelper().addContent(sb.toString(), ResourceType.DRL).build(EventProcessingOption.STREAM, MultithreadEvaluationOption.YES).newKieSession(sessionConfig, null);
    assertTrue(((InternalWorkingMemory) ksession).getAgenda().isParallelAgenda());
    PseudoClockScheduler sessionClock = ksession.getSessionClock();
    sessionClock.setStartupTime(0);
    DebugList<Integer> list = new DebugList<Integer>();
    CountDownLatch done1 = new CountDownLatch(1);
    list.onItemAdded = (l -> {
        if (l.size() == 10) {
            done1.countDown();
        }
    });
    ksession.setGlobal("list", list);
    for (int i = 0; i < 10; i++) {
        ksession.insert(new MyEvent(i, i * 2L));
    }
    new Thread(() -> ksession.fireUntilHalt()).start();
    try {
        done1.await();
    } catch (InterruptedException e) {
        throw new RuntimeException(e);
    }
    assertEquals(10, list.size());
    list.clear();
    CountDownLatch done2 = new CountDownLatch(1);
    list.onItemAdded = (l -> {
        if (l.size() == 5) {
            done2.countDown();
        }
    });
    ksession.insert(1);
    sessionClock.advanceTime(29, TimeUnit.MILLISECONDS);
    try {
        done2.await();
    } catch (InterruptedException e) {
        throw new RuntimeException(e);
    }
    assertEquals(5, list.size());
    list.clear();
    CountDownLatch done3 = new CountDownLatch(1);
    list.onItemAdded = (l -> {
        if (l.size() == 5) {
            done3.countDown();
        }
    });
    sessionClock.advanceTime(12, TimeUnit.MILLISECONDS);
    try {
        done3.await();
    } catch (InterruptedException e) {
        throw new RuntimeException(e);
    }
    assertEquals(5, list.size());
    ksession.halt();
    ksession.dispose();
}
Also used : Arrays(java.util.Arrays) ClassObjectType(org.drools.core.base.ClassObjectType) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) TimeoutException(java.util.concurrent.TimeoutException) EntryPointId(org.drools.core.rule.EntryPointId) MultithreadEvaluationOption(org.kie.internal.conf.MultithreadEvaluationOption) Callable(java.util.concurrent.Callable) ClockType(org.drools.core.ClockType) ResourceType(org.kie.api.io.ResourceType) ArrayList(java.util.ArrayList) KieSessionConfiguration(org.kie.api.runtime.KieSessionConfiguration) Future(java.util.concurrent.Future) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) CompositePartitionAwareObjectSinkAdapter(org.drools.core.reteoo.CompositePartitionAwareObjectSinkAdapter) EntryPointNode(org.drools.core.reteoo.EntryPointNode) KieBase(org.kie.api.KieBase) KieSession(org.kie.api.runtime.KieSession) ExecutorService(java.util.concurrent.ExecutorService) KieHelper(org.kie.internal.utils.KieHelper) EventProcessingOption(org.kie.api.conf.EventProcessingOption) InternalWorkingMemory(org.drools.core.common.InternalWorkingMemory) DebugList(org.drools.compiler.util.debug.DebugList) Test(org.junit.Test) PseudoClockScheduler(org.drools.core.time.impl.PseudoClockScheduler) Executors(java.util.concurrent.Executors) FactHandle(org.kie.api.runtime.rule.FactHandle) ExecutionException(java.util.concurrent.ExecutionException) TimeUnit(java.util.concurrent.TimeUnit) CountDownLatch(java.util.concurrent.CountDownLatch) List(java.util.List) ClockTypeOption(org.kie.api.runtime.conf.ClockTypeOption) InternalKnowledgeBase(org.drools.core.impl.InternalKnowledgeBase) KnowledgeBaseFactory(org.drools.core.impl.KnowledgeBaseFactory) StatefulKnowledgeSessionImpl(org.drools.core.impl.StatefulKnowledgeSessionImpl) Assert(org.junit.Assert) ObjectTypeNode(org.drools.core.reteoo.ObjectTypeNode) KieHelper(org.kie.internal.utils.KieHelper) DebugList(org.drools.compiler.util.debug.DebugList) CountDownLatch(java.util.concurrent.CountDownLatch) PseudoClockScheduler(org.drools.core.time.impl.PseudoClockScheduler) InternalWorkingMemory(org.drools.core.common.InternalWorkingMemory) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) KieSession(org.kie.api.runtime.KieSession) KieSessionConfiguration(org.kie.api.runtime.KieSessionConfiguration) Test(org.junit.Test)

Example 68 with PseudoClockScheduler

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

the class ParallelEvaluationTest method testFireUntilHaltWithExpiration2.

@Test(timeout = 100000L)
public void testFireUntilHaltWithExpiration2() {
    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" + "rule R0 when\n" + "  $A: A( $Aid : value > 0 )\n" + "  $B: B( ($Bid: value <= $Aid) && (value > ($Aid - 1 )))\n" + "then\n" + "  counter.incrementAndGet();\n" + "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" + "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" + "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" + "end";
    KieSessionConfiguration sessionConfig = KnowledgeBaseFactory.newKnowledgeSessionConfiguration();
    sessionConfig.setOption(ClockTypeOption.get(ClockType.PSEUDO_CLOCK.getId()));
    KieSession ksession = new KieHelper().addContent(drl, ResourceType.DRL).build(EventProcessingOption.STREAM, MultithreadEvaluationOption.YES).newKieSession(sessionConfig, null);
    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;
    for (int i = 0; i < eventsNr; i++) {
        ksession.insert(new A(i + 4));
        ksession.insert(new B(i + 4));
        sessionClock.advanceTime(10, TimeUnit.MILLISECONDS);
    }
    try {
        Thread.sleep(1000L);
    } catch (InterruptedException e) {
        throw new RuntimeException(e);
    }
    ksession.halt();
    ksession.dispose();
    assertEquals(eventsNr * 4, counter.get());
}
Also used : KieHelper(org.kie.internal.utils.KieHelper) PseudoClockScheduler(org.drools.core.time.impl.PseudoClockScheduler) InternalWorkingMemory(org.drools.core.common.InternalWorkingMemory) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) KieSession(org.kie.api.runtime.KieSession) KieSessionConfiguration(org.kie.api.runtime.KieSessionConfiguration) Test(org.junit.Test)

Example 69 with PseudoClockScheduler

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

the class StreamsTest method testEventDoesNotExpireIfNotInPattern.

@Test(timeout = 10000)
public void testEventDoesNotExpireIfNotInPattern() throws Exception {
    KieBaseConfiguration kconf = KnowledgeBaseFactory.newKnowledgeBaseConfiguration();
    kconf.setOption(EventProcessingOption.STREAM);
    KieBase kbase = loadKnowledgeBase("test_EventExpiration.drl", kconf);
    KieSessionConfiguration ksessionConfig = KnowledgeBaseFactory.newKnowledgeSessionConfiguration();
    ksessionConfig.setOption(ClockTypeOption.get("pseudo"));
    KieSession ksession = kbase.newKieSession(ksessionConfig, null);
    RuleRuntimeEventListener wml = mock(RuleRuntimeEventListener.class);
    ksession.addEventListener(wml);
    PseudoClockScheduler clock = (PseudoClockScheduler) ksession.<SessionClock>getSessionClock();
    final StockTickInterface st1 = new StockTick(1, "RHT", 100, 1000);
    final StockTickInterface st2 = new StockTick(2, "RHT", 100, 1000);
    ksession.insert(st1);
    ksession.insert(st2);
    verify(wml, times(2)).objectInserted(any(org.kie.api.event.rule.ObjectInsertedEvent.class));
    assertThat(ksession.getObjects().size(), equalTo(2));
    assertThat((Collection<Object>) ksession.getObjects(), hasItems((Object) st1, st2));
    ksession.fireAllRules();
    clock.advanceTime(3, TimeUnit.SECONDS);
    ksession.fireAllRules();
    assertThat(ksession.getObjects().size(), equalTo(0));
}
Also used : KieBaseConfiguration(org.kie.api.KieBaseConfiguration) StockTickInterface(org.drools.compiler.StockTickInterface) RuleRuntimeEventListener(org.kie.api.event.rule.RuleRuntimeEventListener) StockTick(org.drools.compiler.StockTick) KieBase(org.kie.api.KieBase) KieSession(org.kie.api.runtime.KieSession) KieSessionConfiguration(org.kie.api.runtime.KieSessionConfiguration) PseudoClockScheduler(org.drools.core.time.impl.PseudoClockScheduler) Test(org.junit.Test)

Example 70 with PseudoClockScheduler

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

the class StreamsTest method testEventExpirationSetToZero.

@Test(timeout = 10000)
public void testEventExpirationSetToZero() throws Exception {
    KieBaseConfiguration kconf = KnowledgeBaseFactory.newKnowledgeBaseConfiguration();
    kconf.setOption(EventProcessingOption.STREAM);
    KieBase kbase = loadKnowledgeBase("test_EventExpirationSetToZero.drl", kconf);
    KieSessionConfiguration ksessionConfig = KnowledgeBaseFactory.newKnowledgeSessionConfiguration();
    ksessionConfig.setOption(ClockTypeOption.get("pseudo"));
    KieSession ksession = kbase.newKieSession(ksessionConfig, null);
    RuleRuntimeEventListener wml = mock(RuleRuntimeEventListener.class);
    ksession.addEventListener(wml);
    AgendaEventListener ael = mock(AgendaEventListener.class);
    ksession.addEventListener(ael);
    PseudoClockScheduler clock = (PseudoClockScheduler) ksession.<SessionClock>getSessionClock();
    final StockTickInterface st1 = new StockTick(1, "RHT", 100, 1000);
    final StockTickInterface st2 = new StockTick(2, "RHT", 100, 1000);
    ksession.insert(st1);
    ksession.insert(st2);
    assertThat(ksession.fireAllRules(), equalTo(2));
    verify(wml, times(2)).objectInserted(any(org.kie.api.event.rule.ObjectInsertedEvent.class));
    verify(ael, times(2)).matchCreated(any(MatchCreatedEvent.class));
    assertThat(ksession.getObjects().size(), equalTo(2));
    assertThat((Collection<Object>) ksession.getObjects(), hasItems((Object) st1, st2));
    clock.advanceTime(3, TimeUnit.SECONDS);
    ksession.fireAllRules();
    assertThat(ksession.getObjects().size(), equalTo(0));
}
Also used : PseudoClockScheduler(org.drools.core.time.impl.PseudoClockScheduler) KieBaseConfiguration(org.kie.api.KieBaseConfiguration) StockTickInterface(org.drools.compiler.StockTickInterface) RuleRuntimeEventListener(org.kie.api.event.rule.RuleRuntimeEventListener) StockTick(org.drools.compiler.StockTick) KieBase(org.kie.api.KieBase) AgendaEventListener(org.kie.api.event.rule.AgendaEventListener) KieSession(org.kie.api.runtime.KieSession) MatchCreatedEvent(org.kie.api.event.rule.MatchCreatedEvent) KieSessionConfiguration(org.kie.api.runtime.KieSessionConfiguration) 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