- hxpwork
- 等级:
- 性别:
- 文章: 38
- 积分: 460
- 来自: 广州
|
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;
- }
-
-
-
- public String getColor() {
- return this.color;
- }
-
-
-
- public String getName() {
- return this.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
注:按照上面的方法会得到两个相同的结果,也就是规则被成功匹配两次,但结果相同。为何产生这个问题我暂时还没有想清楚,如果有谁了解问题的原因,请留言指点,谢谢!
声明:ITeye文章版权属于作者,受法律保护。没有作者书面许可不得转载。
|
返回顶楼 |
|
|
- dong_ta
- 等级: 初级会员
- 性别:
- 文章: 7
- 积分: 50
- 来自: 济南
|
对,确实是这样
|
返回顶楼 |
|
|