在Drools中實(shí)現(xiàn)規(guī)則的動態(tài)更新可以通過以下步驟:
示例代碼如下:
KieServices kieServices = KieServices.Factory.get();
KieFileSystem kfs = kieServices.newKieFileSystem();
kfs.write(ResourceFactory.newClassPathResource("rules.drl"));
KieBuilder kieBuilder = kieServices.newKieBuilder(kfs).buildAll();
KieContainer kieContainer = kieServices.newKieContainer(kieServices.getRepository().getDefaultReleaseId());
KieBase kieBase = kieContainer.getKieBase();
KieSession kieSession = kieBase.newKieSession();
// 執(zhí)行規(guī)則
kieSession.fireAllRules();
// 更新規(guī)則文件
kfs.delete("rules.drl");
kfs.write(ResourceFactory.newClassPathResource("new_rules.drl"));
kieBuilder = kieServices.newKieBuilder(kfs).buildAll();
kieContainer.updateToVersion(kieServices.getRepository().getDefaultReleaseId());
kieBase = kieContainer.getKieBase();
kieSession = kieBase.newKieSession();
// 重新執(zhí)行規(guī)則
kieSession.fireAllRules();
通過以上步驟,可以實(shí)現(xiàn)在Java中動態(tài)更新Drools規(guī)則。