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());
}
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");
}
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");
}
}
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());
}
}
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;
}
Aggregations