use of org.drools.core.reteoo.PathMemory in project drools by kiegroup.
the class RemoveRuleTest method testSplitTwoBeforeCreatedSegment.
@Test
public void testSplitTwoBeforeCreatedSegment() throws Exception {
InternalKnowledgeBase kbase1 = buildKnowledgeBase("r1", " A(1;) A(2;) B(1;) B(2;) C(1;) C(2;) D(1;) D(2;) E(1;) E(2;)\n");
kbase1.addPackages(buildKnowledgePackage("r2", " A(1;) A(2;) B(1;) B(2;) C(1;) C(2;) D(1;) D(2;) E(1;) E(2;)\n"));
kbase1.addPackages(buildKnowledgePackage("r3", " A(1;) A(2;) B(1;) B(2;) C(1;) C(2;) D(1;) D(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 E(1));
wm.insert(new E(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[1]);
assertNull(smems[3]);
SegmentMemory sm = smems[2];
assertEquals(2, sm.getPos());
assertEquals(4, sm.getSegmentPosMaskBit());
assertEquals(4, 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[2]);
sm = smems[3];
assertEquals(3, sm.getPos());
assertEquals(8, sm.getSegmentPosMaskBit());
assertEquals(8, 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.reteoo.PathMemory 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());
}
use of org.drools.core.reteoo.PathMemory in project drools by kiegroup.
the class LinkingTest method testSubNetworkSharingMemories.
@Test
public void testSubNetworkSharingMemories() throws Exception {
// checks the memory sharing works, and linking, uses the already checked network from testSubNetworkSharing
String str = "";
str += "package org.kie \n";
str += "import " + A.class.getCanonicalName() + "\n";
str += "import " + B.class.getCanonicalName() + "\n";
str += "import " + C.class.getCanonicalName() + "\n";
str += "import " + D.class.getCanonicalName() + "\n";
str += "import " + E.class.getCanonicalName() + "\n";
str += "global java.util.List list \n";
str += "rule rule1 when \n";
str += " A() \n";
str += " B() \n";
str += " C() \n";
str += " D() \n";
str += "then \n";
str += "end \n";
str += "rule rule2 when \n";
str += " A() \n";
str += " exists( B() and C() ) \n";
str += " D() \n";
str += "then \n";
str += "end \n";
str += "rule rule3 when \n";
str += " A() \n";
str += " exists( B() and C() and D() ) \n";
str += " E() \n";
str += "then \n";
str += "end \n";
KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder();
kbuilder.add(ResourceFactory.newByteArrayResource(str.getBytes()), ResourceType.DRL);
assertFalse(kbuilder.getErrors().toString(), kbuilder.hasErrors());
InternalKnowledgeBase kbase = KnowledgeBaseFactory.newKnowledgeBase();
kbase.addPackages(kbuilder.getKnowledgePackages());
ObjectTypeNode node = getObjectTypeNode(kbase, A.class);
InternalWorkingMemory wm = ((StatefulKnowledgeSessionImpl) kbase.newKieSession());
LeftInputAdapterNode liaNodeA = (LeftInputAdapterNode) node.getObjectSinkPropagator().getSinks()[0];
ExistsNode existsNode2 = (ExistsNode) liaNodeA.getSinkPropagator().getSinks()[1];
ExistsNode existsNode3 = (ExistsNode) liaNodeA.getSinkPropagator().getSinks()[2];
JoinNode joinNodeB = (JoinNode) liaNodeA.getSinkPropagator().getSinks()[0];
JoinNode joinNodeC = (JoinNode) joinNodeB.getSinkPropagator().getSinks()[0];
JoinNode joinNodeD1 = (JoinNode) joinNodeC.getSinkPropagator().getSinks()[0];
JoinNode joinNodeD2 = (JoinNode) existsNode2.getSinkPropagator().getSinks()[0];
JoinNode joinNodeE = (JoinNode) existsNode3.getSinkPropagator().getSinks()[0];
RuleTerminalNode rtn1 = (RuleTerminalNode) joinNodeD1.getSinkPropagator().getSinks()[0];
RuleTerminalNode rtn2 = (RuleTerminalNode) joinNodeD2.getSinkPropagator().getSinks()[0];
RuleTerminalNode rtn3 = (RuleTerminalNode) joinNodeE.getSinkPropagator().getSinks()[0];
FactHandle fha = wm.insert(new A());
wm.insert(new B());
wm.insert(new C());
wm.insert(new D());
wm.flushPropagations();
BetaMemory bm = null;
LiaNodeMemory liam = (LiaNodeMemory) wm.getNodeMemory(liaNodeA);
BetaMemory bm1 = (BetaMemory) wm.getNodeMemory(joinNodeB);
BetaMemory bm2 = (BetaMemory) wm.getNodeMemory(joinNodeC);
BetaMemory bm3 = (BetaMemory) wm.getNodeMemory(joinNodeD1);
assertEquals(1, liam.getNodePosMaskBit());
assertEquals(1, bm1.getNodePosMaskBit());
assertEquals(2, bm2.getNodePosMaskBit());
assertEquals(1, bm3.getNodePosMaskBit());
assertNotSame(liam.getSegmentMemory(), bm1.getSegmentMemory());
assertSame(bm1.getSegmentMemory(), bm2.getSegmentMemory());
assertNotSame(bm2.getSegmentMemory(), bm3.getSegmentMemory());
BetaMemory bm4 = (BetaMemory) wm.getNodeMemory(existsNode2);
BetaMemory bm5 = (BetaMemory) wm.getNodeMemory(joinNodeD2);
assertEquals(1, bm4.getNodePosMaskBit());
assertEquals(2, bm5.getNodePosMaskBit());
assertSame(bm4.getSegmentMemory(), bm5.getSegmentMemory());
PathMemory rs1 = (PathMemory) wm.getNodeMemory(rtn1);
PathMemory rs2 = (PathMemory) wm.getNodeMemory(rtn2);
PathMemory rs3 = (PathMemory) wm.getNodeMemory(rtn3);
assertTrue(rs1.isRuleLinked());
assertTrue(rs2.isRuleLinked());
// no E yet
assertFalse(rs3.isRuleLinked());
wm.insert(new E());
wm.flushPropagations();
BetaMemory bm6 = (BetaMemory) wm.getNodeMemory(existsNode3);
BetaMemory bm7 = (BetaMemory) wm.getNodeMemory(joinNodeE);
assertEquals(1, bm6.getNodePosMaskBit());
assertEquals(2, bm7.getNodePosMaskBit());
assertSame(bm6.getSegmentMemory(), bm7.getSegmentMemory());
assertTrue(rs1.isRuleLinked());
assertTrue(rs2.isRuleLinked());
assertTrue(rs3.isRuleLinked());
wm.retract(fha);
// need to have rules evalulated, for unlinking to happen
wm.fireAllRules();
assertFalse(rs1.isRuleLinked());
assertFalse(rs2.isRuleLinked());
assertFalse(rs3.isRuleLinked());
}
use of org.drools.core.reteoo.PathMemory in project drools by kiegroup.
the class LinkingTest method testNestedSubNetwork.
@Test
public void testNestedSubNetwork() throws Exception {
String str = "";
str += "package org.kie \n";
str += "import " + A.class.getCanonicalName() + "\n";
str += "import " + B.class.getCanonicalName() + "\n";
str += "import " + C.class.getCanonicalName() + "\n";
str += "import " + D.class.getCanonicalName() + "\n";
str += "import " + E.class.getCanonicalName() + "\n";
str += "import " + F.class.getCanonicalName() + "\n";
str += "import " + G.class.getCanonicalName() + "\n";
str += "global java.util.List list \n";
str += "rule rule1 when \n";
str += " A() \n";
str += " B() \n";
str += " exists( C() and D() and exists( E() and F() ) ) \n";
str += " G() \n";
str += "then \n";
str += "end \n";
KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder();
kbuilder.add(ResourceFactory.newByteArrayResource(str.getBytes()), ResourceType.DRL);
assertFalse(kbuilder.getErrors().toString(), kbuilder.hasErrors());
InternalKnowledgeBase kbase = KnowledgeBaseFactory.newKnowledgeBase();
kbase.addPackages(kbuilder.getKnowledgePackages());
ObjectTypeNode node = getObjectTypeNode(kbase, A.class);
InternalWorkingMemory wm = ((StatefulKnowledgeSessionImpl) kbase.newKieSession());
LeftInputAdapterNode liaNode = (LeftInputAdapterNode) node.getObjectSinkPropagator().getSinks()[0];
assertEquals(1, liaNode.getSinkPropagator().size());
JoinNode bNode = (JoinNode) liaNode.getSinkPropagator().getSinks()[0];
assertEquals(2, bNode.getSinkPropagator().size());
ExistsNode exists1n = (ExistsNode) bNode.getSinkPropagator().getSinks()[1];
JoinNode cNode = (JoinNode) bNode.getSinkPropagator().getSinks()[0];
JoinNode dNode = (JoinNode) cNode.getSinkPropagator().getSinks()[0];
assertEquals(2, dNode.getSinkPropagator().size());
ExistsNode exists2n = (ExistsNode) dNode.getSinkPropagator().getSinks()[1];
JoinNode eNode = (JoinNode) dNode.getSinkPropagator().getSinks()[0];
JoinNode fNode = (JoinNode) eNode.getSinkPropagator().getSinks()[0];
RightInputAdapterNode riaNode2 = (RightInputAdapterNode) fNode.getSinkPropagator().getSinks()[0];
assertEquals(exists2n, riaNode2.getObjectSinkPropagator().getSinks()[0]);
RightInputAdapterNode riaNode1 = (RightInputAdapterNode) exists2n.getSinkPropagator().getSinks()[0];
assertEquals(exists1n, riaNode1.getObjectSinkPropagator().getSinks()[0]);
JoinNode gNode = (JoinNode) exists1n.getSinkPropagator().getSinks()[0];
RuleTerminalNode rtn = (RuleTerminalNode) gNode.getSinkPropagator().getSinks()[0];
wm.insert(new A());
wm.insert(new B());
wm.insert(new C());
wm.insert(new D());
wm.insert(new F());
wm.insert(new G());
PathMemory rs = (PathMemory) wm.getNodeMemory(rtn);
assertFalse(rs.isRuleLinked());
FactHandle fhE1 = wm.insert(new E());
FactHandle fhE2 = wm.insert(new E());
wm.flushPropagations();
assertTrue(rs.isRuleLinked());
wm.retract(fhE1);
wm.flushPropagations();
assertTrue(rs.isRuleLinked());
wm.retract(fhE2);
wm.flushPropagations();
assertFalse(rs.isRuleLinked());
}
use of org.drools.core.reteoo.PathMemory in project drools by kiegroup.
the class LinkingTest method testSubNetworkRiaLinking.
@Test
public void testSubNetworkRiaLinking() throws Exception {
String str = "";
str += "package org.kie \n";
str += "import " + A.class.getCanonicalName() + "\n";
str += "import " + B.class.getCanonicalName() + "\n";
str += "import " + C.class.getCanonicalName() + "\n";
str += "import " + D.class.getCanonicalName() + "\n";
str += "import " + E.class.getCanonicalName() + "\n";
str += "global java.util.List list \n";
str += "rule rule1 when \n";
str += " A() \n";
str += " B() \n";
str += " exists( C() and D() ) \n";
str += " E() \n";
str += "then \n";
str += "end \n";
KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder();
kbuilder.add(ResourceFactory.newByteArrayResource(str.getBytes()), ResourceType.DRL);
assertFalse(kbuilder.getErrors().toString(), kbuilder.hasErrors());
InternalKnowledgeBase kbase = KnowledgeBaseFactory.newKnowledgeBase();
kbase.addPackages(kbuilder.getKnowledgePackages());
ObjectTypeNode node = getObjectTypeNode(kbase, A.class);
InternalWorkingMemory wm = ((StatefulKnowledgeSessionImpl) kbase.newKieSession());
LeftInputAdapterNode liaNode = (LeftInputAdapterNode) node.getObjectSinkPropagator().getSinks()[0];
assertEquals(1, liaNode.getSinkPropagator().size());
JoinNode bNode = (JoinNode) liaNode.getSinkPropagator().getSinks()[0];
assertEquals(2, bNode.getSinkPropagator().size());
ExistsNode exists1n = (ExistsNode) bNode.getSinkPropagator().getSinks()[1];
JoinNode cNode = (JoinNode) bNode.getSinkPropagator().getSinks()[0];
JoinNode dNode = (JoinNode) cNode.getSinkPropagator().getSinks()[0];
assertEquals(1, dNode.getSinkPropagator().size());
RightInputAdapterNode riaNode1 = (RightInputAdapterNode) dNode.getSinkPropagator().getSinks()[0];
JoinNode eNode = (JoinNode) exists1n.getSinkPropagator().getSinks()[0];
RuleTerminalNode rtn = (RuleTerminalNode) eNode.getSinkPropagator().getSinks()[0];
SegmentUtilities.createSegmentMemory(exists1n, wm);
BetaMemory existsBm = (BetaMemory) wm.getNodeMemory(exists1n);
assertEquals(0, existsBm.getSegmentMemory().getLinkedNodeMask());
FactHandle fhc = wm.insert(new C());
FactHandle fhd = wm.insert(new D());
wm.flushPropagations();
// exists is start of new segment
assertEquals(1, existsBm.getSegmentMemory().getLinkedNodeMask());
wm.retract(fhd);
wm.flushPropagations();
assertEquals(0, existsBm.getSegmentMemory().getLinkedNodeMask());
PathMemory rs = (PathMemory) wm.getNodeMemory(rtn);
assertFalse(rs.isRuleLinked());
wm.insert(new A());
wm.flushPropagations();
assertFalse(rs.isRuleLinked());
wm.insert(new B());
wm.flushPropagations();
assertFalse(rs.isRuleLinked());
wm.insert(new E());
wm.flushPropagations();
assertFalse(rs.isRuleLinked());
wm.insert(new D());
wm.flushPropagations();
assertTrue(rs.isRuleLinked());
wm.retract(fhc);
wm.flushPropagations();
assertFalse(rs.isRuleLinked());
wm.insert(new C());
wm.flushPropagations();
assertTrue(rs.isRuleLinked());
}
Aggregations