Search in sources :

Example 31 with InternalWorkingMemory

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

the class RemoveRuleTest method testPopulatedMultipleSharesRemoveLast.

@Test
public void testPopulatedMultipleSharesRemoveLast() throws Exception {
    InternalKnowledgeBase kbase1 = buildKnowledgeBase("r1", "   A(1;)  A(2;) B(1;) B(2;) C(1;) D() E()\n");
    InternalWorkingMemory wm = ((InternalWorkingMemory) kbase1.newKieSession());
    List list = new ArrayList();
    wm.setGlobal("list", list);
    InternalFactHandle fh1 = (InternalFactHandle) wm.insert(new A(1));
    InternalFactHandle fh2 = (InternalFactHandle) wm.insert(new A(2));
    InternalFactHandle fh3 = (InternalFactHandle) wm.insert(new A(2));
    InternalFactHandle fh4 = (InternalFactHandle) wm.insert(new A(3));
    InternalFactHandle fh5 = (InternalFactHandle) wm.insert(new B(1));
    InternalFactHandle fh6 = (InternalFactHandle) wm.insert(new B(2));
    InternalFactHandle fh7 = (InternalFactHandle) wm.insert(new C(1));
    InternalFactHandle fh8 = (InternalFactHandle) wm.insert(new C(2));
    InternalFactHandle fh9 = (InternalFactHandle) wm.insert(new D(1));
    InternalFactHandle fh10 = (InternalFactHandle) wm.insert(new E(1));
    wm.fireAllRules();
    assertEquals(2, list.size());
    kbase1.addPackages(buildKnowledgePackage("r2", "   A(1;)  A(2;) B(1;) B(2;) C(2;) D() E()\n"));
    kbase1.addPackages(buildKnowledgePackage("r3", "   A(1;)  A(3;) B(1;) B(2;) C(2;) D() E()\n"));
    wm.fireAllRules();
    assertEquals(5, list.size());
    kbase1.removeRule("org.kie", "r3");
    list.clear();
    wm.update(fh1, fh1.getObject());
    wm.update(fh2, fh2.getObject());
    wm.update(fh3, fh3.getObject());
    wm.update(fh4, fh4.getObject());
    wm.update(fh5, fh5.getObject());
    wm.update(fh6, fh6.getObject());
    wm.update(fh7, fh7.getObject());
    wm.update(fh8, fh8.getObject());
    wm.update(fh9, fh9.getObject());
    wm.update(fh10, fh10.getObject());
    wm.fireAllRules();
    assertEquals(4, list.size());
}
Also used : InternalWorkingMemory(org.drools.core.common.InternalWorkingMemory) ArrayList(java.util.ArrayList) ArrayList(java.util.ArrayList) List(java.util.List) InternalFactHandle(org.drools.core.common.InternalFactHandle) InternalKnowledgeBase(org.drools.core.impl.InternalKnowledgeBase) Test(org.junit.Test)

Example 32 with InternalWorkingMemory

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

the class RemoveRuleTest method testPopulatedRuleWithEvals.

@Test
public void testPopulatedRuleWithEvals() throws Exception {
    InternalKnowledgeBase kbase1 = buildKnowledgeBase("r1", "   a:A() B() eval(1==1) eval(1==1) C(1;) \n");
    InternalWorkingMemory wm = ((InternalWorkingMemory) kbase1.newKieSession());
    List list = new ArrayList();
    wm.setGlobal("list", list);
    wm.insert(new A(1));
    wm.insert(new A(2));
    wm.insert(new A(3));
    wm.insert(new B(1));
    wm.insert(new C(1));
    wm.insert(new C(2));
    wm.insert(new D(1));
    wm.insert(new E(1));
    wm.fireAllRules();
    assertEquals(7, countNodeMemories(wm.getNodeMemories()));
    kbase1.addPackages(buildKnowledgePackage("r2", "   a:A() B() eval(1==1) eval(1==1) C(2;) \n"));
    wm.fireAllRules();
    ObjectTypeNode aotn = getObjectTypeNode(kbase1, A.class);
    LeftInputAdapterNode liaNode = (LeftInputAdapterNode) aotn.getObjectSinkPropagator().getSinks()[0];
    JoinNode bNode = (JoinNode) liaNode.getSinkPropagator().getFirstLeftTupleSink();
    EvalConditionNode e1 = (EvalConditionNode) bNode.getSinkPropagator().getFirstLeftTupleSink();
    EvalConditionNode e2 = (EvalConditionNode) e1.getSinkPropagator().getFirstLeftTupleSink();
    JoinNode c1Node = (JoinNode) e2.getSinkPropagator().getFirstLeftTupleSink();
    JoinNode c2Node = (JoinNode) e2.getSinkPropagator().getLastLeftTupleSink();
    LiaNodeMemory lm = (LiaNodeMemory) wm.getNodeMemory(liaNode);
    SegmentMemory sm = lm.getSegmentMemory();
    BetaMemory c1Mem = (BetaMemory) wm.getNodeMemory(c1Node);
    assertSame(sm.getFirst(), c1Mem.getSegmentMemory());
    assertEquals(3, c1Mem.getLeftTupleMemory().size());
    assertEquals(1, c1Mem.getRightTupleMemory().size());
    BetaMemory c2Mem = (BetaMemory) wm.getNodeMemory(c2Node);
    SegmentMemory c2Smem = sm.getFirst().getNext();
    assertSame(c2Smem, c2Mem.getSegmentMemory());
    assertEquals(3, c2Mem.getLeftTupleMemory().size());
    assertEquals(1, c2Mem.getRightTupleMemory().size());
    assertEquals(6, list.size());
    kbase1.removeRule("org.kie", "r2");
    assertEquals(8, countNodeMemories(wm.getNodeMemories()));
    assertNull(sm.getFirst());
    // c1SMem repoints back to original Smem
    assertSame(sm, c1Mem.getSegmentMemory());
    wm.insert(new A(1));
    wm.fireAllRules();
    assertEquals("r1", ((Match) list.get(6)).getRule().getName());
    // only one more added, as second rule as removed
    assertEquals(7, list.size());
}
Also used : SegmentMemory(org.drools.core.reteoo.SegmentMemory) EvalConditionNode(org.drools.core.reteoo.EvalConditionNode) JoinNode(org.drools.core.reteoo.JoinNode) ArrayList(java.util.ArrayList) ObjectTypeNode(org.drools.core.reteoo.ObjectTypeNode) BetaMemory(org.drools.core.reteoo.BetaMemory) LiaNodeMemory(org.drools.core.reteoo.LeftInputAdapterNode.LiaNodeMemory) Match(org.kie.api.runtime.rule.Match) InternalWorkingMemory(org.drools.core.common.InternalWorkingMemory) ArrayList(java.util.ArrayList) List(java.util.List) InternalKnowledgeBase(org.drools.core.impl.InternalKnowledgeBase) LeftInputAdapterNode(org.drools.core.reteoo.LeftInputAdapterNode) Test(org.junit.Test)

Example 33 with InternalWorkingMemory

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

the class RemoveRuleTest method testPopulatedSharedToRtn.

@Test
public void testPopulatedSharedToRtn() throws Exception {
    InternalKnowledgeBase kbase1 = buildKnowledgeBase("r1", "   A() B() C() D() E()\n");
    InternalWorkingMemory wm = ((InternalWorkingMemory) kbase1.newKieSession());
    List list = new ArrayList();
    wm.setGlobal("list", list);
    wm.insert(new A(1));
    wm.insert(new A(2));
    wm.insert(new B(1));
    wm.insert(new C(1));
    wm.insert(new D(1));
    wm.insert(new E(1));
    wm.fireAllRules();
    assertEquals(2, list.size());
    assertEquals(7, countNodeMemories(wm.getNodeMemories()));
    kbase1.addPackages(buildKnowledgePackage("r2", "   A() B() C() D() E()\n"));
    wm.fireAllRules();
    assertEquals(8, countNodeMemories(wm.getNodeMemories()));
    assertEquals(4, list.size());
    RuleTerminalNode rtn1 = getRtn("org.kie.r1", kbase1);
    RuleTerminalNode rtn2 = getRtn("org.kie.r2", kbase1);
    PathMemory pmem1 = (PathMemory) wm.getNodeMemory(rtn1);
    PathMemory pmem2 = (PathMemory) wm.getNodeMemory(rtn2);
    SegmentMemory[] smems1 = pmem1.getSegmentMemories();
    SegmentMemory[] smems2 = pmem2.getSegmentMemories();
    assertEquals(2, smems1.length);
    assertEquals(2, smems2.length);
    assertSame(smems1[0], smems2[0]);
    assertNotSame(smems1[1], smems2[1]);
    SegmentMemory sm = smems1[0];
    assertEquals(smems1[1], sm.getFirst());
    JoinNode eNode1 = (JoinNode) rtn1.getLeftTupleSource();
    JoinNode eNode2 = (JoinNode) rtn2.getLeftTupleSource();
    assertSame(eNode1, eNode2);
    pmem1 = (PathMemory) wm.getNodeMemory(rtn1);
    kbase1.removeRule("org.kie", "r2");
    System.out.println("---");
    assertEquals(7, countNodeMemories(wm.getNodeMemories()));
    assertNull(sm.getFirst());
    pmem1 = (PathMemory) wm.getNodeMemory(rtn1);
    smems1 = pmem1.getSegmentMemories();
    assertEquals(1, smems1.length);
    assertSame(sm, smems1[0]);
    wm.insert(new A(1));
    wm.fireAllRules();
    assertEquals("r1", ((Match) list.get(4)).getRule().getName());
    // only one more added, as second rule as removed
    assertEquals(5, list.size());
}
Also used : SegmentMemory(org.drools.core.reteoo.SegmentMemory) JoinNode(org.drools.core.reteoo.JoinNode) ArrayList(java.util.ArrayList) Match(org.kie.api.runtime.rule.Match) InternalWorkingMemory(org.drools.core.common.InternalWorkingMemory) ArrayList(java.util.ArrayList) List(java.util.List) InternalKnowledgeBase(org.drools.core.impl.InternalKnowledgeBase) RuleTerminalNode(org.drools.core.reteoo.RuleTerminalNode) PathMemory(org.drools.core.reteoo.PathMemory) Test(org.junit.Test)

Example 34 with InternalWorkingMemory

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

the class ParallelEvaluationTest method testWithInsertions.

@Test(timeout = 10000L)
public void testWithInsertions() {
    StringBuilder sb = new StringBuilder(4000);
    sb.append("global java.util.List list;\n");
    int ruleNr = 200;
    for (int i = 0; i < ruleNr; i++) {
        sb.append(getRule(i, "insert( $i + 10 );\ninsert( \"\" + ($i + 10) );\n"));
    }
    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 = 0; i < 10; i++) {
        ksession.insert(i);
        ksession.insert("" + i);
    }
    ksession.fireAllRules();
    assertEquals(ruleNr, 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)

Example 35 with InternalWorkingMemory

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

the class ParallelEvaluationTest method getMultipleParallelKieSessionsFireUntilHaltCallable.

private Callable<Void> getMultipleParallelKieSessionsFireUntilHaltCallable(KieBase kBase, boolean asyncInsert) {
    return () -> {
        KieSession ksession = kBase.newKieSession();
        assertThat(((InternalWorkingMemory) ksession).getAgenda().isParallelAgenda()).isTrue();
        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();
        if (asyncInsert) {
            StatefulKnowledgeSessionImpl session = (StatefulKnowledgeSessionImpl) ksession;
            for (int i = 0; i < 10; i++) {
                session.insertAsync(i);
                session.insertAsync("" + String.valueOf(i));
            }
        } else {
            insertFacts(ksession, 10);
        }
        try {
            done.await();
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }
        assertThat(list.size()).isEqualTo(10);
        return null;
    };
}
Also used : InternalWorkingMemory(org.drools.core.common.InternalWorkingMemory) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) StatefulKnowledgeSessionImpl(org.drools.core.impl.StatefulKnowledgeSessionImpl) KieSession(org.kie.api.runtime.KieSession) CountDownLatch(java.util.concurrent.CountDownLatch) DebugList(org.drools.compiler.util.debug.DebugList)

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