Search in sources :

Example 6 with TupleList

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

the class RightTupleIndexHashTableTest method testTwoDifferentEntriesSameHashCode.

@Test
public void testTwoDifferentEntriesSameHashCode() throws Exception {
    final InternalReadAccessor extractor = store.getReader(TestClass.class, "object");
    final Pattern pattern = new Pattern(0, new ClassObjectType(TestClass.class));
    final Declaration declaration = new Declaration("theObject", extractor, pattern);
    final FieldIndex fieldIndex = new FieldIndex(extractor, declaration, MvelConstraint.INDEX_EVALUATOR);
    final TupleIndexHashTable map = new TupleIndexHashTable(new FieldIndex[] { fieldIndex }, false);
    final TestClass c1 = new TestClass(0, new TestClass(20, "stilton"));
    final InternalFactHandle ch1 = new DefaultFactHandle(1, c1);
    map.add(new RightTupleImpl(ch1, null));
    final TestClass c2 = new TestClass(0, new TestClass(20, "cheddar"));
    final InternalFactHandle ch2 = new DefaultFactHandle(2, c2);
    map.add(new RightTupleImpl(ch2, null));
    // same hashcode, but different values, so it should result in  a size of 2
    assertEquals(2, map.size());
    // however both are in the same table bucket
    assertEquals(1, tablePopulationSize(map));
    // this table bucket will have two FieldIndexEntries, as they are actually two different values
    Entry[] entries = getEntries(map);
    assertEquals(1, entries.length);
    TupleList list = (TupleList) entries[0];
    assertSame(ch2, list.getFirst().getFactHandle());
    assertNull(list.getFirst().getNext());
    assertSame(ch1, list.getNext().getFirst().getFactHandle());
    assertNull(list.getNext().getFirst().getNext());
    assertNull(list.getNext().getNext());
}
Also used : Pattern(org.drools.core.rule.Pattern) FieldIndex(org.drools.core.util.AbstractHashTable.FieldIndex) ClassObjectType(org.drools.core.base.ClassObjectType) RightTupleImpl(org.drools.core.reteoo.RightTupleImpl) TupleIndexHashTable(org.drools.core.util.index.TupleIndexHashTable) TupleList(org.drools.core.util.index.TupleList) DefaultFactHandle(org.drools.core.common.DefaultFactHandle) InternalReadAccessor(org.drools.core.spi.InternalReadAccessor) Declaration(org.drools.core.rule.Declaration) InternalFactHandle(org.drools.core.common.InternalFactHandle) Test(org.junit.Test)

Example 7 with TupleList

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

the class FieldIndexEntryTest method testTwoEntries.

@Test
public void testTwoEntries() {
    final ClassFieldReader extractor = store.getReader(Cheese.class, "type");
    final FieldIndex fieldIndex = new FieldIndex(extractor, null, MvelConstraint.INDEX_EVALUATOR);
    final SingleIndex singleIndex = new SingleIndex(new FieldIndex[] { fieldIndex }, 1);
    Tuple tuple = new RightTupleImpl(new DefaultFactHandle(1, new Cheese("stilton", 10)));
    final TupleList index = new AbstractHashTable.SingleIndexTupleList(singleIndex, tuple, "stilton".hashCode(), false);
    final Cheese stilton1 = new Cheese("stilton", 35);
    final InternalFactHandle h1 = new DefaultFactHandle(1, stilton1);
    final Cheese stilton2 = new Cheese("stilton", 59);
    final InternalFactHandle h2 = new DefaultFactHandle(2, stilton2);
    RightTuple h1RightTuple = new RightTupleImpl(h1, null);
    RightTuple h2RightTuple = new RightTupleImpl(h2, null);
    // test add
    index.add(h1RightTuple);
    index.add(h2RightTuple);
    assertEquals(h1, index.getFirst().getFactHandle());
    assertEquals(h2, ((RightTuple) index.getFirst().getNext()).getFactHandle());
    // test get
    assertEquals(h1, index.get(h1).getFactHandle());
    assertEquals(h2, index.get(h2).getFactHandle());
    // test removal for combinations
    // remove first
    index.remove(h2RightTuple);
    assertEquals(h1RightTuple.getFactHandle(), index.getFirst().getFactHandle());
    // remove second
    index.add(h2RightTuple);
    index.remove(h1RightTuple);
    assertEquals(h2RightTuple.getFactHandle(), index.getFirst().getFactHandle());
    // check index type does not change, as this fact is removed
    stilton1.setType("cheddar");
}
Also used : TupleList(org.drools.core.util.index.TupleList) SingleIndex(org.drools.core.util.AbstractHashTable.SingleIndex) DefaultFactHandle(org.drools.core.common.DefaultFactHandle) FieldIndex(org.drools.core.util.AbstractHashTable.FieldIndex) ClassFieldReader(org.drools.core.base.ClassFieldReader) Cheese(org.drools.core.test.model.Cheese) RightTupleImpl(org.drools.core.reteoo.RightTupleImpl) InternalFactHandle(org.drools.core.common.InternalFactHandle) RightTuple(org.drools.core.reteoo.RightTuple) RightTuple(org.drools.core.reteoo.RightTuple) Tuple(org.drools.core.spi.Tuple) Test(org.junit.Test)

Example 8 with TupleList

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

the class MemoryVisitor method checkRightTupleIndexHashTable.

private void checkRightTupleIndexHashTable(final TupleIndexHashTable memory) {
    final Entry[] entries = memory.getTable();
    int factCount = 0;
    int bucketCount = 0;
    FastIterator it = LinkedList.fastIterator;
    for (Entry entry1 : entries) {
        if (entry1 != null) {
            TupleList rightTupleList = (TupleList) entry1;
            while (rightTupleList != null) {
                if (rightTupleList.getFirst() != null) {
                    Entry entry = rightTupleList.getFirst();
                    while (entry != null) {
                        entry = it.next(entry);
                        factCount++;
                    }
                } else {
                    logger.info("error : fieldIndexHashTable cannot have empty FieldIndexEntry objects");
                }
                rightTupleList = rightTupleList.getNext();
                bucketCount++;
            }
        }
    }
    try {
        final Field field = AbstractHashTable.class.getDeclaredField("size");
        field.setAccessible(true);
        logger.info(indent() + "FieldIndexBuckets: " + field.get(memory) + ":" + bucketCount);
        if ((Integer) field.get(memory) != bucketCount) {
            logger.info(indent() + "error");
        }
    } catch (final Exception e) {
        e.printStackTrace();
    }
    logger.info(indent() + "FieldIndexFacts: " + memory.size() + ":" + factCount);
    if (memory.size() != factCount) {
        logger.info(indent() + "error");
    }
}
Also used : TupleList(org.drools.core.util.index.TupleList) Field(java.lang.reflect.Field) Entry(org.drools.core.util.Entry) FastIterator(org.drools.core.util.FastIterator) IOException(java.io.IOException)

Example 9 with TupleList

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

the class StatefulKnowledgeSessionImpl method getActivationParameters.

public Map getActivationParameters(Activation activation) {
    if (activation instanceof RuleAgendaItem) {
        RuleAgendaItem ruleAgendaItem = (RuleAgendaItem) activation;
        TupleList tupleList = ruleAgendaItem.getRuleExecutor().getLeftTupleList();
        Map result = new TreeMap();
        int i = 0;
        for (Tuple tuple = tupleList.getFirst(); tuple != null; tuple = tuple.getNext()) {
            Map params = getActivationParameters(tuple);
            result.put("Parameters set [" + i++ + "]", (Map.Entry[]) params.entrySet().toArray(new Map.Entry[params.size()]));
        }
        return result;
    } else {
        return getActivationParameters(activation.getTuple());
    }
}
Also used : TupleList(org.drools.core.util.index.TupleList) RuleAgendaItem(org.drools.core.phreak.RuleAgendaItem) PropagationEntry(org.drools.core.phreak.PropagationEntry) TreeMap(java.util.TreeMap) Map(java.util.Map) TreeMap(java.util.TreeMap) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) HashMap(java.util.HashMap) NamedEntryPoint(org.drools.core.common.NamedEntryPoint) WorkingMemoryEntryPoint(org.drools.core.WorkingMemoryEntryPoint) InternalWorkingMemoryEntryPoint(org.drools.core.common.InternalWorkingMemoryEntryPoint) LeftTuple(org.drools.core.reteoo.LeftTuple) Tuple(org.drools.core.spi.Tuple)

Example 10 with TupleList

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

the class DefaultAgenda method isRuleInstanceAgendaItem.

public boolean isRuleInstanceAgendaItem(String ruleflowGroupName, String ruleName, long processInstanceId) {
    propagationList.flush();
    RuleFlowGroup systemRuleFlowGroup = this.getRuleFlowGroup(ruleflowGroupName);
    Match[] matches = ((InternalAgendaGroup) systemRuleFlowGroup).getActivations();
    for (Match match : matches) {
        Activation act = (Activation) match;
        if (act.isRuleAgendaItem()) {
            // The lazy RuleAgendaItem must be fully evaluated, to see if there is a rule match
            RuleExecutor ruleExecutor = ((RuleAgendaItem) act).getRuleExecutor();
            ruleExecutor.evaluateNetwork(this);
            TupleList list = ruleExecutor.getLeftTupleList();
            for (RuleTerminalNodeLeftTuple lt = (RuleTerminalNodeLeftTuple) list.getFirst(); lt != null; lt = (RuleTerminalNodeLeftTuple) lt.getNext()) {
                if (ruleName.equals(lt.getRule().getName())) {
                    if (checkProcessInstance(lt, processInstanceId)) {
                        return true;
                    }
                }
            }
        } else {
            if (ruleName.equals(act.getRule().getName())) {
                if (checkProcessInstance(act, processInstanceId)) {
                    return true;
                }
            }
        }
    }
    return false;
}
Also used : RuleFlowGroup(org.drools.core.spi.RuleFlowGroup) TupleList(org.drools.core.util.index.TupleList) RuleAgendaItem(org.drools.core.phreak.RuleAgendaItem) RuleTerminalNodeLeftTuple(org.drools.core.reteoo.RuleTerminalNodeLeftTuple) RuleExecutor(org.drools.core.phreak.RuleExecutor) Activation(org.drools.core.spi.Activation) Match(org.kie.api.runtime.rule.Match)

Aggregations

TupleList (org.drools.core.util.index.TupleList)18 Test (org.junit.Test)12 InternalFactHandle (org.drools.core.common.InternalFactHandle)7 TupleIndexHashTable (org.drools.core.util.index.TupleIndexHashTable)7 RightTuple (org.drools.core.reteoo.RightTuple)6 RightTupleImpl (org.drools.core.reteoo.RightTupleImpl)6 ArrayList (java.util.ArrayList)5 DefaultFactHandle (org.drools.core.common.DefaultFactHandle)5 LeftTupleImpl (org.drools.core.reteoo.LeftTupleImpl)5 FieldIndex (org.drools.core.util.AbstractHashTable.FieldIndex)5 BetaMemory (org.drools.core.reteoo.BetaMemory)4 Tuple (org.drools.core.spi.Tuple)4 Cheese (org.drools.core.test.model.Cheese)4 FieldIndexHashTableFullIterator (org.drools.core.util.index.TupleIndexHashTable.FieldIndexHashTableFullIterator)4 RuleBaseConfiguration (org.drools.core.RuleBaseConfiguration)3 ClassFieldReader (org.drools.core.base.ClassFieldReader)3 SingleBetaConstraints (org.drools.core.common.SingleBetaConstraints)3 LeftTuple (org.drools.core.reteoo.LeftTuple)3 BetaNodeFieldConstraint (org.drools.core.spi.BetaNodeFieldConstraint)3 SingleIndex (org.drools.core.util.AbstractHashTable.SingleIndex)3