use of org.drools.core.common.InternalWorkingMemory in project drools by kiegroup.
the class AddRuleTest method testSplitOneBeforeCreatedSegment.
@Test
public void testSplitOneBeforeCreatedSegment() throws Exception {
InternalKnowledgeBase kbase1 = buildKnowledgeBase("r1", " A(1;) A(2;) B(1;) B(2;) C(1;) C(2;) X(1;) X(2;) E(1;) E(2;)\n");
kbase1.addPackages(buildKnowledgePackage("r2", " A(1;) A(2;) B(1;) B(2;) C(1;) C(2;) X(1;) X(2;) E(1;) E(2;)\n"));
kbase1.addPackages(buildKnowledgePackage("r3", " A(1;) A(2;) B(1;) B(2;) C(1;) C(2;) X(1;) X(2;)\n"));
kbase1.addPackages(buildKnowledgePackage("r4", " A(1;) A(2;) B(1;) B(2;) C(1;) C(2;) \n"));
InternalWorkingMemory wm = ((InternalWorkingMemory) kbase1.newKieSession());
List list = new ArrayList();
wm.setGlobal("list", list);
wm.insert(new X(1));
wm.insert(new X(2));
wm.flushPropagations();
RuleTerminalNode rtn1 = getRtn("org.kie.r1", kbase1);
RuleTerminalNode rtn2 = getRtn("org.kie.r2", kbase1);
RuleTerminalNode rtn3 = getRtn("org.kie.r3", kbase1);
RuleTerminalNode rtn4 = getRtn("org.kie.r4", kbase1);
PathMemory pm1 = (PathMemory) wm.getNodeMemory(rtn1);
SegmentMemory[] smems = pm1.getSegmentMemories();
assertEquals(4, smems.length);
assertNull(smems[0]);
assertNull(smems[2]);
assertNull(smems[3]);
SegmentMemory sm = smems[1];
assertEquals(1, sm.getPos());
assertEquals(2, sm.getSegmentPosMaskBit());
assertEquals(2, pm1.getLinkedSegmentMask());
PathMemory pm3 = (PathMemory) wm.getNodeMemory(rtn3);
SegmentMemory[] smemsP3 = pm3.getSegmentMemories();
assertEquals(3, smemsP3.length);
assertNull(smemsP3[0]);
assertNull(smemsP3[2]);
sm = smems[1];
assertEquals(1, sm.getPos());
assertEquals(2, sm.getSegmentPosMaskBit());
assertEquals(2, pm1.getLinkedSegmentMask());
kbase1.addPackages(buildKnowledgePackage("r5", " A(1;) A(2;) B(1;) B(2;) \n"));
smems = pm1.getSegmentMemories();
assertEquals(5, smems.length);
assertNull(smems[0]);
assertNull(smems[1]);
assertNull(smems[3]);
assertNull(smems[4]);
sm = smems[2];
assertEquals(2, sm.getPos());
assertEquals(4, sm.getSegmentPosMaskBit());
assertEquals(4, pm1.getLinkedSegmentMask());
smems = pm3.getSegmentMemories();
assertEquals(4, smems.length);
assertNull(smems[0]);
assertNull(smems[1]);
assertNull(smems[3]);
sm = smems[2];
assertEquals(2, sm.getPos());
assertEquals(4, sm.getSegmentPosMaskBit());
assertEquals(4, pm1.getLinkedSegmentMask());
RuleTerminalNode rtn5 = getRtn("org.kie.r5", kbase1);
PathMemory pm5 = (PathMemory) wm.getNodeMemory(rtn5);
smems = pm5.getSegmentMemories();
assertEquals(2, smems.length);
assertNull(smems[0]);
assertNull(smems[1]);
}
use of org.drools.core.common.InternalWorkingMemory 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;
}
use of org.drools.core.common.InternalWorkingMemory 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 X(1));
wm.insert(new E(1));
wm.fireAllRules();
kbase.addPackages(buildKnowledgePackage("r1", " A() B() C(object == 2) X() 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));
}
use of org.drools.core.common.InternalWorkingMemory in project drools by kiegroup.
the class RemoveRuleTest method testPopulatedSharedLiaNodeNoBeta.
@Test
public void testPopulatedSharedLiaNodeNoBeta() throws Exception {
InternalKnowledgeBase kbase1 = buildKnowledgeBase("r1", " A()\n");
InternalWorkingMemory wm = ((InternalWorkingMemory) kbase1.newKieSession());
List list = new ArrayList();
wm.setGlobal("list", list);
A a1 = new A(1);
InternalFactHandle fh1 = (InternalFactHandle) wm.insert(a1);
A a2 = new A(2);
InternalFactHandle fh2 = (InternalFactHandle) wm.insert(a2);
wm.fireAllRules();
assertEquals(2, list.size());
kbase1.addPackages(buildKnowledgePackage("r2", " a : A()\n"));
wm.fireAllRules();
assertEquals(4, list.size());
kbase1.removeRule("org.kie", "r1");
kbase1.removeRule("org.kie", "r2");
list.clear();
assertNull(fh1.getFirstLeftTuple());
assertNull(fh2.getFirstLeftTuple());
wm.update(fh1, a1);
wm.update(fh2, a2);
wm.fireAllRules();
assertEquals(0, list.size());
}
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 X(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());
}
Aggregations