Search in sources :

Example 36 with RightTuple

use of org.drools.core.reteoo.RightTuple 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;
}
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) ObjectTypeNode(org.drools.core.reteoo.ObjectTypeNode) 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) ObjectSource(org.drools.core.reteoo.ObjectSource) FastIterator(org.drools.core.util.FastIterator) LeftTuple(org.drools.core.reteoo.LeftTuple) RightTuple(org.drools.core.reteoo.RightTuple) Tuple(org.drools.core.spi.Tuple) AccumulateContext(org.drools.core.reteoo.AccumulateNode.AccumulateContext) LeftInputAdapterNode(org.drools.core.reteoo.LeftInputAdapterNode)

Example 37 with RightTuple

use of org.drools.core.reteoo.RightTuple in project drools by kiegroup.

the class RightTupleIndexHashTableTest method testRemove.

@Test
public void testRemove() throws Exception {
    final InternalReadAccessor extractor = store.getReader(Cheese.class, "type");
    final Pattern pattern = new Pattern(0, new ClassObjectType(Cheese.class));
    final Declaration declaration = new Declaration("typeOfCheese", extractor, pattern);
    final FieldIndex fieldIndex = new FieldIndex(extractor, declaration, MvelConstraint.INDEX_EVALUATOR);
    final TupleIndexHashTable map = new TupleIndexHashTable(new FieldIndex[] { fieldIndex }, false);
    assertEquals(0, map.size());
    final Cheese stilton1 = new Cheese("stilton", 35);
    final InternalFactHandle stiltonHandle1 = new DefaultFactHandle(1, stilton1);
    RightTuple stiltonRightTuple1 = new RightTupleImpl(stiltonHandle1, null);
    map.add(stiltonRightTuple1);
    final Cheese cheddar1 = new Cheese("cheddar", 35);
    final InternalFactHandle cheddarHandle1 = new DefaultFactHandle(2, cheddar1);
    RightTuple cheddarRightTuple1 = new RightTupleImpl(cheddarHandle1, null);
    map.add(cheddarRightTuple1);
    final Cheese stilton2 = new Cheese("stilton", 81);
    final InternalFactHandle stiltonHandle2 = new DefaultFactHandle(3, stilton2);
    RightTuple stiltonRightTuple2 = new RightTupleImpl(stiltonHandle2, null);
    map.add(stiltonRightTuple2);
    assertEquals(3, map.size());
    assertEquals(2, tablePopulationSize(map));
    // cheddar is in its own bucket, which should be removed once empty. We cannot have
    // empty FieldIndexEntries in the Map, as they get their value  from the first FactEntry.
    map.remove(cheddarRightTuple1);
    assertEquals(2, map.size());
    assertEquals(1, tablePopulationSize(map));
    // We remove t he stiltonHandle2, but there is still  one more stilton, so size  should be the same
    map.remove(stiltonRightTuple2);
    assertEquals(1, map.size());
    assertEquals(1, tablePopulationSize(map));
    // No more stiltons, so the table should be empty
    map.remove(stiltonRightTuple1);
    assertEquals(0, map.size());
    assertEquals(0, tablePopulationSize(map));
}
Also used : Pattern(org.drools.core.rule.Pattern) DefaultFactHandle(org.drools.core.common.DefaultFactHandle) FieldIndex(org.drools.core.util.AbstractHashTable.FieldIndex) ClassObjectType(org.drools.core.base.ClassObjectType) InternalReadAccessor(org.drools.core.spi.InternalReadAccessor) Cheese(org.drools.core.test.model.Cheese) Declaration(org.drools.core.rule.Declaration) RightTupleImpl(org.drools.core.reteoo.RightTupleImpl) TupleIndexHashTable(org.drools.core.util.index.TupleIndexHashTable) InternalFactHandle(org.drools.core.common.InternalFactHandle) RightTuple(org.drools.core.reteoo.RightTuple) Test(org.junit.Test)

Example 38 with RightTuple

use of org.drools.core.reteoo.RightTuple in project drools by kiegroup.

the class FieldIndexEntryTest method testThreeEntries.

@Test
public void testThreeEntries() {
    final ClassFieldReader extractor = store.getReader(Cheese.class, "type");
    final FieldIndex fieldIndex = new FieldIndex(extractor, null, MvelConstraint.INDEX_EVALUATOR);
    final SingleIndex singleIndex = new SingleIndex(new FieldIndex[] { fieldIndex }, 1);
    Tuple tuple = new RightTupleImpl(new DefaultFactHandle(1, new Cheese("stilton", 10)));
    final TupleList index = new AbstractHashTable.SingleIndexTupleList(singleIndex, tuple, "stilton".hashCode(), false);
    final Cheese stilton1 = new Cheese("stilton", 35);
    final InternalFactHandle h1 = new DefaultFactHandle(1, stilton1);
    final Cheese stilton2 = new Cheese("stilton", 59);
    final InternalFactHandle h2 = new DefaultFactHandle(2, stilton2);
    final Cheese stilton3 = new Cheese("stilton", 59);
    final InternalFactHandle h3 = new DefaultFactHandle(3, stilton3);
    RightTuple h1RightTuple = new RightTupleImpl(h1, null);
    RightTuple h2RightTuple = new RightTupleImpl(h2, null);
    RightTuple h3RightTuple = new RightTupleImpl(h3, null);
    // test add
    index.add(h1RightTuple);
    index.add(h2RightTuple);
    index.add(h3RightTuple);
    assertEquals(h1, index.getFirst().getFactHandle());
    assertEquals(h2, ((RightTuple) index.getFirst().getNext()).getFactHandle());
    assertEquals(h3, ((RightTuple) index.getFirst().getNext().getNext()).getFactHandle());
    // test get
    assertEquals(h1, index.get(h1).getFactHandle());
    assertEquals(h2, index.get(h2).getFactHandle());
    assertEquals(h3, index.get(h3).getFactHandle());
    // test removal for combinations
    // remove first
    index.remove(h3RightTuple);
    assertEquals(h1, index.getFirst().getFactHandle());
    assertEquals(h2, ((RightTuple) index.getFirst().getNext()).getFactHandle());
    index.add(h3RightTuple);
    index.remove(h2RightTuple);
    assertEquals(h1, index.getFirst().getFactHandle());
    assertEquals(h3, ((RightTuple) index.getFirst().getNext()).getFactHandle());
    index.add(h2RightTuple);
    index.remove(h1RightTuple);
    assertEquals(h3, index.getFirst().getFactHandle());
    assertEquals(h2, ((RightTuple) index.getFirst().getNext()).getFactHandle());
    index.remove(index.getFirst());
    // check index type does not change, as this fact is removed
    stilton2.setType("cheddar");
}
Also used : TupleList(org.drools.core.util.index.TupleList) SingleIndex(org.drools.core.util.AbstractHashTable.SingleIndex) DefaultFactHandle(org.drools.core.common.DefaultFactHandle) FieldIndex(org.drools.core.util.AbstractHashTable.FieldIndex) ClassFieldReader(org.drools.core.base.ClassFieldReader) Cheese(org.drools.core.test.model.Cheese) RightTupleImpl(org.drools.core.reteoo.RightTupleImpl) InternalFactHandle(org.drools.core.common.InternalFactHandle) RightTuple(org.drools.core.reteoo.RightTuple) RightTuple(org.drools.core.reteoo.RightTuple) Tuple(org.drools.core.spi.Tuple) Test(org.junit.Test)

Example 39 with RightTuple

use of org.drools.core.reteoo.RightTuple in project drools by kiegroup.

the class FieldIndexEntryTest method testSingleEntry.

@Test
public void testSingleEntry() {
    final ClassFieldReader extractor = store.getReader(Cheese.class, "type");
    final FieldIndex fieldIndex = new FieldIndex(extractor, null, MvelConstraint.INDEX_EVALUATOR);
    final SingleIndex singleIndex = new SingleIndex(new FieldIndex[] { fieldIndex }, 1);
    Tuple tuple = new RightTupleImpl(new DefaultFactHandle(1, new Cheese("stilton", 10)));
    final TupleList index = new AbstractHashTable.SingleIndexTupleList(singleIndex, tuple, "stilton".hashCode(), false);
    // Test initial construction
    assertNull(index.getFirst());
    assertEquals("stilton".hashCode(), index.hashCode());
    final Cheese stilton1 = new Cheese("stilton", 35);
    final InternalFactHandle h1 = new DefaultFactHandle(1, stilton1);
    // test add
    RightTuple h1RightTuple = new RightTupleImpl(h1, null);
    index.add(h1RightTuple);
    final Tuple entry1 = index.getFirst();
    assertSame(h1, entry1.getFactHandle());
    assertNull(entry1.getNext());
    assertSame(entry1, index.get(h1));
    // test get
    final Tuple entry2 = index.get(new RightTupleImpl(h1, null));
    assertSame(entry1, entry2);
    // test remove
    index.remove(h1RightTuple);
    assertNull(index.getFirst());
}
Also used : TupleList(org.drools.core.util.index.TupleList) SingleIndex(org.drools.core.util.AbstractHashTable.SingleIndex) DefaultFactHandle(org.drools.core.common.DefaultFactHandle) FieldIndex(org.drools.core.util.AbstractHashTable.FieldIndex) ClassFieldReader(org.drools.core.base.ClassFieldReader) Cheese(org.drools.core.test.model.Cheese) RightTupleImpl(org.drools.core.reteoo.RightTupleImpl) InternalFactHandle(org.drools.core.common.InternalFactHandle) RightTuple(org.drools.core.reteoo.RightTuple) RightTuple(org.drools.core.reteoo.RightTuple) Tuple(org.drools.core.spi.Tuple) Test(org.junit.Test)

Example 40 with RightTuple

use of org.drools.core.reteoo.RightTuple in project drools by kiegroup.

the class IndexedHashtableIteratorTest method testCanReachAllEntriesInLastTableRowRightTupleIndexHashTable.

@Test
public void testCanReachAllEntriesInLastTableRowRightTupleIndexHashTable() {
    // Construct a table with one row, containing one list, containing three entries.
    int numEntries = 3;
    TupleList[] table = new TupleList[3];
    TupleList rtList = new TupleList();
    table[0] = rtList;
    for (int i = 0; i < numEntries; i++) {
        RightTuple rightTuple = new RightTupleImpl();
        rightTuple.setMemory(rtList);
        rtList.add(rightTuple);
    }
    rtList = new TupleList();
    table[2] = rtList;
    for (int i = 0; i < numEntries; i++) {
        RightTuple rightTuple = new RightTupleImpl();
        rightTuple.setMemory(rtList);
        rtList.add(rightTuple);
    }
    rtList = new TupleList();
    table[2].setNext(rtList);
    for (int i = 0; i < numEntries; i++) {
        RightTuple rightTuple = new RightTupleImpl();
        rightTuple.setMemory(rtList);
        rtList.add(rightTuple);
    }
    // test fast
    TupleIndexHashTable.FullFastIterator iter = new TupleIndexHashTable.FullFastIterator(table);
    List<RightTuple> list = new ArrayList<RightTuple>();
    for (RightTuple rightTuple = (RightTuple) iter.next(null); rightTuple != null; rightTuple = (RightTuple) iter.next(rightTuple)) {
        // ensure no duplicate
        assertFalse(contains(list, rightTuple));
        list.add(rightTuple);
    }
    // test normal
    TupleIndexHashTable rthTable = new TupleIndexHashTable();
    rthTable.init(table, 3, numEntries * 3);
    TupleIndexHashTable.FieldIndexHashTableFullIterator iter2 = new TupleIndexHashTable.FieldIndexHashTableFullIterator(rthTable);
    list = new ArrayList<RightTuple>();
    for (RightTuple rightTuple = (RightTuple) iter2.next(); rightTuple != null; rightTuple = (RightTuple) iter2.next()) {
        // ensure no duplicate
        assertFalse(contains(list, rightTuple));
        list.add(rightTuple);
    }
    assertEquals(numEntries * 3, list.size());
}
Also used : ArrayList(java.util.ArrayList) RightTupleImpl(org.drools.core.reteoo.RightTupleImpl) TupleIndexHashTable(org.drools.core.util.index.TupleIndexHashTable) RightTuple(org.drools.core.reteoo.RightTuple) TupleList(org.drools.core.util.index.TupleList) Test(org.junit.Test)

Aggregations

RightTuple (org.drools.core.reteoo.RightTuple)60 LeftTuple (org.drools.core.reteoo.LeftTuple)41 TupleMemory (org.drools.core.reteoo.TupleMemory)30 FastIterator (org.drools.core.util.FastIterator)26 InternalFactHandle (org.drools.core.common.InternalFactHandle)20 BetaMemory (org.drools.core.reteoo.BetaMemory)20 BetaConstraints (org.drools.core.common.BetaConstraints)19 ContextEntry (org.drools.core.rule.ContextEntry)18 PhreakJoinNode.updateChildLeftTuple (org.drools.core.phreak.PhreakJoinNode.updateChildLeftTuple)13 Test (org.junit.Test)13 Tuple (org.drools.core.spi.Tuple)11 RightTupleImpl (org.drools.core.reteoo.RightTupleImpl)10 DefaultFactHandle (org.drools.core.common.DefaultFactHandle)9 AccumulateContext (org.drools.core.reteoo.AccumulateNode.AccumulateContext)8 BetaNode (org.drools.core.reteoo.BetaNode)7 FromMemory (org.drools.core.reteoo.FromNode.FromMemory)7 ArrayList (java.util.ArrayList)6 AccumulateMemory (org.drools.core.reteoo.AccumulateNode.AccumulateMemory)6 ObjectTypeNode (org.drools.core.reteoo.ObjectTypeNode)6 Accumulate (org.drools.core.rule.Accumulate)6