Search in sources :

Example 11 with TupleIndexHashTable

use of org.drools.core.util.index.TupleIndexHashTable 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 12 with TupleIndexHashTable

use of org.drools.core.util.index.TupleIndexHashTable in project drools by kiegroup.

the class RightTupleIndexHashTableTest method testEmptyIterator.

@Test
public void testEmptyIterator() {
    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);
    final Cheese stilton = new Cheese("stilton", 55);
    final InternalFactHandle stiltonHandle = new DefaultFactHandle(2, stilton);
    assertNull(map.getFirst(new LeftTupleImpl(stiltonHandle, null, true)));
}
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) LeftTupleImpl(org.drools.core.reteoo.LeftTupleImpl) Cheese(org.drools.core.test.model.Cheese) Declaration(org.drools.core.rule.Declaration) TupleIndexHashTable(org.drools.core.util.index.TupleIndexHashTable) InternalFactHandle(org.drools.core.common.InternalFactHandle) Test(org.junit.Test)

Example 13 with TupleIndexHashTable

use of org.drools.core.util.index.TupleIndexHashTable 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 TupleIndexHashTable

use of org.drools.core.util.index.TupleIndexHashTable in project drools by kiegroup.

the class IndexedHashtableIteratorTest method testCanReachAllEntriesInLastTableRowLeftTupleIndexHashTable.

@Test
public void testCanReachAllEntriesInLastTableRowLeftTupleIndexHashTable() {
    // 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++) {
        LeftTupleImpl leftTuple = new LeftTupleImpl();
        leftTuple.setMemory(rtList);
        rtList.add(leftTuple);
    }
    rtList = new TupleList();
    table[2] = rtList;
    for (int i = 0; i < numEntries; i++) {
        LeftTupleImpl leftTuple = new LeftTupleImpl();
        leftTuple.setMemory(rtList);
        rtList.add(leftTuple);
    }
    rtList = new TupleList();
    table[2].setNext(rtList);
    for (int i = 0; i < numEntries; i++) {
        LeftTupleImpl leftTuple = new LeftTupleImpl();
        leftTuple.setMemory(rtList);
        rtList.add(leftTuple);
    }
    // test fast
    TupleIndexHashTable.FullFastIterator iter = new TupleIndexHashTable.FullFastIterator(table);
    List<LeftTupleImpl> list = new ArrayList<LeftTupleImpl>();
    for (LeftTupleImpl leftTuple = (LeftTupleImpl) iter.next(null); leftTuple != null; leftTuple = (LeftTupleImpl) iter.next(leftTuple)) {
        // ensure no duplicate
        assertFalse(contains(list, leftTuple));
        list.add(leftTuple);
    }
    assertEquals(numEntries * 3, list.size());
    // test normal
    TupleIndexHashTable lthTable = new TupleIndexHashTable();
    lthTable.init(table, 3, numEntries * 3);
    TupleIndexHashTable.FieldIndexHashTableFullIterator iter2 = new TupleIndexHashTable.FieldIndexHashTableFullIterator(lthTable);
    list = new ArrayList<LeftTupleImpl>();
    for (LeftTupleImpl leftTuple = (LeftTupleImpl) iter2.next(); leftTuple != null; leftTuple = (LeftTupleImpl) iter2.next()) {
        // ensure no duplicate
        assertFalse(contains(list, leftTuple));
        list.add(leftTuple);
    }
    assertEquals(numEntries * 3, list.size());
}
Also used : TupleList(org.drools.core.util.index.TupleList) ArrayList(java.util.ArrayList) LeftTupleImpl(org.drools.core.reteoo.LeftTupleImpl) TupleIndexHashTable(org.drools.core.util.index.TupleIndexHashTable) Test(org.junit.Test)

Example 15 with TupleIndexHashTable

use of org.drools.core.util.index.TupleIndexHashTable in project drools by kiegroup.

the class LeftLeftTupleIndexHashTableIteratorTest method test1.

@Test
public void test1() {
    BetaNodeFieldConstraint constraint0 = getConstraint("d", Operator.EQUAL, "this", Foo.class);
    BetaNodeFieldConstraint[] constraints = new BetaNodeFieldConstraint[] { constraint0 };
    RuleBaseConfiguration config = new RuleBaseConfiguration();
    BetaConstraints betaConstraints = null;
    betaConstraints = new SingleBetaConstraints(constraints, config);
    BetaMemory betaMemory = betaConstraints.createBetaMemory(config, NodeTypeEnums.JoinNode);
    InternalKnowledgeBase kBase = (InternalKnowledgeBase) KnowledgeBaseFactory.newKnowledgeBase();
    KieSession ss = kBase.newKieSession();
    InternalFactHandle fh1 = (InternalFactHandle) ss.insert(new Foo("brie", 1));
    InternalFactHandle fh2 = (InternalFactHandle) ss.insert(new Foo("brie", 1));
    InternalFactHandle fh3 = (InternalFactHandle) ss.insert(new Foo("soda", 1));
    InternalFactHandle fh4 = (InternalFactHandle) ss.insert(new Foo("soda", 1));
    InternalFactHandle fh5 = (InternalFactHandle) ss.insert(new Foo("bread", 3));
    InternalFactHandle fh6 = (InternalFactHandle) ss.insert(new Foo("bread", 3));
    InternalFactHandle fh7 = (InternalFactHandle) ss.insert(new Foo("cream", 3));
    InternalFactHandle fh8 = (InternalFactHandle) ss.insert(new Foo("gorda", 15));
    InternalFactHandle fh9 = (InternalFactHandle) ss.insert(new Foo("beer", 16));
    InternalFactHandle fh10 = (InternalFactHandle) ss.insert(new Foo("mars", 0));
    InternalFactHandle fh11 = (InternalFactHandle) ss.insert(new Foo("snicker", 0));
    InternalFactHandle fh12 = (InternalFactHandle) ss.insert(new Foo("snicker", 0));
    InternalFactHandle fh13 = (InternalFactHandle) ss.insert(new Foo("snicker", 0));
    betaMemory.getLeftTupleMemory().add(new LeftTupleImpl(fh1, null, true));
    betaMemory.getLeftTupleMemory().add(new LeftTupleImpl(fh2, null, true));
    betaMemory.getLeftTupleMemory().add(new LeftTupleImpl(fh3, null, true));
    betaMemory.getLeftTupleMemory().add(new LeftTupleImpl(fh4, null, true));
    betaMemory.getLeftTupleMemory().add(new LeftTupleImpl(fh5, null, true));
    betaMemory.getLeftTupleMemory().add(new LeftTupleImpl(fh6, null, true));
    betaMemory.getLeftTupleMemory().add(new LeftTupleImpl(fh7, null, true));
    betaMemory.getLeftTupleMemory().add(new LeftTupleImpl(fh8, null, true));
    betaMemory.getLeftTupleMemory().add(new LeftTupleImpl(fh9, null, true));
    TupleIndexHashTable hashTable = (TupleIndexHashTable) betaMemory.getLeftTupleMemory();
    // can't create a 0 hashCode, so forcing
    TupleList leftTupleList = new TupleList();
    leftTupleList.add(new LeftTupleImpl(fh10, null, true));
    hashTable.getTable()[0] = leftTupleList;
    leftTupleList = new TupleList();
    leftTupleList.add(new LeftTupleImpl(fh11, null, true));
    leftTupleList.add(new LeftTupleImpl(fh12, null, true));
    leftTupleList.add(new LeftTupleImpl(fh13, null, true));
    ((TupleList) hashTable.getTable()[0]).setNext(leftTupleList);
    Entry[] table = hashTable.getTable();
    List list = new ArrayList();
    for (int i = 0; i < table.length; i++) {
        if (table[i] != null) {
            List entries = new ArrayList();
            entries.add(i);
            Entry entry = table[i];
            while (entry != null) {
                entries.add(entry);
                entry = entry.getNext();
            }
            list.add(entries.toArray());
        }
    }
    assertEquals(5, list.size());
    // This tests the hashcode index allocation. If the rehash function (or any other way hashcodes are computed) changes, these numbers will change.
    Object[] entries = (Object[]) list.get(0);
    assertEquals(0, entries[0]);
    assertEquals(3, entries.length);
    entries = (Object[]) list.get(1);
    assertEquals(102, entries[0]);
    assertEquals(2, entries.length);
    entries = (Object[]) list.get(2);
    assertEquals(103, entries[0]);
    assertEquals(2, entries.length);
    entries = (Object[]) list.get(3);
    assertEquals(115, entries[0]);
    assertEquals(3, entries.length);
    entries = (Object[]) list.get(4);
    assertEquals(117, entries[0]);
    assertEquals(3, entries.length);
    // System.out.println( entries );
    list = new ArrayList<LeftTupleImpl>();
    Iterator it = betaMemory.getLeftTupleMemory().iterator();
    for (LeftTupleImpl leftTuple = (LeftTupleImpl) it.next(); leftTuple != null; leftTuple = (LeftTupleImpl) it.next()) {
        list.add(leftTuple);
    }
    assertEquals(13, list.size());
}
Also used : SingleBetaConstraints(org.drools.core.common.SingleBetaConstraints) BetaConstraints(org.drools.core.common.BetaConstraints) SingleBetaConstraints(org.drools.core.common.SingleBetaConstraints) ArrayList(java.util.ArrayList) BetaMemory(org.drools.core.reteoo.BetaMemory) TupleIndexHashTable(org.drools.core.util.index.TupleIndexHashTable) BetaNodeFieldConstraint(org.drools.core.spi.BetaNodeFieldConstraint) BetaNodeFieldConstraint(org.drools.core.spi.BetaNodeFieldConstraint) RuleBaseConfiguration(org.drools.core.RuleBaseConfiguration) TupleList(org.drools.core.util.index.TupleList) FieldIndexHashTableFullIterator(org.drools.core.util.index.TupleIndexHashTable.FieldIndexHashTableFullIterator) LeftTupleImpl(org.drools.core.reteoo.LeftTupleImpl) KieSession(org.kie.api.runtime.KieSession) ArrayList(java.util.ArrayList) TupleList(org.drools.core.util.index.TupleList) List(java.util.List) InternalFactHandle(org.drools.core.common.InternalFactHandle) InternalKnowledgeBase(org.drools.core.impl.InternalKnowledgeBase) Test(org.junit.Test)

Aggregations

TupleIndexHashTable (org.drools.core.util.index.TupleIndexHashTable)16 Test (org.junit.Test)15 InternalFactHandle (org.drools.core.common.InternalFactHandle)10 FieldIndex (org.drools.core.util.AbstractHashTable.FieldIndex)8 ClassObjectType (org.drools.core.base.ClassObjectType)7 BetaMemory (org.drools.core.reteoo.BetaMemory)7 LeftTupleImpl (org.drools.core.reteoo.LeftTupleImpl)7 RightTuple (org.drools.core.reteoo.RightTuple)7 RightTupleImpl (org.drools.core.reteoo.RightTupleImpl)7 Declaration (org.drools.core.rule.Declaration)7 Pattern (org.drools.core.rule.Pattern)7 InternalReadAccessor (org.drools.core.spi.InternalReadAccessor)7 TupleList (org.drools.core.util.index.TupleList)7 ArrayList (java.util.ArrayList)6 DefaultFactHandle (org.drools.core.common.DefaultFactHandle)6 Cheese (org.drools.core.test.model.Cheese)6 KieBase (org.kie.api.KieBase)5 LeftInputAdapterNode (org.drools.core.reteoo.LeftInputAdapterNode)4 ObjectTypeNode (org.drools.core.reteoo.ObjectTypeNode)4 RuleBaseConfiguration (org.drools.core.RuleBaseConfiguration)3