有几天我试图在Netbeans中运行Drools,但它不能像我想要的那样工作。我甚至试图让它作为一个maven项目工作,但是效果不好。我描述了我为创建项目所做的工作,希望有人可以给我一个提示。
首先,我需要它在没有maven的情况下工作,因为我只能不使用它。但在我将它放入真实项目之前我想测试它。
首先我创建一个新项目
在项目中创建lib文件夹并在其中放入以下jar:
使用此安装程序,我创建了类(在包中:Drools) DroolsMain (没有网络服务器和gui进行测试以加快调试速度)
public class DroolsMain {
private static KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder();
private static Collection<KnowledgePackage> pkgs;
private static KnowledgeBase kbase = KnowledgeBaseFactory.newKnowledgeBase();
private static StatefulKnowledgeSession ksession;
public static void main(final String[] args) {
init();
initMessageObject();
fireRules();
}
private static void init() {
String myRule = "import Drools.Message rule \"Hello World 2\" when message:Message (type==\"Test\") then System.out.println(\"Test, Drools!\"); end";
Resource myResource = ResourceFactory.newReaderResource((Reader) new StringReader(myRule));
kbuilder.add(myResource, ResourceType.DRL);
if(kbuilder.hasErrors()) {
System.out.println(kbuilder.getErrors().toString());
throw new RuntimeException("unable to compile dlr");
}
pkgs = kbuilder.getKnowledgePackages();
kbase.addKnowledgePackages(pkgs);
ksession = kbase.newStatefulKnowledgeSession();
}
private static void fireRules() {
ksession.fireAllRules();
}
private static void initMessageObject() {
Message msg = new Message();
msg.setType("Test");
ksession.insert(msg);
}
}
以及上面使用的消息类
public class Message {
private String type;
private String message;
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}
}
我现在可以运行该代码并返回一条消息。
现在我想和Kie一起尝试......
我只是评论 DroolsMain main()方法中的方法。 并将Kie方法放入当前的Drools文档中。 172-174那里:
private static void kieTest() {
KieServices kieServices = KieServices.Factory.get();
KieContainer kContainer = kieServices.getKieClasspathContainer();
StatelessKieSession kSession = kContainer.newStatelessKieSession();
Applicant bob = new Applicant("Mr. Bob", 16);
//assertTrue(bob.isValid());
kSession.execute(bob);
//assertFalse(bob.isValid());
}
我没有把Applicant.class放在这里,因为它只是一个有3个属性的bean。 我还创建了DRL applicant.drl 文件:
package Drools "Is of valid age"
import Drools.Applicant
when
$a : Applicant(age < 18)
then
$a.setValid(false);
end;
所以这显然不起作用,因为Kie有maven依赖,所以我尝试了这个: 1.添加目录: - DroolsTest /资源/ - DroolsTest / resources / META-INF - DroolsTest / resources / META-INF / maven - DroolsTest / resources / Drools 2.创建DroolsTest / resources / META-INFkmodule.xml
内容:
<?xml version="1.0" encoding="UTF-8"?>
<kmodule xmlns="http://www.drools.org/xsd/kmodule">
<kbase name="Drools" packages="Drools">
<ksession name="ksession-drools" />
</kbase>
</kmodule>
创建DroolsTest / resources / META-INF / maven / pom.properties 内容:( groupId我的包名),(artifactId我的项目名称)
的groupId = Drools的 的artifactId = DroolsTest 版本= 1
编译它会抛出RuntimeException:
Exception in thread "main" java.lang.RuntimeException: Cannot find a default KieSession
at org.drools.compiler.kie.builder.impl.KieContainerImpl.findKieSessionModel(KieContainerImpl.java:555)
at org.drools.compiler.kie.builder.impl.KieContainerImpl.newKieSession(KieContainerImpl.java:548)
at org.drools.compiler.kie.builder.impl.KieContainerImpl.newKieSession(KieContainerImpl.java:531)
at Drools.DroolsMain.kieTest(DroolsMain.java:43)
at Drools.DroolsMain.main(DroolsMain.java:52)
C:\Users\...\AppData\Local\NetBeans\Cache\8.1\executor-snippets\run.xml:53: Java returned: 1
BUILD FAILED (total time: 3 seconds)
是否有解决方案让Kiew在没有maven的情况下工作,或者我可以在没有它的情况下读取文件吗?
更新:
我尝试了launes解决方案:
import java.io.File;
import org.kie.api.KieBase;
import org.kie.api.KieServices;
import org.kie.api.builder.KieBuilder;
import org.kie.api.builder.KieFileSystem;
import org.kie.api.builder.Results;
import org.kie.api.io.Resource;
import org.kie.api.runtime.KieContainer;
import org.kie.api.runtime.KieSession;
public class DroolsMain {
private void ntry() {
KieServices kieServices = KieServices.Factory.get();
KieFileSystem kfs = kieServices.newKieFileSystem();
File rule = new File("src/main/resources/Drools/applicant.drl");
Resource res = kieServices.getResources().newFileSystemResource(rule);
kfs.write(res);
KieBuilder kieBuilder = kieServices.newKieBuilder(kfs).buildAll();
Results results = kieBuilder.getResults();
System.out.println("---Messages---");
System.out.println(results.getMessages());
KieContainer kieContainer = kieServices.newKieContainer(kieServices.getRepository().getDefaultReleaseId());
KieBase kieBase = kieContainer.getKieBase();
KieSession kieSession = kieBase.newKieSession();
}
public static void main(final String[] args) {
DroolsMain dm = new DroolsMain();
dm.ntry();
}
}
我还添加了drools发行版中的所有罐子。现在它编译并没有错误
答案 0 :(得分:2)
我就是这样做的,没有maven,没有XML:
public void build() throws Exception {
KieServices kieServices = KieServices.Factory.get();
KieFileSystem kfs = kieServices.newKieFileSystem();
//...
Resource res = ...;
kfs.write( "src/main/resources/simple.drl", res );
KieBuilder kieBuilder = kieServices.newKieBuilder( kfs ).buildAll();
Results results = kieBuilder.getResults();
if( results.hasMessages( Message.Level.ERROR ) ){
System.out.println( results.getMessages() );
throw new IllegalStateException( "### errors ###" );
}
KieContainer kieContainer =
kieServices.newKieContainer( kieServices.getRepository().getDefaultReleaseId() );
KieBase kieBase = kieContainer.getKieBase();
kieSession = kieBase.newKieSession();
}
我通常使用资源,例如
kieServices.getResources().newInputStreamResource( fis )
因为从文件中读取更灵活。