Skip to content

Commit

Permalink
Revert "[DROOLS-7527] Alyways use AbstractTuple instead of Tuple (Fix…
Browse files Browse the repository at this point in the history
… the secondary super cache problem in Drools) (apache#5447)"

This reverts commit f83ff41
  • Loading branch information
lucamolteni committed Sep 13, 2023
1 parent 2d38873 commit 21371f2
Show file tree
Hide file tree
Showing 6 changed files with 24 additions and 70 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public abstract class AbstractTuple implements Tuple {
protected Tuple stagedPrevious;

private Tuple previous;
private AbstractTuple next;
private Tuple next;

private Sink sink;

Expand Down Expand Up @@ -112,11 +112,11 @@ public void setPrevious(Tuple previous) {
this.previous = previous;
}

public AbstractTuple getNext() {
public Tuple getNext() {
return next;
}

public void setNext(AbstractTuple next) {
public void setNext(Tuple next) {
this.next = next;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -225,7 +225,7 @@ public void addToCreatedHandlesMap(final Map<Object, RightTuple> matches,
// this is for the obscene case where two or more objects returned by "from"
// have the same hash code and evaluate equals() to true, so we need to preserve
// all of them to avoid leaks
rightTuple.setNext((AbstractTuple) existingMatch);
rightTuple.setNext( existingMatch );
}
matches.put( object,
rightTuple );
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
* Is able to return the <code>FactHandleImpl</code> members of the partial match for the requested pattern.
* The pattern refers to the index position of the <code>FactHandleImpl</code> in the underlying implementation.
*/
public interface Tuple extends BaseTuple, Serializable, Entry<AbstractTuple> {
public interface Tuple extends BaseTuple, Serializable, Entry<Tuple> {

short NONE = 0;
short INSERT = 1;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
import java.io.Serializable;

import org.drools.core.common.InternalFactHandle;
import org.drools.core.reteoo.AbstractTuple;
import org.drools.core.reteoo.TupleMemory;
import org.drools.core.reteoo.Tuple;
import org.drools.core.util.Entry;
Expand Down Expand Up @@ -86,8 +85,8 @@ public void removeAdd(Tuple tuple) {
return;
}

AbstractTuple previous = (AbstractTuple) tuple.getPrevious();
AbstractTuple next = tuple.getNext();
Tuple previous = tuple.getPrevious();
Tuple next = tuple.getNext();
if (previous == null) {
next.setPrevious( null );
this.first = next;
Expand All @@ -96,15 +95,15 @@ public void removeAdd(Tuple tuple) {
next.setPrevious( previous );
}

this.last.setNext((AbstractTuple) tuple);
this.last.setNext( tuple );
tuple.setPrevious( this.last );
tuple.setNext( null );
this.last = tuple;
}

public void add(final Tuple tuple) {
if ( this.last != null ) {
this.last.setNext( (AbstractTuple) tuple );
this.last.setNext( tuple );
tuple.setPrevious( this.last );
this.last = tuple;
} else {
Expand All @@ -118,7 +117,7 @@ public void add(final Tuple tuple) {

public void remove(final Tuple tuple) {
Tuple previous = tuple.getPrevious();
AbstractTuple next = tuple.getNext();
Tuple next = tuple.getNext();

if ( previous != null && next != null ) {
// remove from middle
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,18 @@

package org.drools.model.codegen.execmodel;

import java.util.AbstractMap;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.Set;

import org.drools.model.codegen.execmodel.FunctionsTest.Pojo;
import org.drools.model.codegen.execmodel.domain.Address;
import org.drools.model.codegen.execmodel.domain.Adult;
Expand Down Expand Up @@ -1538,61 +1550,4 @@ public void setStrValue(String strValue) {
}

}

@Test
public void testFromGlobalWithDuplicates() {
String str =
"import java.util.concurrent.atomic.AtomicInteger;\n" +
"import " + NamedPerson.class.getCanonicalName() + ";\n" +
"global java.util.List list \n" +
"rule R when \n" +
" $i : AtomicInteger()\n" +
" $o : NamedPerson(age > $i.get()) from list\n" +
"then \n" +
" insert($o); \n" +
"end ";

KieSession ksession = getKieSession(str);

List<NamedPerson> strings = Arrays.asList(new NamedPerson("Mario", 1), new NamedPerson("Mario", 2));

ksession.setGlobal("list", strings);

AtomicInteger i = new AtomicInteger(0);
FactHandle fh = ksession.insert(i);

assertThat(ksession.fireAllRules()).isEqualTo(2);

i.incrementAndGet();
ksession.update(fh, i);

assertThat(ksession.fireAllRules()).isEqualTo(1);
}

public static class NamedPerson {
private final String name;
private final int age;

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

@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
NamedPerson myPerson = (NamedPerson) o;
return Objects.equals(name, myPerson.name);
}

@Override
public int hashCode() {
return Objects.hash(name);
}

public int getAge() {
return age;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ public Object accumulate(Object workingMemoryContext, Object context,
GroupByContext groupByContext = ( GroupByContext ) context;
LeftTuple leftTupleMatch = (LeftTuple) match;
TupleList<AccumulateContextEntry> tupleList = groupByContext.getGroup(workingMemoryContext, innerAccumulate,
leftTupleMatch, getKey(leftTupleMatch, handle, (ReteEvaluator) valueResolver), (ReteEvaluator) valueResolver);
(Tuple) match, getKey( (Tuple) match, handle, (ReteEvaluator) valueResolver), (ReteEvaluator) valueResolver);

return accumulate(workingMemoryContext, match, handle, groupByContext, tupleList, valueResolver);
}
Expand All @@ -126,7 +126,7 @@ public Object accumulate(Object workingMemoryContext, BaseTuple match, FactHandl
@Override
public boolean tryReverse(Object workingMemoryContext, Object context, BaseTuple leftTuple, FactHandle handle,
BaseTuple match, ValueResolver valueResolver) {
LeftTuple tupleMatch = (LeftTuple) match;
Tuple tupleMatch = (Tuple) match;
TupleList<AccumulateContextEntry> memory = tupleMatch.getMemory();
AccumulateContextEntry entry = memory.getContext();
boolean reversed = innerAccumulate.tryReverse(workingMemoryContext, entry, leftTuple, handle, match, valueResolver);
Expand Down

0 comments on commit 21371f2

Please sign in to comment.