use of org.drools.core.util.Iterator in project drools by kiegroup.
the class ActivationIteratorTest method testLianPlusEvaln.
@Test
public void testLianPlusEvaln() {
String str = "package org.kie.test \n" + "\n" + "rule rule1 @Propagation(EAGER) when\n" + " $s : String( this != 'xx' )\n" + " eval( 1 == 1 ) \n" + "then\n" + "end\n" + "rule rule6 @Propagation(EAGER) when\n" + " java.util.Map()\n" + "then\n" + "end\n" + "\n";
KieSession ksession = new KieHelper().addContent(str, ResourceType.DRL).build().newKieSession();
for (int i = 0; i < 5; i++) {
ksession.insert(new String("" + i));
}
evaluateEagerList(ksession);
Iterator it = ActivationIterator.iterator(ksession);
List list = new ArrayList();
for (AgendaItem act = (AgendaItem) it.next(); act != null; act = (AgendaItem) it.next()) {
list.add(act.getRule().getName() + ":" + act.getDeclarationValue("$s") + ":" + act.isQueued());
}
assertContains(new String[] { "rule1:0:true", "rule1:1:true", "rule1:2:true", "rule1:3:true", "rule1:4:true" }, list);
ksession.fireAllRules();
it = ActivationIterator.iterator(ksession);
list = new ArrayList();
for (AgendaItem act = (AgendaItem) it.next(); act != null; act = (AgendaItem) it.next()) {
list.add(act.getRule().getName() + ":" + act.getDeclarationValue("$s") + ":" + act.isQueued());
}
assertContains(new String[] { "rule1:0:false", "rule1:1:false", "rule1:2:false", "rule1:3:false", "rule1:4:false" }, list);
}
use of org.drools.core.util.Iterator in project drools by kiegroup.
the class ActivationIteratorTest method testLianPlusEvalnWithSharing.
@Test
public void testLianPlusEvalnWithSharing() {
// Rule 0 single LiaNode
// Rule 1 and 2 are shared
// Rule 3 shares the LIANode with 1 and 2
// Rule 4 Shares the eval with 3
String str = "package org.kie.test \n" + "\n" + "rule rule0 @Propagation(EAGER) when\n" + " $s : String( this != 'xx' )\n" + "then\n" + "end\n" + "rule rule1 @Propagation(EAGER) when\n" + " $s : String( this != 'xx' )\n" + " eval( Integer.parseInt( $s ) <= 2 ) \n" + "then\n" + "end\n" + "rule rule2 @Propagation(EAGER) when\n" + " $s : String( this != 'xx' )\n" + " eval( Integer.parseInt( $s ) <= 2 ) \n" + "then\n" + "end\n" + "rule rule3 @Propagation(EAGER) when\n" + " $s : String( this != 'xx' )\n" + " eval( Integer.parseInt( $s ) > 2 ) \n" + "then\n" + "end\n" + "rule rule4 @Propagation(EAGER) when\n" + " $s : String( this != 'xx' )\n" + " eval( Integer.parseInt( $s ) > 2 ) \n" + " eval( Integer.parseInt( $s ) > 3 ) \n" + "then\n" + "end\n" + "rule rule5 @Propagation(EAGER) when\n" + " $s : String( this != 'xx' )\n" + " eval( Integer.parseInt( $s ) > 2 ) \n" + " eval( Integer.parseInt( $s ) > 3 ) \n" + "then\n" + "end\n" + "rule rule6 @Propagation(EAGER) when\n" + " java.util.Map()\n" + "then\n" + "end\n" + "\n";
KieSession ksession = new KieHelper().addContent(str, ResourceType.DRL).build().newKieSession();
for (int i = 0; i < 5; i++) {
ksession.insert(new String("" + i));
}
evaluateEagerList(ksession);
Iterator it = ActivationIterator.iterator(ksession);
List list = new ArrayList();
for (AgendaItem act = (AgendaItem) it.next(); act != null; act = (AgendaItem) it.next()) {
list.add(act.getRule().getName() + ":" + act.getDeclarationValue("$s") + ":" + act.isQueued());
}
assertContains(new String[] { "rule0:0:true", "rule0:1:true", "rule0:2:true", "rule0:3:true", "rule0:4:true", "rule1:0:true", "rule1:1:true", "rule1:2:true", "rule2:0:true", "rule2:1:true", "rule2:2:true", "rule3:3:true", "rule3:4:true", "rule4:4:true", "rule5:4:true" }, list);
ksession.fireAllRules();
it = ActivationIterator.iterator(ksession);
list = new ArrayList();
for (AgendaItem act = (AgendaItem) it.next(); act != null; act = (AgendaItem) it.next()) {
list.add(act.getRule().getName() + ":" + act.getDeclarationValue("$s") + ":" + act.isQueued());
}
assertContains(new String[] { "rule0:0:false", "rule0:1:false", "rule0:2:false", "rule0:3:false", "rule0:4:false", "rule1:0:false", "rule1:1:false", "rule1:2:false", "rule2:0:false", "rule2:1:false", "rule2:2:false", "rule3:3:false", "rule3:4:false", "rule4:4:false", "rule5:4:false" }, list);
}
use of org.drools.core.util.Iterator in project drools by kiegroup.
the class LeftTupleIterator method getFirstLeftTuple.
public LeftTuple getFirstLeftTuple(LeftTupleSource source, LeftTupleSink sink, InternalWorkingMemory wm) {
if (source instanceof AccumulateNode) {
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.util.Iterator in project drools by kiegroup.
the class TupleIndexHashTable method toString.
public String toString() {
StringBuilder builder = new StringBuilder();
Iterator it = iterator();
for (Tuple leftTuple = (Tuple) it.next(); leftTuple != null; leftTuple = (Tuple) it.next()) {
builder.append(leftTuple).append("\n");
}
return builder.toString();
}
use of org.drools.core.util.Iterator in project drools by kiegroup.
the class ObjectTypeNodeParser method getFirstAlphaNode.
/**
* Returns the first {@link org.kie.reteoo.AlphaNode} from the specified {@link ObjectHashMap}.
*
* @param hashedAlphaNodes map of hashed AlphaNodes
* @return first alpha from the specified map
* @throws IllegalArgumentException thrown if the map doesn't contain any alpha nodes
*/
private AlphaNode getFirstAlphaNode(final ObjectHashMap hashedAlphaNodes) throws IllegalArgumentException {
AlphaNode firstAlphaNode;
final Iterator iter = hashedAlphaNodes.iterator();
final ObjectHashMap.ObjectEntry entry = (ObjectHashMap.ObjectEntry) iter.next();
if (entry != null) {
firstAlphaNode = (AlphaNode) entry.getValue();
} else {
throw new IllegalArgumentException("ObjectHashMap does not contain any hashed AlphaNodes!");
}
return firstAlphaNode;
}
Aggregations