use of org.drools.core.util.index.TupleList in project drools by kiegroup.
the class PhreakTimerNode method doLeftDeletes.
public void doLeftDeletes(TimerNode timerNode, TimerNodeMemory tm, PathMemory pmem, LeftTupleSink sink, InternalAgenda agenda, TupleSets<LeftTuple> srcLeftTuples, TupleSets<LeftTuple> trgLeftTuples, TupleSets<LeftTuple> stagedLeftTuples) {
TimerService timerService = agenda.getWorkingMemory().getTimerService();
TupleList leftTuples = tm.getInsertOrUpdateLeftTuples();
TupleList deletes = tm.getDeleteLeftTuples();
if (!deletes.isEmpty()) {
for (LeftTuple leftTuple = (LeftTuple) deletes.getFirst(); leftTuple != null; ) {
LeftTuple next = (LeftTuple) leftTuple.getNext();
srcLeftTuples.addDelete(leftTuple);
if (log.isTraceEnabled()) {
log.trace("Timer Add Postponed Delete {}", leftTuple);
}
leftTuple.clear();
leftTuple = next;
}
deletes.clear();
}
for (LeftTuple leftTuple = srcLeftTuples.getDeleteFirst(); leftTuple != null; ) {
LeftTuple next = leftTuple.getStagedNext();
PropagationContext pctx = leftTuple.getPropagationContext();
Object obj = leftTuple.getContextObject();
if (obj instanceof DefaultJobHandle) {
timerService.removeJob((DefaultJobHandle) obj);
} else if (obj instanceof TupleKey && pctx.getReaderContext() != null) {
pctx.getReaderContext().removeTimerNodeScheduler(timerNode.getId(), (TupleKey) obj);
}
pctx = RuleTerminalNode.findMostRecentPropagationContext(leftTuple, pctx);
if (leftTuple.getMemory() != null) {
// it gets removed either way.
leftTuples.remove(leftTuple);
if (pctx.getFactHandle().isExpired()) {
// a expire clashes with insert or update, allow it to propagate once, will handle the expire the second time around
doPropagateChildLeftTuple(sink, trgLeftTuples, stagedLeftTuples, leftTuple);
tm.getDeleteLeftTuples().add(leftTuple);
// make sure it's dirty, so it'll evaluate again
pmem.doLinkRule(agenda);
if (log.isTraceEnabled()) {
log.trace("Timer Postponed Delete {}", leftTuple);
}
}
}
if (leftTuple.getMemory() == null) {
// if it's != null, then it's already been postponed, and the existing child propagated
// only has one child
LeftTuple childLeftTuple = leftTuple.getFirstChild();
if (childLeftTuple != null) {
childLeftTuple.setPropagationContext(leftTuple.getPropagationContext());
RuleNetworkEvaluator.deleteChildLeftTuple(childLeftTuple, trgLeftTuples, stagedLeftTuples);
if (log.isTraceEnabled()) {
log.trace("Timer Delete {}", leftTuple);
}
}
}
leftTuple.clearStaged();
leftTuple = next;
}
}
use of org.drools.core.util.index.TupleList in project drools by kiegroup.
the class PhreakTimerNode method doPropagateChildLeftTuples.
private static void doPropagateChildLeftTuples(TimerNodeMemory tm, LeftTupleSink sink, TupleSets<LeftTuple> trgLeftTuples, TupleSets<LeftTuple> stagedLeftTuples) {
TupleList leftTuples = tm.getInsertOrUpdateLeftTuples();
for (LeftTuple leftTuple = (LeftTuple) leftTuples.getFirst(); leftTuple != null; ) {
LeftTuple next = (LeftTuple) leftTuple.getNext();
doPropagateChildLeftTuple(sink, trgLeftTuples, stagedLeftTuples, leftTuple);
leftTuple.clear();
leftTuple = next;
}
// doLeftDeletes handles deletes, directly into the trgLeftTuples
if (tm.getDeleteLeftTuples().isEmpty()) {
// dirty bit can only be reset when there are no InsertOUdate LeftTuples and no Delete staged LeftTuples.
tm.setNodeCleanWithoutNotify();
}
leftTuples.clear();
}
use of org.drools.core.util.index.TupleList in project drools by kiegroup.
the class IndexingTest method testBuildsIndexedMemory.
@Test(timeout = 10000)
public void testBuildsIndexedMemory() {
// tests indexes are correctly built
String drl = "";
drl += "package org.drools.compiler.test\n";
drl += "import org.drools.compiler.Person\n";
drl += "global java.util.List list\n";
drl += "rule test1\n";
drl += "when\n";
drl += " $p1 : Person($name : name )\n";
// indexed
drl += " $p2 : Person(name == $name)\n";
// indexed
drl += " $p3 : Person(name == $p1.name)\n";
// not indexed
drl += " $p4 : Person(address.street == $p1.address.street)\n";
// indexed
drl += " $p5 : Person(address.street == $p1.name)\n";
// drl += " $p6 : Person( $name == name)\n"; // not indexed and won't compile
// indexed
drl += " $p7 : Person(addresses[\"key\"].street == $p1.name)\n";
// indexed
drl += " $p8 : Person(addresses[0].street == $p1.name)\n";
// not indexed
drl += " $p9 : Person(name == $p1.address.street)\n";
// indexed
drl += " $p10 : Person(addresses[0].street + 'xx' == $p1.name)\n";
// not indexed
drl += " $p11 : Person(addresses[$p1].street == $p1.name)\n";
drl += "then\n";
drl += "end\n";
KieBase kbase = loadKnowledgeBaseFromString(drl);
ObjectTypeNode node = getObjectTypeNode(kbase, Person.class);
InternalWorkingMemory wm = ((StatefulKnowledgeSessionImpl) kbase.newKieSession());
LeftInputAdapterNode liaNode = (LeftInputAdapterNode) node.getObjectSinkPropagator().getSinks()[0];
// $p2
JoinNode j2 = (JoinNode) liaNode.getSinkPropagator().getSinks()[0];
// $p3
JoinNode j3 = (JoinNode) j2.getSinkPropagator().getSinks()[0];
// $p4
JoinNode j4 = (JoinNode) j3.getSinkPropagator().getSinks()[0];
// $p5
JoinNode j5 = (JoinNode) j4.getSinkPropagator().getSinks()[0];
// JoinNode j6 = ( JoinNode ) j5.getSinkPropagator().getSinks()[0]; // $p6 // won't compile
// $p7
JoinNode j7 = (JoinNode) j5.getSinkPropagator().getSinks()[0];
// $p8
JoinNode j8 = (JoinNode) j7.getSinkPropagator().getSinks()[0];
// $p9
JoinNode j9 = (JoinNode) j8.getSinkPropagator().getSinks()[0];
// $p10
JoinNode j10 = (JoinNode) j9.getSinkPropagator().getSinks()[0];
// $p11
JoinNode j11 = (JoinNode) j10.getSinkPropagator().getSinks()[0];
SingleBetaConstraints c = (SingleBetaConstraints) j2.getRawConstraints();
assertEquals("$name", ((IndexableConstraint) c.getConstraint()).getFieldIndex().getDeclaration().getIdentifier());
assertTrue(c.isIndexed());
BetaMemory bm = (BetaMemory) wm.getNodeMemory(j2);
assertTrue(bm.getLeftTupleMemory() instanceof TupleIndexHashTable);
assertTrue(bm.getRightTupleMemory() instanceof TupleIndexHashTable);
c = (SingleBetaConstraints) j3.getRawConstraints();
assertEquals("name", ((IndexableConstraint) c.getConstraint()).getFieldIndex().getDeclaration().getIdentifier());
assertTrue(c.isIndexed());
bm = (BetaMemory) wm.getNodeMemory(j3);
assertTrue(bm.getLeftTupleMemory() instanceof TupleIndexHashTable);
assertTrue(bm.getRightTupleMemory() instanceof TupleIndexHashTable);
c = (SingleBetaConstraints) j4.getRawConstraints();
assertEquals("$p1", c.getConstraint().getRequiredDeclarations()[0].getIdentifier());
assertFalse(c.isIndexed());
bm = (BetaMemory) wm.getNodeMemory(j4);
assertTrue(bm.getLeftTupleMemory() instanceof TupleList);
assertTrue(bm.getRightTupleMemory() instanceof TupleList);
c = (SingleBetaConstraints) j5.getRawConstraints();
assertEquals("name", ((IndexableConstraint) c.getConstraint()).getFieldIndex().getDeclaration().getIdentifier());
assertTrue(c.isIndexed());
bm = (BetaMemory) wm.getNodeMemory(j5);
assertTrue(bm.getLeftTupleMemory() instanceof TupleIndexHashTable);
assertTrue(bm.getRightTupleMemory() instanceof TupleIndexHashTable);
// won't compile
// c = ( SingleBetaConstraints ) j6.getRawConstraints();
// assertEquals( "name", ((VariableConstraint)c.getConstraint()).getRequiredDeclarations()[0].getIdentifier() );
// assertFalse( c.isIndexed() );
// bm = ( BetaMemory ) wm.getNodeMemory( j6 );
// assertTrue( bm.getLeftTupleMemory() instanceof LeftTupleList );
// assertTrue( bm.getRightTupleMemory() instanceof RightTupleList );
c = (SingleBetaConstraints) j7.getRawConstraints();
assertEquals("name", ((IndexableConstraint) c.getConstraint()).getFieldIndex().getDeclaration().getIdentifier());
assertTrue(c.isIndexed());
bm = (BetaMemory) wm.getNodeMemory(j7);
assertTrue(bm.getLeftTupleMemory() instanceof TupleIndexHashTable);
assertTrue(bm.getRightTupleMemory() instanceof TupleIndexHashTable);
c = (SingleBetaConstraints) j8.getRawConstraints();
assertEquals("name", ((IndexableConstraint) c.getConstraint()).getFieldIndex().getDeclaration().getIdentifier());
assertTrue(c.isIndexed());
bm = (BetaMemory) wm.getNodeMemory(j8);
assertTrue(bm.getLeftTupleMemory() instanceof TupleIndexHashTable);
assertTrue(bm.getRightTupleMemory() instanceof TupleIndexHashTable);
c = (SingleBetaConstraints) j9.getRawConstraints();
assertEquals("$p1", c.getConstraint().getRequiredDeclarations()[0].getIdentifier());
assertFalse(c.isIndexed());
bm = (BetaMemory) wm.getNodeMemory(j9);
assertTrue(bm.getLeftTupleMemory() instanceof TupleList);
assertTrue(bm.getRightTupleMemory() instanceof TupleList);
c = (SingleBetaConstraints) j10.getRawConstraints();
assertEquals("name", ((IndexableConstraint) c.getConstraint()).getFieldIndex().getDeclaration().getIdentifier());
assertTrue(c.isIndexed());
bm = (BetaMemory) wm.getNodeMemory(j10);
assertTrue(bm.getLeftTupleMemory() instanceof TupleIndexHashTable);
assertTrue(bm.getRightTupleMemory() instanceof TupleIndexHashTable);
c = (SingleBetaConstraints) j11.getRawConstraints();
assertEquals("$p1", c.getConstraint().getRequiredDeclarations()[0].getIdentifier());
assertFalse(c.isIndexed());
bm = (BetaMemory) wm.getNodeMemory(j11);
assertTrue(bm.getLeftTupleMemory() instanceof TupleList);
assertTrue(bm.getRightTupleMemory() instanceof TupleList);
}
use of org.drools.core.util.index.TupleList 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.util.index.TupleList in project drools by kiegroup.
the class RightTupleIndexHashTableIteratorTest 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);
RightTuple[] tuples = new RightTuple[] { mock(RightTuple.class), mock(RightTuple.class), mock(RightTuple.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));
}
Aggregations