For some reason I cannot get the most basic rule to fire
from a flow. Not sure what is going on, but would appreciate some help.
This is in Drools 5.1.0.M1.
I pretty much followed the example at http://www.developer.com/java/ent/article.php/10933_3821101_4/Rev-Up-the-Drools-5-Java-Rule-Engine.htm,
but it did not work for me. I don’t get an error
message and I don’t see the rule fire and print the system.out.
.rf
<?xml version="1.0"
encoding="UTF-8"?>
<process
xmlns="http://drools.org/drools-5.0/process"
xmlns:xs="http://www.w3.org/2001/XMLSchema-instance"
xs:schemaLocation="http://drools.org/drools-5.0/process
drools-processes-5.0.xsd"
type="RuleFlow" name="flow"
id="com.sample.bpmn.hello" package-name="com.sample">
<header>
</header>
<nodes>
<end id="4"
name="End" x="561" y="140" width="48"
height="48" />
<start id="1" name="Start"
x="66" y="88" width="48" height="48"
/>
<ruleSet id="3"
name="Rule" x="368" y="159" width="80"
height="48" ruleFlowGroup="helloGroup" />
</nodes>
<connections>
<connection from="3"
to="4" />
<connection from="1"
to="3" />
</connections>
</process>
.dlr
package com.sample
import
com.sample.DroolsTest.Message;
rule "Hello World Message"
ruleflow-group "helloGroup"
when
m
: Message( status == Message.HELLO, myMessage : message )
then
System.out.println(
myMessage + "***" );
m.setMessage(
"Goodbye
cruel world Message" );
m.setStatus(
Message.GOODBYE );
update( m );
end
Java
package com.sample;
import org.drools.KnowledgeBase;
import org.drools.KnowledgeBaseFactory;
import org.drools.bpmn2.xml.BPMNSemanticModule;
import org.drools.builder.KnowledgeBuilder;
import org.drools.builder.KnowledgeBuilderConfiguration;
import org.drools.builder.KnowledgeBuilderError;
import org.drools.builder.KnowledgeBuilderErrors;
import org.drools.builder.KnowledgeBuilderFactory;
import org.drools.builder.ResourceType;
import org.drools.compiler.PackageBuilderConfiguration;
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 DroolsTestFlowAndRule {
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");
//
start a new process instance'
Message
message = new Message();
message.setMessage("Hello
World Message");
message.setStatus(Message.HELLO);
ksession.insert(message);
ksession.startProcess("com.sample.bpmn.hello");
ksession.fireAllRules();
logger.close();
}
catch (Throwable t) {
t.printStackTrace();
}
}
private
static KnowledgeBase readKnowledgeBase() throws Exception {
KnowledgeBuilder
kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder();
kbuilder.add(ResourceFactory.newClassPathResource("hello.rf"),
ResourceType.DRF);
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;
}
}
}