Search in sources :

Example 11 with RightTupleImpl

use of org.drools.core.reteoo.RightTupleImpl 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 12 with RightTupleImpl

use of org.drools.core.reteoo.RightTupleImpl 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 13 with RightTupleImpl

use of org.drools.core.reteoo.RightTupleImpl 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)

Example 14 with RightTupleImpl

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

the class BaseLeftTuplesBuilder method update.

public T update(Object... objects) {
    this.testStagedUpdate = true;
    if (objects == null) {
        objects = new Object[0];
    }
    for (int i = 0; i < objects.length; i++) {
        if (!(objects[i] instanceof Pair)) {
            Object o1 = objects[i];
            InternalFactHandle fh1 = (InternalFactHandle) wm.getFactHandle(o1);
            LeftTuple leftTuple = new LeftTupleImpl(fh1, sink, true);
            leftTuples.addUpdate(leftTuple);
        } else {
            Pair p = (Pair) objects[i];
            InternalFactHandle fh1 = (InternalFactHandle) wm.getFactHandle(p.getO1());
            LeftTuple leftTuple1 = new LeftTupleImpl(fh1, sink, true);
            InternalFactHandle fh2 = (InternalFactHandle) wm.getFactHandle(p.getO2());
            LeftTuple leftTuple2 = sink.createLeftTuple(leftTuple1, new RightTupleImpl(fh2), sink);
            leftTuples.addUpdate(leftTuple2);
        }
    }
    return (T) this;
}
Also used : LeftTupleImpl(org.drools.core.reteoo.LeftTupleImpl) RightTupleImpl(org.drools.core.reteoo.RightTupleImpl) InternalFactHandle(org.drools.core.common.InternalFactHandle) LeftTuple(org.drools.core.reteoo.LeftTuple)

Example 15 with RightTupleImpl

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

the class RightMemory method getRightTuples.

public List<RightTuple> getRightTuples(Object... objects) {
    BetaNode node = scenario.getBetaNode();
    BetaMemory bm = scenario.getBm();
    TupleMemory rtm = bm.getRightTupleMemory();
    InternalWorkingMemory wm = scenario.getWorkingMemory();
    if (objects == null) {
        objects = new Object[0];
    }
    List<RightTuple> rightTuples = new ArrayList<RightTuple>();
    for (Object object : objects) {
        InternalFactHandle fh = (InternalFactHandle) wm.insert(object);
        // node.createLeftTuple( fh, node, true );
        RightTuple expectedRightTuple = new RightTupleImpl(fh, node);
        expectedRightTuple.setPropagationContext(new PhreakPropagationContext());
        rightTuples.add(expectedRightTuple);
    }
    scenario.setTestRightMemory(true);
    return rightTuples;
}
Also used : InternalWorkingMemory(org.drools.core.common.InternalWorkingMemory) PhreakPropagationContext(org.drools.core.common.PhreakPropagationContext) BetaNode(org.drools.core.reteoo.BetaNode) ArrayList(java.util.ArrayList) BetaMemory(org.drools.core.reteoo.BetaMemory) RightTupleImpl(org.drools.core.reteoo.RightTupleImpl) InternalFactHandle(org.drools.core.common.InternalFactHandle) RightTuple(org.drools.core.reteoo.RightTuple) TupleMemory(org.drools.core.reteoo.TupleMemory)

Aggregations

RightTupleImpl (org.drools.core.reteoo.RightTupleImpl)16 InternalFactHandle (org.drools.core.common.InternalFactHandle)15 RightTuple (org.drools.core.reteoo.RightTuple)11 Test (org.junit.Test)11 DefaultFactHandle (org.drools.core.common.DefaultFactHandle)8 LeftTupleImpl (org.drools.core.reteoo.LeftTupleImpl)8 Cheese (org.drools.core.test.model.Cheese)8 FieldIndex (org.drools.core.util.AbstractHashTable.FieldIndex)8 Tuple (org.drools.core.spi.Tuple)7 TupleIndexHashTable (org.drools.core.util.index.TupleIndexHashTable)7 ClassObjectType (org.drools.core.base.ClassObjectType)6 InternalReadAccessor (org.drools.core.spi.InternalReadAccessor)6 TupleList (org.drools.core.util.index.TupleList)6 Declaration (org.drools.core.rule.Declaration)5 Pattern (org.drools.core.rule.Pattern)5 ArrayList (java.util.ArrayList)3 ClassFieldReader (org.drools.core.base.ClassFieldReader)3 LeftTuple (org.drools.core.reteoo.LeftTuple)3 SingleIndex (org.drools.core.util.AbstractHashTable.SingleIndex)3 InternalWorkingMemory (org.drools.core.common.InternalWorkingMemory)2