通过jar的形式读取规则文件
KieServices ks = KieServices.Factory.get(); KieRepository kr = ks.getRepository(); String drlFilePath = "deploy"; String path = System.getProperty("user.dir") + File.separatorChar + drlFilePath + File.separatorChar; KieModule kModule = kr.addKieModule(ks.getResources().newFileSystemResource(new File(path+"drools-example-1.0.0-SNAPSHOT.jar"))); KieModule kModule = kr.addKieModule(ks.getResources().newFileSystemResource(new File(path+getDrlFileName()))); KieContainer kContainer = ks.newKieContainer(kModule.getReleaseId()); KieSession ksession = kContainer.newKieSession("OutwardsPay"); /** *规则数据定义 */ GlobalDataBean.AllThrPayList = new ArrayList(){{add("a");add("b");add("c");}}; //支持银行列表 GlobalDataBean.BankPayMap.put("1000",new ArrayList(){{add("a");add("b");}}); GlobalDataBean.BankPayMap.put("1111",new ArrayList(){{add("a");add("c");}}); GlobalDataBean.SubjectPayMap.put("two",new ArrayList(){{add("a");add("c");}}); GlobalDataBean.ClosingPay = new ArrayList<String>(){{add("lianlian");}}; //设置具体数据 PaymentBean paymentBean = new PaymentBean(); paymentBean.setOrder_id("123456789"); paymentBean.setTo_bank_code("1000"); paymentBean.subject="two"; ksession.insert(paymentBean); //规则执行 int count = ksession.fireAllRules(); ksession.dispose(); //打印结果 System.out.println("主体"+paymentBean.getSubject()+"指定第三方支付渠道 :"+paymentBean.getSubjectPayList()); System.out.println("支持银行["+paymentBean.to_bank_code+"] 第三方支付渠道列表:"+paymentBean.getBankPayList()); System.out.println("停业的第三方支付渠道 :"+GlobalDataBean.ClosingPay); System.out.println("执行结果第三方支付渠道 :"+paymentBean.getThrPayList());
public class GlobalDataBean { public static Map<String,List> SubjectPayMap = new HashMap();// 主体指定第三方支付渠道 public static Map<String,List> BankPayMap = new HashMap();// 第三方支付渠道支持的银行,key 银行编号,list 第三方支付渠道列表 public static List<String> ClosingPay = new ArrayList<>(); public static List<String> AllThrPayList = new ArrayList();//全部第三方支付列表 }
public class PaymentBean { public String order_id; // required public String to_bank_code; // required public double trans_amount; // required public String subject; public List<String> subjectPayList = new ArrayList(); //主体指定第三方支付渠道 public List<String> bankPayList = new ArrayList();// 支持某个银行的第三方支付渠道 private List<String> thrPayList = GlobalDataBean.AllThrPayList; public String getOrder_id() { return order_id; } public void setOrder_id(String order_id) { this.order_id = order_id; } public String getTo_bank_code() { return to_bank_code; } public void setTo_bank_code(String to_bank_code) { this.to_bank_code = to_bank_code; } public double getTrans_amount() { return trans_amount; } public void setTrans_amount(double trans_amount) { this.trans_amount = trans_amount; } public String getSubject() { return subject; } public void setSubject(String subject) { this.subject = subject; } public List<String> getSubjectPayList() { return subjectPayList; } public void setSubjectPayList(List<String> subjectPayList) { this.subjectPayList = subjectPayList; // setThrPayList(subjectPayList); } public List<String> getBankPayList() { return bankPayList; } public void setBankPayList(List<String> bankPayList) { this.bankPayList = bankPayList; List<String> temp = new ArrayList<>(); //获取共同存在的支付渠道 for(String pay : bankPayList) { if(thrPayList.contains(pay)) { temp.add(pay); } } setThrPayList(temp); } public List<String> getThrPayList() { return thrPayList; } public void setThrPayList(List thrPayList) { this.thrPayList = thrPayList; } }
//==================================subject rule "payment_subject" // ruleflow-group "payment_bank" no-loop lock-on-active salience 100 when $p:PaymentBean() eval (GlobalDataBean.SubjectPayMap.containsKey($p.subject)) then modify($p){setSubjectPayList((List)GlobalDataBean.SubjectPayMap.get($p.subject))} end //==================================支持银行列表 rule "payment_bank" // ruleflow-group "payment_bank" no-loop lock-on-active salience 90 when $p:PaymentBean() if (GlobalDataBean.BankPayMap.containsKey($p.to_bank_code)) break [addList] then $p.bankPayList = []; //全部第三方支付渠道都不支持的银行 then[addList] modify($p){setBankPayList((List)GlobalDataBean.BankPayMap.get($p.to_bank_code))}; end //=====================================支持时间段(渠道) rule "payment_time" // ruleflow-group "payment_time" no-loop lock-on-active salience 80 when $p:PaymentBean() $closingPay:String() from GlobalDataBean.ClosingPay //循环处理停业第三方支付渠道 then modify($p){getThrPayList().remove($closingPay)}; end
<?xml version="1.0" encoding="UTF-8"?> <kmodule xmlns="http://jboss.org/kie/6.0.0/kmodule"> <kbase name="OutwardsPay" packages="com..payment"> <ksession name="OutwardsPay" /> </kbase> </kmodule>