Search in sources :

Example 36 with InternalWorkingMemory

use of org.drools.core.common.InternalWorkingMemory in project drools by kiegroup.

the class ParallelEvaluationTest method getMultipleParallelKieSessionsWithUpdatesCallable.

private Callable<Void> getMultipleParallelKieSessionsWithUpdatesCallable(KieBase kBase) {
    return new Callable<Void>() {

        @Override
        public Void call() {
            KieSession ksession = kBase.newKieSession();
            assertThat(((InternalWorkingMemory) ksession).getAgenda().isParallelAgenda()).as("Parallel agenda has to be enabled").isTrue();
            List<Integer> list = new DebugList<Integer>();
            ksession.setGlobal("list", list);
            FactHandle[] fhs = new FactHandle[10];
            fhs = insertFacts(ksession, 10);
            ksession.fireAllRules();
            assertThat(list.size()).isEqualTo(10);
            list.clear();
            for (int i = 0; i < 10; i++) {
                ksession.update(fhs[i], i);
            }
            ksession.fireAllRules();
            assertThat(list.size()).isEqualTo(10);
            return null;
        }
    };
}
Also used : InternalWorkingMemory(org.drools.core.common.InternalWorkingMemory) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) FactHandle(org.kie.api.runtime.rule.FactHandle) KieSession(org.kie.api.runtime.KieSession) DebugList(org.drools.compiler.util.debug.DebugList) Callable(java.util.concurrent.Callable)

Example 37 with InternalWorkingMemory

use of org.drools.core.common.InternalWorkingMemory in project drools by kiegroup.

the class ParallelEvaluationTest method testDisableParallelismOnSinglePartition.

@Test(timeout = 10000L)
public void testDisableParallelismOnSinglePartition() {
    String drl = "rule R1 when\n" + "    $i : Integer( this == 4 )" + "    String( length > $i )\n" + "then end \n" + "rule R2 when\n" + "    $i : Integer( this == 4 )" + "    String( length == $i )\n" + "then end \n" + "rule R3 when\n" + "    $i : Integer( this == 4 )" + "    String( length < $i )\n" + "then end \n";
    KieSession ksession = new KieHelper().addContent(drl, ResourceType.DRL).build(MultithreadEvaluationOption.YES).newKieSession();
    InternalWorkingMemory session = (InternalWorkingMemory) ksession;
    // since there is only one partition the multithread evaluation should be disabled and run with the DefaultAgenda
    assertFalse(((InternalWorkingMemory) ksession).getAgenda().isParallelAgenda());
}
Also used : InternalWorkingMemory(org.drools.core.common.InternalWorkingMemory) KieHelper(org.kie.internal.utils.KieHelper) KieSession(org.kie.api.runtime.KieSession) Test(org.junit.Test)

Example 38 with InternalWorkingMemory

use of org.drools.core.common.InternalWorkingMemory in project drools by kiegroup.

the class ParallelEvaluationTest method testImmediateEventsExpiration.

@Test(timeout = 10000L)
public void testImmediateEventsExpiration() {
    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( 1ms ) @timestamp( timestamp ) end\n");
    for (int i = 0; i < 10; i++) {
        sb.append(getRuleWithEvent(i));
    }
    KieSessionConfiguration sessionConfig = KnowledgeBaseFactory.newKnowledgeSessionConfiguration();
    KieSession ksession = new KieHelper().addContent(sb.toString(), ResourceType.DRL).build(EventProcessingOption.STREAM, MultithreadEvaluationOption.YES).newKieSession(sessionConfig, null);
    assertTrue(((InternalWorkingMemory) ksession).getAgenda().isParallelAgenda());
    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());
}
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) Test(org.junit.Test)

Example 39 with InternalWorkingMemory

use of org.drools.core.common.InternalWorkingMemory in project drools by kiegroup.

the class ParallelEvaluationTest method testFireUntilHalt.

@Test(timeout = 10000L)
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, ""));
    }
    KieSession ksession = new KieHelper().addContent(sb.toString(), ResourceType.DRL).build(MultithreadEvaluationOption.YES).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();
    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());
}
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) CountDownLatch(java.util.concurrent.CountDownLatch) DebugList(org.drools.compiler.util.debug.DebugList) InternalWorkingMemory(org.drools.core.common.InternalWorkingMemory) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) KieSession(org.kie.api.runtime.KieSession) Test(org.junit.Test)

Example 40 with InternalWorkingMemory

use of org.drools.core.common.InternalWorkingMemory in project drools by kiegroup.

the class ParallelEvaluationTest method testWithDeletes.

@Test(timeout = 10000L)
public void testWithDeletes() {
    StringBuilder sb = new StringBuilder(400);
    sb.append("global java.util.List list;\n");
    for (int i = 1; i < 11; i++) {
        sb.append(getRule(i, "delete( $i );\n"));
    }
    for (int i = 1; i < 11; i++) {
        sb.append(getNotRule(i));
    }
    KieSession ksession = new KieHelper().addContent(sb.toString(), ResourceType.DRL).build(MultithreadEvaluationOption.YES).newKieSession();
    assertTrue(((InternalWorkingMemory) ksession).getAgenda().isParallelAgenda());
    List<Integer> list = new DebugList<Integer>();
    ksession.setGlobal("list", list);
    for (int i = 1; i < 11; i++) {
        ksession.insert(i);
        ksession.insert("" + i);
    }
    ksession.fireAllRules();
    assertEquals(20, list.size());
}
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) Test(org.junit.Test)

Aggregations

InternalWorkingMemory (org.drools.core.common.InternalWorkingMemory)147 Test (org.junit.Test)107 ObjectTypeNode (org.drools.core.reteoo.ObjectTypeNode)72 KieBase (org.kie.api.KieBase)53 LeftInputAdapterNode (org.drools.core.reteoo.LeftInputAdapterNode)47 ArrayList (java.util.ArrayList)46 InternalKnowledgeBase (org.drools.core.impl.InternalKnowledgeBase)45 SegmentMemory (org.drools.core.reteoo.SegmentMemory)42 List (java.util.List)39 RuleTerminalNode (org.drools.core.reteoo.RuleTerminalNode)36 BetaMemory (org.drools.core.reteoo.BetaMemory)33 KieSession (org.kie.api.runtime.KieSession)32 JoinNode (org.drools.core.reteoo.JoinNode)30 PathMemory (org.drools.core.reteoo.PathMemory)29 ClassObjectType (org.drools.core.base.ClassObjectType)26 LiaNodeMemory (org.drools.core.reteoo.LeftInputAdapterNode.LiaNodeMemory)25 FactHandle (org.kie.api.runtime.rule.FactHandle)23 BetaNode (org.drools.core.reteoo.BetaNode)22 KieHelper (org.kie.internal.utils.KieHelper)22 AlphaNode (org.drools.core.reteoo.AlphaNode)19