- /*
- 规则引擎的强大就在于它可以解决使用普通的编程方法难以解决的问题
- 例如对于下面一个问题的求解,用编程方法很难给出合适的解决方案
- 问题:高尔夫球员的位置?
- 已经知道有四个高尔夫球员,他们的名字是Fred,Joe,Bob,Tom;
- 今天他们分别穿着红色,蓝色,橙色以及格子衣服。并按照从左往右的顺序站成一排。
- 我们将最左边的位置定位1,最右边的位置定为4,中间依次是2,3位置。
- 现在我们已经了解的情况是:
- 1)高尔夫球员Fred,目前不知道他的位置和衣服颜色
- 2)Fred紧挨的右边球员穿蓝色的衣服
- 3)Joe排在第2个位置
- 4)Bob穿着格子短裤
- 5)Tom没有排在第1位或第4位,也没有穿橙色衣服
- 现在要求你根据上述条件获得四个球员分别的位置和穿着。
- 如果要求使用编程方式获得结果,这显然是一个很有挑战性的工作,你可以思考一下。
- 而使用规则引擎的方式来思考问题,我们其实是要从各种可能的排列中将需要的结果挑出来
- 因此我们只需要将球员所有可能的排列组合(位置及颜色)出来,放入Working Memory中
- 然后让规则引擎帮我们挑出来就可以了。
- 另外在这个例子中也要注意规则引发的潜在规则,可以从下面规则的说明中了解详情。
- */
1、Fact设置
- String[] names = new String[] { "Fred", "Joe", "Bob", "Tom" };
- String[] colors = new String[] { "red", "blue", "plaid", "orange" };
- int[] positions = new int[] { 1, 2, 3, 4 };
- for ( int n = 0; n < names.length; n++ ) {
- for ( int c = 0; c < colors.length; c++ ) {
- for ( int p = 0; p < positions.length; p++ ) {
- session.insert( new Golfer( names[n], colors[c], positions[p]) );
- }
- }
- }
2、Golfer对象
- public class Golfer {
- private String name;
- private String color;
- private int position;
- public Golfer() {
- }
- public Golfer(String name,
- String color,
- int position) {
- super();
- this.name = name;
- this.color = color;
- this.position = position;
- }
- /**
- * @return the color
- */
- public String getColor() {
- return this.color;
- }
- /**
- * @return the name
- */
- public String getName() {
- return this.name;
- }
- /**
- * @return the name
- */
- public int getPosition() {
- return this.position;
- }
- }
3、规则
- rule "find solution"
- when
- # 规则:高尔夫球员Fred,目前不知道他的位置和衣服颜色
- Golfer( $fredsName : name == "Fred",
- $fredsPosition : position,
- $fredsColor : color )
- # 规则:Fred的右边球员穿蓝色的衣服
- # 潜在规则:该球员的衣服颜色和Fred不一样,名字不是Fred
- Golfer( $unknownsName : name != "Fred",
- $unknownsPosition : position == ( $fredsPosition + 1 ),
- $unknownsColor : color == "blue",
- color != $fredsColor )
- # Joe排在第2个位置
- # 潜在规则:Joe的位置不是Fred的位置,Joe的衣服颜色不是Fred的颜色
- Golfer( $joesName : name == "Joe",
- $joesPosition : position == 2,
- position != $fredsPosition,
- $joesColor : color != $fredsColor )
- # Bob穿着格子短裤
- # 潜在规则:Bob的名字与穿蓝衣服的球员名字不同,Bob的位置和Fred,Joe,以及蓝衣球员的位置都不同
- # Bob的颜色也于之前三个球员不同
- Golfer( $bobsName : name == "Bob",
- name != $unknownsName,
- $bobsPosition : position != $fredsPosition,
- position != $unknownsPosition,
- position != $joesPosition,
- $bobsColor : color == "plaid",
- color != $fredsColor,
- color != $joesColor,
- color != $unknownsColor )
- # Tom没有排在第1位或第4位,也没有穿橙色衣服
- # 潜在规则:Tom的位置与Fred,Joe,Bob的位置不同;Tom的衣服颜色不是橙色和蓝色,并于另外三人不同
- Golfer( $tomsName : name == "Tom",
- $tomsPosition : position != 1,
- position != 4,
- position != $fredsPosition,
- position != $joesPosition,
- position != $bobsPosition,
- $tomsColor : color != "orange",
- color != "blue",
- color != $fredsColor,
- color != $joesColor,
- color != $bobsColor )
- then
- System.out.println( "Fred " + $fredsPosition + " " + $fredsColor );
- System.out.println( "Joe " + $joesPosition + " " + $joesColor );
- System.out.println( "Bob " + $bobsPosition + " " + $bobsColor );
- System.out.println( "Tom " + $tomsPosition + " " + $tomsColor );
- end
注:按照上面的方法会得到两个相同的结果,也就是规则被成功匹配两次,但结果相同。为何产生这个问题我暂时还没有想清楚,如果有谁了解问题的原因,请留言指点,谢谢!