这是我用于执行 DRL 和规则流的第一个 drools 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;
/**
* 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;
}
}
}
这是相关的规则流文件(工作正常)
<?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>
这是两个 DRL 文件(工作正常)
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
--第二个 DRL 文件
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
--这是上面一组代码的输出:
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
和日志:
现在的问题:我刚刚将 DroolsTest.java 复制到 DroolsTest1.java 并将文件保存在同一个文件夹中。一切都是一样的,但是当我将 DroolsTest1.java 文件作为 java 应用程序执行时,我在控制台中看不到任何输出。我无法理解这背后的任何原因。这是 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;
}
}
}
这是上述文件的输出:
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.
和日志:
可能是什么原因?请帮忙。