Drools java.lang.RuntimeException:意外的全局[结果]
我有一个类,它读取规则文件,插入事实并运行规则Drools java.lang.RuntimeException:意外的全局[结果],drools,kie,Drools,Kie,我有一个类,它读取规则文件,插入事实并运行规则 public class RuleRunner { private KieServices kieServices = KieServices.Factory.get(); public enum RuleType { XLS, DRL; } private void prepareSession(String ruleResource, RuleType type) {
public class RuleRunner {
private KieServices kieServices = KieServices.Factory.get();
public enum RuleType {
XLS,
DRL;
}
private void prepareSession(String ruleResource, RuleType type) {
KieFileSystem kieFileSystem = kieServices.newKieFileSystem();
Resource resource = kieServices.getResources().newClassPathResource(ruleResource);
switch(type) {
case XLS: {
resource.setResourceType(ResourceType.DTABLE);
break;
}
case DRL: {
resource.setResourceType(ResourceType.DRL);
break;
}
}
kieFileSystem.write(resource);
KieBuilder kieBuilder = kieServices.newKieBuilder(kieFileSystem);
kieBuilder.buildAll();
if (hasErrors(kieBuilder)) {
System.out.println("Failed to build!");
return;
}
}
private boolean hasErrors(KieBuilder builder) {
if (builder.getResults().getMessages().size() > 0) {
return true;
}
return false;
}
public void runRules(Object[] facts, GlobalVariable[] variables, String ruleResource, RuleType type) {
prepareSession(ruleResource, type);
KieContainer kieContainer = kieServices.newKieContainer(kieServices.getRepository().getDefaultReleaseId());
KieSession kieSession = kieContainer.newKieSession();
for (GlobalVariable variable: variables) {
kieSession.setGlobal(variable.getVariableName(), variable);
}
for(Object fact: facts) {
kieSession.insert(fact);
}
kieSession.fireAllRules();
kieSession.dispose();
}
}
我有这个规则
package com.pack.drools.apps;
import com.pack.drools.apps.domain.Person;
import com.pack.drools.apps.domain.GlobalVariable;
global GlobalVariable result
rule "if person has less that 10 cash then hes poor"
when
$person:Person(cash < 10)
then
result.setResult(-1);
end
rule "if person has more than 90 cash then hes rich"
when
$person:Person(cash > 90)
then
result.setResult(-2);
end
rule "if person has more than 10 and less than 90 then hes average"
when
$person:Person(cash >= 10 && cash <= 90)
then
result.setResult(-3);
end
我的日志看起来像这样
10:13:00.974 [main] INFO o.d.c.k.b.impl.KieRepositoryImpl - KieModule was added: MemoryKieModule[releaseId=org.default:arti
fact:1.0.0-SNAPSHOT]
10:13:00.982 [main] INFO o.d.c.k.b.impl.ClasspathKieProject - Found kmodule: file:/D:/workTestProjects/simpleDroolsApps/sda
-core/build/resources/main/META-INF/kmodule.xml
10:13:00.982 [main] DEBUG o.d.c.k.b.impl.ClasspathKieProject - KieModule URL type=file url=/D:/workTestProjects/simpleDrools
Apps/sda-core/build/resources/main
10:13:01.026 [main] WARN o.d.c.k.b.impl.ClasspathKieProject - Unable to find pom.properties in /D:/workTestProjects/simpleD
roolsApps/sda-core/build/resources/main
10:13:01.027 [main] WARN o.d.c.k.b.impl.ClasspathKieProject - As folder project tried to fall back to pom.xml, but could no
t find one for null
10:13:01.027 [main] WARN o.d.c.k.b.impl.ClasspathKieProject - Unable to load pom.properties from/D:/workTestProjects/simple
DroolsApps/sda-core/build/resources/main
10:13:01.027 [main] WARN o.d.c.k.b.impl.ClasspathKieProject - Cannot find maven pom properties for this project. Using the
container's default ReleaseId
10:13:01.027 [main] DEBUG o.d.c.k.b.impl.ClasspathKieProject - Discovered classpath module org.default:artifact:1.0.0-SNAPSH
OT
10:13:01.028 [main] INFO o.d.c.k.b.impl.KieRepositoryImpl - KieModule was added: FileKieModule[releaseId=org.default:artifa
ct:1.0.0-SNAPSHOT,file=D:\workTestProjects\simpleDroolsApps\sda-core\build\resources\main]
10:13:01.035 [main] WARN o.d.c.k.b.impl.AbstractKieModule - No files found for KieBase defaultKieBase, searching folder D:\
workTestProjects\simpleDroolsApps\sda-core\build\resources\main
10:13:01.131 [main] DEBUG o.drools.core.impl.KnowledgeBaseImpl - Starting Engine in PHREAK mode
Exception in thread "main" java.lang.RuntimeException: Unexpected global [result]
at org.drools.core.impl.StatefulKnowledgeSessionImpl.setGlobal(StatefulKnowledgeSessionImpl.java:1163)
at pack.rup.drools.apps.core.RuleRunner.runRules(RuleRunner.java:57)
at pack.rup.drools.apps.Main.main(Main.java:27)
:sda-core:run FAILED
在将DRL或XLS资源写入文件系统时,似乎必须使用特定的目录。试一试
String filename = ...; // code that set filename to (e.g.) rule.drl
kieFileSystem.write( "src/main/resources/" + filename, resource );
此外,在您的DRL中,您有
import com.pack.drools.apps.domain....
而在Main.java中有
import pack.rup.drools.apps.domain....
这些进口产品应来自同一包装
编辑以测试会话中的全局变量:
Globals globals = kieSession.getGlobals();
System.out.println( globals.getGlobalKeys() );
在将DRL或XLS资源写入文件系统时,似乎必须使用特定的目录。试一试
String filename = ...; // code that set filename to (e.g.) rule.drl
kieFileSystem.write( "src/main/resources/" + filename, resource );
此外,在您的DRL中,您有
import com.pack.drools.apps.domain....
而在Main.java中有
import pack.rup.drools.apps.domain....
这些进口产品应来自同一包装
编辑以测试会话中的全局变量:
Globals globals = kieSession.getGlobals();
System.out.println( globals.getGlobalKeys() );
我们和我的同事花了2天时间,终于发现当您的DRL由于编译错误而为空时会发生“意外全局”错误,这就是为什么addGlobal()找不到任何全局声明 一旦DRL编译器不对错误抛出任何异常,您可以使用以下方法自己检查:
if (kieBuilder.hasErrors()) {
System.out.print( kieBuilder.getErrors() );
}
我们和我的同事花了2天时间,终于发现当您的DRL由于编译错误而为空时会发生“意外全局”错误,这就是为什么addGlobal()找不到任何全局声明 一旦DRL编译器不对错误抛出任何异常,您可以使用以下方法自己检查:
if (kieBuilder.hasErrors()) {
System.out.print( kieBuilder.getErrors() );
}
如果插入全局变量,则必须使用它。例如,如果您有:
ArrayList<Thing> myThings = new ArrayList<Thing>();
kSession.setGlobal("myThings", myThings);
如果插入全局变量,则必须使用它。例如,如果您有:
ArrayList<Thing> myThings = new ArrayList<Thing>();
kSession.setGlobal("myThings", myThings);
在调试中,我发现它在
kieSession.setGlobal(variable.getVariableName(),variable)上失败代码>line@user1432980当然如果您使用的变量名与DRL中的任何全局变量都不匹配,则会出现此错误。如果您有一个“空”会话,即由于未正确存储资源而没有全局变量和规则的会话,则必然会发生此错误。-你听从我的建议了吗?是的。软件包问题只是一种类型,因为我为这个问题更改了它。指定写入位置的路径没有帮助。好的,请在尝试设置并告诉我输出之前,将这两行代码添加到代码中。事实上,问题已经解决。我复制了您的路径-src/main/resource
,但实际上我将xls
保存在该文件的子包中,当我指定它时,它开始正常工作src/main/resources/com/pack/drools/apps
。在调试中,我发现它在kieSession.setGlobal(variable.getVariableName(),variable)上失败代码>line@user1432980当然如果您使用的变量名与DRL中的任何全局变量都不匹配,则会出现此错误。如果您有一个“空”会话,即由于未正确存储资源而没有全局变量和规则的会话,则必然会发生此错误。-你听从我的建议了吗?是的。软件包问题只是一种类型,因为我为这个问题更改了它。指定写入位置的路径没有帮助。好的,请在尝试设置并告诉我输出之前,将这两行代码添加到代码中。事实上,问题已经解决。我复制了您的路径-src/main/resource
,但实际上我将xls
保存在该文件的子包中,当我指定它时,它开始正常工作<代码>src/main/resources/com/pack/drools/apps
。