Search in sources :

Example 6 with BeanA

use of org.drools.compiler.integrationtests.facts.BeanA in project drools by kiegroup.

the class ConcurrentBasesParallelTest method testQueries.

@Test(timeout = 20000)
public void testQueries() throws InterruptedException {
    final int numberOfObjects = 100;
    final TestExecutor exec = counter -> {
        final String query = "import " + BeanA.class.getCanonicalName() + ";\n" + "query Query " + "    bean : BeanA( seed == " + counter + " ) " + "end";
        final KieBase base = getKieBase(query);
        final KieSession session = base.newKieSession();
        try {
            final BeanA bean = new BeanA(counter);
            session.insert(bean);
            for (int i = 0; i < numberOfObjects; i++) {
                if (i != counter) {
                    session.insert(new BeanA(i));
                }
            }
            final QueryResults results = session.getQueryResults("Query");
            Assertions.assertThat(results).hasSize(1);
            for (final QueryResultsRow row : results) {
                Assertions.assertThat(row.get("bean")).isEqualTo(bean);
            }
            return true;
        } finally {
            session.dispose();
        }
    };
    parallelTest(NUMBER_OF_THREADS, exec);
}
Also used : Arrays(java.util.Arrays) QueryResultsRow(org.kie.api.runtime.rule.QueryResultsRow) BeanA(org.drools.compiler.integrationtests.facts.BeanA) BeanB(org.drools.compiler.integrationtests.facts.BeanB) RunWith(org.junit.runner.RunWith) Test(org.junit.Test) ArrayList(java.util.ArrayList) List(java.util.List) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) QueryResults(org.kie.api.runtime.rule.QueryResults) Assertions(org.assertj.core.api.Assertions) KieBase(org.kie.api.KieBase) KieSession(org.kie.api.runtime.KieSession) Parameterized(org.junit.runners.Parameterized) BeanA(org.drools.compiler.integrationtests.facts.BeanA) QueryResultsRow(org.kie.api.runtime.rule.QueryResultsRow) KieBase(org.kie.api.KieBase) KieSession(org.kie.api.runtime.KieSession) QueryResults(org.kie.api.runtime.rule.QueryResults) Test(org.junit.Test)

Example 7 with BeanA

use of org.drools.compiler.integrationtests.facts.BeanA in project drools by kiegroup.

the class ConcurrentBasesParallelTest method testFireAndGlobalSeparation.

@Test(timeout = 20000)
public void testFireAndGlobalSeparation() throws InterruptedException {
    final TestExecutor exec = counter -> {
        final String rule = "import " + BeanA.class.getCanonicalName() + ";\n" + "global " + AtomicInteger.class.getCanonicalName() + " result;\n" + "rule Rule_" + counter + " " + "when " + "    BeanA()" + "then " + "    result.set(" + counter + ");" + "end";
        final KieBase base = getKieBase(rule);
        final KieSession session = base.newKieSession();
        try {
            session.insert(new BeanA());
            final AtomicInteger r = new AtomicInteger(0);
            session.setGlobal("result", r);
            Assertions.assertThat(session.fireAllRules()).isEqualTo(1);
            Assertions.assertThat(r.get()).isEqualTo(counter);
            return true;
        } finally {
            session.dispose();
        }
    };
    parallelTest(NUMBER_OF_THREADS, exec);
}
Also used : Arrays(java.util.Arrays) QueryResultsRow(org.kie.api.runtime.rule.QueryResultsRow) BeanA(org.drools.compiler.integrationtests.facts.BeanA) BeanB(org.drools.compiler.integrationtests.facts.BeanB) RunWith(org.junit.runner.RunWith) Test(org.junit.Test) ArrayList(java.util.ArrayList) List(java.util.List) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) QueryResults(org.kie.api.runtime.rule.QueryResults) Assertions(org.assertj.core.api.Assertions) KieBase(org.kie.api.KieBase) KieSession(org.kie.api.runtime.KieSession) Parameterized(org.junit.runners.Parameterized) BeanA(org.drools.compiler.integrationtests.facts.BeanA) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) KieBase(org.kie.api.KieBase) KieSession(org.kie.api.runtime.KieSession) Test(org.junit.Test)

Example 8 with BeanA

use of org.drools.compiler.integrationtests.facts.BeanA in project drools by kiegroup.

the class SharedSessionParallelTest method testCorrectFirings.

@Test(timeout = 20000)
public void testCorrectFirings() throws InterruptedException {
    final int threadCount = 100;
    final String drl = "import " + BeanA.class.getCanonicalName() + ";\n" + "global java.util.List globalList;\n" + "rule R1 " + "when " + "    BeanA($n : seed) " + "then " + "    globalList.add(\"\" + $n);" + "end";
    final KieSession kieSession = getKieBase(drl).newKieSession();
    final List<String> list = Collections.synchronizedList(new ArrayList<>());
    final TestExecutor exec = counter -> {
        kieSession.setGlobal("globalList", list);
        kieSession.insert(new BeanA(counter));
        kieSession.fireAllRules();
        return true;
    };
    parallelTest(threadCount, exec);
    kieSession.dispose();
    checkList(threadCount, list);
}
Also used : BeanA(org.drools.compiler.integrationtests.facts.BeanA) CountDownLatch(java.util.concurrent.CountDownLatch) Arrays(java.util.Arrays) List(java.util.List) CyclicBarrier(java.util.concurrent.CyclicBarrier) BeanA(org.drools.compiler.integrationtests.facts.BeanA) RunWith(org.junit.runner.RunWith) Assertions(org.assertj.core.api.Assertions) Test(org.junit.Test) KieSession(org.kie.api.runtime.KieSession) Collections(java.util.Collections) Parameterized(org.junit.runners.Parameterized) ArrayList(java.util.ArrayList) KieSession(org.kie.api.runtime.KieSession) Test(org.junit.Test)

Example 9 with BeanA

use of org.drools.compiler.integrationtests.facts.BeanA in project drools by kiegroup.

the class SharedSessionParallelTest method testLongRunningRule2.

@Test(timeout = 20000)
public void testLongRunningRule2() throws InterruptedException {
    final int threadCount = 100;
    final int seed = 1000;
    final String waitingRule = "rule waitingRule " + "when " + "    String( this == \"wait\" ) " + "then " + "end";
    final String longRunningDrl = "import " + BeanA.class.getCanonicalName() + ";\n" + "global java.util.List list;\n" + "rule longRunning " + "when " + "    $bean : BeanA($n : seed, seed > 0 ) " + "then " + "    modify($bean) { setSeed($n-1) };" + "    list.add(\"\" + $bean.getSeed());" + "end";
    final KieSession kieSession = getKieBase(longRunningDrl, waitingRule).newKieSession();
    final CyclicBarrier barrier = new CyclicBarrier(threadCount);
    final List<String> list = Collections.synchronizedList(new ArrayList<>());
    final TestExecutor exec = counter -> {
        try {
            if (counter == 0) {
                kieSession.setGlobal("list", list);
                kieSession.insert("wait");
                kieSession.insert(new BeanA(seed));
                barrier.await();
                kieSession.fireAllRules();
                return true;
            } else {
                barrier.await();
                kieSession.insert(new BeanA(seed));
                kieSession.fireAllRules();
                return true;
            }
        } catch (final Exception ex) {
            throw new RuntimeException(ex);
        }
    };
    parallelTest(threadCount, exec);
    kieSession.dispose();
    checkList(0, seed, list, seed * threadCount);
}
Also used : CountDownLatch(java.util.concurrent.CountDownLatch) Arrays(java.util.Arrays) List(java.util.List) CyclicBarrier(java.util.concurrent.CyclicBarrier) BeanA(org.drools.compiler.integrationtests.facts.BeanA) RunWith(org.junit.runner.RunWith) Assertions(org.assertj.core.api.Assertions) Test(org.junit.Test) KieSession(org.kie.api.runtime.KieSession) Collections(java.util.Collections) Parameterized(org.junit.runners.Parameterized) ArrayList(java.util.ArrayList) BeanA(org.drools.compiler.integrationtests.facts.BeanA) KieSession(org.kie.api.runtime.KieSession) CyclicBarrier(java.util.concurrent.CyclicBarrier) Test(org.junit.Test)

Example 10 with BeanA

use of org.drools.compiler.integrationtests.facts.BeanA in project drools by kiegroup.

the class SharedSessionParallelTest method testLongRunningRule3.

@Test(timeout = 20000)
public void testLongRunningRule3() throws InterruptedException {
    final int threadCount = 10;
    final int seed = threadCount + 50;
    final int objectCount = 1000;
    final String longRunningDrl = "import " + BeanA.class.getCanonicalName() + ";\n" + "global java.util.List list;\n" + "rule longRunning " + "when " + "    $bean : BeanA($n : seed, seed > " + threadCount + ") " + "then " + "    modify($bean) { setSeed($n-1) };" + "    list.add(\"\" + $bean.getSeed());" + "end";
    final String listDrl = "global java.util.List list2;\n" + "rule listRule " + "when " + "    BeanA($n : seed, seed < " + threadCount + ") " + "then " + "    list2.add(\"\" + $n);" + "end";
    final KieSession kieSession = getKieBase(longRunningDrl, listDrl).newKieSession();
    final CyclicBarrier barrier = new CyclicBarrier(threadCount);
    final List<String> list = Collections.synchronizedList(new ArrayList<>());
    final List<String> list2 = Collections.synchronizedList(new ArrayList<>());
    final TestExecutor exec = counter -> {
        try {
            if (counter % 2 == 0) {
                kieSession.setGlobal("list", list);
                kieSession.setGlobal("list2", list2);
                kieSession.insert(new BeanA(seed));
                barrier.await();
                kieSession.fireAllRules();
                return true;
            } else {
                barrier.await();
                for (int i = 0; i < objectCount; i++) {
                    kieSession.insert(new BeanA(counter));
                }
                kieSession.fireAllRules();
                return true;
            }
        } catch (final Exception ex) {
            throw new RuntimeException(ex);
        }
    };
    parallelTest(threadCount, exec);
    kieSession.dispose();
    final int listExpectedSize = (threadCount / 2 + threadCount % 2) * (seed - threadCount);
    final int list2ExpectedSize = threadCount / 2 * objectCount;
    for (int i = 0; i < threadCount; i++) {
        if (i % 2 == 1) {
            Assertions.assertThat(list2).contains("" + i);
        }
    }
    Assertions.assertThat(list).hasSize(listExpectedSize);
    Assertions.assertThat(list2).hasSize(list2ExpectedSize);
}
Also used : CountDownLatch(java.util.concurrent.CountDownLatch) Arrays(java.util.Arrays) List(java.util.List) CyclicBarrier(java.util.concurrent.CyclicBarrier) BeanA(org.drools.compiler.integrationtests.facts.BeanA) RunWith(org.junit.runner.RunWith) Assertions(org.assertj.core.api.Assertions) Test(org.junit.Test) KieSession(org.kie.api.runtime.KieSession) Collections(java.util.Collections) Parameterized(org.junit.runners.Parameterized) ArrayList(java.util.ArrayList) BeanA(org.drools.compiler.integrationtests.facts.BeanA) KieSession(org.kie.api.runtime.KieSession) CyclicBarrier(java.util.concurrent.CyclicBarrier) Test(org.junit.Test)

Aggregations

ArrayList (java.util.ArrayList)11 Arrays (java.util.Arrays)11 List (java.util.List)11 Assertions (org.assertj.core.api.Assertions)11 BeanA (org.drools.compiler.integrationtests.facts.BeanA)11 Test (org.junit.Test)11 RunWith (org.junit.runner.RunWith)11 Parameterized (org.junit.runners.Parameterized)11 KieSession (org.kie.api.runtime.KieSession)11 Collections (java.util.Collections)9 CountDownLatch (java.util.concurrent.CountDownLatch)9 CyclicBarrier (java.util.concurrent.CyclicBarrier)9 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)2 BeanB (org.drools.compiler.integrationtests.facts.BeanB)2 KieBase (org.kie.api.KieBase)2 QueryResults (org.kie.api.runtime.rule.QueryResults)2 QueryResultsRow (org.kie.api.runtime.rule.QueryResultsRow)2