001: package org.drools.common;
002:
003: import java.util.ArrayList;
004: import java.util.List;
005:
006: import org.drools.RuleBaseConfiguration;
007: import org.drools.base.ClassFieldExtractorCache;
008: import org.drools.base.ClassObjectType;
009: import org.drools.base.evaluators.Operator;
010: import org.drools.base.evaluators.StringFactory;
011: import org.drools.reteoo.BetaMemory;
012: import org.drools.rule.Declaration;
013: import org.drools.rule.Pattern;
014: import org.drools.rule.VariableConstraint;
015: import org.drools.spi.BetaNodeFieldConstraint;
016: import org.drools.spi.Evaluator;
017: import org.drools.spi.FieldExtractor;
018: import org.drools.util.FactHandleIndexHashTable;
019: import org.drools.util.FactHashTable;
020: import org.drools.util.LinkedList;
021: import org.drools.util.LinkedListEntry;
022: import org.drools.util.TupleHashTable;
023: import org.drools.util.TupleIndexHashTable;
024: import org.drools.util.AbstractHashTable.FieldIndex;
025: import org.drools.util.AbstractHashTable.Index;
026:
027: import junit.framework.TestCase;
028:
029: public abstract class BaseBetaConstraintsTest extends TestCase {
030:
031: protected BetaNodeFieldConstraint getConstraint(String identifier,
032: Operator operator, String fieldName, Class clazz) {
033: FieldExtractor extractor = ClassFieldExtractorCache
034: .getExtractor(clazz, fieldName, getClass()
035: .getClassLoader());
036: Declaration declaration = new Declaration(identifier,
037: extractor, new Pattern(0, new ClassObjectType(clazz)));
038: Evaluator evaluator = StringFactory.getInstance().getEvaluator(
039: operator);
040: return new VariableConstraint(extractor, declaration, evaluator);
041: }
042:
043: protected void checkBetaConstraints(
044: VariableConstraint[] constraints, Class cls) {
045: RuleBaseConfiguration config = new RuleBaseConfiguration();
046: int depth = config.getCompositeKeyDepth();
047:
048: BetaConstraints betaConstraints = null;
049:
050: try {
051: betaConstraints = (BetaConstraints) cls.getConstructor(
052: new Class[] { BetaNodeFieldConstraint[].class,
053: RuleBaseConfiguration.class }).newInstance(
054: new Object[] { constraints, config });
055: } catch (Exception e) {
056: throw new RuntimeException(
057: "could not invoke constructor for " + cls.getName());
058: }
059:
060: //BetaConstraints betaConstraints = new DefaultBetaConstraints(constraints, config );
061:
062: constraints = convertToConstraints(betaConstraints
063: .getConstraints());
064:
065: List list = new ArrayList();
066:
067: // get indexed positions
068: for (int i = 0; i < constraints.length && list.size() < depth; i++) {
069: if (constraints[i].getEvaluator().getOperator() == Operator.EQUAL) {
070: list.add(new Integer(i));
071: }
072: }
073:
074: // convert to array
075: int[] indexedPositions = new int[list.size()];
076: for (int i = 0; i < list.size(); i++) {
077: indexedPositions[i] = i;
078: }
079:
080: assertEquals((indexedPositions.length > 0), betaConstraints
081: .isIndexed());
082: assertEquals(indexedPositions.length, betaConstraints
083: .getIndexCount());
084: BetaMemory betaMemory = betaConstraints
085: .createBetaMemory(config);
086:
087: if (indexedPositions.length > 0) {
088: TupleIndexHashTable tupleHashTable = (TupleIndexHashTable) betaMemory
089: .getTupleMemory();
090: assertTrue(tupleHashTable.isIndexed());
091: Index index = tupleHashTable.getIndex();
092:
093: for (int i = 0; i < indexedPositions.length; i++) {
094: checkSameConstraintForIndex(
095: constraints[indexedPositions[i]], index
096: .getFieldIndex(i));
097: }
098:
099: FactHandleIndexHashTable factHashTable = (FactHandleIndexHashTable) betaMemory
100: .getFactHandleMemory();
101: assertTrue(factHashTable.isIndexed());
102: index = factHashTable.getIndex();
103:
104: for (int i = 0; i < indexedPositions.length; i++) {
105: checkSameConstraintForIndex(
106: constraints[indexedPositions[i]], index
107: .getFieldIndex(i));
108: }
109: } else {
110: TupleHashTable tupleHashTable = (TupleHashTable) betaMemory
111: .getTupleMemory();
112: assertFalse(tupleHashTable.isIndexed());
113:
114: FactHashTable factHashTable = (FactHashTable) betaMemory
115: .getFactHandleMemory();
116: assertFalse(factHashTable.isIndexed());
117: }
118: }
119:
120: protected void checkSameConstraintForIndex(
121: VariableConstraint constraint, FieldIndex fieldIndex) {
122: assertSame(constraint.getRequiredDeclarations()[0], fieldIndex
123: .getDeclaration());
124: assertSame(constraint.getEvaluator(), fieldIndex.getEvaluator());
125: assertSame(constraint.getFieldExtractor(), fieldIndex
126: .getExtractor());
127: }
128:
129: protected VariableConstraint[] convertToConstraints(LinkedList list) {
130: final VariableConstraint[] array = new VariableConstraint[list
131: .size()];
132: int i = 0;
133: for (LinkedListEntry entry = (LinkedListEntry) list.getFirst(); entry != null; entry = (LinkedListEntry) entry
134: .getNext()) {
135: array[i++] = (VariableConstraint) entry.getObject();
136: }
137: return array;
138: }
139: }
|