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;
}
}
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;
}
}
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);
}
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());
}
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());
}
Aggregations