Search in sources :

Example 46 with BetaMemory

use of org.drools.core.reteoo.BetaMemory in project drools by kiegroup.

the class LeftMemory method getLeftTuples.

public List<LeftTuple> getLeftTuples(Object... objects) {
    BetaNode node = scenario.getBetaNode();
    BetaMemory bm = scenario.getBm();
    TupleMemory ltm = bm.getLeftTupleMemory();
    InternalWorkingMemory wm = scenario.getWorkingMemory();
    if (objects == null) {
        objects = new Object[0];
    }
    List<LeftTuple> list = new ArrayList<LeftTuple>();
    for (Object object : objects) {
        InternalFactHandle fh = (InternalFactHandle) wm.insert(object);
        LeftTuple expectedLeftTuple = node.createLeftTuple(fh, node, true);
        expectedLeftTuple.setPropagationContext(new PhreakPropagationContext());
        list.add(expectedLeftTuple);
    }
    scenario.setTestLeftMemory(true);
    return list;
}
Also used : InternalWorkingMemory(org.drools.core.common.InternalWorkingMemory) PhreakPropagationContext(org.drools.core.common.PhreakPropagationContext) BetaNode(org.drools.core.reteoo.BetaNode) ArrayList(java.util.ArrayList) BetaMemory(org.drools.core.reteoo.BetaMemory) InternalFactHandle(org.drools.core.common.InternalFactHandle) LeftTuple(org.drools.core.reteoo.LeftTuple) TupleMemory(org.drools.core.reteoo.TupleMemory)

Example 47 with BetaMemory

use of org.drools.core.reteoo.BetaMemory in project drools by kiegroup.

the class RemoveRuleTest method testPopulatedSingleRuleNoSharing.

@Test
public void testPopulatedSingleRuleNoSharing() throws Exception {
    InternalKnowledgeBase kbase = KnowledgeBaseFactory.newKnowledgeBase();
    InternalWorkingMemory wm = ((InternalWorkingMemory) kbase.newKieSession());
    wm.insert(new A(1));
    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();
    kbase.addPackages(buildKnowledgePackage("r1", "   A() B() C(object == 2) D() E()\n"));
    List list = new ArrayList();
    wm.setGlobal("list", list);
    ObjectTypeNode aotn = getObjectTypeNode(kbase, A.class);
    LeftInputAdapterNode liaNode = (LeftInputAdapterNode) aotn.getObjectSinkPropagator().getSinks()[0];
    LiaNodeMemory lm = (LiaNodeMemory) wm.getNodeMemory(liaNode);
    SegmentMemory sm = lm.getSegmentMemory();
    assertNotNull(sm.getStagedLeftTuples().getInsertFirst());
    wm.fireAllRules();
    BetaMemory bMem = (BetaMemory) sm.getNodeMemories().get(1);
    assertEquals(1, bMem.getLeftTupleMemory().size());
    assertEquals(1, bMem.getRightTupleMemory().size());
    BetaMemory eMem = (BetaMemory) sm.getNodeMemories().get(4);
    assertEquals(1, eMem.getLeftTupleMemory().size());
    assertEquals(1, eMem.getRightTupleMemory().size());
    NodeMemories nms = wm.getNodeMemories();
    assertEquals(12, countNodeMemories(nms));
    assertNull(sm.getStagedLeftTuples().getInsertFirst());
    assertEquals(1, list.size());
    assertEquals("r1", ((Match) list.get(0)).getRule().getName());
    kbase.removeRule("org.kie", "r1");
    // still has OTN
    assertEquals(6, countNodeMemories(nms));
}
Also used : SegmentMemory(org.drools.core.reteoo.SegmentMemory) 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) NodeMemories(org.drools.core.common.NodeMemories) InternalKnowledgeBase(org.drools.core.impl.InternalKnowledgeBase) LeftInputAdapterNode(org.drools.core.reteoo.LeftInputAdapterNode) Test(org.junit.Test)

Example 48 with BetaMemory

use of org.drools.core.reteoo.BetaMemory in project drools by kiegroup.

the class RemoveRuleTest method testPopulatedSharedLiaNode.

@Test
public void testPopulatedSharedLiaNode() throws Exception {
    InternalKnowledgeBase kbase1 = buildKnowledgeBase("r1", "   A() B(1;) 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 A(3));
    wm.insert(new B(1));
    wm.insert(new B(2));
    wm.insert(new C(1));
    wm.insert(new D(1));
    wm.insert(new E(1));
    wm.fireAllRules();
    assertEquals(3, list.size());
    assertEquals(7, countNodeMemories(wm.getNodeMemories()));
    kbase1.addPackages(buildKnowledgePackage("r2", "   a : A() B(2;) C() D() E()\n"));
    wm.fireAllRules();
    assertEquals(17, countNodeMemories(wm.getNodeMemories()));
    ObjectTypeNode aotn = getObjectTypeNode(kbase1, A.class);
    LeftInputAdapterNode liaNode = (LeftInputAdapterNode) aotn.getObjectSinkPropagator().getSinks()[0];
    JoinNode b1Node = (JoinNode) liaNode.getSinkPropagator().getFirstLeftTupleSink();
    JoinNode b2Node = (JoinNode) liaNode.getSinkPropagator().getLastLeftTupleSink();
    JoinNode c1Node = (JoinNode) b1Node.getSinkPropagator().getLastLeftTupleSink();
    LiaNodeMemory lm = (LiaNodeMemory) wm.getNodeMemory(liaNode);
    SegmentMemory sm = lm.getSegmentMemory();
    BetaMemory b1Mem = (BetaMemory) wm.getNodeMemory(b1Node);
    assertSame(sm.getFirst(), b1Mem.getSegmentMemory());
    assertEquals(3, b1Mem.getLeftTupleMemory().size());
    assertEquals(1, b1Mem.getRightTupleMemory().size());
    BetaMemory b2Mem = (BetaMemory) wm.getNodeMemory(b2Node);
    SegmentMemory b2Smem = sm.getFirst().getNext();
    assertSame(b2Smem, b2Mem.getSegmentMemory());
    assertEquals(3, b2Mem.getLeftTupleMemory().size());
    assertEquals(1, b2Mem.getRightTupleMemory().size());
    assertEquals(6, list.size());
    BetaMemory c1Mem = (BetaMemory) wm.getNodeMemory(c1Node);
    assertSame(b1Mem.getSegmentMemory(), c1Mem.getSegmentMemory());
    assertNotSame(b1Mem.getSegmentMemory(), b2Mem.getSegmentMemory());
    wm.fireAllRules();
    assertEquals(6, list.size());
    assertEquals(17, countNodeMemories(wm.getNodeMemories()));
    kbase1.removeRule("org.kie", "r2");
    assertEquals(12, countNodeMemories(wm.getNodeMemories()));
    assertSame(sm, b1Mem.getSegmentMemory());
    assertSame(sm, c1Mem.getSegmentMemory());
    assertNull(sm.getFirst());
    assertEquals(3, b1Mem.getLeftTupleMemory().size());
    assertEquals(1, b1Mem.getRightTupleMemory().size());
    // SegmentMemory b2Smem =  sm.getFirst().remove();
    assertSame(b2Smem, b2Mem.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) 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 49 with BetaMemory

use of org.drools.core.reteoo.BetaMemory in project drools by kiegroup.

the class RightMemory method getRightTuples.

public List<RightTuple> getRightTuples(Object... objects) {
    BetaNode node = scenario.getBetaNode();
    BetaMemory bm = scenario.getBm();
    TupleMemory rtm = bm.getRightTupleMemory();
    InternalWorkingMemory wm = scenario.getWorkingMemory();
    if (objects == null) {
        objects = new Object[0];
    }
    List<RightTuple> rightTuples = new ArrayList<RightTuple>();
    for (Object object : objects) {
        InternalFactHandle fh = (InternalFactHandle) wm.insert(object);
        // node.createLeftTuple( fh, node, true );
        RightTuple expectedRightTuple = new RightTupleImpl(fh, node);
        expectedRightTuple.setPropagationContext(new PhreakPropagationContext());
        rightTuples.add(expectedRightTuple);
    }
    scenario.setTestRightMemory(true);
    return rightTuples;
}
Also used : InternalWorkingMemory(org.drools.core.common.InternalWorkingMemory) PhreakPropagationContext(org.drools.core.common.PhreakPropagationContext) BetaNode(org.drools.core.reteoo.BetaNode) ArrayList(java.util.ArrayList) BetaMemory(org.drools.core.reteoo.BetaMemory) RightTupleImpl(org.drools.core.reteoo.RightTupleImpl) InternalFactHandle(org.drools.core.common.InternalFactHandle) RightTuple(org.drools.core.reteoo.RightTuple) TupleMemory(org.drools.core.reteoo.TupleMemory)

Aggregations

BetaMemory (org.drools.core.reteoo.BetaMemory)49 ObjectTypeNode (org.drools.core.reteoo.ObjectTypeNode)25 InternalWorkingMemory (org.drools.core.common.InternalWorkingMemory)24 Test (org.junit.Test)24 LeftInputAdapterNode (org.drools.core.reteoo.LeftInputAdapterNode)23 RightTuple (org.drools.core.reteoo.RightTuple)20 JoinNode (org.drools.core.reteoo.JoinNode)19 LeftTuple (org.drools.core.reteoo.LeftTuple)19 ArrayList (java.util.ArrayList)16 InternalKnowledgeBase (org.drools.core.impl.InternalKnowledgeBase)14 SegmentMemory (org.drools.core.reteoo.SegmentMemory)13 TupleMemory (org.drools.core.reteoo.TupleMemory)13 BetaNode (org.drools.core.reteoo.BetaNode)12 List (java.util.List)11 FastIterator (org.drools.core.util.FastIterator)11 LiaNodeMemory (org.drools.core.reteoo.LeftInputAdapterNode.LiaNodeMemory)10 KieBase (org.kie.api.KieBase)10 InternalFactHandle (org.drools.core.common.InternalFactHandle)9 AccumulateMemory (org.drools.core.reteoo.AccumulateNode.AccumulateMemory)9 BetaConstraints (org.drools.core.common.BetaConstraints)8