use of org.drools.core.reteoo.LeftTupleImpl in project drools by kiegroup.
the class IndexedHashtableIteratorTest method testCanReachAllEntriesInLastTableRowLeftTupleIndexHashTable.
@Test
public void testCanReachAllEntriesInLastTableRowLeftTupleIndexHashTable() {
// Construct a table with one row, containing one list, containing three entries.
int numEntries = 3;
TupleList[] table = new TupleList[3];
TupleList rtList = new TupleList();
table[0] = rtList;
for (int i = 0; i < numEntries; i++) {
LeftTupleImpl leftTuple = new LeftTupleImpl();
leftTuple.setMemory(rtList);
rtList.add(leftTuple);
}
rtList = new TupleList();
table[2] = rtList;
for (int i = 0; i < numEntries; i++) {
LeftTupleImpl leftTuple = new LeftTupleImpl();
leftTuple.setMemory(rtList);
rtList.add(leftTuple);
}
rtList = new TupleList();
table[2].setNext(rtList);
for (int i = 0; i < numEntries; i++) {
LeftTupleImpl leftTuple = new LeftTupleImpl();
leftTuple.setMemory(rtList);
rtList.add(leftTuple);
}
// test fast
TupleIndexHashTable.FullFastIterator iter = new TupleIndexHashTable.FullFastIterator(table);
List<LeftTupleImpl> list = new ArrayList<LeftTupleImpl>();
for (LeftTupleImpl leftTuple = (LeftTupleImpl) iter.next(null); leftTuple != null; leftTuple = (LeftTupleImpl) iter.next(leftTuple)) {
// ensure no duplicate
assertFalse(contains(list, leftTuple));
list.add(leftTuple);
}
assertEquals(numEntries * 3, list.size());
// test normal
TupleIndexHashTable lthTable = new TupleIndexHashTable();
lthTable.init(table, 3, numEntries * 3);
TupleIndexHashTable.FieldIndexHashTableFullIterator iter2 = new TupleIndexHashTable.FieldIndexHashTableFullIterator(lthTable);
list = new ArrayList<LeftTupleImpl>();
for (LeftTupleImpl leftTuple = (LeftTupleImpl) iter2.next(); leftTuple != null; leftTuple = (LeftTupleImpl) iter2.next()) {
// ensure no duplicate
assertFalse(contains(list, leftTuple));
list.add(leftTuple);
}
assertEquals(numEntries * 3, list.size());
}
use of org.drools.core.reteoo.LeftTupleImpl in project drools by kiegroup.
the class LeftLeftTupleIndexHashTableIteratorTest method testLastBucketInTheTable.
@Test
public void testLastBucketInTheTable() {
// JBRULES-2574
// setup the entry array with an element in the first bucket, one
// in the middle and one in the last bucket
Entry[] entries = new Entry[10];
entries[0] = mock(TupleList.class);
entries[5] = mock(TupleList.class);
entries[9] = mock(TupleList.class);
LeftTupleImpl[] tuples = new LeftTupleImpl[] { mock(LeftTupleImpl.class), mock(LeftTupleImpl.class), mock(LeftTupleImpl.class) };
// set return values for methods
when(entries[0].getNext()).thenReturn(null);
when(((TupleList) entries[0]).getFirst()).thenReturn(tuples[0]);
when(entries[5].getNext()).thenReturn(null);
when(((TupleList) entries[5]).getFirst()).thenReturn(tuples[1]);
when(entries[9].getNext()).thenReturn(null);
when(((TupleList) entries[9]).getFirst()).thenReturn(tuples[2]);
// create the mock table for the iterator
AbstractHashTable table = mock(AbstractHashTable.class);
when(table.getTable()).thenReturn(entries);
// create the iterator
FieldIndexHashTableFullIterator iterator = new FieldIndexHashTableFullIterator(table);
// test it
assertThat(iterator.next(), sameInstance((Object) tuples[0]));
assertThat(iterator.next(), sameInstance((Object) tuples[1]));
assertThat(iterator.next(), sameInstance((Object) tuples[2]));
assertThat(iterator.next(), is((Object) null));
}
use of org.drools.core.reteoo.LeftTupleImpl 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());
}
use of org.drools.core.reteoo.LeftTupleImpl in project drools by kiegroup.
the class MVELAccumulateBuilderTest method testSimpleExpression.
@Test
public void testSimpleExpression() {
KnowledgeBuilderImpl pkgBuilder = new KnowledgeBuilderImpl();
pkgBuilder.addPackage(new PackageDescr("pkg1"));
InternalKnowledgePackage pkg = pkgBuilder.getPackage("pkg1");
final RuleDescr ruleDescr = new RuleDescr("rule 1");
final KnowledgeBuilderConfigurationImpl conf = pkgBuilder.getBuilderConfiguration();
DialectCompiletimeRegistry dialectRegistry = pkgBuilder.getPackageRegistry(pkg.getName()).getDialectCompiletimeRegistry();
MVELDialect mvelDialect = (MVELDialect) dialectRegistry.getDialect("mvel");
final RuleBuildContext context = new RuleBuildContext(pkgBuilder, ruleDescr, dialectRegistry, pkg, mvelDialect);
final AccumulateDescr accDescr = new AccumulateDescr();
final PatternDescr inputPattern = new PatternDescr("org.drools.compiler.Cheese", "$cheese");
accDescr.setInputPattern(inputPattern);
accDescr.setInitCode("total = 0;");
accDescr.setActionCode("total += $cheese.price;");
accDescr.setReverseCode("total -= $cheese.price;");
accDescr.setResultCode("new Integer(total)");
final MVELAccumulateBuilder builder = new MVELAccumulateBuilder();
final Accumulate acc = (Accumulate) builder.build(context, accDescr);
((MVELCompileable) acc.getAccumulators()[0]).compile((MVELDialectRuntimeData) pkgBuilder.getPackageRegistry(pkg.getName()).getDialectRuntimeRegistry().getDialectData("mvel"));
InternalKnowledgeBase kBase = KnowledgeBaseFactory.newKnowledgeBase();
StatefulKnowledgeSessionImpl ksession = (StatefulKnowledgeSessionImpl) kBase.newKieSession();
MockLeftTupleSink sink = new MockLeftTupleSink();
final Cheese cheddar1 = new Cheese("cheddar", 10);
final Cheese cheddar2 = new Cheese("cheddar", 8);
final InternalFactHandle f0 = (InternalFactHandle) ksession.insert(new InitialFactImpl());
final InternalFactHandle f1 = (InternalFactHandle) ksession.insert(cheddar1);
final InternalFactHandle f2 = (InternalFactHandle) ksession.insert(cheddar2);
final LeftTupleImpl tuple = new LeftTupleImpl(f0, sink, true);
Object wmContext = acc.createWorkingMemoryContext();
Object accContext = acc.createContext();
acc.init(wmContext, accContext, tuple, ksession);
acc.accumulate(wmContext, accContext, tuple, f1, ksession);
acc.accumulate(wmContext, accContext, tuple, f2, ksession);
assertEquals(new Integer(18), acc.getResult(wmContext, accContext, tuple, ksession));
acc.reverse(wmContext, accContext, tuple, f1, ksession);
assertEquals(new Integer(8), acc.getResult(wmContext, accContext, tuple, ksession));
}
use of org.drools.core.reteoo.LeftTupleImpl in project drools by kiegroup.
the class BaseLeftTuplesBuilder method update.
public T update(Object... objects) {
this.testStagedUpdate = true;
if (objects == null) {
objects = new Object[0];
}
for (int i = 0; i < objects.length; i++) {
if (!(objects[i] instanceof Pair)) {
Object o1 = objects[i];
InternalFactHandle fh1 = (InternalFactHandle) wm.getFactHandle(o1);
LeftTuple leftTuple = new LeftTupleImpl(fh1, sink, true);
leftTuples.addUpdate(leftTuple);
} else {
Pair p = (Pair) objects[i];
InternalFactHandle fh1 = (InternalFactHandle) wm.getFactHandle(p.getO1());
LeftTuple leftTuple1 = new LeftTupleImpl(fh1, sink, true);
InternalFactHandle fh2 = (InternalFactHandle) wm.getFactHandle(p.getO2());
LeftTuple leftTuple2 = sink.createLeftTuple(leftTuple1, new RightTupleImpl(fh2), sink);
leftTuples.addUpdate(leftTuple2);
}
}
return (T) this;
}
Aggregations