前言
这两天研究了一下JBoss Rules,最大的感觉是JBoss Rules的文档写得不错,而且Eclipse IDE插件的功能也挺不错,相比JBoss JBPM好多了。
个人觉得规则引擎就是把一堆if/else逻辑从业务代码转移到配置文件中,这样如果业务规则发生变化就不用更改代码,而且如果描述这种规则的语言足够清晰明白,更改业务规则的任务就可以由业务人员来完成了,Domain Specific Language就是来干这事的。
JBoss Rules的内部机制,这里就不再详细介绍了,已经有先行者对JBoss Rules的参考手册进行了整理,参见www.blogjava.net/guangnian0412/category/11762.html。
我针对一个实际的业务场景写了一个例子,应用场景是这样的,一个汽车网的销售人员的提成有一定的规则,大致的规则根据售出价格的折扣来决定提成比例,比如如果10000元的广告服务,最后售出价格是4500,销售人员的提成是0.3%。
创建工程
假定已经安装了Eclipse JBoss Rules插件,新建一个Rules工程。
创建规则文件
创建一个规则文件如下:
- #描述如何计算销售人员提成比例的规则
- package org.gaofubing.commision
- import org.gaofubing.discount.CommisionComputer;
- #折扣小于5折
- rule "lessThanFive"
- when
- computer:CommisionComputer(discount < 5)
- then
- computer.setDeduct(0.03);
- end
- #折扣等于5折
- rule "equalsFive"
- when
- computer:CommisionComputer(discount == 5)
- then
- computer.setDeduct(0.1);
- end
- #折扣大于5折小于等于6折
- rule "betweenFiveAndSix"
- when
- computer: CommisionComputer(discount:discount,discount > 5,discount <= 6)
- then
- computer.setDeduct(((discount.doubleValue() -5) * 0.5 + 6 * 0.1 ) / discount.doubleValue());
- end
- #折扣大于6折小于等于7折
- rule "betweenSixAndSeven"
- when
- computer: CommisionComputer(discount:discount,discount > 6,discount <= 7)
- then
- computer.setDeduct(((discount.doubleValue() -6) * 0.6 + (6 - 5) * 0.5 + 5 * 0.1 ) / discount.doubleValue());
- end
- #折扣大于7折小于等于8折
- rule "betweenSevenAndEight"
- when
- computer:CommisionComputer(discount:discount,discount > 7,discount <= 8)
- then
- computer.setDeduct(((discount.doubleValue() -7) * 0.65 + (7-6) * 0.6 + (6 - 5) * 0.5 + 5 * 0.1 ) / discount.doubleValue());
- end
- #折扣大于8折小于等于9折
- rule "betweenEightAndNine"
- when
- computer:CommisionComputer(discount:discount,discount > 8,discount <= 9)
- then
- computer.setDeduct(((discount.doubleValue() -8) * 0.7 + (8-7) * 0.65 + (7-6) * 0.6 + (6 - 5) * 0.5 + 5 * 0.1 ) / discount.doubleValue());
- end
- #折扣大于9折
- rule "greaterThanNine"
- when
- computer:CommisionComputer(discount:discount,discount > 9)
- then
- computer.setDeduct(((discount.doubleValue() -9) * 0.85 + (9-8) * 0.7 + (8-7) * 0.65 + (7-6) * 0.6 + (6 - 5) * 0.5 + 5 * 0.1 ) / discount.doubleValue());
- end
在测试的时候发生过一些问题,如果要描述折扣大于8小于9的条件,使用如下代码就不行
- computer: CommisionComputer(discount:discount,discount > 6) && CommisionComputer(discount <= 7)
另外CommisionComputer的discount属性是double类型,Rules的then部分会将其自动封箱,但是不会自动拆箱,所以如果then部分使用discount就会报错,必须使用discount.doubleValue();
创建Java类
CommisionComputer类的主要方法是computeCommision方法接受一个合同价格和售出价格,返回销售人员的提成数额。
- package org.gaofubing.discount;
- import java.io.InputStreamReader;
- import java.io.Reader;
- import org.apache.commons.logging.Log;
- import org.apache.commons.logging.LogFactory;
- import org.drools.RuleBase;
- import org.drools.RuleBaseFactory;
- import org.drools.WorkingMemory;
- import org.drools.compiler.PackageBuilder;
- import org.drools.rule.Package;
- public class CommisionComputer
- {
- private static final String RULE_FILE = "discount.drl";
- protected static Log log = LogFactory.getLog(CommisionComputer.class);
- //折扣
- private double discount;
- //提成比例
- private double deduct;
- /**
- * 根据合同价格和销售价格计算销售人员提成
- * @param contractPrice 合同价格
- * @param saledPrice 销售价格
- * @return 提成
- */
- public double computeCommision(double contractPrice,double saledPrice)
- {
- try {
- //读取规则集,创建工作内存区
- RuleBase ruleBase = readRule(RULE_FILE);
- WorkingMemory workingMemory = ruleBase.newWorkingMemory();
- log.debug("执行规则前的提成比例值为:" + getDeduct());
- //激活规则
- setDiscount(saledPrice * 10d /contractPrice);
- workingMemory.assertObject(this);
- workingMemory.fireAllRules();
- log.debug("执行规则后的提成比例值为:" + getDeduct());
- } catch (Throwable t) {
- t.printStackTrace();
- log.debug("不能成功执行规则",t);
- }
- return contractPrice * getDeduct();
- }
- /**
- * 读取规则文件
- * @param ruleFile 规则文件名称,相对于类路径
- */
- private RuleBase readRule(String ruleFile) throws Exception
- {
- //读取规则文件
- Reader source = new InputStreamReader(CommisionComputer.class.getClassLoader().getResourceAsStream(ruleFile));
- //创建包
- PackageBuilder builder = new PackageBuilder();
- builder.addPackageFromDrl( source );
- Package pkg = builder.getPackage();
- //构建规则集
- RuleBase ruleBase = RuleBaseFactory.newRuleBase();
- ruleBase.addPackage( pkg );
- return ruleBase;
- }
- public double getDiscount()
- {
- return discount;
- }
- public void setDiscount( double discount)
- {
- this.discount = discount;
- }
- public double getDeduct()
- {
- return deduct;
- }
- public void setDeduct( double deduct)
- {
- this.deduct = deduct;
- }
- }
创建测试
测试代码如下:
- public void testDiscountLessThanFive()
- {
- double commision = computer.computeCommision(10000,4500);
- assertEquals(commision,10000*0.03);
- }
- public void testDiscountEqualsFive()
- {
- double commision = computer.computeCommision(10000,5000);
- assertEquals(commision,10000*0.1);
- }
- public void testDiscountBetweenFiveAndSix()
- {
- double commision = computer.computeCommision(10000,5500);
- double discount = 5500 * 10d /10000;
- double deduct = ((discount -5) * 0.5 + 6 * 0.1 ) /discount;
- assertEquals(commision,10000*deduct);
- }
- public void testDiscountBetweenSixAndSeven()
- {
- double commision = computer.computeCommision(10000,6500);
- double discount = 6500 * 10d /10000;
- double deduct = ((discount -6) * 0.6 + (6 - 5) * 0.5 + 5 * 0.1 ) /discount;
- assertEquals(commision,10000*deduct);
- }
- public void testDiscountBetweenSevenAndEight()
- {
- double commision = computer.computeCommision(10000,7500);
- double discount = 7500 * 10d /10000;
- double deduct = ((discount -7) * 0.65 + (7-6) * 0.6 + (6 - 5) * 0.5 + 5 * 0.1 ) /discount;
- assertEquals(commision,10000*deduct);
- }
- public void testDiscountBetweenEightAndNine()
- {
- double commision = computer.computeCommision(10000,8500);
- double discount = 8500 * 10d /10000;
- double deduct = ((discount -8) * 0.7 + (8-7) * 0.65 + (7-6) * 0.6 + (6 - 5) * 0.5 + 5 * 0.1 ) /discount;
- assertEquals(commision,10000*deduct);
- }
- public void testDiscountGreaterThanNine()
- {
- double commision = computer.computeCommision(10000,9500);
- double discount = 9500 * 10d /10000;
- double deduct = ((discount -9) * 0.85 + (9-8) * 0.7 + (8-7) * 0.65 + (7-6) * 0.6 + (6 - 5) * 0.5 + 5 * 0.1 ) /discount;
- assertEquals(commision,10000*deduct);
- }