Search in sources :

Example 6 with FieldIndex

use of org.drools.core.util.AbstractHashTable.FieldIndex in project drools by kiegroup.

the class IndexUtil method getIndexedProperties.

public static List<String> getIndexedProperties(BetaNode betaNode, RuleBaseConfiguration config) {
    int keyDepth = config.getCompositeKeyDepth();
    if (config.getCompositeKeyDepth() < 1) {
        return Collections.emptyList();
    }
    Factory.IndexSpec indexSpec = new Factory.IndexSpec(config.getIndexPrecedenceOption(), keyDepth, betaNode.getType(), betaNode.getConstraints());
    List<String> indexedProps = new ArrayList<String>();
    for (FieldIndex fieldIndex : indexSpec.indexes) {
        indexedProps.add(getter2property(fieldIndex.getExtractor().getNativeReadMethodName()));
    }
    return indexedProps;
}
Also used : FieldIndex(org.drools.core.util.AbstractHashTable.FieldIndex) ArrayList(java.util.ArrayList) BetaNodeFieldConstraint(org.drools.core.spi.BetaNodeFieldConstraint) Constraint(org.drools.core.spi.Constraint) MvelConstraint(org.drools.core.rule.constraint.MvelConstraint) IndexableConstraint(org.drools.core.rule.IndexableConstraint)

Example 7 with FieldIndex

use of org.drools.core.util.AbstractHashTable.FieldIndex in project drools by kiegroup.

the class RightTupleIndexHashTableTest method testResize.

@Test
public void testResize() 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(16, 0.75f, new FieldIndex[] { fieldIndex }, false);
    assertEquals(0, map.size());
    final Cheese stilton1 = new Cheese("stilton", 35);
    map.add(newRightTuple(1, stilton1));
    final Cheese stilton2 = new Cheese("stilton", 81);
    map.add(newRightTuple(2, stilton2));
    final Cheese cheddar1 = new Cheese("cheddar", 35);
    map.add(newRightTuple(3, cheddar1));
    final Cheese cheddar2 = new Cheese("cheddar", 38);
    map.add(newRightTuple(4, cheddar2));
    final Cheese brie = new Cheese("brie", 293);
    map.add(newRightTuple(5, brie));
    final Cheese mozerella = new Cheese("mozerella", 15);
    map.add(newRightTuple(6, mozerella));
    final Cheese dolcelatte = new Cheese("dolcelatte", 284);
    map.add(newRightTuple(7, dolcelatte));
    final Cheese camembert1 = new Cheese("camembert", 924);
    map.add(newRightTuple(8, camembert1));
    final Cheese camembert2 = new Cheese("camembert", 765);
    map.add(newRightTuple(9, camembert2));
    final Cheese redLeicestor = new Cheese("red leicestor", 23);
    map.add(newRightTuple(10, redLeicestor));
    final Cheese wensleydale = new Cheese("wensleydale", 20);
    map.add(newRightTuple(11, wensleydale));
    final Cheese edam = new Cheese("edam", 12);
    map.add(newRightTuple(12, edam));
    final Cheese goude1 = new Cheese("goude", 93);
    map.add(newRightTuple(13, goude1));
    final Cheese goude2 = new Cheese("goude", 88);
    map.add(newRightTuple(14, goude2));
    final Cheese gruyere = new Cheese("gruyere", 82);
    map.add(newRightTuple(15, gruyere));
    final Cheese emmental = new Cheese("emmental", 98);
    map.add(newRightTuple(16, emmental));
    // At this point we have 16 facts but only 12 different types of cheeses
    // so no table resize and thus its size is 16
    assertEquals(16, map.size());
    Entry[] table = map.getTable();
    assertEquals(16, table.length);
    final Cheese feta = new Cheese("feta", 48);
    map.add(newRightTuple(2, feta));
    // This adds our 13th type of cheese. The map is set with an initial capacity of 16 and
    // a threshold of 75%, that after 12 it should resize the map to 32.
    assertEquals(17, map.size());
    table = map.getTable();
    assertEquals(32, table.length);
    final Cheese haloumi = new Cheese("haloumi", 48);
    map.add(newRightTuple(2, haloumi));
    final Cheese chevre = new Cheese("chevre", 48);
    map.add(newRightTuple(2, chevre));
}
Also used : Pattern(org.drools.core.rule.Pattern) 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) TupleIndexHashTable(org.drools.core.util.index.TupleIndexHashTable) Test(org.junit.Test)

Example 8 with FieldIndex

use of org.drools.core.util.AbstractHashTable.FieldIndex 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 9 with FieldIndex

use of org.drools.core.util.AbstractHashTable.FieldIndex 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 10 with FieldIndex

use of org.drools.core.util.AbstractHashTable.FieldIndex 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)

Aggregations

FieldIndex (org.drools.core.util.AbstractHashTable.FieldIndex)11 Test (org.junit.Test)10 DefaultFactHandle (org.drools.core.common.DefaultFactHandle)9 InternalFactHandle (org.drools.core.common.InternalFactHandle)9 Cheese (org.drools.core.test.model.Cheese)9 RightTupleImpl (org.drools.core.reteoo.RightTupleImpl)8 ClassObjectType (org.drools.core.base.ClassObjectType)7 RightTuple (org.drools.core.reteoo.RightTuple)7 Declaration (org.drools.core.rule.Declaration)7 Pattern (org.drools.core.rule.Pattern)7 InternalReadAccessor (org.drools.core.spi.InternalReadAccessor)7 TupleIndexHashTable (org.drools.core.util.index.TupleIndexHashTable)7 Tuple (org.drools.core.spi.Tuple)6 LeftTupleImpl (org.drools.core.reteoo.LeftTupleImpl)4 TupleList (org.drools.core.util.index.TupleList)4 ClassFieldReader (org.drools.core.base.ClassFieldReader)3 SingleIndex (org.drools.core.util.AbstractHashTable.SingleIndex)3 ArrayList (java.util.ArrayList)1 IndexableConstraint (org.drools.core.rule.IndexableConstraint)1 MvelConstraint (org.drools.core.rule.constraint.MvelConstraint)1