drools 手动创建kmoudle.xml文件
@Test public void test() { KieServices kieServices = KieServices.Factory.get(); KieResources resources = kieServices.getResources(); KieModuleModel kieModuleModel = kieServices.newKieModuleModel();//1 KieBaseModel baseModel = kieModuleModel.newKieBaseModel( "FileSystemKBase").addPackage("rules");//2 baseModel.newKieSessionModel("FileSystemKSession");//3 KieFileSystem fileSystem = kieServices.newKieFileSystem(); String xml = kieModuleModel.toXML(); System.out.println(xml);//4 fileSystem.writeKModuleXML(xml);//5 fileSystem.write("src/main/resources/rules/rule.drl", resources .newClassPathResource("kiefilesystem/KieFileSystemTest.drl"));//6 KieBuilder kb = kieServices.newKieBuilder(fileSystem); kb.buildAll();//7 if (kb.getResults().hasMessages(Level.ERROR)) { throw new RuntimeException("Build Errors:\n" + kb.getResults().toString()); } KieContainer kContainer = kieServices.newKieContainer(kieServices .getRepository().getDefaultReleaseId()); assertNotNull(kContainer.getKieBase("FileSystemKBase")); KieSession kSession = kContainer.newKieSession("FileSystemKSession"); kSession.fireAllRules(); } }
- 先创建KieModuleModel;
- 再创建KieBaseModel;
- 然后创建 KieSessionModel;
- 创建完成之后可以生产一个xml文件,就是kmodule.xml文件了;
- 将这个xml文件写入到KieFileSystem中;
- 然后将规则文件等写入到KieFileSystem中;
- 最后通过KieBuilder进行构建就将该kmodule加入到KieRepository中了。这样就将自定义的kmodule加入到引擎中了,就可以按照之前的方法进行使用了。