Search in sources :

Example 1 with Iterator

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;
}
Also used : NotNode(org.drools.core.reteoo.NotNode) ExistsNode(org.drools.core.reteoo.ExistsNode) EvalConditionNode(org.drools.core.reteoo.EvalConditionNode) FastIterator(org.drools.core.util.FastIterator) AccumulateNode(org.drools.core.reteoo.AccumulateNode) AccumulateContext(org.drools.core.reteoo.AccumulateNode.AccumulateContext) BetaMemory(org.drools.core.reteoo.BetaMemory) JoinNode(org.drools.core.reteoo.JoinNode) FromNode(org.drools.core.reteoo.FromNode) LeftTuple(org.drools.core.reteoo.LeftTuple) RightTuple(org.drools.core.reteoo.RightTuple) LeftInputAdapterNode(org.drools.core.reteoo.LeftInputAdapterNode) QueryElementNode(org.drools.core.reteoo.QueryElementNode) Tuple(org.drools.core.spi.Tuple) BetaNode(org.drools.core.reteoo.BetaNode) AccumulateMemory(org.drools.core.reteoo.AccumulateNode.AccumulateMemory) FromMemory(org.drools.core.reteoo.FromNode.FromMemory) ObjectSource(org.drools.core.reteoo.ObjectSource) LeftTupleSink(org.drools.core.reteoo.LeftTupleSink) ObjectTypeNode(org.drools.core.reteoo.ObjectTypeNode) Iterator(org.drools.core.util.Iterator) LeftTupleSource(org.drools.core.reteoo.LeftTupleSource) AccumulateMemory(org.drools.core.reteoo.AccumulateNode.AccumulateMemory) NotNode(org.drools.core.reteoo.NotNode) EvalConditionNode(org.drools.core.reteoo.EvalConditionNode) AccumulateNode(org.drools.core.reteoo.AccumulateNode) JoinNode(org.drools.core.reteoo.JoinNode) QueryElementNode(org.drools.core.reteoo.QueryElementNode) LeftTupleSink(org.drools.core.reteoo.LeftTupleSink) BetaMemory(org.drools.core.reteoo.BetaMemory) FromNode(org.drools.core.reteoo.FromNode) LeftTuple(org.drools.core.reteoo.LeftTuple) ExistsNode(org.drools.core.reteoo.ExistsNode) RightTuple(org.drools.core.reteoo.RightTuple) FastIterator(org.drools.core.util.FastIterator)

Example 2 with Iterator

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());
}
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 3 with Iterator

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);
        }
    }
}
Also used : TruthMaintenanceSystem(org.drools.core.common.TruthMaintenanceSystem) InternalFactHandle(org.drools.core.common.InternalFactHandle) FactHandle(org.kie.api.runtime.rule.FactHandle) 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 4 with Iterator

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());
}
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 5 with Iterator

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());
}
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)

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