use of org.drools.core.reteoo.ExistsNode 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());
}
use of org.drools.core.reteoo.ExistsNode in project drools by kiegroup.
the class LinkingTest method testSubNetworkSharing.
@Test
public void testSubNetworkSharing() throws Exception {
// Checks the network is correctly formed, with sharing
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 liaNode = (LeftInputAdapterNode) node.getObjectSinkPropagator().getSinks()[0];
assertEquals(3, liaNode.getSinkPropagator().size());
ExistsNode existsNode2 = (ExistsNode) liaNode.getSinkPropagator().getSinks()[1];
ExistsNode existsNode3 = (ExistsNode) liaNode.getSinkPropagator().getSinks()[2];
JoinNode joinNodeB = (JoinNode) liaNode.getSinkPropagator().getSinks()[0];
assertSame(joinNodeB.getRightInput(), getObjectTypeNode(kbase, B.class));
JoinNode joinNodeC = (JoinNode) joinNodeB.getSinkPropagator().getSinks()[0];
assertSame(joinNodeC.getRightInput(), getObjectTypeNode(kbase, C.class));
assertEquals(2, joinNodeC.getSinkPropagator().size());
JoinNode joinNodeD = (JoinNode) joinNodeC.getSinkPropagator().getSinks()[0];
assertSame(joinNodeD.getRightInput(), getObjectTypeNode(kbase, D.class));
assertEquals(2, joinNodeD.getSinkPropagator().size());
assertSame(existsNode2, ((RightInputAdapterNode) joinNodeC.getSinkPropagator().getSinks()[1]).getObjectSinkPropagator().getSinks()[0]);
assertSame(existsNode3, ((RightInputAdapterNode) joinNodeD.getSinkPropagator().getSinks()[1]).getObjectSinkPropagator().getSinks()[0]);
}
use of org.drools.core.reteoo.ExistsNode in project drools by kiegroup.
the class LinkingTest method testNonReactiveSubNetworkOwnSegmentMasks.
@Test
public void testNonReactiveSubNetworkOwnSegmentMasks() 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 += " exists( B() and C() ) \n";
str += " exists( eval(1==1) ) \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 += " exists( eval(1==1) ) \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(2, liaNode.getSinkPropagator().size());
JoinNode bNode = (JoinNode) liaNode.getSinkPropagator().getSinks()[0];
JoinNode cNode = (JoinNode) bNode.getSinkPropagator().getSinks()[0];
ExistsNode exists1n = (ExistsNode) liaNode.getSinkPropagator().getSinks()[1];
EvalConditionNode evalNode = (EvalConditionNode) exists1n.getSinkPropagator().getSinks()[0];
ExistsNode exists2n = (ExistsNode) exists1n.getSinkPropagator().getSinks()[1];
JoinNode dNode = (JoinNode) exists2n.getSinkPropagator().getSinks()[0];
RuleTerminalNode rtn = (RuleTerminalNode) dNode.getSinkPropagator().getSinks()[0];
wm.insert(new A());
PathMemory pmem = (PathMemory) wm.getNodeMemory(rtn);
assertEquals(4, pmem.getSegmentMemories().length);
// the exists eval segment does not need to be linked in
assertEquals(11, pmem.getAllLinkedMaskTest());
RightInputAdapterNode.RiaNodeMemory riaMem = (RightInputAdapterNode.RiaNodeMemory) wm.getNodeMemory((MemoryFactory) exists1n.getRightInput());
// second segment must be linked in
assertEquals(2, riaMem.getRiaPathMemory().getAllLinkedMaskTest());
wm.insert(new B());
wm.insert(new C());
assertEquals(2, riaMem.getRiaPathMemory().getSegmentMemories().length);
riaMem = (RightInputAdapterNode.RiaNodeMemory) wm.getNodeMemory((MemoryFactory) exists2n.getRightInput());
// no segments to be linked in
assertEquals(0, riaMem.getRiaPathMemory().getAllLinkedMaskTest());
}
use of org.drools.core.reteoo.ExistsNode in project drools by kiegroup.
the class LinkingTest method testNonReactiveSubNetworkInShareMasks.
@Test
public void testNonReactiveSubNetworkInShareMasks() 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 += " exists( B() and C() ) \n";
str += " exists( eval(1==1) ) \n";
str += " D() \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(2, liaNode.getSinkPropagator().size());
JoinNode bNode = (JoinNode) liaNode.getSinkPropagator().getSinks()[0];
JoinNode cNode = (JoinNode) bNode.getSinkPropagator().getSinks()[0];
ExistsNode exists1n = (ExistsNode) liaNode.getSinkPropagator().getSinks()[1];
EvalConditionNode evalNode = (EvalConditionNode) exists1n.getSinkPropagator().getSinks()[0];
ExistsNode exists2n = (ExistsNode) exists1n.getSinkPropagator().getSinks()[1];
JoinNode dNode = (JoinNode) exists2n.getSinkPropagator().getSinks()[0];
RuleTerminalNode rtn = (RuleTerminalNode) dNode.getSinkPropagator().getSinks()[0];
wm.insert(new A());
PathMemory pmem = (PathMemory) wm.getNodeMemory(rtn);
assertEquals(3, pmem.getSegmentMemories().length);
// D is in the exists segment
assertEquals(7, pmem.getAllLinkedMaskTest());
BetaMemory bm = (BetaMemory) wm.getNodeMemory(dNode);
// check lazy initialization
assertNull(bm.getSegmentMemory());
wm.insert(new D());
wm.flushPropagations();
// only D can be linked in
assertEquals(2, bm.getSegmentMemory().getAllLinkedMaskTest());
}
use of org.drools.core.reteoo.ExistsNode in project drools by kiegroup.
the class LinkingTest method testNestedSubNetworkMasks.
@Test
public void testNestedSubNetworkMasks() 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];
JoinNode bNode = (JoinNode) liaNode.getSinkPropagator().getSinks()[0];
ExistsNode exists1n = (ExistsNode) bNode.getSinkPropagator().getSinks()[1];
JoinNode cNode = (JoinNode) bNode.getSinkPropagator().getSinks()[0];
JoinNode dNode = (JoinNode) cNode.getSinkPropagator().getSinks()[0];
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];
RightInputAdapterNode riaNode1 = (RightInputAdapterNode) exists2n.getSinkPropagator().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 G());
wm.flushPropagations();
LiaNodeMemory liaMem = (LiaNodeMemory) wm.getNodeMemory(liaNode);
BetaMemory bMem = (BetaMemory) wm.getNodeMemory(bNode);
BetaMemory exists1Mem = (BetaMemory) wm.getNodeMemory(exists1n);
BetaMemory cMem = (BetaMemory) wm.getNodeMemory(cNode);
BetaMemory dMem = (BetaMemory) wm.getNodeMemory(dNode);
BetaMemory exists2Mem = (BetaMemory) wm.getNodeMemory(exists2n);
BetaMemory eMem = (BetaMemory) wm.getNodeMemory(eNode);
BetaMemory fMem = (BetaMemory) wm.getNodeMemory(fNode);
BetaMemory gMem = (BetaMemory) wm.getNodeMemory(gNode);
RightInputAdapterNode.RiaNodeMemory riaMem1 = (RightInputAdapterNode.RiaNodeMemory) wm.getNodeMemory(riaNode1);
RightInputAdapterNode.RiaNodeMemory riaMem2 = (RightInputAdapterNode.RiaNodeMemory) wm.getNodeMemory(riaNode2);
PathMemory rs = (PathMemory) wm.getNodeMemory(rtn);
// E and F are not inserted yet, so rule is unlinked
assertFalse(rs.isRuleLinked());
// ---
// assert a and b in same segment
assertSame(liaMem.getSegmentMemory(), bMem.getSegmentMemory());
// exists1 and b not in same segment
assertNotSame(bMem.getSegmentMemory(), exists1Mem.getSegmentMemory());
// exists1 and b are in same segment
assertSame(exists1Mem.getSegmentMemory(), gMem.getSegmentMemory());
// check segment masks
assertEquals(2, rs.getSegmentMemories().length);
assertEquals(3, rs.getAllLinkedMaskTest());
assertEquals(1, rs.getLinkedSegmentMask());
assertEquals(3, liaMem.getSegmentMemory().getAllLinkedMaskTest());
assertEquals(1, liaMem.getNodePosMaskBit());
assertEquals(2, bMem.getNodePosMaskBit());
assertEquals(3, exists1Mem.getSegmentMemory().getAllLinkedMaskTest());
assertEquals(1, exists1Mem.getNodePosMaskBit());
assertEquals(2, gMem.getNodePosMaskBit());
// assert c, d are in the same segment, and that this is the only segment in ria1 memory
assertSame(dMem.getSegmentMemory(), cMem.getSegmentMemory());
// assert d and exists are not in the same segment
assertNotSame(exists2Mem.getSegmentMemory(), dMem.getSegmentMemory());
assertEquals(3, riaMem1.getRiaPathMemory().getSegmentMemories().length);
// only needs to know about segments in the subnetwork
assertEquals(null, riaMem1.getRiaPathMemory().getSegmentMemories()[0]);
assertEquals(dMem.getSegmentMemory(), riaMem1.getRiaPathMemory().getSegmentMemories()[1]);
assertEquals(1, dMem.getSegmentMemory().getPathMemories().size());
assertSame(riaMem1.getRiaPathMemory(), cMem.getSegmentMemory().getPathMemories().get(0));
assertEquals(3, cMem.getSegmentMemory().getAllLinkedMaskTest());
// E and F is not yet inserted, so bit is not set
assertEquals(3, cMem.getSegmentMemory().getLinkedNodeMask());
assertEquals(1, cMem.getNodePosMaskBit());
assertEquals(2, dMem.getNodePosMaskBit());
assertEquals(0, exists2Mem.getNodePosMaskBit());
// insert to lazy initialize exists2Mem segment
FactHandle fhE1 = wm.insert(new E());
FactHandle fhF1 = wm.insert(new F());
wm.flushPropagations();
assertEquals(1, exists2Mem.getNodePosMaskBit());
// only cares that the segment for c, E and exists1 are set, ignores the outer first segment
assertEquals(6, riaMem1.getRiaPathMemory().getAllLinkedMaskTest());
// E and F are inerted, so 6
assertEquals(6, riaMem1.getRiaPathMemory().getLinkedSegmentMask());
wm.delete(fhE1);
wm.delete(fhF1);
wm.flushPropagations();
// E deleted
assertEquals(2, riaMem1.getRiaPathMemory().getLinkedSegmentMask());
// assert e, f are in the same segment, and that this is the only segment in ria2 memory
// subnetworks are recursively created, so segment already exists
assertNotNull(null, eMem.getSegmentMemory());
assertSame(fMem.getSegmentMemory(), eMem.getSegmentMemory());
assertEquals(3, riaMem2.getRiaPathMemory().getSegmentMemories().length);
// only needs to know about segments in the subnetwork
assertEquals(null, riaMem2.getRiaPathMemory().getSegmentMemories()[0]);
// only needs to know about segments in the subnetwork
assertEquals(null, riaMem2.getRiaPathMemory().getSegmentMemories()[1]);
assertEquals(fMem.getSegmentMemory(), riaMem2.getRiaPathMemory().getSegmentMemories()[2]);
assertSame(riaMem2.getRiaPathMemory(), eMem.getSegmentMemory().getPathMemories().get(0));
assertEquals(3, eMem.getSegmentMemory().getAllLinkedMaskTest());
assertEquals(0, eMem.getSegmentMemory().getLinkedNodeMask());
// only cares that the segment for e and f set, ignores the outer two segment
assertEquals(4, riaMem2.getRiaPathMemory().getAllLinkedMaskTest());
// E and F is not yet inserted, so bit is not set
assertEquals(0, riaMem2.getRiaPathMemory().getLinkedSegmentMask());
fhE1 = wm.insert(new E());
wm.insert(new F());
wm.flushPropagations();
// E and F are now inserted yet, so rule is linked
assertTrue(rs.isRuleLinked());
assertEquals(3, rs.getAllLinkedMaskTest());
assertEquals(3, rs.getLinkedSegmentMask());
// retest bits
assertEquals(3, cMem.getSegmentMemory().getAllLinkedMaskTest());
assertEquals(3, cMem.getSegmentMemory().getLinkedNodeMask());
assertEquals(6, riaMem1.getRiaPathMemory().getAllLinkedMaskTest());
assertEquals(6, riaMem1.getRiaPathMemory().getLinkedSegmentMask());
assertEquals(3, eMem.getSegmentMemory().getAllLinkedMaskTest());
assertEquals(3, eMem.getSegmentMemory().getLinkedNodeMask());
assertEquals(4, riaMem2.getRiaPathMemory().getAllLinkedMaskTest());
assertEquals(4, riaMem2.getRiaPathMemory().getLinkedSegmentMask());
wm.delete(fhE1);
wm.flushPropagations();
// retest bits
assertFalse(rs.isRuleLinked());
assertEquals(3, cMem.getSegmentMemory().getLinkedNodeMask());
assertEquals(2, riaMem1.getRiaPathMemory().getLinkedSegmentMask());
assertEquals(2, eMem.getSegmentMemory().getLinkedNodeMask());
assertEquals(0, riaMem2.getRiaPathMemory().getLinkedSegmentMask());
}
Aggregations