`
hxpwork
  • 浏览: 109453 次
  • 性别: Icon_minigender_1
  • 来自: 广州
文章分类
社区版块
存档分类
最新评论

Drools4:对Golfer示例的分析

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

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[] { 1234 };   
  4.   
  5. for ( int n = 0; n < names.length; n++ ) {   
  6.     for ( int c = 0; c < colors.length; c++ ) {   
  7.         for ( int p = 0; p < positions.length; p++ ) {   
  8.             session.insert( new Golfer( names[n], colors[c], positions[p]) );   
  9.         }                   
  10.     }               
  11. }   

2、Golfer对象

  1. public class Golfer {   
  2.     private String name;   
  3.     private String color;   
  4.     private int position;   
  5.        
  6.     public Golfer() {   
  7.            
  8.     }   
  9.        
  10.     public Golfer(String name,   
  11.                   String color,   
  12.                   int position) {   
  13.         super();   
  14.         this.name = name;   
  15.         this.color = color;   
  16.         this.position = position;   
  17.     }   
  18.     /**  
  19.      * @return the color  
  20.      */  
  21.     public String getColor() {   
  22.         return this.color;   
  23.     }   
  24.     /**  
  25.      * @return the name  
  26.      */  
  27.     public String getName() {   
  28.         return this.name;   
  29.     }   
  30.        
  31.     /**  
  32.      * @return the name  
  33.      */  
  34.     public int getPosition() {   
  35.         return this.position;   
  36.     }           
  37.        
  38. }   

3、规则

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

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

 

分享到:
评论
1 楼 dong_ta 2008-06-23  
对,确实是这样

相关推荐

Global site tag (gtag.js) - Google Analytics