drools4: Conway示范分析
drools4: Conway示例分析??????它们是用在进行下一步的变化评估中的,这3个规则中并没有直接改变Cell.CellS
drools4: Conway示例分析
?
?
????它们是用在进行下一步的变化评估中的,这3个规则中并没有直接改变Cell.CellState, ? ????因为CellState是用来进行评估用的,随意改变会造成无限的循环调用,因此规则使用了Phase字段来控制格子状态变化 ? ????在评估中只是改变Phase,最后根据Phase的状态完成所有格子CellState的设置,并将Phase设回EVALUATE状态 ? */?? ?? #?将格子的邻居中少于两个是生存状态的格子的状态设为死 ?? 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?? 1 楼 ganchun-ling 2007-08-14 简直是场及时雨啊!