Here is my 1st drools java file used for executing DRL and rule flow(This is working fine)
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.startProcess("test.new");
ksession.fireAllRules();
logger.close();
} catch (Throwable t) {
t.printStackTrace();
}
}
private static KnowledgeBase readKnowledgeBase() throws Exception {
KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder();
kbuilder.add(ResourceFactory.newClassPathResource("test1.drl"), ResourceType.DRL);
kbuilder.add(ResourceFactory.newClassPathResource("test2.drl"), ResourceType.DRL);
kbuilder.add(ResourceFactory.newClassPathResource("newruleflow.rf"), ResourceType.BPMN2);
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;
}
}
}
This is related rule flow file(working fine)
<?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="test.new" package-name="com.sample">
<header>
</header>
<nodes>
<start id="1" name="Start" x="100" y="100" width="48" height="48" />
<ruleSet id="2" name="Rule" x="273" y="110" width="139" height="26" ruleFlowGroup="t1" />
<end id="4" name="End" x="117" y="200" width="27" height="20" />
<ruleSet id="5" name="Rule" x="468" y="128" width="80" height="48" ruleFlowGroup="t2" />
</nodes>
<connections>
<connection from="1" to="2" />
<connection from="5" to="4" />
<connection from="2" to="5" />
</connections>
</process>
These are two DRL file(working fine)
package com.sample
import com.sample.DroolsTest.Message;
rule "Hello World"
ruleflow-group "t1"
when
m : Message( status == Message.HELLO, myMessage : message )
then
System.out.println( myMessage );
m.setMessage( "Goodbye cruel world" );
m.setStatus( Message.GOODBYE );
System.out.println( "file1" );
update( m );
end
rule "GoodBye"
ruleflow-group "t1"
when
Message( status == Message.GOODBYE, myMessage : message )
then
System.out.println( myMessage );
System.out.println( "file1" );
end
--2nd DRL file
package com.sample
import com.sample.DroolsTest.Message;
rule "Hello World2"
ruleflow-group "t2"
when
m : Message( status == Message.HELLO, myMessage : message )
then
System.out.println( myMessage );
m.setMessage( "Goodbye cruel world" );
m.setStatus( Message.GOODBYE );
System.out.println( "file2" );
update( m );
end
rule "GoodBye2"
ruleflow-group "t2"
when
Message( status == Message.GOODBYE, myMessage : message )
then
System.out.println( myMessage );
System.out.println( "file2" );
end
--Here is the output of above set of codes:
SLF4J: Failed to load class "org.slf4j.impl.StaticLoggerBinder".
SLF4J: Defaulting to no-operation (NOP) logger implementation
SLF4J: See http://www.slf4j.org/codes.html#StaticLoggerBinder for further details.
Hello World
file1
Goodbye cruel world
file1
Goodbye cruel world
file2
and the logs:
NOW THE PROBLEM: I have just duplicated DroolsTest.java to DroolsTest1.java and kept the file in same folder. Everything is same,but when I am executing the DroolsTest1.java file as java application then I cannot see any output in console..I am not able to understand any reason behind that. Here is the DroolsTest1.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;
import com.sample.DroolsTest.Message;
/**
* This is a sample class to launch a rule.
*/
public class DroolsTest1 {
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.startProcess("test.new");
ksession.fireAllRules();
logger.close();
} catch (Throwable t) {
t.printStackTrace();
}
}
private static KnowledgeBase readKnowledgeBase() throws Exception {
KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder();
kbuilder.add(ResourceFactory.newClassPathResource("test1.drl"), ResourceType.DRL);
kbuilder.add(ResourceFactory.newClassPathResource("test2.drl"), ResourceType.DRL);
kbuilder.add(ResourceFactory.newClassPathResource("newruleflow.rf"), ResourceType.BPMN2);
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;
}
}
}
Here is the output of above file:
SLF4J: Failed to load class "org.slf4j.impl.StaticLoggerBinder".
SLF4J: Defaulting to no-operation (NOP) logger implementation
SLF4J: See http://www.slf4j.org/codes.html#StaticLoggerBinder for further details.
and logs:
what could be the reason? please help.
I found the answers:
DroolsTest1.java is importing incorrect import com.sample.DroolsTest.Message;
it should be
import com.sample.DroolsTest1.Message;
So no rule was firing and hence no answer