最近在研究规则引擎的东东。网上找了下,单说drools的文章不少,单独讲JSR94的文章并不多见。现把drools的JSR写法整理发上来,供自己记录,供大家参考。
调用入口:JSR94Sample.java
package info.syang.stu1; import java.io.IOException; import java.io.InputStreamReader; import java.io.Reader; import java.rmi.RemoteException; import java.util.Arrays; import java.util.HashMap; import javax.rules.ConfigurationException; import javax.rules.InvalidRuleSessionException; import javax.rules.RuleExecutionSetNotFoundException; import javax.rules.RuleRuntime; import javax.rules.RuleServiceProvider; import javax.rules.RuleServiceProviderManager; import javax.rules.RuleSessionCreateException; import javax.rules.RuleSessionTypeUnsupportedException; import javax.rules.StatelessRuleSession; import javax.rules.admin.LocalRuleExecutionSetProvider; import javax.rules.admin.RuleAdministrator; import javax.rules.admin.RuleExecutionSet; import javax.rules.admin.RuleExecutionSetCreateException; import javax.rules.admin.RuleExecutionSetRegisterException; import org.drools.jsr94.rules.RuleServiceProviderImpl; /** * Drools的JSR94实现形式 * @author yangsong * */ public class JSR94Sample { private RuleServiceProvider ruleProvider; public void run(){ String uri = RuleServiceProviderImpl.RULE_SERVICE_PROVIDER; try{ //1.注册ruleProvider,并且从RuleServiceProviderManager获取ruleProvider RuleServiceProviderManager.registerRuleServiceProvider(uri, RuleServiceProviderImpl.class); ruleProvider = RuleServiceProviderManager.getRuleServiceProvider(uri); HashMap<String,String> properties = new HashMap<String,String>(); //2.获取RuleAdministrator实例,获取RuleExectuionSetProvider RuleAdministrator admin = ruleProvider.getRuleAdministrator(); LocalRuleExecutionSetProvider ruleExecutionSetProvider = admin.getLocalRuleExecutionSetProvider(properties); //3.创建RuleExecutionSet Reader reader = new InputStreamReader(getClass().getClassLoader().getResourceAsStream("info/syang/stu1/addpoint.drl")); RuleExecutionSet reSet = ruleExecutionSetProvider.createRuleExecutionSet(reader, properties); //4.注册RuleExecutionSet admin.registerRuleExecutionSet("mysample",reSet,properties); //5.获取RuleRuntime, 创建会话 RuleRuntime runtime = ruleProvider.getRuleRuntime(); StatelessRuleSession ruleSession = (StatelessRuleSession)runtime.createRuleSession("mysample",null,RuleRuntime.STATELESS_SESSION_TYPE); //6.运行实例 PointDomain pointDomain = new PointDomain(); fillPointDomain(pointDomain); ruleSession.executeRules(Arrays.asList(pointDomain)); // @SuppressWarnings("unchecked") // List<PointDomain> results = (List<PointDomain>)ruleSession.executeRules(Arrays.asList(pointDomain)); System.out.println("执行完毕BillThisMonth:"+ pointDomain.getBillThisMonth()); System.out.println("执行完毕BuyMoney:" + pointDomain.getBuyMoney()); System.out.println("执行完毕BuyNums:" + pointDomain.getBuyNums()); System.out.println("执行完毕规则引擎决定发送积分:" + pointDomain.getPoint()); }catch(ConfigurationException e){ e.printStackTrace(); } catch (RemoteException e) { e.printStackTrace(); } catch (RuleExecutionSetCreateException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } catch (RuleExecutionSetRegisterException e) { e.printStackTrace(); } catch (RuleSessionTypeUnsupportedException e) { e.printStackTrace(); } catch (RuleSessionCreateException e) { e.printStackTrace(); } catch (RuleExecutionSetNotFoundException e) { e.printStackTrace(); } catch (InvalidRuleSessionException e) { e.printStackTrace(); } } private void fillPointDomain(PointDomain pointDomain){ pointDomain.setUserName("hello kity"); pointDomain.setBackMondy(100d); pointDomain.setBuyMoney(500d); pointDomain.setBackNums(1); pointDomain.setBuyNums(5); pointDomain.setBillThisMonth(5); pointDomain.setBirthDay(true); pointDomain.setPoint(0l); } public static void main(String...strings ){ new JSR94Sample().run(); } }
积分计算对象:PointDomain.java
package info.syang.stu1; /** * 积分计算对象 * @author yangsong * */ public class PointDomain { private String userName; // 是否当日生日 private boolean birthDay; // 增加积分数目 private long point; // 当月购物次数 private int buyNums; // 当月退货次数 private int backNums; // 当月购物总金额 private double buyMoney; // 当月退货总金额 private double backMondy; // 当月信用卡还款次数 private int billThisMonth; /** * 记录积分发送流水,防止重复发放 * @param userName * @param type */ public void recordPointLog(String userName, String type) { System.out.println("增加对" + userName + "的类型为" + type + "的积分操作记录."); } public String getUserName() { return userName; } public void setUserName(String userName) { this.userName = userName; } public boolean isBirthDay() { return birthDay; } public void setBirthDay(boolean birthDay) { this.birthDay = birthDay; } public long getPoint() { return point; } public void setPoint(long point) { this.point = point; } public int getBuyNums() { return buyNums; } public void setBuyNums(int buyNums) { this.buyNums = buyNums; } public int getBackNums() { return backNums; } public void setBackNums(int backNums) { this.backNums = backNums; } public double getBuyMoney() { return buyMoney; } public void setBuyMoney(double buyMoney) { this.buyMoney = buyMoney; } public double getBackMondy() { return backMondy; } public void setBackMondy(double backMondy) { this.backMondy = backMondy; } public int getBillThisMonth() { return billThisMonth; } public void setBillThisMonth(int billThisMonth) { this.billThisMonth = billThisMonth; } }
规则文件:addpoint.drl
//created on: 2014-1-8 import info.syang.stu1.PointDomain; rule birthdayPoint // 过生日,则加10分,并且将当月交易比数翻倍后再计算积分 salience 100 lock-on-active true when $pointDomain : PointDomain(birthDay == true) then $pointDomain.setPoint($pointDomain.getPoint()+10); $pointDomain.setBuyNums($pointDomain.getBuyNums()*2); $pointDomain.setBuyMoney($pointDomain.getBuyMoney()*2); $pointDomain.setBillThisMonth($pointDomain.getBillThisMonth()*2); $pointDomain.recordPointLog($pointDomain.getUserName(),"birthdayPoint"); end rule billThisMonthPoint // 2011-01-08 - 2011-08-08每月信用卡还款3次以上,每满3笔赠送30分 salience 99 lock-on-active true when $pointDomain : PointDomain(billThisMonth >= 3) then $pointDomain.setPoint($pointDomain.getPoint()+$pointDomain.getBillThisMonth()/3*30); $pointDomain.recordPointLog($pointDomain.getUserName(),"billThisMonthPoint"); end rule buyMoneyPoint // 当月购物总金额100以上,每100元赠送10分 salience 98 lock-on-active true when $pointDomain : PointDomain(buyMoney >= 100) then $pointDomain.setPoint($pointDomain.getPoint()+ (int)$pointDomain.getBuyMoney()/100 * 10); $pointDomain.recordPointLog($pointDomain.getUserName(),"buyMoneyPoint"); end rule buyNumsPoint // 当月购物次数5次以上,每五次赠送50分 salience 97 lock-on-active true when $pointDomain : PointDomain(buyNums >= 5) then $pointDomain.setPoint($pointDomain.getPoint()+$pointDomain.getBuyNums()/5 * 50); $pointDomain.recordPointLog($pointDomain.getUserName(),"buyNumsPoint"); end rule allFitPoint // 特别的,如果全部满足了要求,则额外奖励100分 salience 96 lock-on-active true when $pointDomain:PointDomain(buyNums >= 5 && billThisMonth >= 3 && buyMoney >= 100) then $pointDomain.setPoint($pointDomain.getPoint()+ 100); $pointDomain.recordPointLog($pointDomain.getUserName(),"allFitPoint"); end
参考文章:
http://thinkinside.tk/2012/12/07/jsr94.html
http://blog.csdn.net/quzishen/article/details/6163012
我这里收集了一些drools的学习材料,贴上来供有需要的同学参考
http://www.360doc.com/content/06/1010/16/11884_227032.shtml
http://www.360doc.com/content/10/0301/11/871184_17184008.shtml
http://liureying.blog.163.com/blog/static/6151352011111391336589/
http://liureying.blog.163.com/blog/static/615135201111394058945/
http://www.360doc.com/content/11/0314/15/3303212_101031357.shtml
http://thinkinside.tk/2012/12/06/rule_language.html
http://blog.csdn.net/joeyshi/article/details/4146992
http://blog.sina.com.cn/s/blog_4a7a7aa30100089g.html