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 delete.
public T delete(Object... objects) {
this.testStagedDelete = 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.addDelete(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.addDelete(leftTuple2);
}
}
return (T) this;
}
use of org.drools.core.reteoo.RightTupleImpl in project drools by kiegroup.
the class RightTupleIndexHashTableIteratorTest 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);
KieBase kBase = 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.getRightTupleMemory().add(new RightTupleImpl(fh1, null));
betaMemory.getRightTupleMemory().add(new RightTupleImpl(fh2, null));
betaMemory.getRightTupleMemory().add(new RightTupleImpl(fh3, null));
betaMemory.getRightTupleMemory().add(new RightTupleImpl(fh4, null));
betaMemory.getRightTupleMemory().add(new RightTupleImpl(fh5, null));
betaMemory.getRightTupleMemory().add(new RightTupleImpl(fh6, null));
betaMemory.getRightTupleMemory().add(new RightTupleImpl(fh7, null));
betaMemory.getRightTupleMemory().add(new RightTupleImpl(fh8, null));
betaMemory.getRightTupleMemory().add(new RightTupleImpl(fh9, null));
TupleIndexHashTable hashTable = (TupleIndexHashTable) betaMemory.getRightTupleMemory();
// can't create a 0 hashCode, so forcing
TupleList rightTupleList = new TupleList();
rightTupleList.add(new RightTupleImpl(fh10, null));
hashTable.getTable()[0] = rightTupleList;
rightTupleList = new TupleList();
rightTupleList.add(new RightTupleImpl(fh11, null));
rightTupleList.add(new RightTupleImpl(fh12, null));
rightTupleList.add(new RightTupleImpl(fh13, null));
((TupleList) hashTable.getTable()[0]).setNext(rightTupleList);
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());
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.getRightTupleMemory().iterator();
for (RightTuple rightTuple = (RightTuple) it.next(); rightTuple != null; rightTuple = (RightTuple) it.next()) {
list.add(rightTuple);
}
assertEquals(13, list.size());
}
use of org.drools.core.reteoo.RightTupleImpl in project drools by kiegroup.
the class RightTupleIndexHashTableTest method testSingleEntry.
@Test
public void testSingleEntry() 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, MvelConstraint.INDEX_EVALUATOR);
final TupleIndexHashTable map = new TupleIndexHashTable(new FieldIndex[] { fieldIndex }, false);
final Cheese cheddar = new Cheese("cheddar", 10);
final InternalFactHandle cheddarHandle1 = new DefaultFactHandle(0, cheddar);
assertEquals(0, map.size());
assertNull(map.getFirst(new LeftTupleImpl(cheddarHandle1, null, true)));
final Cheese stilton1 = new Cheese("stilton", 35);
RightTuple stiltonRighTuple = new RightTupleImpl(new DefaultFactHandle(1, stilton1), null);
map.add(stiltonRighTuple);
assertEquals(1, map.size());
assertEquals(1, tablePopulationSize(map));
final Cheese stilton2 = new Cheese("stilton", 80);
final InternalFactHandle stiltonHandle2 = new DefaultFactHandle(2, stilton2);
final Tuple tuple = map.getFirst(new LeftTupleImpl(stiltonHandle2, null, true));
assertSame(stiltonRighTuple.getFactHandle(), tuple.getFactHandle());
assertNull(tuple.getNext());
}
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, 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());
}
Aggregations