我正在尝试使用 kie api 6
读取带有规则的 .xlsx
文件。
但是我得到了这个异常(exception)
java.lang.RuntimeException: Resource does not have neither a source nor a target path. Impossible to add it to the bundle. P
lease set either the source or target name of the resource before adding it.null
at org.drools.compiler.kie.builder.impl.KieFileSystemImpl.write(KieFileSystemImpl.java:83)
at com.project.watch.validator.RuleValidator.isValidRuleKieNew(RuleValidator.java:71)
at com.project.watch.validator.RuleValidator.isValidRule(RuleValidator.java:37)
at com.project.watch.core.WatchDir$ValidateFileRunnable.run(WatchDir.java:177)
at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:471)
at java.util.concurrent.FutureTask.runAndReset(FutureTask.java:304)
at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.access$301(ScheduledThreadPoolExecutor.java:
178)
at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.run(ScheduledThreadPoolExecutor.java:293)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
at java.lang.Thread.run(Thread.java:745)
代码如下所示
try {
KieServices ks = KieServices.Factory.get();
KieFileSystem kfs = ks.newKieFileSystem();
kfs.write(ks.getResources().newInputStreamResource(Files.newInputStream(p, StandardOpenOption.READ)));
KieBuilder kb = ks.newKieBuilder(kfs);
if (kb.getResults().getMessages(Level.ERROR).size() != 0) {
logger.lifecycle("Invalid file: {}.", kb.getResults().getMessages());
return false;
}
} catch (Exception e) {
logger.lifecycle("UNEXPECTED ERROR DURING VALIDATION -------------- ", e);
return false;
}
logger.lifecycle("Valid file: {}.", p);
return true;
}
顺便说一下,kfs.write
失败了。
我还通过在控制台中显示文件的路径来检查文件的路径
Attempt to process rule file: D:\PROJECT_FOLDER\project\src\testInteg\resources\rules\rule.xlsx
这是一条正确的路径。
有什么问题?我正在使用 6.3 Final
版本。
最佳答案
我推荐这个口头禅,假设您的String p
包含如下所示的路径名:
FileInputStream fis = new FileInputStream( p );
kfs.write( "src/main/resources/rule.xslx",
kieServices.getResources().newInputStreamResource( fis ) );
还有一些其他形式可以写入
到KieFileSystem
,但我发现这很容易使用。您可以使用 Java 的 API 获取路径名,从 p
派生出 rule.xslx
。
关于drools - 资源既没有源路径也没有目标路径。无法将其添加到 bundle 中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34016848/