Drools6.4动态加载规则(模板的简单应用)

阅读更多
  1. package com.sample; import java.io.Serializable; public class Message implements Serializable{ public static final int HELLO = 0; public static final int GOODBYE = 1; private String message; private int status; public String getMessage() { return message; } public int getStatus() { return status; } public void setMessage(String message) { this.message = message; } public void setStatus(int status) { this.status = status; } }
  2. package com.sample; import java.io.Serializable; public class Message implements Serializable{ public static final int HELLO = 0; public static final int GOODBYE = 1; private String message; private int status; public String getMessage() { return message; } public int getStatus() { return status; } public void setMessage(String message) { this.message = message; } public void setStatus(int status) { this.status = status; } }
  3. public class DataProviderCompilerTest {
  4. public static void main(String[] args) {  
  5. ArrayList rows = new ArrayList();  
  6.        rows.add(new String[] { "1", "status == 0" });  
  7.  
  8.        MyDataProvider tdp = new MyDataProvider(rows);  
  9.        DataProviderCompiler converter = new DataProviderCompiler();
  10.        
  11.         String drl = converter.compile(tdp, "/rules/rule_template_1.drl");  
  12. ArrayList rowss = new ArrayList();  
  13. rowss.add(new String[] { "2", "status == 1" });  
  14.     tdp = new MyDataProvider(rowss);  
  15. String drl2 = converter.compile(tdp, "/rules/rule_template_2.drl");  
  16.        System.out.println(drl);  
  17.        System.out.println(drl2);  
  18.        
  19.        KieServices kieServices = KieServices.Factory.get();  
  20.        KieResources resources = kieServices.getResources();  
  21.        KieModuleModel kieModuleModel = kieServices.newKieModuleModel();//1  
  22.  
  23.        KieBaseModel baseModel = kieModuleModel.newKieBaseModel("FileSystemKBase").addPackage("rules");//2
  24.        baseModel.newKieSessionModel("FileSystemKSession");//3
  25.        KieFileSystem fileSystem = kieServices.newKieFileSystem();
  26.        String xml = kieModuleModel.toXML();  
  27.        System.out.println(xml);//4  
  28.        fileSystem.writeKModuleXML(xml);//5  
  29.        String path = DataProviderCompilerTest.class.getClass().getResource("/").getPath();  
  30.        fileSystem.write("src/main/resources/rules/rule1.drl", drl);  
  31.        fileSystem.write("src/main/resources/rules/rule2.drl", drl2);  
  32.        
  33.        KieBuilder kb = kieServices.newKieBuilder(fileSystem);  
  34.        kb.buildAll();//7  
  35.        
  36.        if (kb.getResults().hasMessages(Level.ERROR)) {  
  37.            throw new RuntimeException("Build Errors:\n" + kb.getResults().toString());  
  38.        }  
  39.        KieContainer kContainer = kieServices.newKieContainer(kieServices.getRepository().getDefaultReleaseId());  
  40.  
  41.        KieSession kSession = kContainer.newKieSession("FileSystemKSession");  
  42.  
  43.        Message message = new Message();  
  44.        message.setMessage("Hello World");  
  45.        message.setStatus(Message.GOODBYE);  
  46.  
  47.        kSession.insert(message);  
  48.        kSession.fireAllRules();  
  49.        kSession.dispose();  
  50.   //   updateDrl(kieServices,kieModuleModel,kb, baseModel, kContainer,fileSystem);
  51. //test();
  52. //test();
  53.     }
  54. }

你可能感兴趣的:(drools)