ITeye博客

Drools4:对Golfer示例的分析

hxpwork · 2007-08-03 00:57 · 企业架构 · Markdown

  1. /*
  2. 规则引擎的强大就在于它可以解决使用普通的编程方法难以解决的问题
  3. 例如对于下面一个问题的求解,用编程方法很难给出合适的解决方案
  4. 问题:高尔夫球员的位置?
  5. 已经知道有四个高尔夫球员,他们的名字是Fred,Joe,Bob,Tom;
  6. 今天他们分别穿着红色,蓝色,橙色以及格子衣服。并按照从左往右的顺序站成一排。
  7. 我们将最左边的位置定位1,最右边的位置定为4,中间依次是2,3位置。
  8. 现在我们已经了解的情况是:
  9. 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设置

  1. String[] names = new String[] { "Fred", "Joe", "Bob", "Tom" };
  2. String[] colors = new String[] { "red", "blue", "plaid", "orange" };
  3. int[] positions = new int[] { 1, 2, 3, 4 };
  4. for ( int n = 0; n < names.length; n++ ) {
  5. for ( int c = 0; c < colors.length; c++ ) {
  6. for ( int p = 0; p < positions.length; p++ ) {
  7. session.insert( new Golfer( names[n], colors[c], positions[p]) );
  8. }
  9. }
  10. }

2、Golfer对象

  1. public class Golfer {
  2. private String name;
  3. private String color;
  4. private int position;
  5. public Golfer() {
  6. }
  7. public Golfer(String name,
  8. String color,
  9. 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、规则

  1. rule "find solution"
  2. when
  3. # 规则:高尔夫球员Fred,目前不知道他的位置和衣服颜色
  4. Golfer( $fredsName : name == "Fred",
  5. $fredsPosition : position,
  6. $fredsColor : color  )
  7. # 规则:Fred的右边球员穿蓝色的衣服
  8. # 潜在规则:该球员的衣服颜色和Fred不一样,名字不是Fred
  9. 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

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

评论(1)

  1. #1 楼 · dong_ta · 2008-06-23 20:56

    对,确实是这样

评论功能维护中,当前仅展示历史评论。