Skip to content

Commit

Permalink
minor semplifications
Browse files Browse the repository at this point in the history
  • Loading branch information
mariofusco committed Jan 16, 2024
1 parent ed9a799 commit 5ab84bd
Show file tree
Hide file tree
Showing 44 changed files with 416 additions and 576 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,8 @@
import java.util.List;

import org.drools.base.definitions.rule.impl.RuleImpl;
import org.drools.core.common.BaseNode;
import org.drools.core.common.ReteEvaluator;
import org.drools.core.common.SuperCacheFixer;
import org.drools.core.reteoo.LeftTuple;
import org.drools.core.reteoo.QueryTerminalNode;
import org.drools.core.reteoo.TupleImpl;
import org.kie.api.runtime.rule.FactHandle;
Expand All @@ -46,7 +44,7 @@ public List<? extends Object> getResults() {

public void rowAdded(RuleImpl rule, TupleImpl tuple, ReteEvaluator reteEvaluator) {
FactHandle[] handles = new FactHandle[SuperCacheFixer.getLeftTupleNode(tuple).getObjectCount()];
TupleImpl entry = (LeftTuple) tuple.skipEmptyHandles();
TupleImpl entry = tuple.skipEmptyHandles();

// Add all the FactHandles
int i = handles.length-1;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@
import org.drools.base.rule.constraint.BetaConstraint;
import org.drools.core.RuleBaseConfiguration;
import org.drools.core.reteoo.BetaMemory;
import org.drools.core.reteoo.BetaMemoryImpl;
import org.drools.core.reteoo.Tuple;
import org.drools.core.reteoo.builder.BuildContext;
import org.drools.core.util.index.TupleList;
Expand Down Expand Up @@ -117,10 +116,10 @@ public boolean isEmpty() {

public BetaMemory createBetaMemory(final RuleBaseConfiguration config,
final int nodeType) {
return new BetaMemoryImpl(config.isSequential() ? null : new TupleList(),
new TupleList(),
EMPTY,
nodeType );
return new BetaMemory(config.isSequential() ? null : new TupleList(),
new TupleList(),
EMPTY,
nodeType );
}

public int hashCode() {
Expand Down
147 changes: 23 additions & 124 deletions drools-core/src/main/java/org/drools/core/common/SuperCacheFixer.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
import org.drools.base.common.NetworkNode;
import org.drools.base.reteoo.NodeTypeEnums;
import org.drools.core.reteoo.AccumulateNode;
import org.drools.core.reteoo.AlphaNode;
import org.drools.core.reteoo.AlphaTerminalNode;
import org.drools.core.reteoo.AsyncReceiveNode;
import org.drools.core.reteoo.AsyncSendNode;
Expand All @@ -35,8 +34,6 @@
import org.drools.core.reteoo.LeftTupleSinkNode;
import org.drools.core.reteoo.LeftTupleSource;
import org.drools.core.reteoo.NotNode;
import org.drools.core.reteoo.ObjectSource;
import org.drools.core.reteoo.ObjectTypeNode;
import org.drools.core.reteoo.ObjectTypeNodeId;
import org.drools.core.reteoo.QueryElementNode;
import org.drools.core.reteoo.QueryTerminalNode;
Expand All @@ -54,9 +51,10 @@
// It's better to always cast to a concrete or abstract class to avoid
// secondary super cache problem. See https://issues.redhat.com/browse/DROOLS-7521
public class SuperCacheFixer {

public static LeftTupleNode getLeftTupleNode(TupleImpl t) {
Sink s = t.getSink();
switch (((BaseNode) s).getType()) {
switch (s.getType()) {
case NodeTypeEnums.RuleTerminalNode : return (RuleTerminalNode) s;
case NodeTypeEnums.QueryTerminalNode: return (QueryTerminalNode) s;
case NodeTypeEnums.RightInputAdapterNode: return (RightInputAdapterNode) s;
Expand All @@ -82,7 +80,7 @@ public static LeftTupleNode getLeftTupleNode(TupleImpl t) {

public static RightTupleSink getRightTupleSink(RightTuple t) {
Sink s = t.getSink();
switch (((BaseNode) s).getType()) {
switch (s.getType()) {
case NodeTypeEnums.AccumulateNode : return (AccumulateNode) s;
case NodeTypeEnums.ExistsNode: return (ExistsNode) s;
case NodeTypeEnums.NotNode: return (NotNode) s;
Expand All @@ -96,7 +94,7 @@ public static RightTupleSink getRightTupleSink(RightTuple t) {
}

public static LeftTupleSinkNode asLeftTupleSink(NetworkNode n) {
switch (((BaseNode) n).getType()) {
switch (n.getType()) {
case NodeTypeEnums.RuleTerminalNode : return (RuleTerminalNode) n;
case NodeTypeEnums.QueryTerminalNode: return (QueryTerminalNode) n;
case NodeTypeEnums.RightInputAdapterNode: return (RightInputAdapterNode) n;
Expand All @@ -118,56 +116,9 @@ public static LeftTupleSinkNode asLeftTupleSink(NetworkNode n) {
}
}

public static NetworkNode checkcast(NetworkNode n) {
switch (((BaseNode)n).getType()) {
case NodeTypeEnums.ObjectTypeNode : return (ObjectTypeNode) n;
case NodeTypeEnums.AlphaNode : return (AlphaNode) n;
case NodeTypeEnums.LeftInputAdapterNode: return (LeftInputAdapterNode) n;
case NodeTypeEnums.AlphaTerminalNode: return (AlphaTerminalNode) n;
case NodeTypeEnums.RuleTerminalNode : return (RuleTerminalNode) n;
case NodeTypeEnums.QueryTerminalNode: return (QueryTerminalNode) n;
case NodeTypeEnums.RightInputAdapterNode: return (RightInputAdapterNode) n;
case NodeTypeEnums.AccumulateNode : return (AccumulateNode) n;
case NodeTypeEnums.ExistsNode: return (ExistsNode) n;
case NodeTypeEnums.NotNode: return (NotNode) n;
case NodeTypeEnums.FromNode: return (FromNode) n;
case NodeTypeEnums.JoinNode: return (JoinNode) n;
case NodeTypeEnums.EvalConditionNode: return (EvalConditionNode) n;
case NodeTypeEnums.AsyncReceiveNode: return (AsyncReceiveNode) n;
case NodeTypeEnums.AsyncSendNode: return (AsyncSendNode) n;
case NodeTypeEnums.ReactiveFromNode: return (ReactiveFromNode) n;
case NodeTypeEnums.ConditionalBranchNode: return (ConditionalBranchNode) n;
case NodeTypeEnums.QueryElementNode: return (QueryElementNode) n;
case NodeTypeEnums.WindowNode: return (WindowNode) n;
case NodeTypeEnums.TimerConditionNode: return (TimerNode) n;
case NodeTypeEnums.MockBetaNode: return (NetworkNode) n;
case NodeTypeEnums.MockAlphaNode: return (NetworkNode) n;
default:
throw new UnsupportedOperationException("Switch needs to include: " + n);
}
}

public static NetworkNode getSink(TupleImpl t) {
return checkcast(t.getSink());
}

public static ObjectTypeNode getObjectTypeNode(TupleImpl t) {
Sink s = t.getSink();
switch (((BaseNode) s).getType()) {
case NodeTypeEnums.RuleTerminalNode : return ((RuleTerminalNode) s).getObjectTypeNode();
case NodeTypeEnums.QueryTerminalNode: return ((QueryTerminalNode) s).getObjectTypeNode();
case NodeTypeEnums.AccumulateNode : return ((AccumulateNode) s).getObjectTypeNode();
case NodeTypeEnums.ExistsNode: return ((ExistsNode) s).getObjectTypeNode();
case NodeTypeEnums.NotNode: return ((NotNode) s).getObjectTypeNode();
case NodeTypeEnums.JoinNode: return ((JoinNode) s).getObjectTypeNode();
default:
throw new UnsupportedOperationException("Node does not have an ObjectType: " + s);
}
}

public static ObjectTypeNodeId getLeftInputOtnId(TupleImpl t) {
Sink s = t.getSink();
switch (((BaseNode) s).getType()) {
switch (s.getType()) {
case NodeTypeEnums.RuleTerminalNode : return ((RuleTerminalNode) s).getLeftInputOtnId();
case NodeTypeEnums.QueryTerminalNode: return ((QueryTerminalNode) s).getLeftInputOtnId();
case NodeTypeEnums.RightInputAdapterNode: return ((RightInputAdapterNode) s).getLeftInputOtnId();
Expand All @@ -191,7 +142,7 @@ public static ObjectTypeNodeId getLeftInputOtnId(TupleImpl t) {

public static ObjectTypeNodeId getRightInputOtnId(TupleImpl t) {
Sink s = t.getSink();
switch (((BaseNode) s).getType()) {
switch (s.getType()) {
case NodeTypeEnums.AccumulateNode : return ((AccumulateNode) s).getRightInputOtnId();
case NodeTypeEnums.ExistsNode: return ((ExistsNode) s).getRightInputOtnId();
case NodeTypeEnums.NotNode: return ((NotNode) s).getRightInputOtnId();
Expand All @@ -204,85 +155,33 @@ public static ObjectTypeNodeId getRightInputOtnId(TupleImpl t) {

public static LeftTupleSource getLeftTupleSource(TupleImpl t) {
Sink s = t.getSink();
LeftTupleSource n;
switch (((BaseNode) s).getType()) {
case NodeTypeEnums.RuleTerminalNode : n = ((RuleTerminalNode) s).getLeftTupleSource(); break;
case NodeTypeEnums.QueryTerminalNode: n = ((QueryTerminalNode) s).getLeftTupleSource(); break;
case NodeTypeEnums.RightInputAdapterNode: n = ((RightInputAdapterNode) s).getLeftTupleSource(); break;
case NodeTypeEnums.AccumulateNode : n = ((AccumulateNode) s).getLeftTupleSource(); break;
case NodeTypeEnums.ExistsNode: n = ((ExistsNode) s).getLeftTupleSource(); break;
case NodeTypeEnums.NotNode: n = ((NotNode) s).getLeftTupleSource(); break;
case NodeTypeEnums.JoinNode: n = ((JoinNode) s).getLeftTupleSource(); break;
case NodeTypeEnums.FromNode: n = ((FromNode) s).getLeftTupleSource(); break;
case NodeTypeEnums.EvalConditionNode: n = ((EvalConditionNode) s).getLeftTupleSource(); break;
case NodeTypeEnums.AsyncReceiveNode: n = ((AsyncReceiveNode) s).getLeftTupleSource(); break;
case NodeTypeEnums.AsyncSendNode: n = ((AsyncSendNode) s).getLeftTupleSource(); break;
case NodeTypeEnums.ReactiveFromNode: n = ((ReactiveFromNode) s).getLeftTupleSource(); break;
case NodeTypeEnums.ConditionalBranchNode: n = ((ConditionalBranchNode) s).getLeftTupleSource(); break;
case NodeTypeEnums.QueryElementNode: n = ((QueryElementNode) s).getLeftTupleSource(); break;
case NodeTypeEnums.TimerConditionNode: n = ((TimerNode) s).getLeftTupleSource(); break;
switch (s.getType()) {
case NodeTypeEnums.RuleTerminalNode : return ((RuleTerminalNode) s).getLeftTupleSource();
case NodeTypeEnums.QueryTerminalNode: return ((QueryTerminalNode) s).getLeftTupleSource();
case NodeTypeEnums.RightInputAdapterNode: return ((RightInputAdapterNode) s).getLeftTupleSource();
case NodeTypeEnums.AccumulateNode : return ((AccumulateNode) s).getLeftTupleSource();
case NodeTypeEnums.ExistsNode: return ((ExistsNode) s).getLeftTupleSource();
case NodeTypeEnums.NotNode: return ((NotNode) s).getLeftTupleSource();
case NodeTypeEnums.JoinNode: return ((JoinNode) s).getLeftTupleSource();
case NodeTypeEnums.FromNode: return ((FromNode) s).getLeftTupleSource();
case NodeTypeEnums.EvalConditionNode: return ((EvalConditionNode) s).getLeftTupleSource();
case NodeTypeEnums.AsyncReceiveNode: return ((AsyncReceiveNode) s).getLeftTupleSource();
case NodeTypeEnums.AsyncSendNode: return ((AsyncSendNode) s).getLeftTupleSource();
case NodeTypeEnums.ReactiveFromNode: return ((ReactiveFromNode) s).getLeftTupleSource();
case NodeTypeEnums.ConditionalBranchNode: return ((ConditionalBranchNode) s).getLeftTupleSource();
case NodeTypeEnums.QueryElementNode: return ((QueryElementNode) s).getLeftTupleSource();
case NodeTypeEnums.TimerConditionNode: return ((TimerNode) s).getLeftTupleSource();
default:
throw new UnsupportedOperationException("Does not have a LeftTupleSource: " + s);
}

switch (n.getType()) {
case NodeTypeEnums.LeftInputAdapterNode: return (LeftInputAdapterNode) n;
case NodeTypeEnums.AlphaTerminalNode: return (AlphaTerminalNode) n;
case NodeTypeEnums.AccumulateNode : return (AccumulateNode) n;
case NodeTypeEnums.ExistsNode: return (ExistsNode) n;
case NodeTypeEnums.NotNode: return (NotNode) n;
case NodeTypeEnums.FromNode: return (FromNode) n;
case NodeTypeEnums.JoinNode: return (JoinNode) n;
case NodeTypeEnums.EvalConditionNode: return (EvalConditionNode) n;
case NodeTypeEnums.AsyncReceiveNode: return (AsyncReceiveNode) n;
case NodeTypeEnums.AsyncSendNode: return (AsyncSendNode) n;
case NodeTypeEnums.ReactiveFromNode: return (ReactiveFromNode) n;
case NodeTypeEnums.ConditionalBranchNode: return (ConditionalBranchNode) n;
case NodeTypeEnums.QueryElementNode: return (QueryElementNode) n;
case NodeTypeEnums.TimerConditionNode: return (TimerNode) n;
default:
throw new UnsupportedOperationException("Switch needs to include: " + n);
}
}

public static ObjectSource getObjectSource(TupleImpl t) {
Sink s = t.getSink();
ObjectSource o;
switch (((BaseNode) s).getType()) {
case NodeTypeEnums.AccumulateNode : o = ((AccumulateNode) s).getRightInput(); break;
case NodeTypeEnums.ExistsNode: o = ((ExistsNode) s).getRightInput(); break;
case NodeTypeEnums.NotNode: o = ((NotNode) s).getRightInput(); break;
case NodeTypeEnums.JoinNode: o = ((JoinNode) s).getRightInput(); break;
default:
throw new UnsupportedOperationException("Node does not have an ObjectSource: " + s);
}

switch (o.getType()) {
case NodeTypeEnums.AlphaNode : return (AlphaNode) o;
case NodeTypeEnums.WindowNode: return (WindowNode) o;
case NodeTypeEnums.ObjectTypeNode : return (ObjectTypeNode) o;
case NodeTypeEnums.RightInputAdapterNode : return (RightInputAdapterNode) o;
default:
throw new UnsupportedOperationException("Node does match an ObjectSource: " + o);
}
}

public static TerminalNode asTerminalNode(TupleImpl t) {
Sink s = t.getSink();
switch (((BaseNode) t.getSink()).getType()) {
switch (t.getSink().getType()) {
case NodeTypeEnums.RuleTerminalNode : return (RuleTerminalNode) s;
case NodeTypeEnums.QueryTerminalNode: return (QueryTerminalNode) s;
default: return null;
}
}

public static TerminalNode asTerminalNode(NetworkNode n) {
switch (((BaseNode)n).getType()) {
case NodeTypeEnums.RuleTerminalNode : return (RuleTerminalNode) n;
case NodeTypeEnums.QueryTerminalNode: return (QueryTerminalNode) n;
default: return null;
}


}
}
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,6 @@
import org.drools.core.reteoo.AccumulateNode.AccumulateMemory;
import org.drools.core.reteoo.AlphaTerminalNode;
import org.drools.core.reteoo.BetaMemory;
import org.drools.core.reteoo.BetaMemoryImpl;
import org.drools.core.reteoo.BetaNode;
import org.drools.core.reteoo.FromNode.FromMemory;
import org.drools.core.reteoo.LeftInputAdapterNode;
Expand Down Expand Up @@ -956,11 +955,11 @@ private static void mergeEagerProtos(SegmentPrototype proto1, SegmentPrototype p

private static void deleteRightInputData(LeftTupleNode node, Memory m, InternalWorkingMemory wm) {
BetaNode bn = (BetaNode) node;
BetaMemoryImpl bm;
BetaMemory bm;
if (bn.getType() == NodeTypeEnums.AccumulateNode) {
bm = ((AccumulateMemory) m).getBetaMemory();
} else {
bm = (BetaMemoryImpl) m;
bm = (BetaMemory) m;
}

TupleMemory rtm = bm.getRightTupleMemory();
Expand Down Expand Up @@ -1159,25 +1158,25 @@ private static void visitChild(InternalWorkingMemory wm, boolean insert, Termina
private static void visitChild(TupleImpl lt, boolean insert, InternalWorkingMemory wm, TerminalNode tn) {
TupleImpl prevLt = null;

LeftTupleSinkNode sink = (LeftTupleSinkNode) SuperCacheFixer.getSink(lt);
LeftTupleSinkNode sink = (LeftTupleSinkNode) lt.getSink();

for ( ; sink != null; sink = sink.getNextLeftTupleSinkNode() ) {
if ( lt != null ) {
if (isAssociatedWith(SuperCacheFixer.getSink(lt), tn)) {
if (isAssociatedWith(lt.getSink(), tn)) {

if (SuperCacheFixer.getSink(lt).getAssociatedTerminalsSize() > 1) {
if (lt.getSink().getAssociatedTerminalsSize() > 1) {
if (lt.getFirstChild() != null) {
for ( TupleImpl child = lt.getFirstChild(); child != null; child = child.getHandleNext() ) {
visitChild(child, insert, wm, tn);
}
} else if (SuperCacheFixer.getSink(lt).getType() == NodeTypeEnums.RightInputAdapterNode) {
} else if (lt.getSink().getType() == NodeTypeEnums.RightInputAdapterNode) {
insertPeerRightTuple(lt, wm, tn, insert);
}
} else if (!insert) {
iterateLeftTuple( lt, wm );
TupleImpl lt2 = null;
for ( TupleImpl peerLt = lt.getPeer();
peerLt != null && isAssociatedWith(SuperCacheFixer.getSink(peerLt), tn) && peerLt.getSink().getAssociatedTerminalsSize() == 1;
peerLt != null && isAssociatedWith(peerLt.getSink(), tn) && peerLt.getSink().getAssociatedTerminalsSize() == 1;
peerLt = peerLt.getPeer() ) {
iterateLeftTuple( peerLt, wm );
lt2 = peerLt;
Expand All @@ -1201,7 +1200,7 @@ private static void visitChild(TupleImpl lt, boolean insert, InternalWorkingMemo
private static void insertPeerRightTuple(TupleImpl lt, InternalWorkingMemory wm, TerminalNode tn, boolean insert ) {
// There's a shared RightInputAdapterNode, so check if one of its sinks is associated only to the new rule
TupleImpl prevLt = null;
RightInputAdapterNode rian = (RightInputAdapterNode) SuperCacheFixer.getSink(lt);
RightInputAdapterNode rian = (RightInputAdapterNode) lt.getSink();

for (ObjectSink sink : rian.getObjectSinkPropagator().getSinks()) {
if (lt != null) {
Expand All @@ -1211,7 +1210,7 @@ private static void insertPeerRightTuple(TupleImpl lt, InternalWorkingMemory wm,
prevLt = lt;
lt = lt.getPeer();
} else if (insert) {
BetaMemoryImpl bm = (BetaMemoryImpl) wm.getNodeMemories().peekNodeMemory(sink);
BetaMemory bm = (BetaMemory) wm.getNodeMemories().peekNodeMemory(sink);
if (bm != null) {
prevLt = TupleFactory.createPeer(rian, prevLt);
bm.linkNode((BetaNode) sink, wm);
Expand Down
Loading

0 comments on commit 5ab84bd

Please sign in to comment.