Search in sources :

Example 16 with BetaNodeFieldConstraint

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);
}
Also used : BetaNodeFieldConstraint(org.drools.core.spi.BetaNodeFieldConstraint) Test(org.junit.Test)

Example 17 with BetaNodeFieldConstraint

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());
}
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) RightTupleImpl(org.drools.core.reteoo.RightTupleImpl) TupleIndexHashTable(org.drools.core.util.index.TupleIndexHashTable) BetaNodeFieldConstraint(org.drools.core.spi.BetaNodeFieldConstraint) RightTuple(org.drools.core.reteoo.RightTuple) BetaNodeFieldConstraint(org.drools.core.spi.BetaNodeFieldConstraint) RuleBaseConfiguration(org.drools.core.RuleBaseConfiguration) TupleList(org.drools.core.util.index.TupleList) KieBase(org.kie.api.KieBase) 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) Test(org.junit.Test)

Example 18 with BetaNodeFieldConstraint

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();
}
Also used : Pattern(org.drools.core.rule.Pattern) BetaConstraints(org.drools.core.common.BetaConstraints) Collect(org.drools.core.rule.Collect) AccumulateNode(org.drools.core.reteoo.AccumulateNode) ArrayList(java.util.ArrayList) TupleStartEqualsConstraint(org.drools.core.common.TupleStartEqualsConstraint) BetaNodeFieldConstraint(org.drools.core.spi.BetaNodeFieldConstraint) SingleAccumulate(org.drools.core.rule.SingleAccumulate) BetaNodeFieldConstraint(org.drools.core.spi.BetaNodeFieldConstraint) AlphaNodeFieldConstraint(org.drools.core.spi.AlphaNodeFieldConstraint) TupleStartEqualsConstraint(org.drools.core.common.TupleStartEqualsConstraint) SingleAccumulate(org.drools.core.rule.SingleAccumulate) Accumulate(org.drools.core.rule.Accumulate) LeftTupleSource(org.drools.core.reteoo.LeftTupleSource) AlphaNodeFieldConstraint(org.drools.core.spi.AlphaNodeFieldConstraint) CollectAccumulator(org.drools.core.base.accumulators.CollectAccumulator) RightInputAdapterNode(org.drools.core.reteoo.RightInputAdapterNode)

Example 19 with BetaNodeFieldConstraint

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;
}
Also used : MvelConstraint(org.drools.core.rule.constraint.MvelConstraint) 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 20 with BetaNodeFieldConstraint

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;
    }
}
Also used : 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)

Aggregations

BetaNodeFieldConstraint (org.drools.core.spi.BetaNodeFieldConstraint)43 Test (org.junit.Test)28 MvelConstraint (org.drools.core.rule.constraint.MvelConstraint)8 IndexableConstraint (org.drools.core.rule.IndexableConstraint)7 Cheese (org.drools.core.test.model.Cheese)6 ArrayList (java.util.ArrayList)5 Constraint (org.drools.core.spi.Constraint)5 BetaConstraints (org.drools.core.common.BetaConstraints)4 MutableTypeConstraint (org.drools.core.rule.MutableTypeConstraint)4 RuleBaseConfiguration (org.drools.core.RuleBaseConfiguration)3 BetaMemory (org.drools.core.reteoo.BetaMemory)3 AlphaNodeFieldConstraint (org.drools.core.spi.AlphaNodeFieldConstraint)3 TupleIndexHashTable (org.drools.core.util.index.TupleIndexHashTable)3 TupleList (org.drools.core.util.index.TupleList)3 List (java.util.List)2 InternalFactHandle (org.drools.core.common.InternalFactHandle)2 SingleBetaConstraints (org.drools.core.common.SingleBetaConstraints)2 TupleStartEqualsConstraint (org.drools.core.common.TupleStartEqualsConstraint)2 InternalKnowledgeBase (org.drools.core.impl.InternalKnowledgeBase)2 AccumulateNode (org.drools.core.reteoo.AccumulateNode)2