Search in sources :

Example 6 with Salience

use of org.drools.core.spi.Salience in project drools by kiegroup.

the class RuleBuilder method buildSalience.

private static void buildSalience(final RuleBuildContext context) {
    String salienceText = context.getRuleDescr().getSalience();
    if (salienceText != null && !salienceText.equals("")) {
        try {
            // First check if it is an Integer
            Salience salience = new SalienceInteger(Integer.parseInt(salienceText));
            context.getRule().setSalience(salience);
        } catch (Exception e) {
            // It wasn't an integer, so build as an expression
            context.getDialect().getSalienceBuilder().build(context);
        }
    }
}
Also used : Salience(org.drools.core.spi.Salience) SalienceInteger(org.drools.core.base.SalienceInteger) ParseException(java.text.ParseException)

Example 7 with Salience

use of org.drools.core.spi.Salience 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 8 with Salience

use of org.drools.core.spi.Salience 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

Salience (org.drools.core.spi.Salience)8 LeftTuple (org.drools.core.reteoo.LeftTuple)4 RuleImpl (org.drools.core.definitions.rule.impl.RuleImpl)3 Test (org.junit.Test)3 SalienceInteger (org.drools.core.base.SalienceInteger)2 ConditionalBranchEvaluator (org.drools.core.reteoo.ConditionalBranchEvaluator)2 ConditionalExecution (org.drools.core.reteoo.ConditionalBranchEvaluator.ConditionalExecution)2 RuleTerminalNode (org.drools.core.reteoo.RuleTerminalNode)2 RuleTerminalNodeLeftTuple (org.drools.core.reteoo.RuleTerminalNodeLeftTuple)2 ParseException (java.text.ParseException)1 ArrayList (java.util.ArrayList)1 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)1 Person (org.drools.compiler.Person)1 WorkingMemory (org.drools.core.WorkingMemory)1 MVELSalienceExpression (org.drools.core.base.mvel.MVELSalienceExpression)1 InternalFactHandle (org.drools.core.common.InternalFactHandle)1 InternalWorkingMemory (org.drools.core.common.InternalWorkingMemory)1 KnowledgeHelper (org.drools.core.spi.KnowledgeHelper)1 Rule (org.kie.api.definition.rule.Rule)1 KieSession (org.kie.api.runtime.KieSession)1