use of org.drools.core.util.Iterator 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.util.Iterator in project drools by kiegroup.
the class DefeasibilityTest method testDefeasibleEntailmentWithStrictOverride.
@Test(timeout = 10000)
public void testDefeasibleEntailmentWithStrictOverride() {
KieSession kSession = getSession("org/drools/compiler/beliefsystem/defeasible/strictOverride.drl");
kSession.fireAllRules();
TruthMaintenanceSystem tms = ((NamedEntryPoint) kSession.getEntryPoint("DEFAULT")).getTruthMaintenanceSystem();
FactType Ctype = kSession.getKieBase().getFactType("org.drools.defeasible", "C");
FactType Xtype = kSession.getKieBase().getFactType("org.drools.defeasible", "X");
ObjectHashMap keys = tms.getEqualityKeyMap();
Iterator iter = keys.iterator();
ObjectHashMap.ObjectEntry entry;
while ((entry = (ObjectHashMap.ObjectEntry) iter.next()) != null) {
EqualityKey key = (EqualityKey) entry.getValue();
Class factClass = key.getFactHandle().getObject().getClass();
if (factClass == Ctype.getFactClass()) {
checkStatus(key, 1, DefeasibilityStatus.DEFINITELY);
} else if (factClass == Xtype.getFactClass()) {
checkStatus(key, 1, DefeasibilityStatus.DEFINITELY);
} else {
fail("Unrecognized object has been logically justified : " + factClass);
}
}
assertEquals(5, kSession.getObjects().size());
}
use of org.drools.core.util.Iterator in project drools by kiegroup.
the class DefeasibilityTest method testRemoveDefiniteJustifier.
@Test(timeout = 10000)
public void testRemoveDefiniteJustifier() {
KieSession kSession = getSession("org/drools/compiler/beliefsystem/defeasible/strictRetract.drl");
FactHandle h = kSession.insert("go");
kSession.fireAllRules();
TruthMaintenanceSystem tms = ((NamedEntryPoint) kSession.getEntryPoint("DEFAULT")).getTruthMaintenanceSystem();
FactType Ctype = kSession.getKieBase().getFactType("org.drools.defeasible", "C");
FactType Atype = kSession.getKieBase().getFactType("org.drools.defeasible", "A");
ObjectHashMap keys = tms.getEqualityKeyMap();
Iterator iter = keys.iterator();
ObjectHashMap.ObjectEntry entry;
while ((entry = (ObjectHashMap.ObjectEntry) iter.next()) != null) {
EqualityKey key = (EqualityKey) entry.getValue();
Class factClass = key.getFactHandle().getObject().getClass();
if (factClass == Ctype.getFactClass()) {
checkStatus(key, 1, DefeasibilityStatus.DEFINITELY);
} else if (factClass == Atype.getFactClass()) {
checkStatus(key, 1, DefeasibilityStatus.DEFINITELY);
} else {
fail("Unrecognized object has been logically justified : " + factClass);
}
}
kSession.retract(h);
kSession.fireAllRules();
keys = tms.getEqualityKeyMap();
iter = keys.iterator();
while ((entry = (ObjectHashMap.ObjectEntry) iter.next()) != null) {
EqualityKey key = (EqualityKey) entry.getValue();
Class factClass = key.getFactHandle().getObject().getClass();
if (factClass == Ctype.getFactClass()) {
checkStatus(key, 1, DefeasibilityStatus.DEFEASIBLY);
} else {
fail("Unrecognized object has been logically justified : " + factClass);
}
}
}
use of org.drools.core.util.Iterator in project drools by kiegroup.
the class DefeasibilityTest method defeasibleEntailmentMultiActivation.
@Test(timeout = 10000)
public void defeasibleEntailmentMultiActivation() {
KieSession kSession = getSession("org/drools/compiler/beliefsystem/defeasible/defeat.drl");
kSession.fireAllRules();
TruthMaintenanceSystem tms = ((NamedEntryPoint) kSession.getEntryPoint("DEFAULT")).getTruthMaintenanceSystem();
FactType Xtype = kSession.getKieBase().getFactType("org.drools.defeasible", "X");
ObjectHashMap keys = tms.getEqualityKeyMap();
Iterator iter = keys.iterator();
ObjectHashMap.ObjectEntry entry;
while ((entry = (ObjectHashMap.ObjectEntry) iter.next()) != null) {
EqualityKey key = (EqualityKey) entry.getValue();
Class factClass = key.getFactHandle().getObject().getClass();
if (factClass == Xtype.getFactClass()) {
checkStatus(key, 3, DefeasibilityStatus.UNDECIDABLY);
} else {
fail("Unrecognized object has been logically justified : " + factClass);
}
}
assertEquals(3, kSession.getObjects().size());
}
use of org.drools.core.util.Iterator in project drools by kiegroup.
the class DefeasibilityTest method testStrictEntailment.
@Test(timeout = 10000)
public void testStrictEntailment() {
KieSession kSession = getSession("org/drools/compiler/beliefsystem/defeasible/strict.drl");
kSession.fireAllRules();
TruthMaintenanceSystem tms = ((NamedEntryPoint) kSession.getEntryPoint("DEFAULT")).getTruthMaintenanceSystem();
FactType Ctype = kSession.getKieBase().getFactType("org.drools.defeasible", "C");
FactType Dtype = kSession.getKieBase().getFactType("org.drools.defeasible", "D");
ObjectHashMap keys = tms.getEqualityKeyMap();
Iterator iter = keys.iterator();
ObjectHashMap.ObjectEntry entry;
while ((entry = (ObjectHashMap.ObjectEntry) iter.next()) != null) {
EqualityKey key = (EqualityKey) entry.getValue();
Class factClass = key.getFactHandle().getObject().getClass();
if (factClass == Ctype.getFactClass()) {
checkStatus(key, 2, DefeasibilityStatus.DEFINITELY);
} else if (factClass == Dtype.getFactClass()) {
checkStatus(key, 1, DefeasibilityStatus.DEFINITELY);
} else {
fail("Unrecognized object has been logically justified : " + factClass);
}
}
assertEquals(5, kSession.getObjects().size());
}
Aggregations