Rikkola commented on code in PR #6909: URL: https://github.com/apache/incubator-kie/pull/6909#discussion_r3914148206
########## drools-core/src/main/java/org/drools/core/reteoo/SequenceNode.java: ########## @@ -0,0 +1,588 @@ +/* + * 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; + +import org.drools.base.base.ObjectType; +import org.drools.base.common.RuleBasePartitionId; +import org.drools.base.reteoo.DynamicFilter; +import org.drools.base.reteoo.DynamicFilterProto; +import org.drools.base.reteoo.NodeTypeEnums; +import org.drools.base.reteoo.sequencing.Sequence.SequenceMemory; +import org.drools.base.rule.Pattern; +import org.drools.core.RuleBaseConfiguration; +import org.drools.core.common.InternalFactHandle; +import org.drools.core.common.InternalWorkingMemory; +import org.drools.core.common.Memory; +import org.drools.core.common.MemoryFactory; +import org.drools.core.common.PropagationContext; +import org.drools.core.common.ReteEvaluator; +import org.drools.core.common.TupleSets; +import org.drools.core.common.TupleSetsImpl; +import org.drools.core.common.UpdateContext; +import org.drools.core.reteoo.SequenceNode.SequenceNodeMemory; +import org.drools.core.reteoo.builder.BuildContext; +import org.drools.base.reteoo.sequencing.Sequencer; +import org.drools.core.reteoo.sequencing.SequencerMemoryImpl; +import org.drools.base.reteoo.sequencing.SequencerMemory; +import org.drools.base.util.AbstractLinkedListNode; +import org.drools.base.util.LinkedList; +import org.drools.core.util.index.TupleList; +import org.drools.util.bitmask.BitMask; + +import java.io.Externalizable; +import java.io.IOException; +import java.io.ObjectInput; +import java.io.ObjectOutput; +import java.util.List; + +public class SequenceNode extends LeftTupleSource + implements + LeftTupleNode, + LeftTupleSinkNode, + MemoryFactory<SequenceNodeMemory> { + + private static final long serialVersionUID = 510l; + + protected boolean tupleMemoryEnabled; + + private Sequencer sequencer; + + private AlphaAdapter[] alphaAdapters; + + private DynamicFilterProto[] dynamicFilters; + + private LeftTupleSinkNode previousTupleSinkNode; + + private LeftTupleSinkNode nextTupleSinkNode; + + public SequenceNode() { + + } + + public SequenceNode(final int id, + final LeftTupleSource tupleSource, + final BuildContext context) { + super(id, context); + setLeftTupleSource(tupleSource); + this.tupleMemoryEnabled = context.isTupleMemoryEnabled(); + + initMasks(context); + + hashcode = calculateHashCode(); + setStreamMode(true); + } + + public void setAlphaAdapters(AlphaAdapter[] adapters) { + this.alphaAdapters = adapters; + } + + public DynamicFilterProto[] getDynamicFilters() { + return dynamicFilters; + } + + public void setDynamicFilters(DynamicFilterProto[] dynamicFilters) { + this.dynamicFilters = dynamicFilters; + } + + public void doAttach(BuildContext context) { + super.doAttach(context); + this.leftInput.addTupleSink( this, context ); + } + + public void networkUpdated(UpdateContext updateContext) { + this.leftInput.networkUpdated(updateContext); + } + + + /** + * Produce a debug string. + * + * @return The debug string. + */ + public String toString() { + return "[SequenceNode(" + this.id + ")]]"; + } + + private int calculateHashCode() { + int result = this.leftInput.hashCode(); + if (this.sequencer != null) { + result = 31 * result + System.identityHashCode(this.sequencer.getSequence()); + } + return result; + } + + @Override + public boolean equals(final Object object) { + if (this == object) { + return true; + } + + if ( object == null || !(object instanceof SequenceNode) || this.hashCode() != object.hashCode() ) { + return false; + } + + SequenceNode other = (SequenceNode) object; + return this.leftInput.getId() == other.leftInput.getId() + && this.sequencer.getSequence() == other.sequencer.getSequence(); + } + + public SequenceNodeMemory createMemory(final RuleBaseConfiguration config, ReteEvaluator reteEvaluator) { + // activeFilters is indexed by adapter index (one slot per distinct object type). + // AlphaAdapter.assertObject looks up activeFilters[adapterIndex] to dispatch to matching filters. + // filters is indexed by pattern/filter index (one slot per step). + // The two sizes differ when multiple steps share the same object type. + LinkedList<DynamicFilter>[] activeFilters = new LinkedList[alphaAdapters.length]; + SequenceNodeMemory memory = new SequenceNodeMemory(this, activeFilters, new DynamicFilter[dynamicFilters.length]); + return memory; + } + + public SequencerMemory createSequencerMemory(TupleImpl lt, LeftTupleSink sink, SequenceNodeMemory nodeMemory) { + SequencerMemory sequencerMemory = new SequencerMemoryImpl(sequencer, lt, sink, this, nodeMemory); + + return sequencerMemory; + } + + public Sequencer getSequencer() { + return sequencer; + } + + public void setSequencer(Sequencer sequencer) { + this.sequencer = sequencer; + // hashcode must include the Sequencer's Sequence so that two SequenceNodes + // on the same left-input but with different sequences are never merged by + // the node-sharing mechanism. Recomputed here because setSequencer() is + // called after construction (in PhreakNodeFactory.buildSequenceNode). + this.hashcode = calculateHashCode(); + } + + public boolean isLeftTupleMemoryEnabled() { + return tupleMemoryEnabled; + } + + /** + * Returns the next node + * @return + * The next TupleSinkNode + */ + public LeftTupleSinkNode getNextLeftTupleSinkNode() { + return this.nextTupleSinkNode; + } + + /** + * Sets the next node + * @param next + * The next TupleSinkNode + */ + public void setNextLeftTupleSinkNode(final LeftTupleSinkNode next) { + this.nextTupleSinkNode = next; + } + + /** + * Returns the previous node + * @return + * The previous TupleSinkNode + */ + public LeftTupleSinkNode getPreviousLeftTupleSinkNode() { + return this.previousTupleSinkNode; + } + + /** + * Sets the previous node + * @param previous + * The previous TupleSinkNode + */ + public void setPreviousLeftTupleSinkNode(final LeftTupleSinkNode previous) { + this.previousTupleSinkNode = previous; + } + + public int getType() { + return NodeTypeEnums.SequenceNode; + } + + public static class SequenceNodeMemory extends AbstractLinkedListNode<Memory> + implements + Externalizable, + Memory { + + private static final long serialVersionUID = 510l; + + private TupleMemory leftTupleMemory; + + private TupleSets stagedChildTuples; + + private SegmentMemory memory; + + private LinkedList<DynamicFilter>[] activeFilters; + + private DynamicFilter[] filters; + + private SequenceNode node; + + private long nodePosMaskBit; + + public SequenceNodeMemory(SequenceNode node, LinkedList<DynamicFilter>[] activeFilters, DynamicFilter[] filters) { + this.node = node; + this.stagedChildTuples = new TupleSetsImpl(); + this.leftTupleMemory = new TupleList(); + this.activeFilters = activeFilters; + this.filters = filters; + } + + public void addActiveFilter(DynamicFilter filter) { + int adapterIdx = filter.getAdapterIndex(); + if (this.activeFilters[adapterIdx] == null) { + this.activeFilters[adapterIdx] = new LinkedList<>(); + } + this.activeFilters[adapterIdx].add(filter); + } + + public void removeActiveFilter(DynamicFilter filter) { + this.activeFilters[filter.getAdapterIndex()].remove(filter); + } + + public LinkedList<DynamicFilter>[] getActiveFilters() { + return this.activeFilters; + } + + public DynamicFilter[] getFilters() { + return filters; + } + + public void setFilters(DynamicFilter[] filters) { + this.filters = filters; + } + + public TupleMemory getLeftTupleMemory() { + return leftTupleMemory; + } + + public TupleSets getStagedChildTuples() { + return stagedChildTuples; + } + + public SegmentMemory getMemory() { + return memory; + } + + public void readExternal(ObjectInput in) throws IOException, + ClassNotFoundException { + throw new UnsupportedOperationException("SequenceNodeMemory does not support serialization"); + } + + public void writeExternal(ObjectOutput out) throws IOException { + throw new UnsupportedOperationException("SequenceNodeMemory does not support serialization"); + } + + public int getNodeType() { + return NodeTypeEnums.SequenceNode; + } + + public void setSegmentMemory(SegmentMemory smem) { + this.memory = smem; + } + + public SegmentMemory getSegmentMemory() { + return this.memory; + } + + public void reset() { } Review Comment: Was an issue and fixed. -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected] --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
