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();
    }
}

 

 

  1. 先创建KieModuleModel;
  2. 再创建KieBaseModel;
  3. 然后创建 KieSessionModel;
  4. 创建完成之后可以生产一个xml文件,就是kmodule.xml文件了;
  5. 将这个xml文件写入到KieFileSystem中;
  6. 然后将规则文件等写入到KieFileSystem中;
  7. 最后通过KieBuilder进行构建就将该kmodule加入到KieRepository中了。这样就将自定义的kmodule加入到引擎中了,就可以按照之前的方法进行使用了。
posted @ 2018-03-07 11:22  lgp20151222  阅读(1494)  评论(0编辑  收藏  举报