use of org.drools.core.reteoo.ExistsNode in project drools by kiegroup.
the class LeftTupleIterator method getNextLeftTuple.
public LeftTuple getNextLeftTuple(LeftTupleSource source, LeftTupleSink sink, LeftTuple leftTuple, InternalWorkingMemory wm) {
if (otnIterator != null) {
LeftTuple leftParent = leftTuple.getLeftParent();
while (leftTuple != null) {
leftTuple = leftTuple.getHandleNext();
for (; leftTuple != null; leftTuple = leftTuple.getHandleNext()) {
// Iterate to find the next left tuple for this sink, skip tuples for other sinks due to sharing split
if (leftTuple.getTupleSink() == sink) {
return leftTuple;
}
}
}
// We have a parent LeftTuple so try there next
if (leftParent != null) {
// we know it has to be evalNode query element node
while (leftParent != null) {
leftParent = getNextLeftTuple(source.getLeftTupleSource(), (LeftTupleSink) source, leftParent, wm);
if (leftParent != null) {
for (leftTuple = leftParent.getFirstChild(); leftTuple != null; leftTuple = leftTuple.getHandleNext()) {
if (leftTuple.getTupleSink() == sink) {
return leftTuple;
}
}
}
}
return null;
}
// We have exhausted the current FactHandle, now try the next
while (otnIterator.hasNext()) {
InternalFactHandle handle = otnIterator.next();
leftTuple = handle.findFirstLeftTuple(lt -> lt.getTupleSink() == sink);
if (leftTuple != null) {
return leftTuple;
}
}
// We've exhausted this OTN so set the iterator to null
otnIterator = null;
} else if (source instanceof AccumulateNode) {
// when using phreak, accumulate result tuples will not link to leftParent, but to parent instead
BetaMemory memory = ((AccumulateMemory) wm.getNodeMemory((MemoryFactory) source)).getBetaMemory();
FastIterator localIt = memory.getLeftTupleMemory().fullFastIterator(leftTuple.getParent());
LeftTuple childLeftTuple = leftTuple;
leftTuple = childLeftTuple.getParent();
while (leftTuple != null) {
if (childLeftTuple == null) {
childLeftTuple = leftTuple.getFirstChild();
} else {
childLeftTuple = childLeftTuple.getHandleNext();
}
for (; childLeftTuple != null; childLeftTuple = childLeftTuple.getHandleNext()) {
if (childLeftTuple.getTupleSink() == sink) {
return childLeftTuple;
}
}
leftTuple = (LeftTuple) localIt.next(leftTuple);
}
} else if (source instanceof JoinNode || source instanceof NotNode || source instanceof FromNode || source instanceof AccumulateNode) {
BetaMemory memory;
FastIterator localIt;
if (source instanceof FromNode) {
memory = ((FromMemory) wm.getNodeMemory((MemoryFactory) source)).getBetaMemory();
} else if (source instanceof AccumulateNode) {
memory = ((AccumulateMemory) wm.getNodeMemory((MemoryFactory) source)).getBetaMemory();
} else {
memory = (BetaMemory) wm.getNodeMemory((MemoryFactory) source);
}
localIt = memory.getLeftTupleMemory().fullFastIterator(leftTuple.getLeftParent());
LeftTuple childLeftTuple = leftTuple;
leftTuple = childLeftTuple.getLeftParent();
while (leftTuple != null) {
if (childLeftTuple == null) {
childLeftTuple = leftTuple.getFirstChild();
} else {
childLeftTuple = childLeftTuple.getHandleNext();
}
for (; childLeftTuple != null; childLeftTuple = childLeftTuple.getHandleNext()) {
if (childLeftTuple.getTupleSink() == sink) {
return childLeftTuple;
}
}
leftTuple = (LeftTuple) localIt.next(leftTuple);
}
}
if (source instanceof ExistsNode) {
BetaMemory memory = (BetaMemory) wm.getNodeMemory((MemoryFactory) source);
RightTuple rightTuple = leftTuple.getLeftParent().getBlocker();
FastIterator localIt = memory.getRightTupleMemory().fullFastIterator(rightTuple);
for (LeftTuple childleftTuple = leftTuple.getHandleNext(); childleftTuple != null; childleftTuple = childleftTuple.getHandleNext()) {
if (childleftTuple.getTupleSink() == sink) {
return childleftTuple;
}
}
leftTuple = leftTuple.getLeftParent();
// now move onto next RightTuple
while (rightTuple != null) {
if (rightTuple.getBlocked() != null) {
if (leftTuple != null) {
leftTuple = leftTuple.getBlockedNext();
} else {
leftTuple = rightTuple.getBlocked();
}
for (; leftTuple != null; leftTuple = leftTuple.getBlockedNext()) {
for (LeftTuple childleftTuple = leftTuple.getFirstChild(); childleftTuple != null; childleftTuple = childleftTuple.getHandleNext()) {
if (childleftTuple.getTupleSink() == sink) {
return childleftTuple;
}
}
}
}
rightTuple = (RightTuple) localIt.next(rightTuple);
}
} else if (source instanceof EvalConditionNode || source instanceof QueryElementNode) {
LeftTuple childLeftTuple = leftTuple;
if (leftTuple != null) {
leftTuple = leftTuple.getLeftParent();
while (leftTuple != null) {
if (childLeftTuple != null) {
childLeftTuple = childLeftTuple.getHandleNext();
} else {
childLeftTuple = leftTuple.getFirstChild();
}
for (; childLeftTuple != null; childLeftTuple = childLeftTuple.getHandleNext()) {
if (childLeftTuple.getTupleSink() == sink) {
return childLeftTuple;
}
}
if (source instanceof EvalConditionNode) {
leftTuple = getNextLeftTuple(source.getLeftTupleSource(), (LeftTupleSink) source, leftTuple, wm);
} else {
leftTuple = getNextLeftTuple(source.getLeftTupleSource(), (LeftTupleSink) source, leftTuple, wm);
}
}
}
}
return null;
}
use of org.drools.core.reteoo.ExistsNode in project drools by kiegroup.
the class BetaNodeBuilder method build.
public BetaNode build() {
NodeFactory nFactory = buildContext.getComponentFactory().getNodeFactoryService();
EntryPointNode epn = buildContext.getKnowledgeBase().getRete().getEntryPointNodes().values().iterator().next();
ObjectTypeNode otn = nFactory.buildObjectTypeNode(buildContext.getNextId(), epn, new ClassObjectType(leftType), buildContext);
LeftInputAdapterNode leftInput = nFactory.buildLeftInputAdapterNode(buildContext.getNextId(), otn, buildContext);
ObjectSource rightInput = nFactory.buildObjectTypeNode(buildContext.getNextId(), epn, new ClassObjectType(rightType), buildContext);
ReteTesterHelper reteTesterHelper = new ReteTesterHelper();
Pattern pattern = new Pattern(0, new ClassObjectType(leftType));
// BetaNodeFieldConstraint betaConstraint = null;
BetaConstraints betaConstraints = null;
if (constraintFieldName != null) {
ClassFieldAccessorStore store = (ClassFieldAccessorStore) reteTesterHelper.getStore();
InternalReadAccessor extractor = store.getReader(leftType, leftFieldName);
Declaration declr = new Declaration(leftVariableName, extractor, pattern);
betaConstraints = new SingleBetaConstraints(reteTesterHelper.getBoundVariableConstraint(rightType, constraintFieldName, declr, constraintOperator), buildContext.getKnowledgeBase().getConfiguration());
} else {
betaConstraints = new EmptyBetaConstraints();
}
switch(nodeType) {
case NodeTypeEnums.JoinNode:
return new JoinNode(0, leftInput, rightInput, betaConstraints, buildContext);
case NodeTypeEnums.NotNode:
return new NotNode(0, leftInput, rightInput, betaConstraints, buildContext);
case NodeTypeEnums.ExistsNode:
return new ExistsNode(0, leftInput, rightInput, betaConstraints, buildContext);
}
throw new IllegalStateException("Unable to build Node");
}
use of org.drools.core.reteoo.ExistsNode in project drools by kiegroup.
the class NodePositionInPathTest method test.
@Test
public void test() {
String drl = "rule R1 when\n" + " Integer()\n" + " exists( Integer() and Integer() )\n" + " String()\n" + "then\n" + "end\n" + "rule R2 when\n" + " Integer()\n" + " exists( Integer() and String() )\n" + "then\n" + "end\n";
KieBase kbase = new KieHelper().addContent(drl, ResourceType.DRL).build();
ReteDumper.dumpRete(((InternalKnowledgeBase) kbase));
Rete rete = ((KnowledgeBaseImpl) kbase).getRete();
LeftInputAdapterNode liaNode = null;
for (ObjectTypeNode otn : rete.getObjectTypeNodes()) {
Class<?> otnType = ((ClassObjectType) otn.getObjectType()).getClassType();
if (Integer.class == otnType) {
liaNode = (LeftInputAdapterNode) otn.getObjectSinkPropagator().getSinks()[0];
}
}
assertEquals(0, liaNode.getPositionInPath());
LeftTupleSink[] liaSinks = liaNode.getSinkPropagator().getSinks();
BetaNode join1 = (BetaNode) liaSinks[0];
assertEquals(1, join1.getPositionInPath());
ExistsNode ex1 = (ExistsNode) liaSinks[1];
assertEquals(1, ex1.getPositionInPath());
BetaNode join2 = (BetaNode) ex1.getSinkPropagator().getSinks()[0];
assertEquals(2, join2.getPositionInPath());
RuleTerminalNode rtn1 = (RuleTerminalNode) join2.getSinkPropagator().getSinks()[0];
assertEquals(3, rtn1.getPositionInPath());
ExistsNode ex2 = (ExistsNode) liaSinks[2];
assertEquals(1, ex2.getPositionInPath());
RuleTerminalNode rtn2 = (RuleTerminalNode) ex2.getSinkPropagator().getSinks()[0];
assertEquals(2, rtn2.getPositionInPath());
BetaNode join3 = (BetaNode) join1.getSinkPropagator().getSinks()[0];
assertEquals(2, join3.getPositionInPath());
RightInputAdapterNode ria1 = (RightInputAdapterNode) join3.getSinkPropagator().getSinks()[0];
assertEquals(3, ria1.getPositionInPath());
BetaNode join4 = (BetaNode) join1.getSinkPropagator().getSinks()[1];
assertEquals(2, join4.getPositionInPath());
RightInputAdapterNode ria2 = (RightInputAdapterNode) join4.getSinkPropagator().getSinks()[0];
assertEquals(3, ria2.getPositionInPath());
LeftTupleNode[] rtn1PathNodes = rtn1.getPathNodes();
assertEquals(4, rtn1PathNodes.length);
checkNodePosition(rtn1PathNodes, liaNode);
checkNodePosition(rtn1PathNodes, ex1);
checkNodePosition(rtn1PathNodes, join2);
checkNodePosition(rtn1PathNodes, rtn1);
LeftTupleNode[] rtn2PathNodes = rtn2.getPathNodes();
assertEquals(3, rtn2PathNodes.length);
checkNodePosition(rtn2PathNodes, liaNode);
checkNodePosition(rtn2PathNodes, ex2);
checkNodePosition(rtn2PathNodes, rtn2);
LeftTupleNode[] ria1PathNodes = ria1.getPathNodes();
assertEquals(4, ria1PathNodes.length);
checkNodePosition(ria1PathNodes, liaNode);
checkNodePosition(ria1PathNodes, join1);
checkNodePosition(ria1PathNodes, join3);
checkNodePosition(ria1PathNodes, ria1);
LeftTupleNode[] ria2PathNodes = ria2.getPathNodes();
assertEquals(4, ria2PathNodes.length);
checkNodePosition(ria2PathNodes, liaNode);
checkNodePosition(ria2PathNodes, join1);
checkNodePosition(ria2PathNodes, join4);
checkNodePosition(ria2PathNodes, ria2);
}
use of org.drools.core.reteoo.ExistsNode 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.ExistsNode 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());
}
Aggregations