`
- 浏览:
113055 次
- 性别:
- 来自:
广州
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- 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
分享到:
Global site tag (gtag.js) - Google Analytics
相关推荐
David Wallace Croft在2008年11月6日向达拉斯规则组展示了一个名为“DroolsGameofLifeExample”的案例,该案例使用Drools规则引擎实现了Conway的生命游戏。生命游戏是一种零玩家游戏,初始状态后的变化完全由其规则...
嵌入式系统开发_STM32微控制器_ESP8266WiFi模块_心率传感器_加速度计_OLED显示屏_蓝牙40_低功耗设计_实时操作系统_智能手表_多功能健康监测_运动数据记录_
驾校自动化_网页自动化爬虫技术_Python27多线程HTTP请求模拟_龙泉驾校2014版约车系统自动预约助手_通过模拟登录和循环请求实现自动约车功能_支持失败自动递增车号重试_
Linux系统编程_操作系统内核_系统调用_进程线程_信号处理_文件IO_进程间通信_多线程同步_网络编程_UNIX环境编程_中文翻译勘误_错误修正_代码示例优化_技术文档校对_开
wanjunshe_Python-Tensorflow_12888_1745868924470
scratch少儿编程逻辑思维游戏源码-铅笔画.zip
即时通讯应用开发_基于LeanCloud云服务与Android原生开发_集成QQ第三方登录与即时聊天功能的社交应用_实现用户注册登录创建聊天室发送文字消息展示用户信息头像昵称并提供
scratch少儿编程逻辑思维游戏源码-伞兵大乱斗(云变量).zip
scratch少儿编程逻辑思维游戏源码-楼层酷跑.zip
scratch少儿编程逻辑思维游戏源码-零下之寒颤.zip
scratch少儿编程逻辑思维游戏源码-密室逃生.zip
少儿编程scratch项目源代码文件案例素材-爪猫足球.zip
命令行完成git本地仓库创建、将代码提交到暂存区、查看暂存区信息、将代码提交到本地仓库、将本地仓库关联到远程仓库、推送到远程仓库全过程的截图
少儿编程scratch项目源代码文件案例素材-纸.zip
scratch少儿编程逻辑思维游戏源码-日本冒险.zip
scratch少儿编程逻辑思维游戏源码-狼人杀跑酷.zip
scratch少儿编程逻辑思维游戏源码-史莱姆杀手.zip
少儿编程scratch项目源代码文件案例素材-粘粘世界.zip
scratch少儿编程逻辑思维游戏源码-米克 demo.zip
网络游戏开发_Netty4X框架_多协议支持_高性能分布式游戏服务器_基于TCP_UDP_HTTP_WebSocket协议栈_Protobuf自定义通信协议_RPC远程调用_My