---
title: "试用JBoss Rules"
author: "white182517"
date: "2007-04-13T15:11:57Z"
tags: []
slug: "jboss-rules"
draft: false
lastmod: "2007-04-13T15:11:57Z"
id: 70652
url: "https://flytogether.iteye.com/blog/70652"
---

### 前言

这两天研究了一下JBoss Rules，最大的感觉是JBoss Rules的文档写得不错，而且Eclipse IDE插件的功能也挺不错，相比JBoss JBPM好多了。

个人觉得规则引擎就是把一堆if/else逻辑从业务代码转移到配置文件中，这样如果业务规则发生变化就不用更改代码，而且如果描述这种规则的语言足够清晰明白，更改业务规则的任务就可以由业务人员来完成了，Domain Specific Language就是来干这事的。

JBoss Rules的内部机制，这里就不再详细介绍了，已经有先行者对JBoss Rules的参考手册进行了整理，参见[www.blogjava.net/guangnian0412/category/11762.html](http://www.blogjava.net/guangnian0412/category/11762.html)。

我针对一个实际的业务场景写了一个例子，应用场景是这样的，一个汽车网的销售人员的提成有一定的规则，大致的规则根据售出价格的折扣来决定提成比例，比如如果10000元的广告服务，最后售出价格是4500，销售人员的提成是0.3%。

### 创建工程

假定已经安装了Eclipse JBoss Rules插件，新建一个Rules工程。

### 创建规则文件

创建一个规则文件如下：

ruby 代码

01. #描述如何计算销售人员提成比例的规则
02. package org.gaofubing.commision
03. import org.gaofubing.discount.CommisionComputer;
04. #折扣小于5折
05. rule "lessThanFive"
06. when
07. computer:CommisionComputer(discount &lt; 5)
08. then
09. computer.setDeduct(0.03);
10. end
11. #折扣等于5折
12. rule "equalsFive"
13. when
14. computer:CommisionComputer(discount == 5)
15. then
16. computer.setDeduct(0.1);
17. end
18. #折扣大于5折小于等于6折
19. rule "betweenFiveAndSix"
20. when
21. computer:  CommisionComputer(discount:discount,discount &gt; 5,discount &lt;= 6)
22. then
23. computer.setDeduct(((discount.doubleValue() -5) * 0.5 + 6 * 0.1 ) / discount.doubleValue());
24. end
25. #折扣大于6折小于等于7折
26. rule "betweenSixAndSeven"
27. when
28. computer: CommisionComputer(discount:discount,discount &gt; 6,discount &lt;= 7)
29. then
30. computer.setDeduct(((discount.doubleValue() -6) * 0.6 + (6 - 5) * 0.5 + 5 * 0.1 ) / discount.doubleValue());
31. end
32. #折扣大于7折小于等于8折
33. rule "betweenSevenAndEight"
34. when
35. computer:CommisionComputer(discount:discount,discount &gt; 7,discount &lt;= 8)
36. then
37. computer.setDeduct(((discount.doubleValue() -7) * 0.65 + (7-6) * 0.6 + (6 - 5) * 0.5 + 5 * 0.1 ) / discount.doubleValue());
38. end
39. #折扣大于8折小于等于9折
40. rule "betweenEightAndNine"
41. when
42. computer:CommisionComputer(discount:discount,discount &gt; 8,discount &lt;= 9)
43. then
44. computer.setDeduct(((discount.doubleValue() -8) * 0.7 + (8-7) * 0.65 + (7-6) * 0.6 + (6 - 5) * 0.5 + 5 * 0.1 ) / discount.doubleValue());
45. end
46. #折扣大于9折
47. rule "greaterThanNine"
48. when
49. computer:CommisionComputer(discount:discount,discount &gt; 9)
50. then
51. 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());
52. end

在测试的时候发生过一些问题，如果要描述折扣大于8小于9的条件，使用如下代码就不行

java 代码

1. computer: CommisionComputer(discount:discount,discount &gt; 6) && CommisionComputer(discount &lt;= 7)

另外CommisionComputer的discount属性是double类型，Rules的then部分会将其自动封箱，但是不会自动拆箱，所以如果then部分使用discount就会报错，必须使用discount.doubleValue();

### 创建Java类

CommisionComputer类的主要方法是computeCommision方法接受一个合同价格和售出价格，返回销售人员的提成数额。

java 代码

01. package org.gaofubing.discount;
02. import java.io.InputStreamReader;
03. import java.io.Reader;
04. import org.apache.commons.logging.Log;
05. import org.apache.commons.logging.LogFactory;
06. import org.drools.RuleBase;
07. import org.drools.RuleBaseFactory;
08. import org.drools.WorkingMemory;
09. import org.drools.compiler.PackageBuilder;
10. import org.drools.rule.Package;
11. public class CommisionComputer
12. {
13. private static final String RULE\_FILE = "discount.drl";
14. protected static Log log = LogFactory.getLog(CommisionComputer.class);
15. //折扣
16. private double discount;
17. //提成比例
18. private double deduct;
19. /\**
20. * 根据合同价格和销售价格计算销售人员提成
21. * @param contractPrice 合同价格
22. * @param saledPrice 销售价格
23. * @return 提成
24. \*/
25. public double computeCommision(double contractPrice,double saledPrice)
26. {
27. try {
28. //读取规则集,创建工作内存区
29. RuleBase ruleBase = readRule(RULE\_FILE);
30. WorkingMemory workingMemory = ruleBase.newWorkingMemory();
31. log.debug("执行规则前的提成比例值为：" + getDeduct());
32. //激活规则
33. setDiscount(saledPrice * 10d /contractPrice);
34. workingMemory.assertObject(this);
35. workingMemory.fireAllRules();
36. log.debug("执行规则后的提成比例值为：" + getDeduct());
37. } catch (Throwable t) {
38. t.printStackTrace();
39. log.debug("不能成功执行规则",t);
40. }
41. return contractPrice * getDeduct();
42. }
43. /\**
44. * 读取规则文件
45. * @param ruleFile 规则文件名称，相对于类路径
46. \*/
47. private RuleBase readRule(String ruleFile) throws Exception
48. {
49. //读取规则文件
50. Reader source = new InputStreamReader(CommisionComputer.class.getClassLoader().getResourceAsStream(ruleFile));
51. //创建包
52. PackageBuilder builder = new PackageBuilder();
53. builder.addPackageFromDrl( source );
54. Package pkg = builder.getPackage();
55. //构建规则集
56. RuleBase ruleBase = RuleBaseFactory.newRuleBase();
57. ruleBase.addPackage( pkg );
58. return ruleBase;
59. }
60. public double getDiscount()
61. {
62. return discount;
63. }
64. public void setDiscount( double discount)
65. {
66. this.discount = discount;
67. }
68. public double getDeduct()
69. {
70. return deduct;
71. }
72. public void setDeduct( double deduct)
73. {
74. this.deduct = deduct;
75. }
76. }

### 创建测试

测试代码如下：

java 代码

01. public void testDiscountLessThanFive()
02. {
03. double commision = computer.computeCommision(10000,4500);
04. assertEquals(commision,10000\*0.03);
05. }
06. public void testDiscountEqualsFive()
07. {
08. double commision = computer.computeCommision(10000,5000);
09. assertEquals(commision,10000\*0.1);
10. }
11. public void testDiscountBetweenFiveAndSix()
12. {
13. double commision = computer.computeCommision(10000,5500);
14. double discount = 5500 * 10d /10000;
15. double deduct = ((discount -5) * 0.5 + 6 * 0.1 ) /discount;
16. assertEquals(commision,10000\*deduct);
17. }
18. public void testDiscountBetweenSixAndSeven()
19. {
20. double commision = computer.computeCommision(10000,6500);
21. double discount = 6500 * 10d /10000;
22. double deduct = ((discount -6) * 0.6 + (6 - 5) * 0.5 + 5 * 0.1 ) /discount;
23. assertEquals(commision,10000\*deduct);
24. }
25. public void testDiscountBetweenSevenAndEight()
26. {
27. double commision = computer.computeCommision(10000,7500);
28. double discount = 7500 * 10d /10000;
29. double deduct = ((discount -7) * 0.65 + (7-6) * 0.6 + (6 - 5) * 0.5 + 5 * 0.1 ) /discount;
30. assertEquals(commision,10000\*deduct);
31. }
32. public void testDiscountBetweenEightAndNine()
33. {
34. double commision = computer.computeCommision(10000,8500);
35. double discount = 8500 * 10d /10000;
36. double deduct = ((discount -8) * 0.7 + (8-7) * 0.65 + (7-6) * 0.6 + (6 - 5) * 0.5 + 5 * 0.1 ) /discount;
37. assertEquals(commision,10000\*deduct);
38. }
39. public void testDiscountGreaterThanNine()
40. {
41. double commision = computer.computeCommision(10000,9500);
42. double discount = 9500 * 10d /10000;
43. 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;
44. assertEquals(commision,10000\*deduct);
45. }