規則引擎 Drools 6.4.0Final版本2
阿新 • • 發佈:2019-02-19
上一篇文章講了 規則引擎的簡單版本,可以說 是 drl配置檔案版本,現在將一下資料庫版本,這樣資料就可以共享,靈活性更加大一些吧,.
規則引擎版本1
其實原理和上一篇一樣,都是一樣的模式,今天簡單介紹一下,
private KieContainer loadKieContainerDao() throws IOException {
KieServices kieService = KieServices.Factory.get();
final KieRepository kieRepository = kieService.getRepository ();
kieRepository.addKieModule(kieRepository::getDefaultReleaseId);
KieFileSystem kfs = kieService.newKieFileSystem();
String[] files = listRulesDao();
for (String file : files) {
String myString = IOUtils.toString(new ByteArrayInputStream(file.getBytes()), "UTF-8" );
String path = "src/main/resources/xxx.drl";
kfs.write(path, myString );
}
KieBuilder kieBuilder = kieService.newKieBuilder(kfs);
kieBuilder.buildAll(); // kieModule is automatically deployed to KieRepository if successfully built.
return kieService.newKieContainer (kieRepository.getDefaultReleaseId());
}
本demo用string代替,讀者課自行處理,得到自己想要的類容;
private String[] listRulesDao() {
String s = "package drools;\n" +
"import com.sdingba.Drools.demo1.person;\n" +
"\n" +
"rule showdemo\n" +
"when\n" +
" $person : person(age>3)\n" +
"// System.out.println(\"xxxxx\");\n" +
"then\n" +
" $person.toString(\"sdfsdf\");\n" +
" $person.setName(\"xxxxxx\");\n" +
"end";
return new String[]{s};
}
@Bean
@ConditionalOnMissingBean(KieSession.class)
public KieSession kieSession() throws IOException {
return loadKieContainerDao().newKieSession();
}
其他原理同上一篇部落格一樣,以及測試程式碼,這兒不多說…