Search in sources :

Example 11 with DebugList

use of org.drools.compiler.util.debug.DebugList in project drools by kiegroup.

the class ParallelEvaluationTest method test.

@Test(timeout = 10000L)
public void test() {
    StringBuilder sb = new StringBuilder(400);
    sb.append("global java.util.List list;\n");
    for (int i = 0; i < 10; i++) {
        sb.append(getRule(i, ""));
    }
    KieBase kbase = new KieHelper().addContent(sb.toString(), ResourceType.DRL).build(MultithreadEvaluationOption.YES);
    EntryPointNode epn = ((InternalKnowledgeBase) kbase).getRete().getEntryPointNode(EntryPointId.DEFAULT);
    ObjectTypeNode otn = epn.getObjectTypeNodes().get(new ClassObjectType(Integer.class));
    assertTrue(((CompositePartitionAwareObjectSinkAdapter) otn.getObjectSinkPropagator()).isHashed());
    KieSession ksession = kbase.newKieSession();
    assertTrue(((InternalWorkingMemory) ksession).getAgenda().isParallelAgenda());
    List<Integer> list = new DebugList<Integer>();
    ksession.setGlobal("list", list);
    for (int i = 0; i < 10; i++) {
        ksession.insert(i);
        ksession.insert("" + i);
    }
    ksession.fireAllRules();
    assertEquals(10, list.size());
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) InternalWorkingMemory(org.drools.core.common.InternalWorkingMemory) EntryPointNode(org.drools.core.reteoo.EntryPointNode) ClassObjectType(org.drools.core.base.ClassObjectType) KieBase(org.kie.api.KieBase) ObjectTypeNode(org.drools.core.reteoo.ObjectTypeNode) KieHelper(org.kie.internal.utils.KieHelper) KieSession(org.kie.api.runtime.KieSession) DebugList(org.drools.compiler.util.debug.DebugList) Test(org.junit.Test)

Example 12 with DebugList

use of org.drools.compiler.util.debug.DebugList 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 13 with DebugList

use of org.drools.compiler.util.debug.DebugList 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 14 with DebugList

use of org.drools.compiler.util.debug.DebugList in project drools by kiegroup.

the class ParallelEvaluationTest method testFireUntilHalt2.

@Test(timeout = 10000L)
public void testFireUntilHalt2() {
    int rulesNr = 4;
    int factsNr = 1;
    int fireNr = rulesNr * factsNr;
    String drl = "import " + A.class.getCanonicalName() + ";\n" + "import " + B.class.getCanonicalName() + ";\n" + "global java.util.concurrent.atomic.AtomicInteger counter\n" + "global java.util.concurrent.CountDownLatch done\n" + "global java.util.List list;\n";
    for (int i = 0; i < rulesNr; i++) {
        drl += getFireUntilHaltRule(fireNr, i);
    }
    KieBase kbase = new KieHelper().addContent(drl, ResourceType.DRL).build(MultithreadEvaluationOption.YES);
    for (int loop = 0; loop < 10; loop++) {
        System.out.println("Starting loop " + loop);
        KieSession ksession = kbase.newKieSession();
        assertTrue(((InternalWorkingMemory) ksession).getAgenda().isParallelAgenda());
        CountDownLatch done = new CountDownLatch(1);
        ksession.setGlobal("done", done);
        AtomicInteger counter = new AtomicInteger(0);
        ksession.setGlobal("counter", counter);
        List<String> list = new DebugList<String>();
        ksession.setGlobal("list", list);
        new Thread(() -> {
            ksession.fireUntilHalt();
        }).start();
        A a = new A(rulesNr + 1);
        ksession.insert(a);
        for (int i = 0; i < factsNr; i++) {
            ksession.insert(new B(rulesNr + i + 3));
        }
        try {
            done.await();
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }
        assertEquals(fireNr, counter.get());
        ksession.halt();
        ksession.dispose();
        System.out.println("Loop " + loop + " terminated");
    }
}
Also used : KieHelper(org.kie.internal.utils.KieHelper) CountDownLatch(java.util.concurrent.CountDownLatch) DebugList(org.drools.compiler.util.debug.DebugList) InternalWorkingMemory(org.drools.core.common.InternalWorkingMemory) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) KieBase(org.kie.api.KieBase) KieSession(org.kie.api.runtime.KieSession) Test(org.junit.Test)

Example 15 with DebugList

use of org.drools.compiler.util.debug.DebugList in project drools by kiegroup.

the class ParallelEvaluationTest method testDisableParallelismWithSalience.

@Test(timeout = 10000L)
public void testDisableParallelismWithSalience() {
    StringBuilder sb = new StringBuilder(400);
    sb.append("global java.util.List list;\n");
    for (int i = 0; i < 10; i++) {
        sb.append(getRule(i, "", "salience " + i));
    }
    KieBase kbase = new KieHelper().addContent(sb.toString(), ResourceType.DRL).build(MultithreadEvaluationOption.YES);
    KieSession ksession = kbase.newKieSession();
    // multithread evaluation is not allowed when using salience
    assertFalse(((InternalWorkingMemory) ksession).getAgenda().isParallelAgenda());
    List<Integer> list = new DebugList<Integer>();
    ksession.setGlobal("list", list);
    for (int i = 0; i < 10; i++) {
        ksession.insert(i);
        ksession.insert("" + i);
    }
    ksession.fireAllRules();
    assertEquals(10, list.size());
    assertEquals(list, Arrays.asList(9, 8, 7, 6, 5, 4, 3, 2, 1, 0));
}
Also used : InternalWorkingMemory(org.drools.core.common.InternalWorkingMemory) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) KieBase(org.kie.api.KieBase) KieHelper(org.kie.internal.utils.KieHelper) KieSession(org.kie.api.runtime.KieSession) DebugList(org.drools.compiler.util.debug.DebugList) Test(org.junit.Test)

Aggregations

DebugList (org.drools.compiler.util.debug.DebugList)19 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)17 KieSession (org.kie.api.runtime.KieSession)17 InternalWorkingMemory (org.drools.core.common.InternalWorkingMemory)16 Test (org.junit.Test)15 KieHelper (org.kie.internal.utils.KieHelper)15 KieBase (org.kie.api.KieBase)9 FactHandle (org.kie.api.runtime.rule.FactHandle)7 Callable (java.util.concurrent.Callable)6 ArrayList (java.util.ArrayList)5 List (java.util.List)5 CountDownLatch (java.util.concurrent.CountDownLatch)5 StatefulKnowledgeSessionImpl (org.drools.core.impl.StatefulKnowledgeSessionImpl)5 ResourceType (org.kie.api.io.ResourceType)5 KieSessionConfiguration (org.kie.api.runtime.KieSessionConfiguration)5 ClassObjectType (org.drools.core.base.ClassObjectType)4 EntryPointNode (org.drools.core.reteoo.EntryPointNode)4 ObjectTypeNode (org.drools.core.reteoo.ObjectTypeNode)4 PseudoClockScheduler (org.drools.core.time.impl.PseudoClockScheduler)4 Arrays (java.util.Arrays)3