Search in sources :

Example 1 with ConditionalBranchEvaluator

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

the class ConditionalBranchBuilder method buildConditionalBranchEvaluator.

private ConditionalBranchEvaluator buildConditionalBranchEvaluator(BuildContext context, ConditionalBranch conditionalBranch) {
    RuleTerminalNode terminalNode = buildTerminalNodeForNamedConsequence(context, conditionalBranch.getNamedConsequence());
    terminalNode.networkUpdated(new UpdateContext());
    // adds the terminal node to the list of nodes created/added by this sub-rule
    context.getNodes().add(terminalNode);
    return new ConditionalBranchEvaluator(conditionalBranch.getEvalCondition(), context.getTupleSource().getPartitionId(), terminalNode, conditionalBranch.getNamedConsequence().isBreaking(), conditionalBranch.getElseBranch() != null ? buildConditionalBranchEvaluator(context, conditionalBranch.getElseBranch()) : null);
}
Also used : ConditionalBranchEvaluator(org.drools.core.reteoo.ConditionalBranchEvaluator) UpdateContext(org.drools.core.common.UpdateContext) RuleTerminalNode(org.drools.core.reteoo.RuleTerminalNode)

Example 2 with ConditionalBranchEvaluator

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

the class ConditionalBranchBuilder method build.

public void build(BuildContext context, BuildUtils utils, RuleConditionElement rce) {
    ConditionalBranch conditionalBranch = (ConditionalBranch) rce;
    ConditionalBranchEvaluator branchEvaluator = buildConditionalBranchEvaluator(context, conditionalBranch);
    context.pushRuleComponent(rce);
    ConditionalBranchNode node = context.getComponentFactory().getNodeFactoryService().buildConditionalBranchNode(context.getNextId(), context.getTupleSource(), branchEvaluator, context);
    context.setTupleSource(utils.attachNode(context, node));
    context.popRuleComponent();
}
Also used : ConditionalBranchEvaluator(org.drools.core.reteoo.ConditionalBranchEvaluator) ConditionalBranch(org.drools.core.rule.ConditionalBranch) ConditionalBranchNode(org.drools.core.reteoo.ConditionalBranchNode)

Example 3 with ConditionalBranchEvaluator

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

the class PhreakBranchNode method doLeftUpdates.

public void doLeftUpdates(ConditionalBranchNode branchNode, ConditionalBranchMemory cbm, LeftTupleSink sink, InternalAgenda agenda, TupleSets<LeftTuple> srcLeftTuples, TupleSets<LeftTuple> trgLeftTuples, TupleSets<LeftTuple> stagedLeftTuples, RuleExecutor executor) {
    ConditionalBranchEvaluator branchEvaluator = branchNode.getBranchEvaluator();
    RuleAgendaItem ruleAgendaItem = executor.getRuleAgendaItem();
    int salienceInt = 0;
    Salience salience = ruleAgendaItem.getRule().getSalience();
    if (!salience.isDynamic()) {
        salienceInt = ruleAgendaItem.getRule().getSalience().getValue();
        salience = null;
    }
    for (LeftTuple leftTuple = srcLeftTuples.getUpdateFirst(); leftTuple != null; ) {
        LeftTuple next = leftTuple.getStagedNext();
        BranchTuples branchTuples = getBranchTuples(sink, leftTuple);
        RuleTerminalNode oldRtn = null;
        if (branchTuples.rtnLeftTuple != null) {
            oldRtn = branchTuples.rtnLeftTuple.getTupleSink();
        }
        ConditionalExecution conditionalExecution = branchEvaluator.evaluate(leftTuple, agenda.getWorkingMemory(), cbm.context);
        RuleTerminalNode newRtn = null;
        boolean breaking = false;
        if (conditionalExecution != null) {
            newRtn = (RuleTerminalNode) conditionalExecution.getSink().getFirstLeftTupleSink();
            breaking = conditionalExecution.isBreaking();
        }
        // Handle conditional branches
        if (oldRtn != null) {
            if (newRtn == null) {
                // old exits, new does not, so delete
                if (branchTuples.rtnLeftTuple.getMemory() != null) {
                    executor.removeLeftTuple(branchTuples.rtnLeftTuple);
                }
                PhreakRuleTerminalNode.doLeftDelete(agenda, executor, branchTuples.rtnLeftTuple);
            } else if (newRtn == oldRtn) {
                // old and new on same branch, so update
                PhreakRuleTerminalNode.doLeftTupleUpdate(newRtn, executor, agenda, salienceInt, salience, branchTuples.rtnLeftTuple);
            } else {
                // old and new on different branches, delete one and insert the other
                if (branchTuples.rtnLeftTuple.getMemory() != null) {
                    executor.removeLeftTuple(branchTuples.rtnLeftTuple);
                }
                PhreakRuleTerminalNode.doLeftDelete(agenda, executor, branchTuples.rtnLeftTuple);
                branchTuples.rtnLeftTuple = newRtn.createLeftTuple(leftTuple, newRtn, leftTuple.getPropagationContext(), true);
                PhreakRuleTerminalNode.doLeftTupleInsert(newRtn, executor, agenda, executor.getRuleAgendaItem(), salienceInt, salience, branchTuples.rtnLeftTuple);
            }
        } else if (newRtn != null) {
            // old does not exist, new exists, so insert
            branchTuples.rtnLeftTuple = newRtn.createLeftTuple(leftTuple, newRtn, leftTuple.getPropagationContext(), true);
            PhreakRuleTerminalNode.doLeftTupleInsert(newRtn, executor, agenda, executor.getRuleAgendaItem(), salienceInt, salience, branchTuples.rtnLeftTuple);
        }
        // Handle main branch
        if (branchTuples.mainLeftTuple != null) {
            normalizeStagedTuples(stagedLeftTuples, branchTuples.mainLeftTuple);
            if (!breaking) {
                // child exist, new one does, so update
                trgLeftTuples.addUpdate(branchTuples.mainLeftTuple);
            } else {
                // child exist, new one does not, so delete
                trgLeftTuples.addDelete(branchTuples.mainLeftTuple);
            }
        } else if (!breaking) {
            // child didn't exist, new one does, so insert
            trgLeftTuples.addInsert(sink.createLeftTuple(leftTuple, sink, leftTuple.getPropagationContext(), true));
        }
        leftTuple.clearStaged();
        leftTuple = next;
    }
}
Also used : ConditionalBranchEvaluator(org.drools.core.reteoo.ConditionalBranchEvaluator) ConditionalExecution(org.drools.core.reteoo.ConditionalBranchEvaluator.ConditionalExecution) Salience(org.drools.core.spi.Salience) LeftTuple(org.drools.core.reteoo.LeftTuple) RuleTerminalNode(org.drools.core.reteoo.RuleTerminalNode)

Example 4 with ConditionalBranchEvaluator

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

the class PhreakBranchNode method doLeftInserts.

public void doLeftInserts(ConditionalBranchNode branchNode, ConditionalBranchMemory cbm, LeftTupleSink sink, InternalAgenda agenda, TupleSets<LeftTuple> srcLeftTuples, TupleSets<LeftTuple> trgLeftTuples, RuleExecutor executor) {
    ConditionalBranchEvaluator branchEvaluator = branchNode.getBranchEvaluator();
    RuleAgendaItem ruleAgendaItem = executor.getRuleAgendaItem();
    int salienceInt = 0;
    Salience salience = ruleAgendaItem.getRule().getSalience();
    if (!salience.isDynamic()) {
        salienceInt = ruleAgendaItem.getRule().getSalience().getValue();
        salience = null;
    }
    for (LeftTuple leftTuple = srcLeftTuples.getInsertFirst(); leftTuple != null; ) {
        LeftTuple next = leftTuple.getStagedNext();
        boolean breaking = false;
        ConditionalExecution conditionalExecution = branchEvaluator.evaluate(leftTuple, agenda.getWorkingMemory(), cbm.context);
        boolean useLeftMemory = RuleNetworkEvaluator.useLeftMemory(branchNode, leftTuple);
        if (conditionalExecution != null) {
            RuleTerminalNode rtn = (RuleTerminalNode) conditionalExecution.getSink().getFirstLeftTupleSink();
            LeftTuple branchedLeftTuple = rtn.createLeftTuple(leftTuple, rtn, leftTuple.getPropagationContext(), useLeftMemory);
            PhreakRuleTerminalNode.doLeftTupleInsert(rtn, executor, agenda, executor.getRuleAgendaItem(), salienceInt, salience, branchedLeftTuple);
            breaking = conditionalExecution.isBreaking();
        }
        if (!breaking) {
            trgLeftTuples.addInsert(sink.createLeftTuple(leftTuple, sink, leftTuple.getPropagationContext(), useLeftMemory));
        }
        leftTuple.clearStaged();
        leftTuple = next;
    }
}
Also used : ConditionalBranchEvaluator(org.drools.core.reteoo.ConditionalBranchEvaluator) ConditionalExecution(org.drools.core.reteoo.ConditionalBranchEvaluator.ConditionalExecution) Salience(org.drools.core.spi.Salience) LeftTuple(org.drools.core.reteoo.LeftTuple) RuleTerminalNode(org.drools.core.reteoo.RuleTerminalNode)

Aggregations

ConditionalBranchEvaluator (org.drools.core.reteoo.ConditionalBranchEvaluator)4 RuleTerminalNode (org.drools.core.reteoo.RuleTerminalNode)3 ConditionalExecution (org.drools.core.reteoo.ConditionalBranchEvaluator.ConditionalExecution)2 LeftTuple (org.drools.core.reteoo.LeftTuple)2 Salience (org.drools.core.spi.Salience)2 UpdateContext (org.drools.core.common.UpdateContext)1 ConditionalBranchNode (org.drools.core.reteoo.ConditionalBranchNode)1 ConditionalBranch (org.drools.core.rule.ConditionalBranch)1