- hxpwork
- 等级:
- 性别:
- 文章: 38
- 积分: 460
- 来自: 广州
|
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- package org.drools.examples
-
- import org.drools.examples.conway.Cell;
- import org.drools.examples.conway.CellGrid;
- import org.drools.examples.conway.Neighbor;
- import org.drools.examples.conway.Phase;
- import org.drools.examples.conway.CellState;
-
- import org.drools.WorkingMemory;
- import org.drools.common.InternalWorkingMemoryActions;
- import org.drools.RuleBase;
-
-
-
-
-
-
-
- # 建立格子与其右上角格子的邻居关系
- rule "register north east"
- ruleflow-group "register neighbor"
- when
- # 获得棋盘列数
- CellGrid( $numberOfColumns : numberOfColumns )
- # 获得棋盘内的每个格子,最右边除外,因为最右边没有右上角
- $cell: Cell( $row : row > 0, $col : col < ( $numberOfColumns - 1 ) )
- # 获得上面格子的东北角格子
- $northEast : Cell( row == ($row - 1), col == ( $col + 1 ) )
- then
- # 为这两个格子建立邻居关系
- insert( new Neighbor( $cell, $northEast ) );
- insert( new Neighbor( $northEast, $cell ) );
- end
-
- # 建立格子与其正上方格子的邻居关系
- rule "register north"
- ruleflow-group "register neighbor"
- when
- $cell: Cell( $row : row > 0, $col : col )
- $north : Cell( row == ($row - 1), col == $col )
- then
- insert( new Neighbor( $cell, $north ) );
- insert( new Neighbor( $north, $cell ) );
- end
-
- # 建立格子与其左上角格子的邻居关系
- rule "register north west"
- ruleflow-group "register neighbor"
- when
- $cell: Cell( $row : row > 0, $col : col > 0 )
- $northWest : Cell( row == ($row - 1), col == ( $col - 1 ) )
- then
- insert( new Neighbor( $cell, $northWest ) );
- insert( new Neighbor( $northWest, $cell ) );
- end
-
- # 建立格子与其左边格子的邻居关系
- rule "register west"
- ruleflow-group "register neighbor"
- when
- $cell: Cell( $row : row >= 0, $col : col > 0 )
- $west : Cell( row == $row, col == ( $col - 1 ) )
- then
- insert( new Neighbor( $cell, $west ) );
- insert( new Neighbor( $west, $cell ) );
- end
-
-
-
-
-
-
-
-
- # 将格子的邻居中少于两个是生存状态的格子的状态设为死
- rule "Kill The Lonely"
- ruleflow-group "evaluate"
- no-loop
- when
- # A live cell has fewer than 2 live neighbors
- theCell: Cell(liveNeighbors < 2, cellState == CellState.LIVE, phase == Phase.EVALUATE)
- then
- theCell.setPhase(Phase.KILL);
- update( theCell );
- end
-
- # 将格子的邻居中超过3个状态是生存的格子状态设为死
- rule "Kill The Overcrowded"
- ruleflow-group "evaluate"
- no-loop
- when
- # A live cell has more than 3 live neighbors
- theCell: Cell(liveNeighbors > 3, cellState == CellState.LIVE, phase == Phase.EVALUATE)
- then
- theCell.setPhase(Phase.KILL);
- update( theCell );
- end
-
- # 将格子的邻居中正好有3个是生存状态的死亡格子变为生
- rule "Give Birth"
- ruleflow-group "evaluate"
- no-loop
- when
- # A dead cell has 3 live neighbors
- theCell: Cell(liveNeighbors == 3, cellState == CellState.DEAD, phase == Phase.EVALUATE)
- then
- theCell.setPhase(Phase.BIRTH);
- update( theCell );
- end
-
- # 取消ruleflow-group为"calculate"的所有激活规则
- # clearRuleFlowGroup - Clears the RuleFlow group, cancelling all its Activations
- # 因为在"generation"后,"calculate"组的规则还留在引擎中,如果不事先取消,就会引起无限循环
- rule "reset calculate"
- ruleflow-group "reset calculate"
- when
- then
- WorkingMemory wm = drools.getWorkingMemory();
- wm.clearRuleFlowGroup( "calculate" );
- end
-
- # 将所有格子的Phase为Kill的格子状态设置为死,并将处理阶段Phase设置为DONE
- rule "kill"
- ruleflow-group "kill"
- no-loop
- when
- theCell: Cell(phase == Phase.KILL)
- then
- theCell.setCellState(CellState.DEAD);
- theCell.setPhase(Phase.DONE);
- update( theCell );
- end
-
- # 将所有格子的Phase为Birth的格子状态设置为生,并将处理阶段Phase设置为完成
- rule "birth"
- ruleflow-group "birth"
- no-loop
- when
- theCell: Cell(phase == Phase.BIRTH)
- then
- theCell.setCellState(CellState.LIVE);
- theCell.setPhase(Phase.DONE);
- update( theCell );
- end
-
- # 根据格子的生存状态改变邻居格子中LiveNeighbors属性的计数
- rule "Calculate Live"
- ruleflow-group "calculate"
- lock-on-active # 本规则更新的数据在规则流处理完成前不激活新的规则
- when
- # 获得状态为生存的格子
- theCell: Cell(cellState == CellState.LIVE)
- # 找到该格子的每一个邻居
- Neighbor(cell == theCell, $neighbor : neighbor)
- then
- # 为这个格子的每一个邻居的LiveNeighbors属性加1
- $neighbor.setLiveNeighbors( $neighbor.getLiveNeighbors() + 1 );
- # 将邻居格子的处理阶段Phase设置为EVALUATE
- $neighbor.setPhase( Phase.EVALUATE );
- update( $neighbor );
- System.out.println( "--live--" );
- System.out.println( "theCell: row"+theCell.getRow()+"col"+theCell.getCol());
- System.out.println ( "Neighbor: row:"+$neighbor.getRow()+"col"+$neighbor.getCol()+";LiveNeighbors:"+ $neighbor.getLiveNeighbors()) ;
- end
-
- # 类似上一规则,只是进行递减操作
- # 对于这个规则,不太熟悉规则引擎的程序员可能会有所迷惑,所有的格子初始化状态都是DEAD
- # 那这样的话很多格子的LiveNeighbors就会变成负数了,有这样的想法是因为忽略了规则引擎不是从数据里找到规则合适的地方
- # 而是在数据插入或发生变化时找到规则,一开始初始化所有格子为DEAD时确实激活了"Calculate Dead"的很多实例,但是在
- # setPattern时首先执行了"reset calculate",这取消了之前的所有"Calculate Dead"的激活实例
- # 然后运行"kill all"规则,如果是第一次,该规则不改变任何数据也不会激发新的规则
- # 然后是根据数据设置格子的生存状态,此时上面的"Calculate Live"规则被激活
- # 在后面的规则运算中每次执行"evalaute"规则组都要运行"reset calculate"也是这个原因
- # 然后在运行了"birth"和"kill"规则后才执行"Calculate Live"和"Calculate Dead"规则
- rule "Calculate Dead"
- ruleflow-group "calculate"
- lock-on-active # 本规则更新的数据在规则流处理完成前不激活新的规则
- when
- theCell: Cell(cellState == CellState.DEAD)
- Neighbor(cell == theCell, $neighbor : neighbor )
- then
- $neighbor.setLiveNeighbors( $neighbor.getLiveNeighbors() - 1 );
- $neighbor.setPhase( Phase.EVALUATE );
- update( $neighbor );
- System.out.println( "--dead--" );
- System.out.println( "theCell: row"+theCell.getRow()+"col"+theCell.getCol());
- System.out.println ( "Neighbor: row:"+$neighbor.getRow()+"col"+$neighbor.getCol()+";LiveNeighbors:"+ $neighbor.getLiveNeighbors()) ;
- end
-
- # 将所有生存状态的格子设为死亡
- rule "Kill All"
- ruleflow-group "kill all"
- no-loop
- when
- theCell: Cell(cellState == CellState.LIVE)
- then
- theCell.setCellState(CellState.DEAD);
- update( theCell );
- end
声明:ITeye文章版权属于作者,受法律保护。没有作者书面许可不得转载。
|
返回顶楼 |
|
|