Skip to content

Commit

Permalink
[DROOLS-335] recalculate dynamic salience on tuple update in PhreakRu…
Browse files Browse the repository at this point in the history
…leTerminalNode
  • Loading branch information
mariofusco committed May 16, 2014
1 parent 604f86c commit f8f7210
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 4 deletions.
Expand Up @@ -3,15 +3,16 @@
import org.drools.compiler.Cheese;
import org.drools.compiler.CommonTestMethodBase;
import org.junit.Test;
import org.kie.api.io.ResourceType;
import org.kie.internal.KnowledgeBase;
import org.kie.internal.builder.KnowledgeBuilder;
import org.kie.internal.builder.KnowledgeBuilderFactory;
import org.kie.internal.builder.conf.RuleEngineOption;
import org.kie.internal.io.ResourceFactory;
import org.kie.internal.runtime.StatefulKnowledgeSession;
import org.kie.api.io.ResourceType;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class NamedConsequencesTest extends CommonTestMethodBase {
Expand Down Expand Up @@ -672,4 +673,66 @@ public void testMultipleIfElseInARow() {
assertTrue(results.contains("Car is red"));
assertTrue(results.contains("Car is NOT cheap"));
}

@Test
public void testDynamicSalience() {
// DROOLS-335
String str =
"import " + Fact.class.getCanonicalName() + ";\n" +
"global java.util.List results;\n" +
"rule R1 salience( -$id ) when\n" +
" fact : Fact( status == Fact.Status.UNKNOWN, $id : id)\n" +
" count : Long() from accumulate ( $s:Fact(this != fact, status==Fact.Status.NO, id < fact.id), count( $s ) )" +
" if (count.intValue() > 1) break[yes]\n" +
"then\n" +
" results.add(\"n\" + $id);" +
" fact.setStatus(Fact.Status.NO);\n" +
" update(fact);\n" +
"then[yes]\n" +
" results.add(\"y\" + $id);" +
" fact.setStatus(Fact.Status.YES);\n" +
" update(fact);\n" +
"end\n" +
" \n" +
"rule R2 salience 1 when\n" +
" fact : Fact( status == Fact.Status.NO, $id : id )\n" +
" Fact( status == Fact.Status.YES, id > $id )\n" +
"then\n" +
" delete(fact);\n" +
"end";

KnowledgeBase kbase = loadKnowledgeBaseFromString(str);
StatefulKnowledgeSession ksession = kbase.newStatefulKnowledgeSession();

List<String> results = new ArrayList<String>();
ksession.setGlobal("results", results);
for (int i = 1; i < 7; i++) {
ksession.insert(new Fact(i));
}

ksession.fireAllRules();
assertEquals(Arrays.asList("n1", "n2", "y3", "n4", "n5", "y6"), results);
}

public static class Fact {
public enum Status { UNKNOWN, NO, YES };
private final int id;
private Status status = Status.UNKNOWN;

public Fact(int id) {
this.id = id;
}

public int getId() {
return id;
}

public Status getStatus() {
return status;
}

public void setStatus(Status status) {
this.status = status;
}
}
}
Expand Up @@ -55,7 +55,7 @@ public void doLeftInserts(TerminalNode rtnNode,
int salienceInt = 0;
Salience salience = ruleAgendaItem.getRule().getSalience();
if ( !salience.isDynamic() ) {
salienceInt = ruleAgendaItem.getRule().getSalience().getValue();
salienceInt = salience.getValue();
salience = null;
}

Expand Down Expand Up @@ -132,8 +132,7 @@ public void doLeftUpdates(TerminalNode rtnNode,
int salienceInt = 0;
Salience salience = ruleAgendaItem.getRule().getSalience();
if ( !salience.isDynamic() ) {
salienceInt = ruleAgendaItem.getRule().getSalience().getValue();
} else {
salienceInt = salience.getValue();
salience = null;
}

Expand Down

0 comments on commit f8f7210

Please sign in to comment.