Skip to content

Commit

Permalink
[PLANNER-488] remove deleted RightTuples from the RightTupleMemory be…
Browse files Browse the repository at this point in the history
…fore processing the updates
  • Loading branch information
mariofusco committed May 2, 2016
1 parent 319b3b4 commit 11c04ff
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 44 deletions.
15 changes: 9 additions & 6 deletions drools-compiler/src/test/java/org/drools/compiler/Person.java
Expand Up @@ -18,6 +18,8 @@

import org.drools.core.factmodel.traits.Traitable;

import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import java.io.Serializable;
import java.math.BigDecimal;
import java.math.BigInteger;
Expand All @@ -26,9 +28,6 @@
import java.util.List;
import java.util.Map;

import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;

@Traitable
@XmlAccessorType(XmlAccessType.FIELD)
public class Person
Expand Down Expand Up @@ -67,13 +66,17 @@ public Person() {

}

public Person(String name,
int age) {
super();
public Person(String name, int age) {
this.name = name;
this.age = age;
}

public Person(String name, int age, boolean happy) {
this.name = name;
this.age = age;
this.happy = happy;
}

public Person(final String name) {
this(name,
"",
Expand Down
Expand Up @@ -8452,4 +8452,47 @@ public void testComplexEvals2() {
kieSession.insert( true );
assertEquals(4, kieSession.fireAllRules());
}

@Test
public void testDeletedRightTupleInChangedBucket() {
// PLANNER-488
String drl =
"import " + Person.class.getCanonicalName() + "\n" +
"rule R when\n" +
" Person( $name: name, $age: age )\n" +
" not Person( happy, name == $name, age == $age-1 )\n" +
"then\n" +
"end";

KieSession kieSession = new KieHelper().addContent( drl, ResourceType.DRL )
.build().newKieSession();

Person p1 = new Person( "C", 1, true );
Person p2 = new Person( "B", 1, true );
Person p3 = new Person( "B", 2, true );
Person p4 = new Person( "A", 2 );

FactHandle fh1 = kieSession.insert( p1 );
FactHandle fh2 = kieSession.insert( p2 );
FactHandle fh3 = kieSession.insert( p3 );
FactHandle fh4 = kieSession.insert( p4 );

kieSession.fireAllRules();

p4.setName( "B" );
p4.setHappy( true );
kieSession.update( fh4, p4 );

kieSession.fireAllRules();

p3.setName( "A" );
p3.setHappy( false );
kieSession.update( fh3, p3 );
p1.setName( "B" );
kieSession.update( fh1, p1 );
p2.setName( "C" );
kieSession.update( fh2, p2 );

kieSession.fireAllRules();
}
}
Expand Up @@ -738,21 +738,17 @@ public static void doUpdatesReorderLeftMemory(BetaMemory bm,
TupleMemory ltm = bm.getLeftTupleMemory();

// sides must first be re-ordered, to ensure iteration integrity
for (LeftTuple leftTuple = srcLeftTuples.getUpdateFirst(); leftTuple != null; ) {
LeftTuple next = leftTuple.getStagedNext();
for (LeftTuple leftTuple = srcLeftTuples.getUpdateFirst(); leftTuple != null; leftTuple = leftTuple.getStagedNext()) {
ltm.remove(leftTuple);
leftTuple = next;
}

for (LeftTuple leftTuple = srcLeftTuples.getUpdateFirst(); leftTuple != null; ) {
LeftTuple next = leftTuple.getStagedNext();
for (LeftTuple leftTuple = srcLeftTuples.getUpdateFirst(); leftTuple != null; leftTuple = leftTuple.getStagedNext()) {
ltm.add(leftTuple);
for (LeftTuple childLeftTuple = leftTuple.getFirstChild(); childLeftTuple != null; ) {
LeftTuple childNext = childLeftTuple.getHandleNext();
childLeftTuple.reAddRight();
childLeftTuple = childNext;
}
leftTuple = next;
}
}

Expand All @@ -761,16 +757,13 @@ public static void doUpdatesExistentialReorderLeftMemory(BetaMemory bm,
TupleMemory ltm = bm.getLeftTupleMemory();

// sides must first be re-ordered, to ensure iteration integrity
for (LeftTuple leftTuple = srcLeftTuples.getUpdateFirst(); leftTuple != null; ) {
LeftTuple next = leftTuple.getStagedNext();
for (LeftTuple leftTuple = srcLeftTuples.getUpdateFirst(); leftTuple != null; leftTuple = leftTuple.getStagedNext()) {
if ( leftTuple.getMemory() != null ) {
ltm.remove(leftTuple);
}
leftTuple = next;
}

for (LeftTuple leftTuple = srcLeftTuples.getUpdateFirst(); leftTuple != null; ) {
LeftTuple next = leftTuple.getStagedNext();
for (LeftTuple leftTuple = srcLeftTuples.getUpdateFirst(); leftTuple != null; leftTuple = leftTuple.getStagedNext()) {
RightTuple blocker = leftTuple.getBlocker();
if ( blocker == null ) {
ltm.add(leftTuple);
Expand All @@ -783,25 +776,21 @@ public static void doUpdatesExistentialReorderLeftMemory(BetaMemory bm,
// it's blocker is also being updated, so remove to force it to start from the beginning
blocker.removeBlocked( leftTuple );
}
leftTuple = next;
}
}

public static void doUpdatesReorderRightMemory(BetaMemory bm,
TupleSets<RightTuple> srcRightTuples) {
TupleMemory rtm = bm.getRightTupleMemory();

for (RightTuple rightTuple = srcRightTuples.getUpdateFirst(); rightTuple != null; ) {
RightTuple next = rightTuple.getStagedNext();
for (RightTuple rightTuple = srcRightTuples.getUpdateFirst(); rightTuple != null; rightTuple = rightTuple.getStagedNext()) {
if ( rightTuple.getMemory() != null ) {
rightTuple.setTempRightTupleMemory(rightTuple.getMemory());
rtm.remove(rightTuple);
}
rightTuple = next;
}

for (RightTuple rightTuple = srcRightTuples.getUpdateFirst(); rightTuple != null; ) {
RightTuple next = rightTuple.getStagedNext();
for (RightTuple rightTuple = srcRightTuples.getUpdateFirst(); rightTuple != null; rightTuple = rightTuple.getStagedNext()) {
if ( rightTuple.getTempRightTupleMemory() != null ) {
rtm.add(rightTuple);
for (LeftTuple childLeftTuple = rightTuple.getFirstChild(); childLeftTuple != null; ) {
Expand All @@ -810,7 +799,6 @@ public static void doUpdatesReorderRightMemory(BetaMemory bm,
childLeftTuple = childNext;
}
}
rightTuple = next;
}
}

Expand All @@ -825,8 +813,11 @@ public static void doUpdatesExistentialReorderRightMemory(BetaMemory bm,
// this is to avoid split bucket when an updated rightTuple hasn't been moved yet
// and so it is the first entry in the wrong bucket

for (RightTuple rightTuple = srcRightTuples.getUpdateFirst(); rightTuple != null; ) {
RightTuple next = rightTuple.getStagedNext();
for (RightTuple rightTuple = srcRightTuples.getDeleteFirst(); rightTuple != null; rightTuple = rightTuple.getStagedNext()) {
rtm.remove(rightTuple);
}

for (RightTuple rightTuple = srcRightTuples.getUpdateFirst(); rightTuple != null; rightTuple = rightTuple.getStagedNext()) {
if (rightTuple.getMemory() != null) {
rightTuple.setTempRightTupleMemory(rightTuple.getMemory());

Expand Down Expand Up @@ -856,30 +847,24 @@ public static void doUpdatesExistentialReorderRightMemory(BetaMemory bm,
rightTuple.setBlocked(null);
rtm.remove(rightTuple);
}
rightTuple = next;
}

for (RightTuple rightTuple = srcRightTuples.getUpdateFirst(); rightTuple != null; ) {
RightTuple next = rightTuple.getStagedNext();
if ( rightTuple.getTempRightTupleMemory() != null ) {
for (RightTuple rightTuple = srcRightTuples.getUpdateFirst(); rightTuple != null; rightTuple = rightTuple.getStagedNext()) {
rtm.add( rightTuple );

rtm.add( rightTuple );

if (resumeFromCurrent) {
RightTuple tempRightTuple = rightTuple.getTempNextRightTuple();
if ( rightTuple.getBlocked() != null && tempRightTuple == null && rightTuple.getMemory() == rightTuple.getTempRightTupleMemory() ) {
// the next RightTuple was null, but current RightTuple was added back into the same bucket, so reset as root blocker to re-match can be attempted
rightTuple.setTempNextRightTuple( rightTuple );
}
if (resumeFromCurrent) {
RightTuple tempRightTuple = rightTuple.getTempNextRightTuple();
if ( rightTuple.getBlocked() != null && tempRightTuple == null && rightTuple.getMemory() == rightTuple.getTempRightTupleMemory() ) {
// the next RightTuple was null, but current RightTuple was added back into the same bucket, so reset as root blocker to re-match can be attempted
rightTuple.setTempNextRightTuple( rightTuple );
}
}

for (LeftTuple childLeftTuple = rightTuple.getFirstChild(); childLeftTuple != null; ) {
LeftTuple childNext = childLeftTuple.getRightParentNext();
childLeftTuple.reAddLeft();
childLeftTuple = childNext;
}
for (LeftTuple childLeftTuple = rightTuple.getFirstChild(); childLeftTuple != null; ) {
LeftTuple childNext = childLeftTuple.getRightParentNext();
childLeftTuple.reAddLeft();
childLeftTuple = childNext;
}
rightTuple = next;
}
}

Expand Down

0 comments on commit 11c04ff

Please sign in to comment.