我正在尝试使用 Drools 决策表。我面临的问题是,drools 没有为我分配当前对象的变量。
我从流口水那里得到这个错误:
Drools 错误:无法解决规则编译错误患者
我的 excel 文件如下所示:
我的部分实验室代码如下所示:
DecisionTableConfiguration dtconf = KnowledgeBuilderFactory.newDecisionTableConfiguration();
dtconf.setInputType(DecisionTableInputType.XLS);
dtconf.setWorksheetName("Sheet1");
KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder();
Resource r = ResourceFactory.newClassPathResource("rules.xls");
kbuilder.add(r, ResourceType.DTABLE, dtconf);
SpreadsheetCompiler compiler = new SpreadsheetCompiler();
String drl = compiler.compile(r.getInputStream(), InputType.XLS);
System.out.println(drl);
if(kbuilder.hasErrors()) {
for(KnowledgeBuilderError err : kbuilder.getErrors()) {
System.out.println("Drools error: " + err.getMessage());
}
model.addAttribute("droolErrors", kbuilder.getErrors());
return "index";
}
KnowledgeBase knowledgeBase = KnowledgeBaseFactory.newKnowledgeBase();
knowledgeBase.addKnowledgePackages(kbuilder.getKnowledgePackages());
StatefulKnowledgeSession dsess = knowledgeBase.newStatefulKnowledgeSession();
for(Patient p : patients) {
dsess.insert(p);
}
model.addAttribute("factCount", dsess.getFactCount());
KnowledgeRuntimeLogger logger = KnowledgeRuntimeLoggerFactory.newFileLogger(dsess, "test");
int res = dsess.fireAllRules();
System.out.println(res);
dsess.dispose();
logger.close();
生成的 drl 如下所示:
package com.sorkmos.drools.domain;
//generated from Decision Table
import com.sorkmos.drools.domain.*;
// rule values at C11, header at C6
rule "Risks rules_11"
when
Patient(age == 75)
then
patient.setAge(22);
end
Drools error: Rule Compilation error patient cannot be resolved
很明显我需要设置“患者”变量。但我不知道怎么做。一个正确的 drl 版本应该是这样的:
package com.sorkmos.drools.domain;
//generated from Decision Table
import com.sorkmos.drools.domain.*;
// rule values at C11, header at C6
rule "Risks rules_11"
when
Patient(age == 75);
patient: Patient();
then
patient.setAge(22);
end