Search in sources :

Example 16 with TupleIndexHashTable

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

the class BaseBetaConstraintsTest method checkBetaConstraints.

protected void checkBetaConstraints(BetaNodeFieldConstraint[] constraints, Class cls, short betaNodeType) {
    RuleBaseConfiguration config = new RuleBaseConfiguration();
    int depth = config.getCompositeKeyDepth();
    BetaConstraints betaConstraints;
    try {
        betaConstraints = (BetaConstraints) cls.getConstructor(new Class[] { BetaNodeFieldConstraint[].class, RuleBaseConfiguration.class }).newInstance(constraints, config);
    } catch (Exception e) {
        throw new RuntimeException("could not invoke constructor for " + cls.getName());
    }
    betaConstraints.initIndexes(depth, betaNodeType);
    // BetaConstraints betaConstraints = new DefaultBetaConstraints(constraints, config );
    constraints = betaConstraints.getConstraints();
    List<Integer> list = new ArrayList<Integer>();
    // get indexed positions
    for (int i = 0; i < constraints.length && list.size() < depth; i++) {
        if (((IndexableConstraint) constraints[i]).isIndexable(betaNodeType)) {
            list.add(i);
        }
    }
    // convert to array
    int[] indexedPositions = new int[list.size()];
    for (int i = 0; i < list.size(); i++) {
        indexedPositions[i] = i;
    }
    assertEquals((indexedPositions.length > 0), betaConstraints.isIndexed());
    assertEquals(indexedPositions.length, betaConstraints.getIndexCount());
    BetaMemory betaMemory = betaConstraints.createBetaMemory(config, NodeTypeEnums.JoinNode);
    if (indexedPositions.length > 0) {
        if (((IndexableConstraint) constraints[indexedPositions[0]]).getConstraintType() == ConstraintType.EQUAL) {
            TupleIndexHashTable tupleHashTable = (TupleIndexHashTable) betaMemory.getLeftTupleMemory();
            assertTrue(tupleHashTable.isIndexed());
            Index index = tupleHashTable.getIndex();
            for (int i = 0; i < indexedPositions.length; i++) {
                checkSameConstraintForIndex((IndexableConstraint) constraints[indexedPositions[i]], index.getFieldIndex(i));
            }
            TupleIndexHashTable factHashTable = (TupleIndexHashTable) betaMemory.getRightTupleMemory();
            assertTrue(factHashTable.isIndexed());
            index = factHashTable.getIndex();
            for (int i = 0; i < indexedPositions.length; i++) {
                checkSameConstraintForIndex((IndexableConstraint) constraints[indexedPositions[i]], index.getFieldIndex(i));
            }
        } else {
        }
    } else {
        TupleList tupleHashTable = (TupleList) betaMemory.getLeftTupleMemory();
        assertFalse(tupleHashTable.isIndexed());
        TupleList factHashTable = (TupleList) betaMemory.getRightTupleMemory();
        assertFalse(factHashTable.isIndexed());
    }
}
Also used : ArrayList(java.util.ArrayList) BetaMemory(org.drools.core.reteoo.BetaMemory) FieldIndex(org.drools.core.util.AbstractHashTable.FieldIndex) Index(org.drools.core.util.AbstractHashTable.Index) TupleIndexHashTable(org.drools.core.util.index.TupleIndexHashTable) BetaNodeFieldConstraint(org.drools.core.spi.BetaNodeFieldConstraint) IndexableConstraint(org.drools.core.rule.IndexableConstraint) RuleBaseConfiguration(org.drools.core.RuleBaseConfiguration) TupleList(org.drools.core.util.index.TupleList) IndexableConstraint(org.drools.core.rule.IndexableConstraint)

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