Search in sources :

Example 21 with BetaNodeFieldConstraint

use of org.drools.core.spi.BetaNodeFieldConstraint in project drools by kiegroup.

the class DoubleBetaConstraints method cloneIfInUse.

public DoubleBetaConstraints cloneIfInUse() {
    if (constraints[0] instanceof MutableTypeConstraint && ((MutableTypeConstraint) constraints[0]).setInUse()) {
        BetaNodeFieldConstraint[] clonedConstraints = new BetaNodeFieldConstraint[constraints.length];
        for (int i = 0; i < constraints.length; i++) {
            clonedConstraints[i] = constraints[i].cloneIfInUse();
        }
        DoubleBetaConstraints clone = new DoubleBetaConstraints(clonedConstraints, indexPrecedenceOption, disableIndexing);
        clone.indexed = indexed;
        return clone;
    }
    return this;
}
Also used : MutableTypeConstraint(org.drools.core.rule.MutableTypeConstraint) BetaNodeFieldConstraint(org.drools.core.spi.BetaNodeFieldConstraint) MvelConstraint(org.drools.core.rule.constraint.MvelConstraint) BetaNodeFieldConstraint(org.drools.core.spi.BetaNodeFieldConstraint) MutableTypeConstraint(org.drools.core.rule.MutableTypeConstraint)

Example 22 with BetaNodeFieldConstraint

use of org.drools.core.spi.BetaNodeFieldConstraint in project drools by kiegroup.

the class QuadroupleBetaConstraints method cloneIfInUse.

public QuadroupleBetaConstraints cloneIfInUse() {
    if (constraints[0] instanceof MutableTypeConstraint && ((MutableTypeConstraint) constraints[0]).setInUse()) {
        BetaNodeFieldConstraint[] clonedConstraints = new BetaNodeFieldConstraint[constraints.length];
        for (int i = 0; i < constraints.length; i++) {
            clonedConstraints[i] = constraints[i].cloneIfInUse();
        }
        QuadroupleBetaConstraints clone = new QuadroupleBetaConstraints(clonedConstraints, indexPrecedenceOption, disableIndexing);
        clone.indexed = indexed;
        return clone;
    }
    return this;
}
Also used : MutableTypeConstraint(org.drools.core.rule.MutableTypeConstraint) BetaNodeFieldConstraint(org.drools.core.spi.BetaNodeFieldConstraint) MvelConstraint(org.drools.core.rule.constraint.MvelConstraint) BetaNodeFieldConstraint(org.drools.core.spi.BetaNodeFieldConstraint) MutableTypeConstraint(org.drools.core.rule.MutableTypeConstraint)

Example 23 with BetaNodeFieldConstraint

use of org.drools.core.spi.BetaNodeFieldConstraint in project drools by kiegroup.

the class TripleBetaConstraints method cloneIfInUse.

public TripleBetaConstraints cloneIfInUse() {
    if (constraints[0] instanceof MutableTypeConstraint && ((MutableTypeConstraint) constraints[0]).setInUse()) {
        BetaNodeFieldConstraint[] clonedConstraints = new BetaNodeFieldConstraint[constraints.length];
        for (int i = 0; i < constraints.length; i++) {
            clonedConstraints[i] = constraints[i].cloneIfInUse();
        }
        TripleBetaConstraints clone = new TripleBetaConstraints(clonedConstraints, indexPrecedenceOption, disableIndexing);
        clone.indexed = indexed;
        return clone;
    }
    return this;
}
Also used : MutableTypeConstraint(org.drools.core.rule.MutableTypeConstraint) BetaNodeFieldConstraint(org.drools.core.spi.BetaNodeFieldConstraint) MvelConstraint(org.drools.core.rule.constraint.MvelConstraint) BetaNodeFieldConstraint(org.drools.core.spi.BetaNodeFieldConstraint) MutableTypeConstraint(org.drools.core.rule.MutableTypeConstraint)

Example 24 with BetaNodeFieldConstraint

use of org.drools.core.spi.BetaNodeFieldConstraint in project drools by kiegroup.

the class LeftLeftTupleIndexHashTableIteratorTest 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);
    InternalKnowledgeBase kBase = (InternalKnowledgeBase) 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.getLeftTupleMemory().add(new LeftTupleImpl(fh1, null, true));
    betaMemory.getLeftTupleMemory().add(new LeftTupleImpl(fh2, null, true));
    betaMemory.getLeftTupleMemory().add(new LeftTupleImpl(fh3, null, true));
    betaMemory.getLeftTupleMemory().add(new LeftTupleImpl(fh4, null, true));
    betaMemory.getLeftTupleMemory().add(new LeftTupleImpl(fh5, null, true));
    betaMemory.getLeftTupleMemory().add(new LeftTupleImpl(fh6, null, true));
    betaMemory.getLeftTupleMemory().add(new LeftTupleImpl(fh7, null, true));
    betaMemory.getLeftTupleMemory().add(new LeftTupleImpl(fh8, null, true));
    betaMemory.getLeftTupleMemory().add(new LeftTupleImpl(fh9, null, true));
    TupleIndexHashTable hashTable = (TupleIndexHashTable) betaMemory.getLeftTupleMemory();
    // can't create a 0 hashCode, so forcing
    TupleList leftTupleList = new TupleList();
    leftTupleList.add(new LeftTupleImpl(fh10, null, true));
    hashTable.getTable()[0] = leftTupleList;
    leftTupleList = new TupleList();
    leftTupleList.add(new LeftTupleImpl(fh11, null, true));
    leftTupleList.add(new LeftTupleImpl(fh12, null, true));
    leftTupleList.add(new LeftTupleImpl(fh13, null, true));
    ((TupleList) hashTable.getTable()[0]).setNext(leftTupleList);
    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());
    // This tests the hashcode index allocation. If the rehash function (or any other way hashcodes are computed) changes, these numbers will change.
    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.getLeftTupleMemory().iterator();
    for (LeftTupleImpl leftTuple = (LeftTupleImpl) it.next(); leftTuple != null; leftTuple = (LeftTupleImpl) it.next()) {
        list.add(leftTuple);
    }
    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) TupleIndexHashTable(org.drools.core.util.index.TupleIndexHashTable) BetaNodeFieldConstraint(org.drools.core.spi.BetaNodeFieldConstraint) BetaNodeFieldConstraint(org.drools.core.spi.BetaNodeFieldConstraint) RuleBaseConfiguration(org.drools.core.RuleBaseConfiguration) TupleList(org.drools.core.util.index.TupleList) 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) InternalKnowledgeBase(org.drools.core.impl.InternalKnowledgeBase) Test(org.junit.Test)

Example 25 with BetaNodeFieldConstraint

use of org.drools.core.spi.BetaNodeFieldConstraint 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

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