找不到默认的 KieSession
Cannot find a default KieSession
我正在尝试使用 KieModule 创建 jar。请看一下代码。
public static ReleaseId createKJarWithMultipleResources(String id, String[] resourceFiles) throws IOException {
KieServices ks = KieServices.Factory.get();
KieModuleModel kproj = ks.newKieModuleModel();
KieFileSystem kfs = ks.newKieFileSystem();
for (int i = 0; i < resourceFiles.length; i++) {
kfs.write("src/main/resources/" + id.replaceAll("\.", "/")
+ "/" + i + ".drl", resourceFiles[i]);
}
KieBaseModel kBase1 = kproj.newKieBaseModel(id)
.setEqualsBehavior(EqualityBehaviorOption.EQUALITY)
.setEventProcessingMode(EventProcessingOption.STREAM);
KieSessionModel ksession1 = kBase1
.newKieSessionModel(id + ".KSession1")
.setType(KieSessionModel.KieSessionType.STATEFUL)
.setClockType(ClockTypeOption.get("pseudo"));
kfs.writeKModuleXML(kproj.toXML());
KieBuilder kieBuilder = ks.newKieBuilder(kfs).buildAll();
Results results = kieBuilder.getResults();
if( results.hasMessages( org.kie.api.builder.Message.Level.ERROR ) ){
System.out.println( results.getMessages() );
throw new IllegalStateException( "### errors ###" );
}
KieModule kieModule = kieBuilder.getKieModule();
return kieModule.getReleaseId();
}
但是当我尝试使用以下代码使用 jar 时:
KieContainer kieContainer =
kieServices.newKieContainer(createKJarWithMultipleResources("1",
new String[]
{new String(Files.readAllBytes(Paths.get("path to drl file")))}
));
KieSession kSession = kieContainer.newKieSession();
我收到以下错误:
java.lang.RuntimeException: Cannot find a default KieSession
at org.drools.compiler.kie.builder.impl.KieContainerImpl.findKieSessionModel(KieContainerImpl.java:628)
at org.drools.compiler.kie.builder.impl.KieContainerImpl.newKieSession(KieContainerImpl.java:621)
at org.drools.compiler.kie.builder.impl.KieContainerImpl.newKieSession(KieContainerImpl.java:604)
at com.sample.DroolsTest.test2(DroolsTest.java:87)
我错过了什么吗?任何帮助将不胜感激。
问题已解决。我只需要更改代码中的以下几行:
KieSessionModel ksession1 = kBase1
.newKieSessionModel(id + ".KSession1")
.setType(KieSessionModel.KieSessionType.STATEFUL)
.setClockType(ClockTypeOption.get("pseudo"))
.setDefault(true);
注意 setDefault(true)
,原始代码中没有。
我在 src/main/resources/META-INF/ 文件夹中创建了 kmodule.xml,使用这样的 conetnet
<?xml version="1.0" encoding="UTF-8"?>
<kmodule xmlns="http://www.drools.org/xsd/kmodule"/>
它对我有用
没有这个文件,它失败了
我正在尝试使用 KieModule 创建 jar。请看一下代码。
public static ReleaseId createKJarWithMultipleResources(String id, String[] resourceFiles) throws IOException {
KieServices ks = KieServices.Factory.get();
KieModuleModel kproj = ks.newKieModuleModel();
KieFileSystem kfs = ks.newKieFileSystem();
for (int i = 0; i < resourceFiles.length; i++) {
kfs.write("src/main/resources/" + id.replaceAll("\.", "/")
+ "/" + i + ".drl", resourceFiles[i]);
}
KieBaseModel kBase1 = kproj.newKieBaseModel(id)
.setEqualsBehavior(EqualityBehaviorOption.EQUALITY)
.setEventProcessingMode(EventProcessingOption.STREAM);
KieSessionModel ksession1 = kBase1
.newKieSessionModel(id + ".KSession1")
.setType(KieSessionModel.KieSessionType.STATEFUL)
.setClockType(ClockTypeOption.get("pseudo"));
kfs.writeKModuleXML(kproj.toXML());
KieBuilder kieBuilder = ks.newKieBuilder(kfs).buildAll();
Results results = kieBuilder.getResults();
if( results.hasMessages( org.kie.api.builder.Message.Level.ERROR ) ){
System.out.println( results.getMessages() );
throw new IllegalStateException( "### errors ###" );
}
KieModule kieModule = kieBuilder.getKieModule();
return kieModule.getReleaseId();
}
但是当我尝试使用以下代码使用 jar 时:
KieContainer kieContainer =
kieServices.newKieContainer(createKJarWithMultipleResources("1",
new String[]
{new String(Files.readAllBytes(Paths.get("path to drl file")))}
));
KieSession kSession = kieContainer.newKieSession();
我收到以下错误:
java.lang.RuntimeException: Cannot find a default KieSession
at org.drools.compiler.kie.builder.impl.KieContainerImpl.findKieSessionModel(KieContainerImpl.java:628)
at org.drools.compiler.kie.builder.impl.KieContainerImpl.newKieSession(KieContainerImpl.java:621)
at org.drools.compiler.kie.builder.impl.KieContainerImpl.newKieSession(KieContainerImpl.java:604)
at com.sample.DroolsTest.test2(DroolsTest.java:87)
我错过了什么吗?任何帮助将不胜感激。
问题已解决。我只需要更改代码中的以下几行:
KieSessionModel ksession1 = kBase1
.newKieSessionModel(id + ".KSession1")
.setType(KieSessionModel.KieSessionType.STATEFUL)
.setClockType(ClockTypeOption.get("pseudo"))
.setDefault(true);
注意 setDefault(true)
,原始代码中没有。
我在 src/main/resources/META-INF/ 文件夹中创建了 kmodule.xml,使用这样的 conetnet
<?xml version="1.0" encoding="UTF-8"?>
<kmodule xmlns="http://www.drools.org/xsd/kmodule"/>
它对我有用
没有这个文件,它失败了