Here is an example from the eclipse basic test project, i have added a new Rule allways to be fired with
lock-on-active true and also added a function to make a printiln in the console to see what gets evaluated.
Code to run:
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;
/**
* This is a sample class to launch a rule.
*/
public class DroolsTest {
public static final void main(String[] args) {
try {
// load up the knowledge base
KnowledgeBase kbase = readKnowledgeBase();
StatefulKnowledgeSession ksession = kbase.newStatefulKnowledgeSession();
KnowledgeRuntimeLogger logger = KnowledgeRuntimeLoggerFactory.newFileLogger(ksession, "test");
// go !
Message message = new Message();
message.setMessage("Hello World");
message.setStatus(Message.HELLO);
ksession.insert(message);
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 {
public static final int HELLO = 0;
public static final int GOODBYE = 1;
private String message;
private int status;
public String getMessage() {
return this.message;
}
public void setMessage(String message) {
this.message = message;
}
public int getStatus() {
return this.status;
}
public void setStatus(int status) {
this.status = status;
}
}
}
Rules file:
package com.sample
import com.sample.DroolsTest.Message;
function boolean testEval(String arg) {
System.out.println(arg);
return true;
}
rule "Hello World"
when
eval(testEval("Hello World eval up"))
m : Message( status == Message.HELLO, myMessage : message )
eval(testEval("Hello World eval down "))
then
System.out.println( myMessage );
m.setMessage( "Goodbye cruel world" );
m.setStatus( Message.GOODBYE );
update( m );
end
rule "Allways"
lock-on-active true
when
eval(testEval("Allways eval up"))
Message()
eval(testEval("Allways eval down"))
then
System.out.println("Allways has run");
end
rule "GoodBye"
when
eval(testEval("GoodBye eval up"))
Message( status == Message.GOODBYE, myMessage : message )
eval(testEval("GoodBye eval down"))
then
System.out.println( myMessage );
end
Out put:
Hello World eval up
Hello World eval down
Allways eval up
Allways eval down
GoodBye eval up
Allways has run
Hello World
GoodBye eval down
Allways eval down
Goodbye cruel world
Why the rules Allways has been evaluated again after been fired, for what i understand if the rule has lock-on-active true It shouldnt be evaluated again since it shouldnt run any more.