Search in sources :

Example 1 with Salience

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

the class PhreakRuleTerminalNode method doLeftInserts.

public void doLeftInserts(TerminalNode rtnNode, InternalAgenda agenda, TupleSets<LeftTuple> srcLeftTuples, RuleExecutor executor) {
    RuleAgendaItem ruleAgendaItem = executor.getRuleAgendaItem();
    int salienceInt = 0;
    Salience salience = ruleAgendaItem.getRule().getSalience();
    if (!salience.isDynamic()) {
        salienceInt = salience.getValue();
        salience = null;
    }
    if (rtnNode.getRule().getAutoFocus() && !ruleAgendaItem.getAgendaGroup().isActive()) {
        agenda.setFocus(ruleAgendaItem.getAgendaGroup());
    }
    for (LeftTuple leftTuple = srcLeftTuples.getInsertFirst(); leftTuple != null; ) {
        LeftTuple next = leftTuple.getStagedNext();
        doLeftTupleInsert(rtnNode, executor, agenda, ruleAgendaItem, salienceInt, salience, leftTuple);
        leftTuple.clearStaged();
        leftTuple = next;
    }
}
Also used : Salience(org.drools.core.spi.Salience) LeftTuple(org.drools.core.reteoo.LeftTuple) RuleTerminalNodeLeftTuple(org.drools.core.reteoo.RuleTerminalNodeLeftTuple)

Example 2 with Salience

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

the class PhreakRuleTerminalNode method doLeftUpdates.

public void doLeftUpdates(TerminalNode rtnNode, InternalAgenda agenda, TupleSets<LeftTuple> srcLeftTuples, RuleExecutor executor) {
    RuleAgendaItem ruleAgendaItem = executor.getRuleAgendaItem();
    if (rtnNode.getRule().getAutoFocus() && !ruleAgendaItem.getAgendaGroup().isActive()) {
        agenda.setFocus(ruleAgendaItem.getAgendaGroup());
    }
    int salienceInt = 0;
    Salience salience = ruleAgendaItem.getRule().getSalience();
    if (!salience.isDynamic()) {
        salienceInt = salience.getValue();
        salience = null;
    }
    // Salience salienceInt = ruleAgendaItem.getRule().getSalience();
    for (LeftTuple leftTuple = srcLeftTuples.getUpdateFirst(); leftTuple != null; ) {
        LeftTuple next = leftTuple.getStagedNext();
        doLeftTupleUpdate(rtnNode, executor, agenda, salienceInt, salience, leftTuple);
        leftTuple.clearStaged();
        leftTuple = next;
    }
}
Also used : Salience(org.drools.core.spi.Salience) LeftTuple(org.drools.core.reteoo.LeftTuple) RuleTerminalNodeLeftTuple(org.drools.core.reteoo.RuleTerminalNodeLeftTuple)

Example 3 with Salience

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

the class Misc2Test method testCustomDynamicSalience.

@Test
public void testCustomDynamicSalience() {
    String drl = "package org.drools.test; " + "import " + Person.class.getName() + "; " + "global java.util.List list; " + "rule A " + "when " + "     $person : Person( name == 'a' ) " + "then" + "     list.add( $person.getAge() ); " + "end " + "rule B " + "when " + "     $person : Person( name == 'b' ) " + "then" + "     list.add( $person.getAge() ); " + "end " + "";
    KieHelper helper = new KieHelper();
    helper.addContent(drl, ResourceType.DRL);
    KieSession session = helper.build().newKieSession();
    List<Integer> list = new ArrayList<Integer>();
    session.setGlobal("list", list);
    for (Rule r : session.getKieBase().getKiePackage("org.drools.test").getRules()) {
        ((RuleImpl) r).setSalience(new Salience() {

            @Override
            public int getValue(KnowledgeHelper khelper, Rule rule, WorkingMemory workingMemory) {
                if (khelper == null) {
                    return 0;
                }
                InternalFactHandle h = (InternalFactHandle) khelper.getMatch().getFactHandles().get(0);
                return ((Person) h.getObject()).getAge();
            }

            @Override
            public int getValue() {
                throw new IllegalStateException("Should not have been called...");
            }

            @Override
            public boolean isDynamic() {
                return true;
            }
        });
    }
    session.insert(new Person("a", 1));
    session.insert(new Person("a", 5));
    session.insert(new Person("a", 3));
    session.insert(new Person("b", 4));
    session.insert(new Person("b", 2));
    session.insert(new Person("b", 6));
    session.fireAllRules();
    assertEquals(Arrays.asList(6, 5, 4, 3, 2, 1), list);
}
Also used : WorkingMemory(org.drools.core.WorkingMemory) InternalWorkingMemory(org.drools.core.common.InternalWorkingMemory) ArrayList(java.util.ArrayList) KieHelper(org.kie.internal.utils.KieHelper) RuleImpl(org.drools.core.definitions.rule.impl.RuleImpl) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Salience(org.drools.core.spi.Salience) KieSession(org.kie.api.runtime.KieSession) StatelessKieSession(org.kie.api.runtime.StatelessKieSession) KnowledgeHelper(org.drools.core.spi.KnowledgeHelper) Rule(org.kie.api.definition.rule.Rule) InternalFactHandle(org.drools.core.common.InternalFactHandle) Person(org.drools.compiler.Person) Test(org.junit.Test)

Example 4 with Salience

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

the class RuleTest method testGetSalienceValue.

@Test
public void testGetSalienceValue() {
    final RuleImpl rule = new RuleImpl("myrule");
    final int salienceValue = 100;
    Salience salience = new SalienceInteger(salienceValue);
    rule.setSalience(salience);
    assertEquals(salienceValue, rule.getSalienceValue());
    assertFalse(rule.isSalienceDynamic());
}
Also used : Salience(org.drools.core.spi.Salience) RuleImpl(org.drools.core.definitions.rule.impl.RuleImpl) SalienceInteger(org.drools.core.base.SalienceInteger) Test(org.junit.Test)

Example 5 with Salience

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

the class RuleTest method testIsSalienceDynamic.

@Test
public void testIsSalienceDynamic() {
    final RuleImpl rule = new RuleImpl("myrule");
    Salience salience = new MVELSalienceExpression();
    rule.setSalience(salience);
    assertTrue(rule.isSalienceDynamic());
}
Also used : Salience(org.drools.core.spi.Salience) MVELSalienceExpression(org.drools.core.base.mvel.MVELSalienceExpression) RuleImpl(org.drools.core.definitions.rule.impl.RuleImpl) Test(org.junit.Test)

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