use of org.drools.core.util.FastIterator in project drools by kiegroup.
the class PhreakActivationIterator method processLeftTuples.
public static void processLeftTuples(LeftTupleSource node, List<AgendaItem> agendaItems, Set<RuleTerminalNode> nodeSet, InternalWorkingMemory wm) {
LeftTupleSource node1 = node;
while (NodeTypeEnums.LeftInputAdapterNode != node1.getType()) {
node1 = node1.getLeftTupleSource();
}
int maxShareCount = node1.getAssociationsSize();
while (NodeTypeEnums.LeftInputAdapterNode != node.getType()) {
Memory memory = wm.getNodeMemory((MemoryFactory) node);
if (memory.getSegmentMemory() == null) {
// segment has never been initialized, which means the rule has never been linked.
return;
}
if (node.getAssociationsSize() == maxShareCount) {
// the recurse must start from the first split node, otherwise we get partial overlaps in propagations
if (NodeTypeEnums.isBetaNode(node)) {
BetaMemory bm;
if (NodeTypeEnums.AccumulateNode == node.getType()) {
AccumulateMemory am = (AccumulateMemory) memory;
bm = am.getBetaMemory();
FastIterator it = bm.getLeftTupleMemory().fullFastIterator();
Tuple lt = BetaNode.getFirstTuple(bm.getLeftTupleMemory(), it);
for (; lt != null; lt = (LeftTuple) it.next(lt)) {
AccumulateContext accctx = (AccumulateContext) lt.getContextObject();
collectFromPeers(accctx.getResultLeftTuple(), agendaItems, nodeSet, wm);
}
} else if (NodeTypeEnums.ExistsNode == node.getType()) {
bm = (BetaMemory) wm.getNodeMemory((MemoryFactory) node);
// done off the RightTupleMemory, as exists only have unblocked tuples on the left side
FastIterator it = bm.getRightTupleMemory().fullFastIterator();
RightTuple rt = (RightTuple) BetaNode.getFirstTuple(bm.getRightTupleMemory(), it);
for (; rt != null; rt = (RightTuple) it.next(rt)) {
for (LeftTuple lt = rt.getBlocked(); lt != null; lt = lt.getBlockedNext()) {
if (lt.getFirstChild() != null) {
collectFromPeers(lt.getFirstChild(), agendaItems, nodeSet, wm);
}
}
}
} else {
bm = (BetaMemory) wm.getNodeMemory((MemoryFactory) node);
FastIterator it = bm.getLeftTupleMemory().fullFastIterator();
Tuple lt = BetaNode.getFirstTuple(bm.getLeftTupleMemory(), it);
for (; lt != null; lt = (LeftTuple) it.next(lt)) {
if (lt.getFirstChild() != null) {
collectFromLeftInput(lt.getFirstChild(), agendaItems, nodeSet, wm);
}
}
}
return;
} else if (NodeTypeEnums.FromNode == node.getType()) {
FromMemory fm = (FromMemory) wm.getNodeMemory((MemoryFactory) node);
TupleMemory ltm = fm.getBetaMemory().getLeftTupleMemory();
FastIterator it = ltm.fullFastIterator();
for (LeftTuple lt = (LeftTuple) ltm.getFirst(null); lt != null; lt = (LeftTuple) it.next(lt)) {
if (lt.getFirstChild() != null) {
collectFromLeftInput(lt.getFirstChild(), agendaItems, nodeSet, wm);
}
}
return;
}
}
node = node.getLeftTupleSource();
}
// No beta or from nodes, so must retrieve LeftTuples from the LiaNode.
// This is done by scanning all the LeftTuples referenced from the FactHandles in the ObjectTypeNode
LeftInputAdapterNode lian = (LeftInputAdapterNode) node;
Memory memory = wm.getNodeMemory((MemoryFactory) node);
if (memory.getSegmentMemory() == null) {
// segment has never been initialized, which means the rule has never been linked.
return;
}
ObjectSource os = lian.getObjectSource();
while (os.getType() != NodeTypeEnums.ObjectTypeNode) {
os = os.getParentObjectSource();
}
ObjectTypeNode otn = (ObjectTypeNode) os;
final ObjectTypeNodeMemory omem = wm.getNodeMemory(otn);
LeftTupleSink firstLiaSink = lian.getSinkPropagator().getFirstLeftTupleSink();
java.util.Iterator<InternalFactHandle> it = omem.iterator();
while (it.hasNext()) {
InternalFactHandle fh = it.next();
fh.forEachLeftTuple(lt -> {
if (lt.getTupleSink() == firstLiaSink) {
collectFromLeftInput(lt, agendaItems, nodeSet, wm);
}
});
}
}
use of org.drools.core.util.FastIterator in project drools by kiegroup.
the class Scenario method equalsLeftMemory.
public void equalsLeftMemory(List<LeftTuple> leftTuples) {
TupleMemory ltm = bm.getLeftTupleMemory();
int length = 0;
for (LeftTuple expectedLeftTuple : leftTuples) {
FastIterator it = betaNode.getLeftIterator(ltm);
Tuple actualLeftTuple = null;
for (actualLeftTuple = BetaNode.getFirstTuple(ltm, it); actualLeftTuple != null; actualLeftTuple = (LeftTuple) it.next(actualLeftTuple)) {
if (expectedLeftTuple.equals(actualLeftTuple)) {
length++;
break;
}
}
if (actualLeftTuple == null) {
fail("Could not find LeftTuple: " + expectedLeftTuple);
}
}
if (leftTuples.size() != ltm.size()) {
fail("LeftTuple memory size did not match: " + length);
}
}
use of org.drools.core.util.FastIterator in project drools by kiegroup.
the class DefeasibleBeliefSet method cancel.
public void cancel(PropagationContext propagationContext) {
// get all but last, as that we'll do via the BeliefSystem, for cleanup
// note we don't update negative, conflict counters. It's needed for the last cleanup operation
FastIterator it = iterator();
for (M node = getFirst(); node != tailUndefeated; ) {
// get next, as we are about to remove it
M temp = (M) it.next(node);
LogicalDependency<M> dep = node.getLogicalDependency();
dep.getJustifier().getLogicalDependencies().remove(dep);
remove(node);
node = temp;
}
DefeasibleMode<M> node = getFirst();
LogicalDependency<M> dep = node.getLogicalDependency();
dep.getJustifier().getLogicalDependencies().remove(dep);
}
use of org.drools.core.util.FastIterator in project drools by kiegroup.
the class IndexingTest method testFullFastIteratorResume.
@Test(timeout = 10000)
public void testFullFastIteratorResume() throws Exception {
String str = "";
str += "package org.drools.compiler.test \n";
str += "import org.drools.compiler.Person \n";
str += "query peeps( String $name, int $age ) \n";
str += " not $p2 : Person( $name := name, age > $age ) \n";
str += "end\n";
KieBase kbase = loadKnowledgeBaseFromString(str);
List<ObjectTypeNode> nodes = ((KnowledgeBaseImpl) kbase).getRete().getObjectTypeNodes();
ObjectTypeNode node = null;
for (ObjectTypeNode n : nodes) {
if (((ClassObjectType) n.getObjectType()).getClassType() == DroolsQuery.class) {
node = n;
break;
}
}
StatefulKnowledgeSessionImpl wm = ((StatefulKnowledgeSessionImpl) kbase.newKieSession());
AlphaNode alphanode = (AlphaNode) node.getObjectSinkPropagator().getSinks()[0];
LeftInputAdapterNode liaNode = (LeftInputAdapterNode) alphanode.getObjectSinkPropagator().getSinks()[0];
NotNode n = (NotNode) liaNode.getSinkPropagator().getSinks()[0];
DoubleNonIndexSkipBetaConstraints c = (DoubleNonIndexSkipBetaConstraints) n.getRawConstraints();
// assertEquals( "$name", ((VariableConstraint)c.getConstraint()).getRequiredDeclarations()[0].getIdentifier() );
assertTrue(c.isIndexed());
BetaMemory bm = (BetaMemory) wm.getNodeMemory(n);
System.out.println(bm.getLeftTupleMemory().getClass());
System.out.println(bm.getRightTupleMemory().getClass());
assertTrue(bm.getLeftTupleMemory() instanceof TupleIndexHashTable);
assertTrue(bm.getRightTupleMemory() instanceof TupleIndexHashTable);
final Map<String, Integer> map = new HashMap<String, Integer>();
map.put("inserted", new Integer(0));
map.put("deleted", new Integer(0));
map.put("updated", new Integer(0));
wm.openLiveQuery("peeps", new Object[] { Variable.v, 99 }, new ViewChangedEventListener() {
@Override
public void rowInserted(Row row) {
}
@Override
public void rowDeleted(Row row) {
}
@Override
public void rowUpdated(Row row) {
}
});
Map<String, InternalFactHandle> peeps = new HashMap<String, InternalFactHandle>();
Person p = new Person("x0", 100);
InternalFactHandle fh = (InternalFactHandle) wm.insert(p);
peeps.put(p.getName(), fh);
for (int i = 1; i < 100; i++) {
p = new Person("x" + i, 101);
fh = (InternalFactHandle) wm.insert(p);
wm.fireAllRules();
peeps.put(p.getName(), fh);
}
List<RightTuple> list = new ArrayList<RightTuple>(100);
FastIterator it = n.getRightIterator(bm.getRightTupleMemory());
for (RightTuple rt = n.getFirstRightTuple(null, bm.getRightTupleMemory(), null, it); rt != null; rt = (RightTuple) it.next(rt)) {
list.add(rt);
}
assertEquals(100, list.size());
// check we can resume from each entry in the list above.
for (int i = 0; i < 100; i++) {
RightTuple rightTuple = list.get(i);
// resumes from the current rightTuple
it = n.getRightIterator(bm.getRightTupleMemory(), rightTuple);
int j = i + 1;
for (RightTuple rt = (RightTuple) it.next(rightTuple); rt != null; rt = (RightTuple) it.next(rt)) {
assertSame(list.get(j), rt);
j++;
}
}
}
use of org.drools.core.util.FastIterator in project drools by kiegroup.
the class MemoryVisitor method checkRightTupleIndexHashTable.
private void checkRightTupleIndexHashTable(final TupleIndexHashTable memory) {
final Entry[] entries = memory.getTable();
int factCount = 0;
int bucketCount = 0;
FastIterator it = LinkedList.fastIterator;
for (Entry entry1 : entries) {
if (entry1 != null) {
TupleList rightTupleList = (TupleList) entry1;
while (rightTupleList != null) {
if (rightTupleList.getFirst() != null) {
Entry entry = rightTupleList.getFirst();
while (entry != null) {
entry = it.next(entry);
factCount++;
}
} else {
logger.info("error : fieldIndexHashTable cannot have empty FieldIndexEntry objects");
}
rightTupleList = rightTupleList.getNext();
bucketCount++;
}
}
}
try {
final Field field = AbstractHashTable.class.getDeclaredField("size");
field.setAccessible(true);
logger.info(indent() + "FieldIndexBuckets: " + field.get(memory) + ":" + bucketCount);
if ((Integer) field.get(memory) != bucketCount) {
logger.info(indent() + "error");
}
} catch (final Exception e) {
e.printStackTrace();
}
logger.info(indent() + "FieldIndexFacts: " + memory.size() + ":" + factCount);
if (memory.size() != factCount) {
logger.info(indent() + "error");
}
}
Aggregations