use of org.drools.core.reteoo.RightTupleImpl in project drools by kiegroup.
the class BaseLeftTuplesBuilder method insert.
public T insert(Object... objects) {
this.testStagedInsert = 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.addInsert(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.addInsert(leftTuple2);
}
}
return (T) this;
}
use of org.drools.core.reteoo.RightTupleImpl 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;
}
use of org.drools.core.reteoo.RightTupleImpl in project drools by kiegroup.
the class RightTupleIndexHashTableTest method testRemove.
@Test
public void testRemove() throws Exception {
final InternalReadAccessor extractor = store.getReader(Cheese.class, "type");
final Pattern pattern = new Pattern(0, new ClassObjectType(Cheese.class));
final Declaration declaration = new Declaration("typeOfCheese", extractor, pattern);
final FieldIndex fieldIndex = new FieldIndex(extractor, declaration);
final TupleIndexHashTable map = new TupleIndexHashTable(new FieldIndex[] { fieldIndex }, false);
assertEquals(0, map.size());
final Cheese stilton1 = new Cheese("stilton", 35);
final InternalFactHandle stiltonHandle1 = new DefaultFactHandle(1, stilton1);
RightTuple stiltonRightTuple1 = new RightTupleImpl(stiltonHandle1, null);
map.add(stiltonRightTuple1);
final Cheese cheddar1 = new Cheese("cheddar", 35);
final InternalFactHandle cheddarHandle1 = new DefaultFactHandle(2, cheddar1);
RightTuple cheddarRightTuple1 = new RightTupleImpl(cheddarHandle1, null);
map.add(cheddarRightTuple1);
final Cheese stilton2 = new Cheese("stilton", 81);
final InternalFactHandle stiltonHandle2 = new DefaultFactHandle(3, stilton2);
RightTuple stiltonRightTuple2 = new RightTupleImpl(stiltonHandle2, null);
map.add(stiltonRightTuple2);
assertEquals(3, map.size());
assertEquals(2, tablePopulationSize(map));
// cheddar is in its own bucket, which should be removed once empty. We cannot have
// empty FieldIndexEntries in the Map, as they get their value from the first FactEntry.
map.remove(cheddarRightTuple1);
assertEquals(2, map.size());
assertEquals(1, tablePopulationSize(map));
// We remove t he stiltonHandle2, but there is still one more stilton, so size should be the same
map.remove(stiltonRightTuple2);
assertEquals(1, map.size());
assertEquals(1, tablePopulationSize(map));
// No more stiltons, so the table should be empty
map.remove(stiltonRightTuple1);
assertEquals(0, map.size());
assertEquals(0, tablePopulationSize(map));
}
use of org.drools.core.reteoo.RightTupleImpl 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);
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.reteoo.RightTupleImpl in project drools by kiegroup.
the class FieldIndexEntryTest method testSingleEntry.
@Test
public void testSingleEntry() {
final ClassFieldReader extractor = store.getReader(Cheese.class, "type");
final FieldIndex fieldIndex = new FieldIndex(extractor, new Declaration("id", extractor, null));
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.IndexTupleList(singleIndex, new AbstractHashTable.SingleHashEntry("stilton".hashCode(), "stilton"));
// Test initial construction
assertNull(index.getFirst());
final Cheese stilton1 = new Cheese("stilton", 35);
final InternalFactHandle h1 = new DefaultFactHandle(1, stilton1);
// test add
RightTuple h1RightTuple = new RightTupleImpl(h1, null);
index.add(h1RightTuple);
final Tuple entry1 = index.getFirst();
assertSame(h1, entry1.getFactHandle());
assertNull(entry1.getNext());
assertSame(entry1, index.get(h1));
// test get
final Tuple entry2 = index.get(new RightTupleImpl(h1, null));
assertSame(entry1, entry2);
// test remove
index.remove(h1RightTuple);
assertNull(index.getFirst());
}
Aggregations