Search in sources :

Example 66 with KieSession

use of org.kie.api.runtime.KieSession in project drools by kiegroup.

the class MultithreadTest method testFireUntilHaltAndDispose.

@Test(timeout = 10000)
public void testFireUntilHaltAndDispose() throws InterruptedException {
    // DROOLS-1103
    final String drl = "rule R no-loop timer( int: 1s )\n" + "when\n" + "    String()\n" + "then\n" + "end";
    final KieHelper helper = new KieHelper();
    helper.addContent(drl, ResourceType.DRL);
    final KieBase kbase = helper.build(EventProcessingOption.STREAM);
    final KieSessionConfiguration ksconf = KieServices.Factory.get().newKieSessionConfiguration();
    ksconf.setOption(TimedRuleExecutionOption.YES);
    final KieSession ksession = kbase.newKieSession(ksconf, null);
    new Thread() {

        @Override
        public void run() {
            ksession.fireUntilHalt();
        }
    }.start();
    try {
        Thread.sleep(100);
    } catch (final InterruptedException e) {
    // do nothing
    }
    ksession.insert("xxx");
    try {
        Thread.sleep(100);
    } catch (final InterruptedException e) {
    // do nothing
    }
    ksession.dispose();
}
Also used : KieBase(org.kie.api.KieBase) KieHelper(org.kie.internal.utils.KieHelper) KieSession(org.kie.api.runtime.KieSession) KieSessionConfiguration(org.kie.api.runtime.KieSessionConfiguration) Test(org.junit.Test)

Example 67 with KieSession

use of org.kie.api.runtime.KieSession in project drools by kiegroup.

the class MultithreadTest method testConcurrentDelete.

@Test(timeout = 20000)
public void testConcurrentDelete() {
    final String drl = "import " + SlowBean.class.getCanonicalName() + ";\n" + "rule R when\n" + "  $sb1: SlowBean() \n" + "  $sb2: SlowBean( id > $sb1.id ) \n" + "then " + "  System.out.println($sb2 + \" > \"+ $sb1);" + "end\n";
    final KieSession ksession = new KieHelper().addContent(drl, ResourceType.DRL).build().newKieSession();
    final int BEAN_NR = 4;
    for (int step = 0; step < 2; step++) {
        final FactHandle[] fhs = new FactHandle[BEAN_NR];
        for (int i = 0; i < BEAN_NR; i++) {
            fhs[i] = ksession.insert(new SlowBean(i + step * BEAN_NR));
        }
        final CyclicBarrier barrier = new CyclicBarrier(2);
        new Thread(new Runnable() {

            @Override
            public void run() {
                ksession.fireAllRules();
                try {
                    barrier.await();
                } catch (final Exception e) {
                    throw new RuntimeException(e);
                }
            }
        }).start();
        try {
            Thread.sleep(15L);
        } catch (final InterruptedException e) {
            throw new RuntimeException(e);
        }
        for (int i = 0; i < BEAN_NR; i++) {
            if (i % 2 == 1) {
                ksession.delete(fhs[i]);
            }
        }
        try {
            barrier.await();
        } catch (final Exception e) {
            throw new RuntimeException(e);
        }
        System.out.println("Done step " + step);
    }
}
Also used : FactHandle(org.kie.api.runtime.rule.FactHandle) KieHelper(org.kie.internal.utils.KieHelper) EntryPoint(org.kie.api.runtime.rule.EntryPoint) CyclicBarrier(java.util.concurrent.CyclicBarrier) KieSession(org.kie.api.runtime.KieSession) Test(org.junit.Test)

Example 68 with KieSession

use of org.kie.api.runtime.KieSession in project drools by kiegroup.

the class SharedSessionParallelTest method testCheckOneThreadOnly.

@Test(timeout = 20000)
public void testCheckOneThreadOnly() throws InterruptedException {
    final int threadCount = 100;
    final List<String> list = Collections.synchronizedList(new ArrayList<>());
    final String drl = "import " + BeanA.class.getCanonicalName() + ";\n" + "global java.util.List list;\n" + "rule R1 " + "when " + "    BeanA($n : seed) " + "then " + "    list.add(\"\" + $n);" + "end";
    final KieSession kieSession = getKieBase(drl).newKieSession();
    final CountDownLatch latch = new CountDownLatch(threadCount);
    final TestExecutor exec = counter -> {
        kieSession.setGlobal("list", list);
        kieSession.insert(new BeanA(counter));
        latch.countDown();
        if (counter == 0) {
            try {
                latch.await();
            } catch (final InterruptedException e) {
                e.printStackTrace();
                return false;
            }
            return kieSession.fireAllRules() == threadCount;
        }
        return true;
    };
    parallelTest(threadCount, exec);
    kieSession.dispose();
    Assertions.assertThat(list).hasSize(threadCount);
    for (int i = 0; i < threadCount; i++) {
        Assertions.assertThat(list).contains("" + i);
    }
}
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) CountDownLatch(java.util.concurrent.CountDownLatch) Test(org.junit.Test)

Example 69 with KieSession

use of org.kie.api.runtime.KieSession in project drools by kiegroup.

the class SharedSessionParallelTest method testNoExceptions.

@Test(timeout = 20000)
public void testNoExceptions() throws InterruptedException {
    final String drl = "rule R1 when String() then end";
    final int repetitions = 100;
    final int numberOfObjects = 1000;
    final int countOfThreads = 100;
    for (int i = 0; i < repetitions; i++) {
        final KieSession kieSession = getKieBase(drl).newKieSession();
        parallelTest(countOfThreads, counter -> {
            try {
                for (int j = 0; j < numberOfObjects; j++) {
                    kieSession.insert("test_" + numberOfObjects);
                }
                kieSession.fireAllRules();
                return true;
            } catch (final Exception ex) {
                throw new RuntimeException(ex);
            }
        });
        kieSession.dispose();
    }
}
Also used : KieSession(org.kie.api.runtime.KieSession) Test(org.junit.Test)

Example 70 with KieSession

use of org.kie.api.runtime.KieSession in project drools by kiegroup.

the class SharedSessionParallelTest method testCountdownBean.

@Test(timeout = 20000)
public void testCountdownBean() throws InterruptedException {
    final int threadCount = 100;
    final int seed = 1000;
    final String drl = "import " + BeanA.class.getCanonicalName() + ";\n" + "global java.util.List list;\n" + "rule countdown " + "when " + "    $bean : BeanA($n : seed, seed >  0 ) " + "then " + "    modify($bean) { setSeed($n-1) };" + "    list.add(\"\" + $bean.getSeed());" + "end";
    final KieSession kieSession = getKieBase(drl).newKieSession();
    final CyclicBarrier barrier = new CyclicBarrier(threadCount);
    final List<String> list = Collections.synchronizedList(new ArrayList<>());
    final BeanA bean = new BeanA(seed);
    final TestExecutor exec = counter -> {
        try {
            if (counter == 0) {
                kieSession.setGlobal("list", list);
                kieSession.insert(bean);
            }
            barrier.await();
            kieSession.fireAllRules();
            return true;
        } catch (final Exception ex) {
            throw new RuntimeException(ex);
        }
    };
    parallelTest(threadCount, exec);
    kieSession.dispose();
    checkList(seed, list);
    Assertions.assertThat(bean).hasFieldOrPropertyWithValue("seed", 0);
}
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) CyclicBarrier(java.util.concurrent.CyclicBarrier) Test(org.junit.Test)

Aggregations

KieSession (org.kie.api.runtime.KieSession)5328 Test (org.junit.Test)4824 KieBase (org.kie.api.KieBase)2414 ArrayList (java.util.ArrayList)2317 List (java.util.List)1105 StatelessKieSession (org.kie.api.runtime.StatelessKieSession)769 FactHandle (org.kie.api.runtime.rule.FactHandle)598 Person (org.drools.modelcompiler.domain.Person)519 HashMap (java.util.HashMap)416 ProcessInstance (org.kie.api.runtime.process.ProcessInstance)415 KieServices (org.kie.api.KieServices)382 KieHelper (org.kie.internal.utils.KieHelper)355 KieContainer (org.kie.api.runtime.KieContainer)298 RuntimeEngine (org.kie.api.runtime.manager.RuntimeEngine)265 InternalFactHandle (org.drools.core.common.InternalFactHandle)259 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)234 AbstractBaseTest (org.jbpm.test.util.AbstractBaseTest)234 ReleaseId (org.kie.api.builder.ReleaseId)232 KieSessionConfiguration (org.kie.api.runtime.KieSessionConfiguration)229 KnowledgeBuilder (org.kie.internal.builder.KnowledgeBuilder)207