Search in sources :

Example 1 with SubnetworkTuple

use of org.drools.core.reteoo.SubnetworkTuple in project drools by kiegroup.

the class RuleNetworkEvaluator method doRiaNode2.

private void doRiaNode2(InternalWorkingMemory wm, TupleSets<LeftTuple> srcTuples, RightInputAdapterNode riaNode) {
    ObjectSink[] sinks = riaNode.getObjectSinkPropagator().getSinks();
    BetaNode betaNode = (BetaNode) sinks[0];
    BetaMemory bm;
    Memory nodeMem = wm.getNodeMemory(betaNode);
    if (NodeTypeEnums.AccumulateNode == betaNode.getType()) {
        bm = ((AccumulateMemory) nodeMem).getBetaMemory();
    } else {
        bm = (BetaMemory) nodeMem;
    }
    TupleSets<RightTuple> rightTuples = bm.getStagedRightTuples();
    // Build up iteration array for other sinks
    BetaNode[] bns = null;
    BetaMemory[] bms = null;
    int length = sinks.length;
    if (length > 1) {
        bns = new BetaNode[sinks.length - 1];
        bms = new BetaMemory[sinks.length - 1];
        for (int i = 1; i < length; i++) {
            bns[i - 1] = (BetaNode) sinks[i];
            Memory nodeMem2 = wm.getNodeMemory(bns[i - 1]);
            if (NodeTypeEnums.AccumulateNode == betaNode.getType()) {
                bms[i - 1] = ((AccumulateMemory) nodeMem2).getBetaMemory();
            } else {
                bms[i - 1] = (BetaMemory) nodeMem2;
            }
        }
    }
    // subtract one, as first is not in the array;
    length--;
    for (SubnetworkTuple subnetworkTuple = (SubnetworkTuple) srcTuples.getInsertFirst(); subnetworkTuple != null; ) {
        SubnetworkTuple next = (SubnetworkTuple) subnetworkTuple.getStagedNext();
        if (bm.getStagedRightTuples().isEmpty()) {
            bm.setNodeDirtyWithoutNotify();
        }
        subnetworkTuple.prepareStagingOnRight();
        rightTuples.addInsert(subnetworkTuple);
        if (bns != null) {
            for (int i = 0; i < length; i++) {
                if (bms[i].getStagedRightTuples().isEmpty()) {
                    bms[i].setNodeDirtyWithoutNotify();
                }
                subnetworkTuple = riaNode.createPeer(subnetworkTuple);
                bms[i].getStagedRightTuples().addInsert(subnetworkTuple);
            }
        }
        subnetworkTuple = next;
    }
    for (SubnetworkTuple subnetworkTuple = (SubnetworkTuple) srcTuples.getDeleteFirst(); subnetworkTuple != null; ) {
        SubnetworkTuple next = (SubnetworkTuple) subnetworkTuple.getStagedNext();
        if (rightTuples.isEmpty()) {
            bm.setNodeDirtyWithoutNotify();
        }
        switch(subnetworkTuple.getStagedTypeOnRight()) {
            // handle clash with already staged entries
            case Tuple.INSERT:
                rightTuples.removeInsert(subnetworkTuple);
                break;
            case Tuple.UPDATE:
                rightTuples.removeUpdate(subnetworkTuple);
                break;
        }
        subnetworkTuple.prepareStagingOnRight();
        rightTuples.addDelete(subnetworkTuple);
        if (bns != null) {
            for (int i = 0; i < length; i++) {
                subnetworkTuple = (SubnetworkTuple) subnetworkTuple.getPeer();
                if (bms[i].getStagedRightTuples().isEmpty()) {
                    bms[i].setNodeDirtyWithoutNotify();
                }
                bms[i].getStagedRightTuples().addDelete(subnetworkTuple);
                subnetworkTuple.setStagedOnRight();
            }
        }
        subnetworkTuple = next;
    }
    for (SubnetworkTuple subnetworkTuple = (SubnetworkTuple) srcTuples.getUpdateFirst(); subnetworkTuple != null; ) {
        SubnetworkTuple next = (SubnetworkTuple) subnetworkTuple.getStagedNext();
        if (rightTuples.isEmpty()) {
            bm.setNodeDirtyWithoutNotify();
        }
        subnetworkTuple.prepareStagingOnRight();
        rightTuples.addUpdate(subnetworkTuple);
        if (bns != null) {
            for (int i = 0; i < length; i++) {
                subnetworkTuple = (SubnetworkTuple) subnetworkTuple.getPeer();
                if (bms[i].getStagedRightTuples().isEmpty()) {
                    bms[i].setNodeDirtyWithoutNotify();
                }
                bms[i].getStagedRightTuples().addUpdate(subnetworkTuple);
                subnetworkTuple.setStagedOnRight();
            }
        }
        subnetworkTuple = next;
    }
    srcTuples.resetAll();
}
Also used : BetaNode(org.drools.core.reteoo.BetaNode) Memory(org.drools.core.common.Memory) QueryElementNodeMemory(org.drools.core.reteoo.QueryElementNode.QueryElementNodeMemory) PathMemory(org.drools.core.reteoo.PathMemory) InternalWorkingMemory(org.drools.core.common.InternalWorkingMemory) ConditionalBranchMemory(org.drools.core.reteoo.ConditionalBranchNode.ConditionalBranchMemory) AccumulateMemory(org.drools.core.reteoo.AccumulateNode.AccumulateMemory) TupleMemory(org.drools.core.reteoo.TupleMemory) TimerNodeMemory(org.drools.core.reteoo.TimerNode.TimerNodeMemory) BetaMemory(org.drools.core.reteoo.BetaMemory) EvalMemory(org.drools.core.reteoo.EvalConditionNode.EvalMemory) SegmentMemory(org.drools.core.reteoo.SegmentMemory) FromMemory(org.drools.core.reteoo.FromNode.FromMemory) RiaPathMemory(org.drools.core.reteoo.RiaPathMemory) ObjectSink(org.drools.core.reteoo.ObjectSink) BetaMemory(org.drools.core.reteoo.BetaMemory) RightTuple(org.drools.core.reteoo.RightTuple) SubnetworkTuple(org.drools.core.reteoo.SubnetworkTuple)

Example 2 with SubnetworkTuple

use of org.drools.core.reteoo.SubnetworkTuple in project drools by kiegroup.

the class AddRemoveRulesTest method testRemoveRuleWithSharedRia.

@Test
public void testRemoveRuleWithSharedRia() {
    final String rule1Name = "rule1";
    final String rule2Name = "rule2";
    final String rule1 = "rule " + rule1Name + " \n" + "when \n" + "   Integer() \n" + "   not(Integer() and Integer()) \n" + "then \n" + "System.out.println('test rule 1'); \n" + "end";
    final String rule2 = "rule " + rule2Name + " \n" + "when \n" + "   Integer() \n" + "   exists(Integer() and Integer()) \n" + "then \n" + "System.out.println('test  rule 2'); \n" + "end";
    final KieSession session = base.newKieSession();
    this.addRuleToEngine(rule1);
    final InternalFactHandle fh = (InternalFactHandle) session.insert(1);
    session.fireAllRules();
    this.addRuleToEngine(rule2);
    final SubnetworkTuple tuple = (SubnetworkTuple) fh.getFirstLeftTuple().getFirstChild().getFirstChild();
    assertNotNull(tuple.getPeer());
    this.deleteRule(rule2Name);
    assertNull(tuple.getPeer());
}
Also used : StatelessKieSession(org.kie.api.runtime.StatelessKieSession) KieSession(org.kie.api.runtime.KieSession) InternalFactHandle(org.drools.core.common.InternalFactHandle) SubnetworkTuple(org.drools.core.reteoo.SubnetworkTuple) Test(org.junit.Test)

Aggregations

SubnetworkTuple (org.drools.core.reteoo.SubnetworkTuple)2 InternalFactHandle (org.drools.core.common.InternalFactHandle)1 InternalWorkingMemory (org.drools.core.common.InternalWorkingMemory)1 Memory (org.drools.core.common.Memory)1 AccumulateMemory (org.drools.core.reteoo.AccumulateNode.AccumulateMemory)1 BetaMemory (org.drools.core.reteoo.BetaMemory)1 BetaNode (org.drools.core.reteoo.BetaNode)1 ConditionalBranchMemory (org.drools.core.reteoo.ConditionalBranchNode.ConditionalBranchMemory)1 EvalMemory (org.drools.core.reteoo.EvalConditionNode.EvalMemory)1 FromMemory (org.drools.core.reteoo.FromNode.FromMemory)1 ObjectSink (org.drools.core.reteoo.ObjectSink)1 PathMemory (org.drools.core.reteoo.PathMemory)1 QueryElementNodeMemory (org.drools.core.reteoo.QueryElementNode.QueryElementNodeMemory)1 RiaPathMemory (org.drools.core.reteoo.RiaPathMemory)1 RightTuple (org.drools.core.reteoo.RightTuple)1 SegmentMemory (org.drools.core.reteoo.SegmentMemory)1 TimerNodeMemory (org.drools.core.reteoo.TimerNode.TimerNodeMemory)1 TupleMemory (org.drools.core.reteoo.TupleMemory)1 Test (org.junit.Test)1 KieSession (org.kie.api.runtime.KieSession)1