Search in sources :

Example 11 with KieHelper

use of org.kie.internal.utils.KieHelper in project drools by kiegroup.

the class MultithreadTest method testConcurrentFireAndDispose.

@Test(timeout = 10000)
public void testConcurrentFireAndDispose() 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);
    final Thread t1 = new Thread() {

        @Override
        public void run() {
            LOG.info("before: sleep, dispose().");
            try {
                Thread.sleep(100);
            } catch (final InterruptedException _e) {
            }
            LOG.info("before: dispose().");
            ksession.dispose();
            LOG.info("after: dispose().");
        }
    };
    t1.setDaemon(true);
    t1.start();
    try {
        int i = 0;
        LOG.info("before: while.");
        while (true) {
            ksession.insert("" + i++);
            ksession.fireAllRules();
        }
    } catch (final IllegalStateException e) {
        // java.lang.IllegalStateException: Illegal method call. This session was previously disposed.
        // ignore and exit
        LOG.info("after: while.");
    } catch (final java.util.concurrent.RejectedExecutionException e) {
        e.printStackTrace();
        Assertions.fail("java.util.concurrent.RejectedExecutionException should not happen");
    }
    LOG.info("last line of test.");
}
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) EntryPoint(org.kie.api.runtime.rule.EntryPoint) Test(org.junit.Test)

Example 12 with KieHelper

use of org.kie.internal.utils.KieHelper in project drools by kiegroup.

the class MultithreadTest method testJittingShortComparison.

@Test(timeout = 20000)
public void testJittingShortComparison() {
    // DROOLS-1633
    final String drl = "import " + BeanA.class.getCanonicalName() + "\n;" + "global java.util.List list;" + "rule R when\n" + "  $a1: BeanA($sv1 : shortValue)\n" + "  $b2: BeanA(shortValue != $sv1)\n" + "then\n" + "  list.add(\"FIRED\");\n" + "end";
    final List<String> list = Collections.synchronizedList(new ArrayList());
    final RuleBaseConfiguration rbc = (RuleBaseConfiguration) KieServices.Factory.get().newKieBaseConfiguration();
    rbc.setJittingThreshold(0);
    final KieBase kbase = new KieHelper().addContent(drl, ResourceType.DRL).build(rbc);
    final int threadNr = 1000;
    final Thread[] threads = new Thread[threadNr];
    for (int i = 0; i < threadNr; i++) {
        threads[i] = new Thread(new SessionRunner(kbase, list));
    }
    for (int i = 0; i < threadNr; i++) {
        threads[i].start();
    }
    for (int i = 0; i < threadNr; i++) {
        try {
            threads[i].join();
        } catch (final InterruptedException e) {
            throw new RuntimeException(e);
        }
    }
    Assertions.assertThat(list).hasSize(0);
}
Also used : ArrayList(java.util.ArrayList) KieHelper(org.kie.internal.utils.KieHelper) EntryPoint(org.kie.api.runtime.rule.EntryPoint) RuleBaseConfiguration(org.drools.core.RuleBaseConfiguration) KieBase(org.kie.api.KieBase) Test(org.junit.Test)

Example 13 with KieHelper

use of org.kie.internal.utils.KieHelper 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 14 with KieHelper

use of org.kie.internal.utils.KieHelper 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 15 with KieHelper

use of org.kie.internal.utils.KieHelper in project drools by kiegroup.

the class MarshallingTest method testMarshallWithTimedRule.

@Test
public void testMarshallWithTimedRule() {
    // DROOLS-795
    String drl = "rule \"Rule A Timeout\"\n" + "when\n" + "    String( this == \"ATrigger\" )\n" + "then\n" + "   insert (new String( \"A-Timer\") );\n" + "end\n" + "\n" + "rule \"Timer For rule A Timeout\"\n" + "    timer ( int: 5s )\n" + "when\n" + "   String( this == \"A-Timer\")\n" + "then\n" + "   delete ( \"A-Timer\" );\n" + "   delete ( \"ATrigger\" );\n" + "end\n";
    KieBase kbase = new KieHelper().addContent(drl, ResourceType.DRL).build(EqualityBehaviorOption.EQUALITY, DeclarativeAgendaOption.ENABLED, EventProcessingOption.STREAM);
    KieSessionConfiguration sessionConfig = KnowledgeBaseFactory.newKnowledgeSessionConfiguration();
    sessionConfig.setOption(ClockTypeOption.get("pseudo"));
    KieSession ksession = kbase.newKieSession(sessionConfig, null);
    ksession.insert("ATrigger");
    assertEquals(1, ksession.getFactCount());
    ksession.fireAllRules();
    assertEquals(2, ksession.getFactCount());
    SessionPseudoClock clock = ksession.getSessionClock();
    clock.advanceTime(4, TimeUnit.SECONDS);
    assertEquals(2, ksession.getFactCount());
    ksession.fireAllRules();
    assertEquals(2, ksession.getFactCount());
    ksession = marshallAndUnmarshall(kbase, ksession, sessionConfig);
    clock = ksession.getSessionClock();
    clock.advanceTime(4, TimeUnit.SECONDS);
    assertEquals(2, ksession.getFactCount());
    ksession.fireAllRules();
    assertEquals(0, ksession.getFactCount());
}
Also used : SessionPseudoClock(org.kie.api.time.SessionPseudoClock) 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)

Aggregations

KieHelper (org.kie.internal.utils.KieHelper)467 Test (org.junit.Test)427 KieSession (org.kie.api.runtime.KieSession)355 ArrayList (java.util.ArrayList)194 KieBase (org.kie.api.KieBase)152 StatelessKieSession (org.kie.api.runtime.StatelessKieSession)77 Person (org.drools.compiler.Person)61 FactHandle (org.kie.api.runtime.rule.FactHandle)55 List (java.util.List)44 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)41 RuleUnitExecutor (org.kie.api.runtime.rule.RuleUnitExecutor)38 InternalRuleUnitExecutor (org.drools.core.impl.InternalRuleUnitExecutor)37 KieSessionConfiguration (org.kie.api.runtime.KieSessionConfiguration)34 Man (org.drools.compiler.oopath.model.Man)29 Child (org.drools.compiler.oopath.model.Child)24 Woman (org.drools.compiler.oopath.model.Woman)23 InternalWorkingMemory (org.drools.core.common.InternalWorkingMemory)23 InternalFactHandle (org.drools.core.common.InternalFactHandle)18 ObjectTypeNode (org.drools.core.reteoo.ObjectTypeNode)18 PseudoClockScheduler (org.drools.core.time.impl.PseudoClockScheduler)18