use of org.drools.core.reteoo.NotNode in project drools by kiegroup.
the class LeftTupleIterator method getFirstLeftTuple.
public LeftTuple getFirstLeftTuple(LeftTupleSource source, LeftTupleSink sink, InternalWorkingMemory wm) {
if (source instanceof AccumulateNode) {
// TODO WHAT ABOUTGROUPBY ? (mdp)
AccumulateMemory accmem = (AccumulateMemory) wm.getNodeMemory((MemoryFactory) source);
BetaMemory memory = accmem.getBetaMemory();
FastIterator localIt = memory.getLeftTupleMemory().fullFastIterator();
Tuple leftTuple = BetaNode.getFirstTuple(memory.getLeftTupleMemory(), localIt);
if (leftTuple != null) {
AccumulateContext accctx = (AccumulateContext) leftTuple.getContextObject();
return accctx.getResultLeftTuple();
}
return null;
}
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();
Tuple leftTuple = BetaNode.getFirstTuple(memory.getLeftTupleMemory(), localIt);
while (leftTuple != null) {
for (LeftTuple childleftTuple = leftTuple.getFirstChild(); 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);
FastIterator localIt = memory.getRightTupleMemory().fullFastIterator();
RightTuple rightTuple = (RightTuple) BetaNode.getFirstTuple(memory.getRightTupleMemory(), localIt);
while (rightTuple != null) {
if (rightTuple.getBlocked() != null) {
for (LeftTuple leftTuple = rightTuple.getBlocked(); 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 LeftInputAdapterNode) {
ObjectSource os = ((LeftInputAdapterNode) source).getParentObjectSource();
while (!(os instanceof ObjectTypeNode)) {
os = os.getParentObjectSource();
}
ObjectTypeNode otn = (ObjectTypeNode) os;
otnIterator = wm.getNodeMemory(otn).iterator();
while (otnIterator.hasNext()) {
InternalFactHandle handle = otnIterator.next();
LeftTuple leftTuple = handle.findFirstLeftTuple(lt -> lt.getTupleSink() == sink);
if (leftTuple != null) {
return leftTuple;
}
}
} else if (source instanceof EvalConditionNode || source instanceof QueryElementNode) {
LeftTuple parentLeftTuple = getFirstLeftTuple(source.getLeftTupleSource(), (LeftTupleSink) source, wm);
while (parentLeftTuple != null) {
for (LeftTuple leftTuple = parentLeftTuple.getFirstChild(); leftTuple != null; leftTuple = leftTuple.getHandleNext()) {
if (leftTuple.getTupleSink() == sink) {
return leftTuple;
}
}
parentLeftTuple = getNextLeftTuple(source.getLeftTupleSource(), (LeftTupleSink) source, parentLeftTuple, wm);
}
}
return null;
}
use of org.drools.core.reteoo.NotNode in project drools by kiegroup.
the class LinkingTest method testNotNodeMasksWithoutConstraints.
@Test
public void testNotNodeMasksWithoutConstraints() 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 += " not( B( ) ) \n";
str += " C() \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());
wm.insert(new A());
wm.flushPropagations();
NotNode notNode = (NotNode) liaNode.getSinkPropagator().getSinks()[0];
JoinNode cNode = (JoinNode) notNode.getSinkPropagator().getSinks()[0];
RuleTerminalNode rtn = (RuleTerminalNode) cNode.getSinkPropagator().getSinks()[0];
PathMemory pmem = (PathMemory) wm.getNodeMemory(rtn);
assertEquals(1, pmem.getSegmentMemories().length);
assertEquals(1, pmem.getAllLinkedMaskTest());
SegmentMemory sm = pmem.getSegmentMemories()[0];
assertEquals(7, sm.getAllLinkedMaskTest());
assertEquals(3, sm.getLinkedNodeMask());
assertFalse(sm.isSegmentLinked());
assertFalse(pmem.isRuleLinked());
wm.insert(new C());
wm.flushPropagations();
assertEquals(7, sm.getLinkedNodeMask());
assertTrue(sm.isSegmentLinked());
assertTrue(pmem.isRuleLinked());
}
use of org.drools.core.reteoo.NotNode in project drools by kiegroup.
the class LinkingTest method testNotNodeMasksWithConstraints.
@Test
public void testNotNodeMasksWithConstraints() 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 : A() \n";
str += " not( B( value == $a.value ) ) \n";
str += " C() \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());
wm.insert(new A());
wm.flushPropagations();
NotNode notNode = (NotNode) liaNode.getSinkPropagator().getSinks()[0];
JoinNode cNode = (JoinNode) notNode.getSinkPropagator().getSinks()[0];
RuleTerminalNode rtn = (RuleTerminalNode) cNode.getSinkPropagator().getSinks()[0];
PathMemory pmem = (PathMemory) wm.getNodeMemory(rtn);
assertEquals(1, pmem.getSegmentMemories().length);
assertEquals(1, pmem.getAllLinkedMaskTest());
SegmentMemory sm = pmem.getSegmentMemories()[0];
assertEquals(5, sm.getAllLinkedMaskTest());
assertEquals(3, sm.getLinkedNodeMask());
assertFalse(sm.isSegmentLinked());
assertFalse(pmem.isRuleLinked());
wm.insert(new C());
wm.flushPropagations();
// only 5 is needed to link, the 'not' turns on but it has no unfleunce either way
assertEquals(7, sm.getLinkedNodeMask());
assertTrue(sm.isSegmentLinked());
assertTrue(pmem.isRuleLinked());
}
use of org.drools.core.reteoo.NotNode in project drools by kiegroup.
the class LinkingTest method testNotNodeUnlinksWithNoConstriants.
@Test
public void testNotNodeUnlinksWithNoConstriants() {
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 : A() \n";
str += " not B() \n";
str += " $c : C() \n";
str += "then \n";
str += " list.add( 'x' ); \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 aotn = getObjectTypeNode(kbase, A.class);
InternalWorkingMemory wm = ((StatefulKnowledgeSessionImpl) kbase.newKieSession());
List list = new ArrayList();
wm.setGlobal("list", list);
LeftInputAdapterNode aNode = (LeftInputAdapterNode) aotn.getObjectSinkPropagator().getSinks()[0];
NotNode bNode = (NotNode) aNode.getSinkPropagator().getSinks()[0];
JoinNode cNode = (JoinNode) bNode.getSinkPropagator().getSinks()[0];
SegmentUtilities.createSegmentMemory(cNode, wm);
LiaNodeMemory amem = (LiaNodeMemory) wm.getNodeMemory(aNode);
// Only NotNode is linked in
assertEquals(2, amem.getSegmentMemory().getLinkedNodeMask());
FactHandle fha = wm.insert(new A());
FactHandle fhb = wm.insert(new B());
FactHandle fhc = wm.insert(new C());
wm.fireAllRules();
assertEquals(0, list.size());
// NotNode unlinks, which is allowed because it has no variable constraints
assertEquals(5, amem.getSegmentMemory().getLinkedNodeMask());
// NotNode links back in again, which is allowed because it has no variable constraints
wm.retract(fhb);
wm.flushPropagations();
assertEquals(7, amem.getSegmentMemory().getLinkedNodeMask());
wm.fireAllRules();
assertEquals(1, list.size());
// Now try with lots of facthandles on NotNode
list.clear();
List<FactHandle> handles = new ArrayList<FactHandle>();
for (int i = 0; i < 5; i++) {
handles.add(wm.insert(new B()));
}
wm.fireAllRules();
assertEquals(0, list.size());
assertEquals(5, amem.getSegmentMemory().getLinkedNodeMask());
for (FactHandle fh : handles) {
wm.retract(fh);
}
wm.flushPropagations();
assertEquals(7, amem.getSegmentMemory().getLinkedNodeMask());
wm.fireAllRules();
assertEquals(1, list.size());
}
use of org.drools.core.reteoo.NotNode in project drools by kiegroup.
the class SegmentCreationTest method tesShareInSubnetwork.
@Test
public void tesShareInSubnetwork() throws Exception {
KieBase kbase = buildKnowledgeBase(" A() \n", " A() B() C() \n", " A() not ( B() and C() ) \n");
InternalWorkingMemory wm = ((InternalWorkingMemory) kbase.newKieSession());
ObjectTypeNode aotn = getObjectTypeNode(kbase, LinkingTest.A.class);
LeftInputAdapterNode liaNode = (LeftInputAdapterNode) aotn.getObjectSinkPropagator().getSinks()[0];
RuleTerminalNode rtn1 = (RuleTerminalNode) liaNode.getSinkPropagator().getSinks()[0];
JoinNode bNode = (JoinNode) liaNode.getSinkPropagator().getSinks()[1];
JoinNode cNode = (JoinNode) bNode.getSinkPropagator().getSinks()[0];
RuleTerminalNode rtn2 = (RuleTerminalNode) cNode.getSinkPropagator().getSinks()[0];
RightInputAdapterNode riaNode = (RightInputAdapterNode) cNode.getSinkPropagator().getSinks()[1];
NotNode notNode = (NotNode) liaNode.getSinkPropagator().getSinks()[2];
RuleTerminalNode rtn3 = (RuleTerminalNode) notNode.getSinkPropagator().getSinks()[0];
wm.insert(new LinkingTest.A());
wm.insert(new LinkingTest.B());
wm.insert(new LinkingTest.C());
wm.flushPropagations();
// LiaNode is in it's own segment
LiaNodeMemory liaMem = (LiaNodeMemory) wm.getNodeMemory(liaNode);
SegmentMemory smem = liaMem.getSegmentMemory();
assertEquals(liaNode, smem.getRootNode());
assertEquals(liaNode, smem.getTipNode());
SegmentMemory rtnSmem1 = smem.getFirst();
assertEquals(rtn1, rtnSmem1.getRootNode());
assertEquals(rtn1, rtnSmem1.getTipNode());
SegmentMemory bSmem = rtnSmem1.getNext();
assertEquals(bNode, bSmem.getRootNode());
assertEquals(cNode, bSmem.getTipNode());
// segment is not initialized yet
assertNull(bSmem.getFirst());
wm.fireAllRules();
SegmentMemory rtn2Smem = bSmem.getFirst();
assertEquals(rtn2, rtn2Smem.getRootNode());
assertEquals(rtn2, rtn2Smem.getTipNode());
SegmentMemory riaSmem = rtn2Smem.getNext();
assertEquals(riaNode, riaSmem.getRootNode());
assertEquals(riaNode, riaSmem.getTipNode());
SegmentMemory notSmem = bSmem.getNext();
assertEquals(notNode, notSmem.getRootNode());
assertEquals(rtn3, notSmem.getTipNode());
}
Aggregations