Search in sources :

Example 11 with DebugList

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

the class ParallelEvaluationTest method testWithUpdates.

@Test(timeout = 40000L)
public void testWithUpdates() {
    StringBuilder sb = new StringBuilder(400);
    sb.append("global java.util.List list;\n");
    for (int i = 0; i < 10; i++) {
        sb.append(getRule(i, ""));
    }
    final KieModule kieModule = KieUtil.getKieModuleFromDrls("test", kieBaseTestConfiguration, sb.toString());
    final KieBase kbase = KieBaseUtil.newKieBaseFromKieModuleWithAdditionalOptions(kieModule, kieBaseTestConfiguration, MultithreadEvaluationOption.YES);
    KieSession ksession = kbase.newKieSession();
    assertTrue(((InternalWorkingMemory) ksession).getAgenda().isParallelAgenda());
    List<Integer> list = new DebugList<Integer>();
    ksession.setGlobal("list", list);
    FactHandle[] fhs = new FactHandle[10];
    for (int i = 0; i < 10; i++) {
        fhs[i] = ksession.insert(i);
        ksession.insert("" + i);
    }
    ksession.fireAllRules();
    assertEquals(10, list.size());
    list.clear();
    for (int i = 0; i < 10; i++) {
        ksession.update(fhs[i], i);
    }
    ksession.fireAllRules();
    assertEquals(10, list.size());
}
Also used : InternalWorkingMemory(org.drools.core.common.InternalWorkingMemory) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) FactHandle(org.kie.api.runtime.rule.FactHandle) KieBase(org.kie.api.KieBase) KieSession(org.kie.api.runtime.KieSession) DebugList(org.drools.mvel.compiler.util.debug.DebugList) KieModule(org.kie.api.builder.KieModule) Test(org.junit.Test)

Example 12 with DebugList

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

the class ParallelEvaluationTest method getMultipleParallelKieSessionsWithInsertionsCallable.

private Callable<Void> getMultipleParallelKieSessionsWithInsertionsCallable(KieBase kBase, int ruleNr) {
    return new Callable<Void>() {

        @Override
        public Void call() {
            KieSession ksession = kBase.newKieSession();
            assertTrue(((InternalWorkingMemory) ksession).getAgenda().isParallelAgenda());
            List<Integer> list = new DebugList<Integer>();
            ksession.setGlobal("list", list);
            insertFacts(ksession, 10);
            ksession.fireAllRules();
            assertEquals(ruleNr, list.size());
            return null;
        }
    };
}
Also used : InternalWorkingMemory(org.drools.core.common.InternalWorkingMemory) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) KieSession(org.kie.api.runtime.KieSession) DebugList(org.drools.mvel.compiler.util.debug.DebugList) Callable(java.util.concurrent.Callable)

Example 13 with DebugList

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

the class ParallelEvaluationTest method testDisableParallelismWithSalience.

@Test(timeout = 40000L)
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));
    }
    final KieModule kieModule = KieUtil.getKieModuleFromDrls("test", kieBaseTestConfiguration, sb.toString());
    final KieBase kbase = KieBaseUtil.newKieBaseFromKieModuleWithAdditionalOptions(kieModule, kieBaseTestConfiguration, 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) KieSession(org.kie.api.runtime.KieSession) DebugList(org.drools.mvel.compiler.util.debug.DebugList) KieModule(org.kie.api.builder.KieModule) Test(org.junit.Test)

Example 14 with DebugList

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

the class ParallelEvaluationTest method testFireUntilHalt2.

@Test(timeout = 40000L)
@Ignore("this test is failing on Jenkins but not locally, we need to figure out why")
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);
    }
    final KieModule kieModule = KieUtil.getKieModuleFromDrls("test", kieBaseTestConfiguration, drl);
    final KieBase kbase = KieBaseUtil.newKieBaseFromKieModuleWithAdditionalOptions(kieModule, kieBaseTestConfiguration, 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();
        try {
            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());
        } finally {
            ksession.halt();
            ksession.dispose();
        }
        System.out.println("Loop " + loop + " terminated");
    }
}
Also used : CountDownLatch(java.util.concurrent.CountDownLatch) DebugList(org.drools.mvel.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) KieModule(org.kie.api.builder.KieModule) Ignore(org.junit.Ignore) Test(org.junit.Test)

Example 15 with DebugList

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

the class ParallelEvaluationTest method testFireUntilHalt.

@Test(timeout = 40000L)
public void testFireUntilHalt() {
    StringBuilder sb = new StringBuilder(400);
    sb.append("global java.util.List list;\n");
    for (int i = 0; i < 10; i++) {
        sb.append(getRule(i, ""));
    }
    final KieModule kieModule = KieUtil.getKieModuleFromDrls("test", kieBaseTestConfiguration, sb.toString());
    final KieBase kbase = KieBaseUtil.newKieBaseFromKieModuleWithAdditionalOptions(kieModule, kieBaseTestConfiguration, MultithreadEvaluationOption.YES);
    KieSession ksession = kbase.newKieSession();
    assertTrue(((InternalWorkingMemory) ksession).getAgenda().isParallelAgenda());
    CountDownLatch done = new CountDownLatch(1);
    DebugList<Integer> list = new DebugList<Integer>();
    list.onItemAdded = (l -> {
        if (l.size() == 10) {
            ksession.halt();
            done.countDown();
        }
    });
    ksession.setGlobal("list", list);
    new Thread(ksession::fireUntilHalt).start();
    try {
        for (int i = 0; i < 10; i++) {
            ksession.insert(i);
            ksession.insert("" + i);
        }
        try {
            done.await();
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }
        assertEquals(10, list.size());
    } finally {
        ksession.halt();
        ksession.dispose();
    }
}
Also used : Arrays(java.util.Arrays) ClassObjectType(org.drools.core.base.ClassObjectType) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) RunWith(org.junit.runner.RunWith) 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) StatefulKnowledgeSessionImpl(org.drools.kiesession.session.StatefulKnowledgeSessionImpl) KieBaseTestConfiguration(org.drools.testcoverage.common.util.KieBaseTestConfiguration) ArrayList(java.util.ArrayList) KieSessionConfiguration(org.kie.api.runtime.KieSessionConfiguration) Future(java.util.concurrent.Future) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) RuleBaseFactory(org.drools.core.impl.RuleBaseFactory) CompositePartitionAwareObjectSinkAdapter(org.drools.core.reteoo.CompositePartitionAwareObjectSinkAdapter) EntryPointNode(org.drools.core.reteoo.EntryPointNode) TestParametersUtil(org.drools.testcoverage.common.util.TestParametersUtil) KieBase(org.kie.api.KieBase) KieSession(org.kie.api.runtime.KieSession) ExecutorService(java.util.concurrent.ExecutorService) InternalKnowledgeBase(org.drools.kiesession.rulebase.InternalKnowledgeBase) Parameterized(org.junit.runners.Parameterized) InternalWorkingMemory(org.drools.core.common.InternalWorkingMemory) Collection(java.util.Collection) Assert.assertTrue(org.junit.Assert.assertTrue) 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) KieUtil(org.drools.testcoverage.common.util.KieUtil) KieModule(org.kie.api.builder.KieModule) DebugList(org.drools.mvel.compiler.util.debug.DebugList) ClockTypeOption(org.kie.api.runtime.conf.ClockTypeOption) Ignore(org.junit.Ignore) Assert.assertFalse(org.junit.Assert.assertFalse) KieBaseUtil(org.drools.testcoverage.common.util.KieBaseUtil) ObjectTypeNode(org.drools.core.reteoo.ObjectTypeNode) Assert.assertEquals(org.junit.Assert.assertEquals) CountDownLatch(java.util.concurrent.CountDownLatch) DebugList(org.drools.mvel.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) KieModule(org.kie.api.builder.KieModule) Test(org.junit.Test)

Aggregations

AtomicInteger (java.util.concurrent.atomic.AtomicInteger)17 DebugList (org.drools.mvel.compiler.util.debug.DebugList)17 KieSession (org.kie.api.runtime.KieSession)17 InternalWorkingMemory (org.drools.core.common.InternalWorkingMemory)16 Test (org.junit.Test)13 KieBase (org.kie.api.KieBase)13 KieModule (org.kie.api.builder.KieModule)13 Callable (java.util.concurrent.Callable)6 CountDownLatch (java.util.concurrent.CountDownLatch)5 StatefulKnowledgeSessionImpl (org.drools.kiesession.session.StatefulKnowledgeSessionImpl)5 KieBaseTestConfiguration (org.drools.testcoverage.common.util.KieBaseTestConfiguration)5 KieSessionConfiguration (org.kie.api.runtime.KieSessionConfiguration)5 FactHandle (org.kie.api.runtime.rule.FactHandle)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 Ignore (org.junit.Ignore)4 ArrayList (java.util.ArrayList)3 Arrays (java.util.Arrays)3