当通过编译编译规则文件时, 目录必须是 src/main/resources/ , 否则会报WARN ,无法检测到错误:
WARN "No files found for KieBase defaultKieBase"
编程编译文件如下:
/**
* 尝试编译单条规则
*/
@Override
public RuleManagerResponse tryCompileSingleRules(RuleManagerRequest req) {
logger.info("开始试编译规则 :" + req.getContent());
RuleManagerResponse response = new RuleManagerResponse();
String baseName = TRY_COMPILE_BASE + req.getPackageName();
String sessionName = TRY_COMPILE_SESSION + req.getFileName();
KieServices ks = KieServices.Factory.get();
KieModuleModel kmodulemodel = ks.newKieModuleModel();
KieBaseModel kmodel = kmodulemodel.newKieBaseModel(baseName);
kmodel.addPackage(baseName);
kmodel.newKieSessionModel(sessionName);
KieFileSystem kf = ks.newKieFileSystem();
kf.write(TRY_COMPILE_PATH + baseName + "/" + sessionName, req.getContent());
String xml = kmodulemodel.toXML();
kf.writeKModuleXML(xml);
logger.debug(xml);
KieBuilder kb = ks.newKieBuilder(kf);
kb.buildAll();
if (kb.getResults().hasMessages(Message.Level.ERROR)) {
logger.info("试编译出现错误信息:" + kb.getResults().toString());
response.setError(kb.getResults().toString());
return response;
}
logger.info("试编译成功, 删除之。。。");
kmodulemodel.removeKieBaseModel(baseName);
xml = kmodulemodel.toXML();
kf.writeKModuleXML(xml);
kf.delete(TRY_COMPILE_PATH + baseName + "/" + sessionName);
KieBuilder kieBuilder = ks.newKieBuilder(kf);
kieBuilder.buildAll();
if (kieBuilder.getResults().hasMessages(Message.Level.ERROR)) {
logger.info("试编译成功后删除时 出现错误信息:" + kieBuilder.getResults().toString());
}
logger.info("试编译成功完成!");
return response;
}
网友评论