use of org.drools.core.spi.BetaNodeFieldConstraint in project drools by kiegroup.
the class SingleBetaConstraintsTest method testIndexed.
@Test
public void testIndexed() {
BetaNodeFieldConstraint constraint0 = getConstraint("cheeseType0", Operator.EQUAL, "type", Cheese.class);
BetaNodeFieldConstraint[] constraints = new BetaNodeFieldConstraint[] { constraint0 };
checkBetaConstraints(constraints, SingleBetaConstraints.class);
}
use of org.drools.core.spi.BetaNodeFieldConstraint in project drools by kiegroup.
the class RightTupleIndexHashTableIteratorTest 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);
KieBase kBase = 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.getRightTupleMemory().add(new RightTupleImpl(fh1, null));
betaMemory.getRightTupleMemory().add(new RightTupleImpl(fh2, null));
betaMemory.getRightTupleMemory().add(new RightTupleImpl(fh3, null));
betaMemory.getRightTupleMemory().add(new RightTupleImpl(fh4, null));
betaMemory.getRightTupleMemory().add(new RightTupleImpl(fh5, null));
betaMemory.getRightTupleMemory().add(new RightTupleImpl(fh6, null));
betaMemory.getRightTupleMemory().add(new RightTupleImpl(fh7, null));
betaMemory.getRightTupleMemory().add(new RightTupleImpl(fh8, null));
betaMemory.getRightTupleMemory().add(new RightTupleImpl(fh9, null));
TupleIndexHashTable hashTable = (TupleIndexHashTable) betaMemory.getRightTupleMemory();
// can't create a 0 hashCode, so forcing
TupleList rightTupleList = new TupleList();
rightTupleList.add(new RightTupleImpl(fh10, null));
hashTable.getTable()[0] = rightTupleList;
rightTupleList = new TupleList();
rightTupleList.add(new RightTupleImpl(fh11, null));
rightTupleList.add(new RightTupleImpl(fh12, null));
rightTupleList.add(new RightTupleImpl(fh13, null));
((TupleList) hashTable.getTable()[0]).setNext(rightTupleList);
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());
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.getRightTupleMemory().iterator();
for (RightTuple rightTuple = (RightTuple) it.next(); rightTuple != null; rightTuple = (RightTuple) it.next()) {
list.add(rightTuple);
}
assertEquals(13, list.size());
}
use of org.drools.core.spi.BetaNodeFieldConstraint in project drools by kiegroup.
the class CollectBuilder method build.
/**
* @inheritDoc
*/
public void build(final BuildContext context, final BuildUtils utils, final RuleConditionElement rce) {
boolean existSubNetwort = false;
final Collect collect = (Collect) rce;
context.pushRuleComponent(collect);
final List<BetaNodeFieldConstraint> resultBetaConstraints = context.getBetaconstraints();
final List<AlphaNodeFieldConstraint> resultAlphaConstraints = context.getAlphaConstraints();
final Pattern sourcePattern = collect.getSourcePattern();
// get builder for the pattern
final ReteooComponentBuilder builder = utils.getBuilderFor(sourcePattern);
// save tuple source and pattern offset for later if needed
final LeftTupleSource tupleSource = context.getTupleSource();
final int currentPatternIndex = context.getCurrentPatternOffset();
// builds the source pattern
builder.build(context, utils, sourcePattern);
// if object source is null, then we need to adapt tuple source into a subnetwork
if (context.getObjectSource() == null) {
RightInputAdapterNode riaNode = context.getComponentFactory().getNodeFactoryService().buildRightInputNode(context.getNextId(), context.getTupleSource(), tupleSource, context);
// attach right input adapter node to convert tuple source into an object source
context.setObjectSource(utils.attachNode(context, riaNode));
// restore tuple source from before the start of the sub network
context.setTupleSource(tupleSource);
// create a tuple start equals constraint and set it in the context
final TupleStartEqualsConstraint constraint = TupleStartEqualsConstraint.getInstance();
final List<BetaNodeFieldConstraint> betaConstraints = new ArrayList<BetaNodeFieldConstraint>();
betaConstraints.add(constraint);
context.setBetaconstraints(betaConstraints);
existSubNetwort = true;
}
BetaConstraints binder = utils.createBetaNodeConstraint(context, context.getBetaconstraints(), false);
// indexing for the results should be always disabled
BetaConstraints resultBinder = utils.createBetaNodeConstraint(context, resultBetaConstraints, true);
CollectAccumulator accumulator = new CollectAccumulator(collect, existSubNetwort);
Accumulate accumulate = new SingleAccumulate(sourcePattern, sourcePattern.getRequiredDeclarations(), accumulator);
AccumulateNode accNode = context.getComponentFactory().getNodeFactoryService().buildAccumulateNode(context.getNextId(), context.getTupleSource(), context.getObjectSource(), resultAlphaConstraints.toArray(new AlphaNodeFieldConstraint[resultAlphaConstraints.size()]), // source binder
binder, resultBinder, accumulate, existSubNetwort, context);
context.setTupleSource(utils.attachNode(context, accNode));
// source pattern was bound, so nulling context
context.setObjectSource(null);
context.setCurrentPatternOffset(currentPatternIndex);
context.popRuleComponent();
}
use of org.drools.core.spi.BetaNodeFieldConstraint in project drools by kiegroup.
the class IndexUtil method findDualConstraint.
private static int findDualConstraint(BetaNodeFieldConstraint[] constraints, int comparisonPos) {
if (!(USE_RANGE_INDEX && constraints[comparisonPos] instanceof MvelConstraint)) {
return -1;
}
MvelConstraint firstConstraint = (MvelConstraint) constraints[comparisonPos];
String leftValue = getLeftValueInExpression(firstConstraint.getExpression());
for (int i = comparisonPos + 1; i < constraints.length; i++) {
if (constraints[i] instanceof MvelConstraint) {
MvelConstraint dualConstraint = (MvelConstraint) constraints[i];
if (isDual(firstConstraint, leftValue, dualConstraint)) {
return i;
}
}
}
return -1;
}
use of org.drools.core.spi.BetaNodeFieldConstraint in project drools by kiegroup.
the class IndexUtil method sortRangeIndexable.
private static void sortRangeIndexable(BetaNodeFieldConstraint[] constraints, boolean[] indexable, int i) {
int dualConstraintPosition = findDualConstraint(constraints, i);
swap(constraints, i, 0);
indexable[0] = true;
if (dualConstraintPosition > 0) {
swap(constraints, dualConstraintPosition, 1);
indexable[1] = true;
}
}
Aggregations