---
title: "Drools4：对Golfer示例的分析"
author: "hxpwork"
date: "2007-08-03T00:57:47Z"
tags: []
categories: ["企业架构"]
slug: "drools4-golfer"
draft: false
lastmod: "2007-08-03T00:57:47Z"
id: 108314
url: "https://hxpwork.iteye.com/blog/108314"
---

01. /*
02. 规则引擎的强大就在于它可以解决使用普通的编程方法难以解决的问题
03. 例如对于下面一个问题的求解，用编程方法很难给出合适的解决方案
04. 问题：高尔夫球员的位置？
05. 已经知道有四个高尔夫球员，他们的名字是Fred，Joe，Bob，Tom；
06. 今天他们分别穿着红色，蓝色，橙色以及格子衣服。并按照从左往右的顺序站成一排。
07. 我们将最左边的位置定位1，最右边的位置定为4，中间依次是2，3位置。
08. 现在我们已经了解的情况是：
09. 1)高尔夫球员Fred，目前不知道他的位置和衣服颜色
10. 2)Fred紧挨的右边球员穿蓝色的衣服
11. 3)Joe排在第2个位置
12. 4)Bob穿着格子短裤
13. 5)Tom没有排在第1位或第4位，也没有穿橙色衣服
14. 现在要求你根据上述条件获得四个球员分别的位置和穿着。
15. 如果要求使用编程方式获得结果，这显然是一个很有挑战性的工作，你可以思考一下。
16. 而使用规则引擎的方式来思考问题，我们其实是要从各种可能的排列中将需要的结果挑出来
17. 因此我们只需要将球员所有可能的排列组合（位置及颜色）出来，放入Working Memory中
18. 然后让规则引擎帮我们挑出来就可以了。
19. 另外在这个例子中也要注意规则引发的潜在规则，可以从下面规则的说明中了解详情。
20. \*/

1、Fact设置

01. String\[] names = new String\[] { "Fred", "Joe", "Bob", "Tom" };
02. String\[] colors = new String\[] { "red", "blue", "plaid", "orange" };
03. int\[] positions = new int\[] { 1, 2, 3, 4 };
04. for ( int n = 0; n &lt; names.length; n++ ) {
05. for ( int c = 0; c &lt; colors.length; c++ ) {
06. for ( int p = 0; p &lt; positions.length; p++ ) {
07. session.insert( new Golfer( names\[n], colors\[c], positions\[p]) );
08. }
09. }
10. }

2、Golfer对象

01. public class Golfer {
02. private String name;
03. private String color;
04. private int position;
05. public Golfer() {
06. }
07. public Golfer(String name,
08. String color,
09. int position) {
10. super();
11. this.name = name;
12. this.color = color;
13. this.position = position;
14. }
15. /\**
16. * @return the color
17. \*/
18. public String getColor() {
19. return this.color;
20. }
21. /\**
22. * @return the name
23. \*/
24. public String getName() {
25. return this.name;
26. }
27. /\**
28. * @return the name
29. \*/
30. public int getPosition() {
31. return this.position;
32. }
33. }

3、规则

01. rule "find solution"
02. when
03. # 规则：高尔夫球员Fred，目前不知道他的位置和衣服颜色
04. Golfer( $fredsName : name == "Fred",
05. $fredsPosition : position,
06. $fredsColor : color  )
07. # 规则：Fred的右边球员穿蓝色的衣服
08. # 潜在规则：该球员的衣服颜色和Fred不一样，名字不是Fred
09. Golfer( $unknownsName : name != "Fred",
10. $unknownsPosition :  position == ( $fredsPosition + 1 ),
11. $unknownsColor : color == "blue",
12. color != $fredsColor )
13. # Joe排在第2个位置
14. # 潜在规则：Joe的位置不是Fred的位置，Joe的衣服颜色不是Fred的颜色
15. Golfer( $joesName : name == "Joe",
16. $joesPosition  : position == 2,
17. position != $fredsPosition,
18. $joesColor : color != $fredsColor )
19. # Bob穿着格子短裤
20. # 潜在规则：Bob的名字与穿蓝衣服的球员名字不同，Bob的位置和Fred，Joe，以及蓝衣球员的位置都不同
21. #         Bob的颜色也于之前三个球员不同
22. Golfer( $bobsName : name == "Bob",
23. name != $unknownsName,
24. $bobsPosition : position != $fredsPosition,
25. position != $unknownsPosition,
26. position != $joesPosition,
27. $bobsColor : color == "plaid",
28. color != $fredsColor,
29. color != $joesColor,
30. color != $unknownsColor )
31. # Tom没有排在第1位或第4位，也没有穿橙色衣服
32. # 潜在规则：Tom的位置与Fred，Joe，Bob的位置不同；Tom的衣服颜色不是橙色和蓝色，并于另外三人不同
33. Golfer( $tomsName : name == "Tom",
34. $tomsPosition : position != 1,
35. position != 4,
36. position != $fredsPosition,
37. position != $joesPosition,
38. position != $bobsPosition,
39. $tomsColor : color != "orange",
40. color != "blue",
41. color != $fredsColor,
42. color != $joesColor,
43. color != $bobsColor )
44. then
45. System.out.println( "Fred " + $fredsPosition + " " + $fredsColor );
46. System.out.println( "Joe " + $joesPosition + " " + $joesColor );
47. System.out.println( "Bob " + $bobsPosition + " " + $bobsColor );
48. System.out.println( "Tom " + $tomsPosition + " " + $tomsColor );
49. end

注：按照上面的方法会得到两个相同的结果，也就是规则被成功匹配两次，但结果相同。为何产生这个问题我暂时还没有想清楚，如果有谁了解问题的原因，请留言指点，谢谢！