Skip to content

Commit

Permalink
Refactor Left and Right Tuples aligned with Super Cache improvements a…
Browse files Browse the repository at this point in the history
…pache#5648 (apache#5649)

* -BetaConstraints no longer depends on ReteEvaluator.

* -Trying to separate BetaMemory so BetaConstraints can go in -base.

* -Refactoring Tuples around concrete classes.

* -Centralise and improve super cache handling fix.
-Improve NodeTypeEnums to contain more information (at no cost) and replace as many instanceof as possible.

* -Move Super Cache helper methods into standalone class SuperCacheFixer.

* -Remove RightTuple interface.

* -Refactory creation to a Factory with switch for different implementations.

* -Added missing headers.
-Remove unecessary if statement with printout.
-Removed incorrectly added maven module.

* minor semplifications

---------

Co-authored-by: mariofusco <mario.fusco@gmail.com>
  • Loading branch information
2 people authored and rgdoliveira committed Jan 19, 2024
1 parent 4487801 commit d94c2cc
Show file tree
Hide file tree
Showing 267 changed files with 4,612 additions and 5,456 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public class DelegateMethodsHandler extends AbstractCompilerHandler {
private static final String FIXED_PART = "" +

"\n" +
" public short getType() {\n" +
" public int getType() {\n" +
" return objectTypeNode.getType();\n" +
" }\n" +
"\n" +
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,7 @@ private void traverseSink(ObjectSink sink, NetworkHandler handler) {

handler.startBetaNode(betaNode);
handler.endBetaNode(betaNode);
} else if (sink.getType() == NodeTypeEnums.LeftInputAdapterNode) {
} else if (NodeTypeEnums.isLeftInputAdapterNode(sink)) {
LeftInputAdapterNode leftInputAdapterNode = (LeftInputAdapterNode) sink;

handler.startLeftInputAdapterNode(leftInputAdapterNode);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public interface NetworkNode extends Serializable {
*/
RuleBasePartitionId getPartitionId();

short getType();
int getType();

Rule[] getAssociatedRules();

Expand Down
132 changes: 76 additions & 56 deletions drools-base/src/main/java/org/drools/base/reteoo/NodeTypeEnums.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,93 +21,113 @@
import org.drools.base.common.NetworkNode;

/**
*
* ObjectSource : < NodeTypeEnums.ObjectSource *
* LeftTupleSource: > LeftTupleSource
* BetaNode : > BetaNode
*
* ObjectSink : % 2 == 0
* LeftSource : % 2 != 0
*
*/
public class NodeTypeEnums {
// ObjectSource, ObjectSink
public static final short EntryPointNode = 10;
public static final short ReteNode = 20;
public static final short ObjectTypeNode = 30;
public static final short AlphaNode = 40;
public static final int ObjectSourceMask = 1;
public static final int TupleSourceMask = 1 << 2;
public static final int ObjectSinkMask = 1 << 3;
public static final int TupleSinkMask = 1 << 4;
public static final int TupleNodeMask = 1 << 5;
public static final int LeftInputAdapterMask = 1 << 6;
public static final int TerminalNodeMask = 1 << 7;
public static final int EndNodeMask = 1 << 8;
public static final int BetaMask = 1 << 9;

public static final int MemoryFactoryMask = 1 << 10;

public static final int shift = 15; // This must shift the node IDs, enough so their bits are not mutated by the masks.

public static final short WindowNode = 60;
// ObjectSource, ObjectSink
public static final int EntryPointNode = (100 << shift) | ObjectSourceMask | ObjectSinkMask;
public static final int ReteNode = (120 << shift) | ObjectSourceMask | ObjectSinkMask;
public static final int ObjectTypeNode = (130 << shift) | ObjectSourceMask | ObjectSinkMask;
public static final int AlphaNode = (140 << shift) | ObjectSourceMask | ObjectSinkMask;
public static final int WindowNode = (150 << shift) | ObjectSourceMask | ObjectSinkMask | MemoryFactoryMask;

// ObjectSource, LeftTupleSink
public static final short RightInputAdapterNode = 71; // also ObjectSource %2 != 0

public static final short ObjectSource = 80;

// LefTTupleSink
public static final short QueryTerminalNode = 91;
public static final short RuleTerminalNode = 101;

// LeftTupleSource, LefTTupleSink
public static final short LeftTupleSource = 111;
public static final short LeftInputAdapterNode = 120; // also ObjectSink %2 == 0
public static final short EvalConditionNode = 131;
public static final short TimerConditionNode = 133;
public static final short AsyncSendNode = 135;
public static final short AsyncReceiveNode = 137;
public static final short QueryRiaFixerNode = 141;
public static final short FromNode = 151;
public static final short ReactiveFromNode = 153;
public static final short UnificationNode = 165; // these two need to be merged
public static final short QueryElementNode = 165;
public static final short ConditionalBranchNode = 167;

// LeftTupleSource, LefTTupleSink, BetaNode
public static final short BetaNode = 171;
public static final short JoinNode = 181;
public static final short NotNode = 191;
public static final short ExistsNode = 201;
public static final short AccumulateNode = 211;
public static final short ForallNotNode = 221;
public static final short ElseNode = 231;
//public static final short CollectNode = 5; // no longer used, since accumulate nodes execute collect logic now

// mdp not sure what number this should be yet
public static final short OperatorNode = 19;
public static final int RightInputAdapterNode = (160 << shift) | ObjectSourceMask | TupleSinkMask |
TupleNodeMask | EndNodeMask | MemoryFactoryMask;
// LefTTupleSink, LeftTupleNode
public static final int RuleTerminalNode = (180 << shift) | TupleSinkMask | TerminalNodeMask |
TupleNodeMask | EndNodeMask | MemoryFactoryMask;
public static final int QueryTerminalNode = (190 << shift) | TupleSinkMask | TerminalNodeMask |
TupleNodeMask | EndNodeMask | MemoryFactoryMask;

// LeftTupleSource, LeftTupleNode
public static final int LeftInputAdapterNode = (210 << shift) | ObjectSinkMask | TupleSourceMask |
TupleNodeMask | LeftInputAdapterMask | MemoryFactoryMask;
public static final int AlphaTerminalNode = (220 << shift) | ObjectSinkMask | TupleSourceMask | TupleNodeMask | LeftInputAdapterMask | MemoryFactoryMask;

// LeftTupleSource, LefTTupleSink, LeftTupleNode
public static final int EvalConditionNode = (230 << shift) | TupleSourceMask | TupleSinkMask | TupleNodeMask | MemoryFactoryMask;
public static final int TimerConditionNode = (240 << shift) | TupleSourceMask | TupleSinkMask | TupleNodeMask | MemoryFactoryMask;
public static final int AsyncSendNode = (250 << shift) | TupleSourceMask | TupleSinkMask | TupleNodeMask | MemoryFactoryMask;
public static final int AsyncReceiveNode = (260 << shift) | TupleSourceMask | TupleSinkMask | TupleNodeMask | MemoryFactoryMask;
public static final int FromNode = (270 << shift) | TupleSourceMask | TupleSinkMask | TupleNodeMask | MemoryFactoryMask;
public static final int ReactiveFromNode = (280 << shift) | TupleSourceMask | TupleSinkMask | TupleNodeMask | MemoryFactoryMask;

public static final int QueryElementNode = (300 << shift) | TupleSourceMask | TupleSinkMask | TupleNodeMask | MemoryFactoryMask;
public static final int ConditionalBranchNode = (310 << shift) | TupleSourceMask | TupleSinkMask | TupleNodeMask | MemoryFactoryMask;

// LeftTupleSource, LefTTupleSink, LeftTupleNode, BetaNode
public static final int BetaNode = (320 << shift) | TupleSourceMask | TupleSinkMask | BetaMask | TupleNodeMask | MemoryFactoryMask;
public static final int JoinNode = (330 << shift) | TupleSourceMask | TupleSinkMask | BetaMask | TupleNodeMask | MemoryFactoryMask;
public static final int NotNode = (340 << shift) | TupleSourceMask | TupleSinkMask | BetaMask | TupleNodeMask | MemoryFactoryMask;
public static final int ExistsNode = (350 << shift) | TupleSourceMask | TupleSinkMask | BetaMask | TupleNodeMask | MemoryFactoryMask;
public static final int AccumulateNode = (360 << shift) | TupleSourceMask | TupleSinkMask | BetaMask | TupleNodeMask | MemoryFactoryMask;

public static final int MockBetaNode = (400 << shift) | TupleSourceMask | TupleSinkMask | BetaMask | TupleNodeMask;
public static final int MockAlphaNode = (500 << shift) | ObjectSourceMask | ObjectSinkMask;


public static boolean isObjectSource(NetworkNode node) {
return node.getType() < NodeTypeEnums.ObjectSource;
return (node.getType() & ObjectSourceMask) != 0;
}

public static boolean isObjectSink(NetworkNode node) {
return node.getType() % 2 == 0;
return (node.getType() & ObjectSinkMask) != 0;
}

public static boolean isLeftTupleSource(NetworkNode node) {
return node.getType() > NodeTypeEnums.LeftTupleSource;
return (node.getType() & TupleSourceMask) != 0;
}

public static boolean isBetaNode(NetworkNode node) {
return node.getType() > NodeTypeEnums.BetaNode;
return (node.getType() & BetaMask) != 0;
}

public static boolean isBetaNodeWithRian(NetworkNode node) {
return node.getType() > NodeTypeEnums.BetaNode && node.isRightInputIsRiaNode();
return isBetaNode(node) && node.isRightInputIsRiaNode();
}

public static boolean isTerminalNode(NetworkNode node) {
return node.getType() == QueryTerminalNode || node.getType() == RuleTerminalNode;
return (node.getType() & TerminalNodeMask) != 0;
}

public static boolean isLeftTupleSink(NetworkNode node) {
return node.getType() % 2 != 0;
return (node.getType() & TupleSinkMask) != 0;
}

public static boolean isEndNode(NetworkNode node) {
return NodeTypeEnums.isTerminalNode(node) || NodeTypeEnums.RightInputAdapterNode == node.getType();
return (node.getType() & EndNodeMask) != 0;
}

public static boolean isLeftTupleNode(NetworkNode node) {
return isLeftTupleSource(node) || isLeftTupleSink(node);
return (node.getType() & TupleNodeMask) != 0;
}

public static boolean isMemoryFactory(NetworkNode node) {
return (node.getType() & MemoryFactoryMask) != 0;
}
/**
* This is here because AlphaTerminalNode extends LeftInputAdapter node, so cannot be switched by getType return,
* when all you need ot know is if it's an lian.
* @param node
* @return
*/
public static boolean isLeftInputAdapterNode(NetworkNode node) {
return (node.getType() & LeftInputAdapterMask) != 0;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public interface IndexableConstraint extends Constraint {

boolean isUnification();

boolean isIndexable(short nodeType, KieBaseConfiguration config);
boolean isIndexable(int nodeType, KieBaseConfiguration config);

ConstraintTypeOperator getConstraintType();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ public boolean isUnification() {
return false;
}

public boolean isIndexable(short nodeType, KieBaseConfiguration config) {
public boolean isIndexable(int nodeType, KieBaseConfiguration config) {
return true;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ public String getOperator() {
return this.operator;
}

public boolean isIndexableForNode(short nodeType, IndexableConstraint constraint, KieBaseConfiguration config) {
public boolean isIndexableForNode(int nodeType, IndexableConstraint constraint, KieBaseConfiguration config) {
switch (this) {
case EQUAL:
return true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
import org.drools.base.base.ValueType;
import org.drools.base.reteoo.NodeTypeEnums;
import org.drools.base.rule.IndexableConstraint;
import org.drools.base.rule.accessor.ReadAccessor;
import org.drools.base.rule.accessor.TupleValueExtractor;
import org.drools.base.rule.constraint.BetaConstraint;
import org.kie.api.KieBaseConfiguration;
Expand All @@ -34,7 +33,7 @@ public class IndexUtil {
static boolean USE_COMPARISON_INDEX = true;
static boolean USE_COMPARISON_INDEX_JOIN = true;

public static boolean compositeAllowed(BetaConstraint[] constraints, short betaNodeType, KieBaseConfiguration config) {
public static boolean compositeAllowed(BetaConstraint[] constraints, int betaNodeType, KieBaseConfiguration config) {
// 1) If there is 1 or more unification restrictions it cannot be composite
// 2) Ensures any non unification restrictions are first
int firstUnification = -1;
Expand All @@ -61,15 +60,15 @@ public static boolean compositeAllowed(BetaConstraint[] constraints, short betaN
return (firstUnification == -1);
}

public static boolean isIndexable(BetaConstraint constraint, short nodeType, KieBaseConfiguration config) {
public static boolean isIndexable(BetaConstraint constraint, int nodeType, KieBaseConfiguration config) {
return constraint instanceof IndexableConstraint && ((IndexableConstraint)constraint).isIndexable(nodeType, config) && !isBigDecimalEqualityConstraint((IndexableConstraint)constraint);
}

public static boolean canHaveRangeIndex(short nodeType, IndexableConstraint constraint, KieBaseConfiguration config) {
public static boolean canHaveRangeIndex(int nodeType, IndexableConstraint constraint, KieBaseConfiguration config) {
return canHaveRangeIndexForNodeType(nodeType, config) && areRangeIndexCompatibleOperands(constraint);
}

private static boolean canHaveRangeIndexForNodeType(short nodeType, KieBaseConfiguration config) {
private static boolean canHaveRangeIndexForNodeType(int nodeType, KieBaseConfiguration config) {
if (USE_COMPARISON_INDEX_JOIN && config.getOption(BetaRangeIndexOption.KEY).isBetaRangeIndexEnabled()) {
boolean b = USE_COMPARISON_INDEX && (nodeType == NodeTypeEnums.NotNode || nodeType == NodeTypeEnums.ExistsNode || nodeType == NodeTypeEnums.JoinNode);
return USE_COMPARISON_INDEX && (nodeType == NodeTypeEnums.NotNode || nodeType == NodeTypeEnums.ExistsNode || nodeType == NodeTypeEnums.JoinNode);
Expand Down Expand Up @@ -105,7 +104,7 @@ private static boolean areRangeIndexCompatibleOperands(IndexableConstraint const
return false;
}

public static boolean isIndexableForNode(short nodeType, BetaConstraint constraint, KieBaseConfiguration config) {
public static boolean isIndexableForNode(int nodeType, BetaConstraint constraint, KieBaseConfiguration config) {
if ( !(constraint instanceof IndexableConstraint) ) {
return false;
}
Expand Down Expand Up @@ -133,7 +132,7 @@ private static boolean isBigDecimalEqualityConstraint(TupleValueExtractor tupleV
return tupleValueExtractor != null && tupleValueExtractor.getValueType() == ValueType.BIG_DECIMAL_TYPE;
}

public static boolean[] isIndexableForNode(IndexPrecedenceOption indexPrecedenceOption, short nodeType, int keyDepth, BetaConstraint[] constraints, KieBaseConfiguration config) {
public static boolean[] isIndexableForNode(IndexPrecedenceOption indexPrecedenceOption, int nodeType, int keyDepth, BetaConstraint[] constraints, KieBaseConfiguration config) {
if (keyDepth < 1) {
return new boolean[constraints.length];
}
Expand All @@ -143,7 +142,7 @@ public static boolean[] isIndexableForNode(IndexPrecedenceOption indexPrecedence
findIndexableWithPatternOrder(nodeType, keyDepth, constraints, config);
}

private static boolean[] findIndexableWithEqualityPriority(short nodeType, int keyDepth, BetaConstraint[] constraints, KieBaseConfiguration config) {
private static boolean[] findIndexableWithEqualityPriority(int nodeType, int keyDepth, BetaConstraint[] constraints, KieBaseConfiguration config) {
boolean[] indexable = new boolean[constraints.length];
if (hasEqualIndexable(keyDepth, indexable, constraints)) {
return indexable;
Expand All @@ -163,7 +162,7 @@ private static boolean[] findIndexableWithEqualityPriority(short nodeType, int k
return indexable;
}

private static boolean[] findIndexableWithPatternOrder(short nodeType, int keyDepth, BetaConstraint[] constraints, KieBaseConfiguration config) {
private static boolean[] findIndexableWithPatternOrder(int nodeType, int keyDepth, BetaConstraint[] constraints, KieBaseConfiguration config) {
boolean[] indexable = new boolean[constraints.length];
for (int i = 0; i < constraints.length; i++) {
if (isIndexable(constraints[i], nodeType, config)) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
/**
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.drools.core.reteoo;

public class ObjectTypeNodeId {

public static final ObjectTypeNodeId DEFAULT_ID = new ObjectTypeNodeId(-1, 0);

private final int otnId;
private final int id;

public ObjectTypeNodeId(int otnId, int id) {
this.otnId = otnId;
this.id = id;
}

@Override
public String toString() {
return "ObjectTypeNode.Id[" + otnId + "#" + id + "]";
}

@Override
public boolean equals(Object o) {
if (this == o) {
return true;
}
if (!(o instanceof ObjectTypeNodeId)) {
return false;
}

ObjectTypeNodeId otherId = (ObjectTypeNodeId) o;
return id == otherId.id && otnId == otherId.otnId;
}

@Override
public int hashCode() {
return 31 * otnId + 37 * id;
}

public boolean before(ObjectTypeNodeId otherId) {
return otherId != null && (otnId < otherId.otnId || (otnId == otherId.otnId && id < otherId.id));
}

public int getId() {
return id;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,10 @@
import org.drools.tms.beliefsystem.BeliefSystem;
import org.drools.tms.beliefsystem.ModedAssertion;
import org.drools.tms.LogicalDependency;
import org.drools.core.util.AbstractBaseLinkedListNode;
import org.drools.core.util.AbstractLinkedListNode;
import org.drools.base.beliefsystem.Mode;

public class BayesHardEvidence<M extends BayesHardEvidence<M>> extends AbstractBaseLinkedListNode<M> implements ModedAssertion<M> {
public class BayesHardEvidence<M extends BayesHardEvidence<M>> extends AbstractLinkedListNode<M> implements ModedAssertion<M> {
private double[] distribution;
private BeliefSystem<M> beliefSystem;
private LogicalDependency<M> dep;
Expand Down
Loading

0 comments on commit d94c2cc

Please sign in to comment.