Search in sources :

Example 16 with Iterator

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);
    }
}
Also used : ClassFieldReader(org.drools.core.base.ClassFieldReader) Iterator(org.drools.core.util.Iterator) ObjectHashMap(org.drools.core.util.ObjectHashMap)

Example 17 with Iterator

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();
}
Also used : FastIterator(org.drools.core.util.FastIterator) Iterator(org.drools.core.util.Iterator) Tuple(org.drools.core.spi.Tuple)

Example 18 with Iterator

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"));
}
Also used : TruthMaintenanceSystem(org.drools.core.common.TruthMaintenanceSystem) EqualityKey(org.drools.core.common.EqualityKey) NamedEntryPoint(org.drools.core.common.NamedEntryPoint) ArrayList(java.util.ArrayList) Iterator(org.drools.core.util.Iterator) ObjectHashMap(org.drools.core.util.ObjectHashMap) KieSession(org.kie.api.runtime.KieSession) FactType(org.kie.api.definition.type.FactType) Test(org.junit.Test)

Example 19 with Iterator

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();
}
Also used : TruthMaintenanceSystem(org.drools.core.common.TruthMaintenanceSystem) EqualityKey(org.drools.core.common.EqualityKey) NamedEntryPoint(org.drools.core.common.NamedEntryPoint) Iterator(org.drools.core.util.Iterator) ObjectHashMap(org.drools.core.util.ObjectHashMap) KieSession(org.kie.api.runtime.KieSession) FactType(org.kie.api.definition.type.FactType) Test(org.junit.Test)

Example 20 with Iterator

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);
}
Also used : InternalWorkingMemory(org.drools.core.common.InternalWorkingMemory) InternalAgenda(org.drools.core.common.InternalAgenda) ActiveActivationIterator(org.drools.core.common.ActiveActivationIterator) Iterator(org.drools.core.util.Iterator) ArrayList(java.util.ArrayList) KieHelper(org.kie.internal.utils.KieHelper) KieSession(org.kie.api.runtime.KieSession) ArrayList(java.util.ArrayList) List(java.util.List) AgendaItem(org.drools.core.common.AgendaItem) Test(org.junit.Test)

Aggregations

Iterator (org.drools.core.util.Iterator)33 Test (org.junit.Test)24 KieSession (org.kie.api.runtime.KieSession)23 ArrayList (java.util.ArrayList)19 List (java.util.List)13 AgendaItem (org.drools.core.common.AgendaItem)13 ObjectHashMap (org.drools.core.util.ObjectHashMap)13 KieHelper (org.kie.internal.utils.KieHelper)13 ActivationIterator (org.drools.core.common.ActivationIterator)12 EqualityKey (org.drools.core.common.EqualityKey)10 NamedEntryPoint (org.drools.core.common.NamedEntryPoint)10 TruthMaintenanceSystem (org.drools.core.common.TruthMaintenanceSystem)10 FactType (org.kie.api.definition.type.FactType)10 Tuple (org.drools.core.spi.Tuple)4 FastIterator (org.drools.core.util.FastIterator)4 InternalFactHandle (org.drools.core.common.InternalFactHandle)3 ObjectEntry (org.drools.core.util.ObjectHashMap.ObjectEntry)3 FactHandle (org.kie.api.runtime.rule.FactHandle)3 AccumulateNode (org.drools.core.reteoo.AccumulateNode)2 AccumulateContext (org.drools.core.reteoo.AccumulateNode.AccumulateContext)2