- hxpwork
- 等级:
- 性别:
- 文章: 38
- 积分: 460
- 来自: 广州
|
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- package org.drools.benchmark.manners
-
- # 从客人中找出一位放入第一个座位
- # 根据Drools引擎的特性,应当是最后一个加入的Guest
- # 利用Context.state属性控制该规则只执行一次
- rule assignFirstSeat
- when
- # Context用于流程控制
- context : Context( state == Context.START_UP )
- #
- # 原例中使用的是 Guest() , 获得最后插入的Fact,但是这样会引起之前提到的Bug
- # 因此在下面进行了改进,保证找出三个兴趣都有的客人
- #
- guest : Guest( $name:name , $hobby : hobby )
- Guest ( name == $name , hobby != $hobby , $hobby1 : hobby )
- Guest ( name == $name , hobby != $hobby , hobby != $hobby1 )
-
- count : Count()
- then
- String guestName = guest.getName();
-
- # Seating用于记录每个座位上的客人,是寻找下一个座位的客人的依据
- Seating seating = new Seating( count.getValue(), 1, true, 1, guestName, 1, guestName);
- insert( seating );
-
- # Path用来避免已经入座的客人再次被选中
- Path path = new Path( count.getValue(), 1, guestName );
- insert( path );
-
- # 用于计数,并作为Seating的ID
- count.setValue( count.getValue() + 1 );
- update( count );
-
- System.out.println( "assign first seat : " + seating + " : " + path );
-
- # 进入找下一个座位环节
- context.setState( Context.ASSIGN_SEATS );
- update( context );
- end
-
- # 寻找相邻的同伴
- rule findSeating
- when
- context : Context( state == Context.ASSIGN_SEATS )
- # 获得最近安排好座位的客人
- $s:Seating( seatingId:id, seatingPid:pid, pathDone == true, seatingRightSeat:rightSeat, seatingRightGuestName:rightGuestName )
- # 找到一位有共同兴趣但性别不同的相邻客人
- Guest( name == seatingRightGuestName, rightGuestSex:sex, rightGuestHobby:hobby )
- Guest( leftGuestName:name , sex != rightGuestSex, hobby == rightGuestHobby )
-
- count : Count()
- # 排除已经入座的客人
- #
- # 原例中使用了Path.id作为一个约束条件,而事实上要排除已经入座的客人只要判断guestName就可以了
- # 因此原例中的makePath规则如果注释掉,然后将下面的语句改为
- # not ( Path( guestName == leftGuestName) )
- # 执行后也可以得到完全相同的结果
- # Manners是作为规则引擎的测试基准发布的,因此这里完全按照标准实现,所以这里虽然
- # 可以进行改进,但是可能因为为了测试规则引擎,因此保留这种算法
- #
- not ( Path( id == seatingId, guestName == leftGuestName) )
-
- # 排除入座客人已经和使用过的兴趣,保证两边相邻的客人共同兴趣不一样
- not ( Chosen( id == seatingId, guestName == leftGuestName, hobby == rightGuestHobby) )
- then
- int rightSeat = seatingRightSeat;
- int seatId = seatingId;
- int countValue = count.getValue();
-
- # 安排一个新的座位
- Seating seating = new Seating( countValue, seatId, false, rightSeat, seatingRightGuestName, rightSeat + 1, leftGuestName );
- insert( seating );
- # 记录已经入座的客人
- Path path = new Path( countValue, rightSeat + 1, leftGuestName );
- insert( path );
- # 记录已经使用的兴趣爱好
- Chosen chosen = new Chosen( seatId, leftGuestName, rightGuestHobby );
- insert( chosen );
-
- System.err.println( "find seating : " + seating + " : " + path + " : " + chosen);
- count.setValue( countValue + 1 );
- update( count );
-
- context.setState( Context.MAKE_PATH );
- update( context );
-
- retract ( $s );
- end
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- rule makePath
- when
- Context( state == Context.MAKE_PATH )
- Seating( seatingId:id, seatingPid:pid, pathDone == false )
- $path : Path( id == seatingPid, pathGuestName:guestName, pathSeat:seat )
- not Path( id == seatingId, guestName == pathGuestName )
- then
- System.out.println ( "makePath :" + $path );
- insert( new Path( seatingId, pathSeat, pathGuestName ) );
- end
-
- # 结束座位的路径设置
- rule pathDone
- when
- context : Context( state == Context.MAKE_PATH )
- seating : Seating( pathDone == false )
- then
- seating.setPathDone( true );
- update( seating );
-
- context.setState( Context.CHECK_DONE );
- update( context );
- end
-
- # 检查是否安排的客人座位已经到达指定人数,结束规则运算
- rule areWeDone
- when
- context : Context( state == Context.CHECK_DONE )
- LastSeat( lastSeat: seat )
- Seating( rightSeat == lastSeat )
- then
- context.setState(Context.PRINT_RESULTS );
- update( context );
- end
-
- # 如果之前的areWeDone规则没有允许,则继续座位查找
- rule continue
- when
- context : Context( state == Context.CHECK_DONE )
- then
- context.setState( Context.ASSIGN_SEATS );
- update( context );
- end
-
- # 打印完成提示
- rule allDone
- when
- context : Context( state == Context.PRINT_RESULTS )
- then
- System.out.println( "All Done" );
- end
-
-
-
-
-
声明:ITeye文章版权属于作者,受法律保护。没有作者书面许可不得转载。
|
返回顶楼 |
|
|
- hxpwork
- 等级:
- 性别:
- 文章: 38
- 积分: 460
- 来自: 广州
|
另外补充一点,Manners是作为对规则引擎的基准测试发布的,这里的例子是完全按照论文中要求的实现,所以MakePath规则虽然可能有更好的实现方式,但是它存在的目的是为了测验规则引擎的性能,因此被保留下来。
|
返回顶楼 |
|
|