zbb20170605 Drools java 规则引擎
总体视图
http://pan.baidu.com/disk/home?errno=0&errmsg=Auth%20Login%20Sucess&&bduss=&ssnerror=0#list/path=%2F%E9%87%91%E5%B1%B1%E5%BF%AB%E7%9B%98%E6%96%87%E4%BB%B6%2F%E5%BF%AB%E7%9B%98%2F%E6%88%91%E7%9A%84%E8%B5%84%E6%96%99%2F%E5%AD%A6%E4%B9%A0%E6%96%87%E4%BB%B6%2F%E5%B8%B8%E7%94%A8%E4%BB%A3%E7%A0%81%E6%95%B4%E5%90%88%E4%BF%AE%E6%95%B4%2FDrools&vmode=grid
Test.java
package com.zbb.drools; import java.util.Scanner; public class Test { public static void main(String[] args) { PointRuleEngineImp pointRuleEngineImp = new PointRuleEngineImp(); PointDomain domain = new PointDomain(); while (true) { Scanner scanner = new Scanner(System.in); String nextLine = scanner.nextLine(); if ("s".equals(nextLine)) { System.out.println("初始化规则引擎..."); pointRuleEngineImp.initEngine(); System.out.println("初始化规则引擎结束."); } else if ("e".equals(nextLine)) { domain.setBuyNums(5); System.out.println(domain.getPoint()); pointRuleEngineImp.executeEngine(domain); System.out.println(domain.getPoint()); } else { System.out.println("非法输入!"); } } } }
RuleBaseFacatory.java
package com.zbb.drools; import org.drools.RuleBase; import org.drools.RuleBaseFactory; /** * RuleBaseFacatory 单实例RuleBase生成工具 * @author quzishen */ public class RuleBaseFacatory { private static RuleBase ruleBase; public static RuleBase getRuleBase(){ return null != ruleBase ? ruleBase : RuleBaseFactory.newRuleBase(); } }
PointRuleEngineImp.java
package com.zbb.drools; import org.drools.RuleBase; import org.drools.StatefulSession; import org.drools.compiler.PackageBuilder; import org.drools.spi.Activation; import org.drools.spi.AgendaFilter; public class PointRuleEngineImp implements PointRuleEngine{ private RuleBase ruleBase; @Override public void initEngine() { // TODO Auto-generated method stub //设置事件格式 System.setProperty("drools.dateformat","yyyy-MM-dd HH:mm:ss"); ruleBase=RuleBaseFacatory.getRuleBase(); try { PackageBuilder packageBuilderFromDrlFile = DroolsUtil.getPackageBuilderFromDrlFile(); ruleBase.addPackage(packageBuilderFromDrlFile.getPackage()); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); }; } @Override public void refreshEngine() { // TODO Auto-generated method stub } @Override public void executeEngine(final PointDomain pointDomain) { // TODO Auto-generated method stub if(null==ruleBase){ return ; } StatefulSession session = ruleBase.newStatefulSession(); session.insert(pointDomain); session.fireAllRules(new AgendaFilter() { @Override public boolean accept(Activation arg0) { // TODO Auto-generated method stub return !arg0.getRule().getName().contains("_test"); } }); session.dispose(); } }
PointRuleEngine.java
package com.zbb.drools; public interface PointRuleEngine { public void initEngine(); public void refreshEngine(); public void executeEngine(final PointDomain pointDomain); }
PointDomain.java
package com.zbb.drools; public class PointDomain { private String userName; private int account; private int buyNums; public String getUserName() { return userName; } public void setUserName(String userName) { this.userName = userName; } public int getAccount() { return account; } public void setAccount(int account) { this.account = account; } public int getBuyNums() { return buyNums; } public void setBuyNums(int buyNums) { this.buyNums = buyNums; }// 增加积分数目 private long point; public long getPoint() { return point; } public void setPoint(long point) { this.point = point; } /** * 记录积分发送流水,防止重复发放 * * @param userName * 用户名 * @param type * 积分发放类型 */ public void recordPointLog(String userName, String type) { System.out.println("增加对" + userName + "的类型为" + type + "的积分操作记录."); } }
DroolsUtil.java
package com.zbb.drools; import java.io.File; import java.io.FileNotFoundException; import java.io.FileReader; import java.io.IOException; import java.io.Reader; import java.util.ArrayList; import java.util.List; import org.drools.compiler.PackageBuilder; public class DroolsUtil { /** * 从drl规则文件中读取规则 * * @throws FileNotFoundException */ public static PackageBuilder getPackageBuilderFromDrlFile() throws Exception { // 获取脚本文件 List<String> drlFile = getDrlFile(); // 装载脚本文件 List<Reader> readRuleFromDrlFile = readRuleFromDrlFile(drlFile); PackageBuilder builder = new PackageBuilder(); for (Reader reader : readRuleFromDrlFile) { builder.addPackageFromDrl(reader); } if (builder.hasErrors()) { throw new Exception(builder.getErrors().toString()); } return builder; } /** * 装载脚本文件 * * @throws FileNotFoundException */ private static List<Reader> readRuleFromDrlFile(List<String> drlFile) throws FileNotFoundException { if (null == drlFile || drlFile.size() == 0) { return null; } List<Reader> readers = new ArrayList<>(); for (String ss : drlFile) { readers.add(new FileReader(new File(ss))); } return readers; } /** * 获取规则文件 */ private static List<String> getDrlFile() { List<String> drlFilePath = new ArrayList<String>(); drlFilePath.add(getPath() + "/src/com/zbb/drools/drl/addpoint.drl"); // drlFilePath.add("E:/azz/drools/drl/subpoint.drl"); return drlFilePath; } /** * 获取项目路径 * * @return */ public static String getPath() { File file = new File(""); System.out.println(file.getAbsolutePath()); try { System.out.println(file.getCanonicalPath()); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } return file.getAbsolutePath(); }; }
addpoint.drl
package com.zbb.drools import com.zbb.drools.PointDomain; 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 buyNumsPoint2 // 当月购物次数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