I found weird drool engine behavior. I have two rules with different areas. As described in the saliva documentation
3.3.4.1. Conflict resolution
Conflict resolution is required when there are many rules for the agenda. (The basics of this are in the Quick Start chapter.) As a rule, firing rules can have side effects, the rule mechanism needs to know in which order the rules should shoot (for example, shooting ruleA can cause ruleB to be removed from the agenda )
Default conflict resolution strategies used by Drools: Consciousness and LIFO (last, first exit).
The most noticeable is the manifestation (or priority), in which case the user can indicate that a certain rule has a higher priority (giving it a higher number) than other rules. In this case, a rule with greater significance would be preferable. LIFO priorities based on the assigned working memory The value of the action counter with all the rules created during the same action receiving the same value. the order of execution of a set of shootings with the same priority value is arbitrary.
But following my two rules with 5 objects for each type gives strange results. On some objects, the rule with expression 1 is executed earlier than the rule with significance 10. If I remove the update from the rule, the first rules with the output of 10 will be executed, and only after that with expression 1.
package com.sample
import com.sample.DroolsTest.Message;
import com.sample.DroolsTest.Message2;
rule "Hello World2"
salience 10
when
m : Message()
m2 : Message2(status <0)
then
System.out.println( "Second Rule With Salience 10");
System.out.println( "m status = "+m.getStatus());
System.out.println( "m2 status = "+m2.getStatus());
m2.setStatus(m2.getStatus()*(-1));
update(m2);
end
rule "Hello World3"
salience 1
when
m2 : Message2()
m : Message()
then
System.out.println( "Third Rule With Salience 1");
System.out.println( "m status = "+m.getStatus());
System.out.println( "m2 status = "+m2.getStatus());
end
rule "GoodBye"
salience 0
when
eval(true)
then
System.out.println( "End" );
end
java,
package com.sample;
import org.drools.KnowledgeBase;
import org.drools.KnowledgeBaseFactory;
import org.drools.builder.KnowledgeBuilder;
import org.drools.builder.KnowledgeBuilderError;
import org.drools.builder.KnowledgeBuilderErrors;
import org.drools.builder.KnowledgeBuilderFactory;
import org.drools.builder.ResourceType;
import org.drools.io.ResourceFactory;
import org.drools.logger.KnowledgeRuntimeLogger;
import org.drools.logger.KnowledgeRuntimeLoggerFactory;
import org.drools.runtime.StatefulKnowledgeSession;
public class DroolsTest {
public static final void main(String[] args) {
try {
KnowledgeBase kbase = readKnowledgeBase();
StatefulKnowledgeSession ksession = kbase.newStatefulKnowledgeSession();
KnowledgeRuntimeLogger logger = KnowledgeRuntimeLoggerFactory.newFileLogger(ksession, "test");
System.out.println("Start");
for(int i=0; i<5; i++){
Message message = new Message(i);
ksession.insert(message);
Message2 message2 = new Message2(-i);
ksession.insert(message2);
}
ksession.fireAllRules();
logger.close();
} catch (Throwable t) {
t.printStackTrace();
}
}
private static KnowledgeBase readKnowledgeBase() throws Exception {
KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder();
kbuilder.add(ResourceFactory.newClassPathResource("Sample.drl"), ResourceType.DRL);
KnowledgeBuilderErrors errors = kbuilder.getErrors();
if (errors.size() > 0) {
for (KnowledgeBuilderError error: errors) {
System.err.println(error);
}
throw new IllegalArgumentException("Could not parse knowledge.");
}
KnowledgeBase kbase = KnowledgeBaseFactory.newKnowledgeBase();
kbase.addKnowledgePackages(kbuilder.getKnowledgePackages());
return kbase;
}
public static class Message {
private int status;
public int getStatus() {
return this.status;
}
public Message(int status) {
super();
this.status = status;
}
public void setStatus(int status) {
this.status = status;
}
}
public static class Message2 {
private int status;
public Message2(int status) {
this.status = status;
}
public int getStatus() {
return this.status;
}
public void setStatus(int status) {
this.status = status;
}
}
}
, .
PS: , - , , .