Skip to content

Commit

Permalink
[DROOLS-7537] remove no longer needed activation count in event fact …
Browse files Browse the repository at this point in the history
…handle (#5486)

* [DROOLS-7537] remove no longer needed activation count in event fact handle

* fix imports
  • Loading branch information
mariofusco committed Aug 23, 2023
1 parent 82a3c61 commit 0722b59
Show file tree
Hide file tree
Showing 13 changed files with 61 additions and 120 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ public class DefaultEventHandle extends DefaultFactHandle implements EventHandle
private long duration;
private boolean expired;
private boolean pendingRemoveFromStore;
private long activationsCount;
private int otnCount;

private DefaultEventHandle linkedFactHandle;
Expand Down Expand Up @@ -190,38 +189,6 @@ public void setPendingRemoveFromStore(boolean pendingRemove) {
}
}

public long getActivationsCount() {
if ( linkedFactHandle != null ) {
return linkedFactHandle.getActivationsCount();
} else {
return activationsCount;
}
}

public void setActivationsCount(long activationsCount) {
if ( linkedFactHandle != null ) {
linkedFactHandle.setActivationsCount( activationsCount );
} else {
this.activationsCount = activationsCount;
}
}

public void increaseActivationsCount() {
if ( linkedFactHandle != null ) {
linkedFactHandle.increaseActivationsCount();
} else {
this.activationsCount++;
}
}

public void decreaseActivationsCount() {
if ( linkedFactHandle != null ) {
linkedFactHandle.decreaseActivationsCount();
} else {
this.activationsCount--;
}
}

public void increaseOtnCount() {
otnCount++;
}
Expand All @@ -246,7 +213,6 @@ public DefaultEventHandle clone() {
getStartTimestamp(),
getDuration(),
getEntryPointId() );
clone.setActivationsCount( getActivationsCount() );
clone.setOtnCount( getOtnCount() );
clone.setExpired( isExpired() );
clone.setEqualityKey( getEqualityKey() );
Expand All @@ -264,7 +230,6 @@ private DefaultEventHandle cloneWithoutTuples() {
getStartTimestamp(),
getDuration(),
getEntryPointId() );
clone.setActivationsCount( getActivationsCount() );
clone.setOtnCount( getOtnCount() );
clone.setExpired( isExpired() );
clone.setEqualityKey( getEqualityKey() );
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,6 @@
import org.drools.core.reteoo.PathMemory;
import org.drools.core.reteoo.RuleTerminalNodeLeftTuple;
import org.drools.core.reteoo.TerminalNode;
import org.drools.core.reteoo.Tuple;
import org.drools.core.rule.consequence.InternalMatch;
import org.drools.core.rule.consequence.KnowledgeHelper;
import org.drools.util.StringUtils;
Expand Down Expand Up @@ -189,7 +188,6 @@ public void cancelActivation(InternalMatch internalMatch) {
if (internalMatch.getActivationGroupNode() != null ) {
internalMatch.getActivationGroupNode().getActivationGroup().removeActivation(internalMatch);
}
((Tuple) internalMatch).decreaseActivationCountForEvents();

getAgendaEventSupport().fireActivationCancelled(internalMatch, reteEvaluator, MatchCancelledCause.WME_MODIFY);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@

import org.drools.core.common.DefaultEventHandle;
import org.drools.core.common.DefaultFactHandle;
import org.drools.core.common.InternalWorkingMemoryEntryPoint;
import org.drools.core.common.PropagationContext;
import org.drools.core.common.ReteEvaluator;
import org.drools.core.common.WorkingMemoryAction;
Expand Down Expand Up @@ -88,19 +87,14 @@ public void internalExecute(ReteEvaluator reteEvaluator) {
ObjectTypeNode.expireRightTuple(rt);
} );

expireFactHandle( reteEvaluator, factHandle );
expireFactHandle( factHandle );
}

private static void expireFactHandle( ReteEvaluator reteEvaluator, DefaultEventHandle factHandle) {
private static void expireFactHandle( DefaultEventHandle factHandle) {
factHandle.decreaseOtnCount();
if (factHandle.getOtnCount() <= 0) {
factHandle.setExpired( true );
if (factHandle.getActivationsCount() == 0) {
String epId = factHandle.getEntryPointName();
( (InternalWorkingMemoryEntryPoint) reteEvaluator.getEntryPoint( epId ) ).removeFromObjectStore( factHandle );
} else {
factHandle.setPendingRemoveFromStore( true );
}
factHandle.setPendingRemoveFromStore( true );
}
}

Expand Down Expand Up @@ -156,7 +150,7 @@ public void internalExecute(ReteEvaluator reteEvaluator ) {
});

if (isMainPartition()) {
expireFactHandle( reteEvaluator, factHandle );
expireFactHandle( factHandle );
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,6 @@ public static void doLeftTupleInsert(TerminalNode rtnNode, RuleExecutor executor
}

executor.addLeftTuple( leftTuple );
leftTuple.increaseActivationCountForEvents(); // increased here, decreased in Agenda's cancelActivation and fireActivation

activationsManager.addItemToActivationGroup( rtnLeftTuple );
if ( !rtnNode.isFireDirect() && executor.isDeclarativeAgendaEnabled() ) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public abstract class AbstractTuple implements Tuple {
private Tuple previous;
private AbstractTuple next;

protected Sink sink;
private Sink sink;

protected Tuple handlePrevious;
protected Tuple handleNext;
Expand Down Expand Up @@ -131,26 +131,6 @@ public FactHandle get(Declaration declaration) {
return get(declaration.getTupleIndex());
}

@Override
public void increaseActivationCountForEvents() {
for ( Tuple entry = skipEmptyHandles(); entry != null; entry = entry.getParent() ) {
if(entry.getFactHandle().isEvent()) {
// can be null for eval, not and exists that have no right input
((DefaultEventHandle)entry.getFactHandle()).increaseActivationsCount();
}
}
}

@Override
public void decreaseActivationCountForEvents() {
for ( Tuple entry = skipEmptyHandles(); entry != null; entry = entry.getParent() ) {
if(entry.getFactHandle().isEvent()) {
// can be null for eval, not and exists that have no right input
((DefaultEventHandle)entry.getFactHandle()).decreaseActivationsCount();
}
}
}

@Override
public Tuple getTuple(int index) {
Tuple entry = this;
Expand Down Expand Up @@ -200,4 +180,12 @@ public boolean isExpired() {
public void setExpired() {
this.expired = true;
}

protected Sink getSink() {
return sink;
}

protected void setSink(Sink sink) {
this.sink = sink;
}
}
38 changes: 19 additions & 19 deletions drools-core/src/main/java/org/drools/core/reteoo/LeftTuple.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,16 @@

package org.drools.core.reteoo;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;

import org.drools.core.common.InternalFactHandle;
import org.drools.core.common.PropagationContext;
import org.drools.core.util.index.TupleList;
import org.kie.api.runtime.rule.FactHandle;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;

/**
* A parent class for all specific LeftTuple specializations
*
Expand Down Expand Up @@ -67,7 +67,7 @@ public LeftTuple(InternalFactHandle factHandle,
Sink sink,
boolean leftTupleMemoryEnabled) {
setFactHandle( factHandle );
this.sink = sink;
setSink(sink);
if ( leftTupleMemoryEnabled ) {
factHandle.addTupleInPosition( this );
}
Expand All @@ -80,7 +80,7 @@ public LeftTuple(InternalFactHandle factHandle,
this.index = leftTuple.getIndex() + 1;
this.parent = leftTuple.getNextParentWithHandle();
this.leftParent = leftTuple;
this.sink = sink;
setSink(sink);
}

public LeftTuple(LeftTuple leftTuple,
Expand All @@ -102,7 +102,7 @@ public LeftTuple(LeftTuple leftTuple,
leftTuple.setLastChild( this );
}

this.sink = sink;
setSink(sink);
}

public LeftTuple(LeftTuple leftTuple,
Expand Down Expand Up @@ -133,7 +133,7 @@ public LeftTuple(LeftTuple leftTuple,
rightTuple.setFirstChild( this );
}
rightTuple.setLastChild( this );
this.sink = sink;
setSink(sink);
}

public LeftTuple(LeftTuple leftTuple,
Expand Down Expand Up @@ -205,7 +205,7 @@ public LeftTuple(LeftTuple leftTuple,
}
}

this.sink = sink;
setSink(sink);
}

public LeftTuple getNextParentWithHandle() {
Expand Down Expand Up @@ -344,14 +344,14 @@ public int getIndex() {

@Override
public LeftTupleSink getTupleSink() {
return (LeftTupleSink)sink;
return (LeftTupleSink)getSink();
}

/* Had to add the set method because sink adapters must override
* the tuple sink set when the tuple was created.
*/
public void setLeftTupleSink( LeftTupleSink sink ) {
this.sink = sink;
setSink(sink);
}

public LeftTuple getLeftParent() {
Expand Down Expand Up @@ -407,7 +407,7 @@ public FactHandle get(int index) {

public FactHandle[] toFactHandles() {
// always use the count of the node that created join (not the sink target)
FactHandle[] handles = new FactHandle[((LeftTupleSinkNode)sink).getLeftTupleSource().getObjectCount()];
FactHandle[] handles = new FactHandle[((LeftTupleSinkNode)getSink()).getLeftTupleSource().getObjectCount()];
LeftTuple entry = (LeftTuple) skipEmptyHandles();
for(int i = handles.length-1; i >= 0; i--) {
handles[i] = entry.getFactHandle();
Expand All @@ -418,7 +418,7 @@ public FactHandle[] toFactHandles() {

public Object[] toObjects(boolean reverse) {
// always use the count of the node that created join (not the sink target)
Object[] objs = new Object[((LeftTupleSinkNode)sink).getLeftTupleSource().getObjectCount()];
Object[] objs = new Object[((LeftTupleSinkNode)getSink()).getLeftTupleSource().getObjectCount()];
LeftTuple entry = (LeftTuple) skipEmptyHandles();

if (!reverse) {
Expand Down Expand Up @@ -601,8 +601,8 @@ protected String toExternalString() {
}
builder.append( Arrays.toString( ids ) )
.append( " sink=" )
.append( this.sink.getClass().getSimpleName() )
.append( "(" ).append( sink.getId() ).append( ")" );
.append( this.getSink().getClass().getSimpleName() )
.append( "(" ).append( getSink().getId() ).append( ")" );
return builder.toString();
}

Expand All @@ -619,7 +619,7 @@ public void initPeer(LeftTuple original, LeftTupleSink sink) {

setFactHandle( original.getFactHandle() );
setPropagationContext( original.getPropagationContext() );
this.sink = sink;
setSink(sink);
}

@Override
Expand All @@ -629,12 +629,12 @@ public Object getObject(int index) {

@Override
public ObjectTypeNode.Id getInputOtnId() {
return sink != null ? getTupleSink().getLeftInputOtnId() : null;
return getSink() != null ? getTupleSink().getLeftInputOtnId() : null;
}

@Override
public LeftTupleSource getTupleSource() {
return sink != null ? getTupleSink().getLeftTupleSource() : null;
return getSink() != null ? getTupleSink().getLeftTupleSource() : null;
}

public short getStagedTypeForQueries() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,14 +46,14 @@ public RightTupleImpl(InternalFactHandle handle) {
public RightTupleImpl(InternalFactHandle handle,
RightTupleSink sink) {
this( handle );
this.sink = sink;
setSink(sink);

// add to end of RightTuples on handle
handle.addLastRightTuple( this );
}

public RightTupleSink getTupleSink() {
return (RightTupleSink) sink;
return (RightTupleSink) getSink();
}

public void reAdd() {
Expand All @@ -71,7 +71,7 @@ public void unlinkFromRightParent() {
this.memory = null;
this.firstChild = null;
this.lastChild = null;
this.sink = null;
setSink(null);
}

public void unlinkFromLeftParent() {
Expand Down Expand Up @@ -249,7 +249,7 @@ public Tuple getSubTuple( int elements ) {

@Override
public ObjectTypeNode.Id getInputOtnId() {
return sink != null ? getTupleSink().getRightInputOtnId() : null;
return getSink() != null ? getTupleSink().getRightInputOtnId() : null;
}

@Override
Expand Down
Loading

0 comments on commit 0722b59

Please sign in to comment.