use of org.drools.core.util.Iterator in project drools by kiegroup.
the class ObjectTypeNodeParser method traverseHashedAlphaNodes.
private void traverseHashedAlphaNodes(ObjectHashMap hashedAlphaNodes, NetworkHandler handler) {
if (hashedAlphaNodes != null && hashedAlphaNodes.size() > 0) {
AlphaNode firstAlpha = getFirstAlphaNode(hashedAlphaNodes);
ClassFieldReader hashedFieldReader = getClassFieldReaderForHashedAlpha(firstAlpha);
// start the hashed alphas
handler.startHashedAlphaNodes(hashedFieldReader);
Iterator iter = hashedAlphaNodes.iterator();
for (ObjectHashMap.ObjectEntry entry = (ObjectHashMap.ObjectEntry) iter.next(); entry != null; entry = (ObjectHashMap.ObjectEntry) iter.next()) {
CompositeObjectSinkAdapter.HashKey hashKey = (CompositeObjectSinkAdapter.HashKey) entry.getKey();
AlphaNode alphaNode = (AlphaNode) entry.getValue();
handler.startHashedAlphaNode(alphaNode, hashKey.getObjectValue());
// traverse the propagator for each alpha
traversePropagator(alphaNode.getObjectSinkPropagator(), handler);
handler.endHashedAlphaNode(alphaNode, hashKey.getObjectValue());
}
// end of the hashed alphas
handler.endHashedAlphaNodes(hashedFieldReader);
}
}
use of org.drools.core.util.Iterator in project drools by kiegroup.
the class TupleList method toString.
public String toString() {
StringBuilder builder = new StringBuilder();
Iterator it = iterator();
for (Tuple tuple = (Tuple) it.next(); tuple != null; tuple = (Tuple) it.next()) {
builder.append(tuple).append("\n");
}
return builder.toString();
}
use of org.drools.core.util.Iterator in project drools by kiegroup.
the class DefeasibilityTest method testDefeatOutcomePosNeg.
@Test(timeout = 10000)
public void testDefeatOutcomePosNeg() {
KieSession kSession = getSession("org/drools/compiler/beliefsystem/defeasible/negDefeatPos.drl");
ArrayList list = new ArrayList();
kSession.setGlobal("list", list);
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, 1, DefeasibilityStatus.DEFEASIBLY);
} else {
fail("Unrecognized object has been logically justified : " + factClass);
}
}
assertEquals(2, kSession.getObjects().size());
assertEquals(1, getNegativeObjects(kSession).size());
assertEquals(1, list.size());
assertTrue(list.contains("-1"));
}
use of org.drools.core.util.Iterator in project drools by kiegroup.
the class DefeasibilityTest method testMultipleDefeats.
@Test(timeout = 10000)
public void testMultipleDefeats() {
KieSession kSession = getSession("org/drools/compiler/beliefsystem/defeasible/multiDefeat.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, 2, DefeasibilityStatus.DEFEATEDLY);
} else {
fail("Unrecognized object has been logically justified : " + factClass);
}
}
for (Object o : kSession.getObjects()) {
System.out.println(o);
}
assertEquals(2, kSession.getObjects().size());
kSession.fireAllRules();
}
use of org.drools.core.util.Iterator in project drools by kiegroup.
the class ActiveActivationsIteratorTest method testActiveActivationsIteratorTest.
@Test
public void testActiveActivationsIteratorTest() {
String str = "package org.kie.test \n" + "\n" + "rule rule0 @Propagation(EAGER) agenda-group 'a1' salience ( Integer.parseInt('1'+$s) ) when\n" + " $s : String( this != 'xx' )\n" + "then\n" + "end\n" + "rule rule1 @Propagation(EAGER) agenda-group 'a2' salience ( Integer.parseInt('1'+$s)) when\n" + " $s : String( this != 'xx' )\n" + " eval( Integer.parseInt( $s ) <= 2 ) \n" + "then\n" + "end\n" + "rule rule2 @Propagation(EAGER) agenda-group 'a3' salience ( Integer.parseInt('1'+$s)) when\n" + " $s : String( this != 'xx' )\n" + " eval( Integer.parseInt( $s ) <= 2 ) \n" + "then\n" + " kcontext.getKieRuntime().halt();\n" + "end\n" + "rule rule3 @Propagation(EAGER) ruleflow-group 'r1' salience ( Integer.parseInt('1'+$s)) when\n" + " $s : String( this != 'xx' )\n" + " eval( Integer.parseInt( $s ) > 2 ) \n" + "then\n" + "end\n" + "rule rule4 @Propagation(EAGER) ruleflow-group 'r1' salience ( Integer.parseInt('1'+$s) ) 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" + "rule rule7 @Propagation(EAGER) when\n" + " $s : String( this != 'xx' )\n" + "then\n" + "end\n" + "\n";
KieSession ksession = new KieHelper().addContent(str, ResourceType.DRL).build().newKieSession();
for (int i = 0; i < 3; i++) {
ksession.insert(new String("" + i));
}
((InternalWorkingMemory) ksession).flushPropagations();
((InternalAgenda) ksession.getAgenda()).evaluateEagerList();
Iterator it = ActiveActivationIterator.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[] { "rule7:2:true", "rule7:0:true", "rule7:1:true", "rule0:2:true", "rule0:0:true", "rule0:1:true", "rule1:2:true", "rule1:0:true", "rule1:1:true", "rule2:2:true", "rule2:0:true", "rule2:1:true" }, list);
ksession.fireAllRules();
it = ActiveActivationIterator.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:2:true", "rule0:0:true", "rule0:1:true", "rule1:2:true", "rule1:0:true", "rule1:1:true", "rule2:2:true", "rule2:0:true", "rule2:1:true" }, list);
}
Aggregations