`
- 浏览:
109370 次
- 性别:
- 来自:
广州
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- 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
相关推荐
Drools4 Demo 实例 Drools4示例 Drools4例子
drools-示例:Drools:规则引擎
使用Java开源框架Drools实现的一个示例 可以直接运行
NULL 博文链接:https://wu1g119.iteye.com/blog/2287940
drools学习的各个知识点的小示例,与springboot的整合项目,动态从库中加载规则的项目
你好Drools 示例项目
使用Drools5.1 规则引擎做的一个关于规则流的小示例,含规则流的rf 和 Xml 格式文件,以及一个自动生成顺序规则流的方法。 在 eclipse indigo 上运行通过。
drools drools drools drools drools
Drools入门-环境搭建,分析及示例
Drools是一个Java语言版本的基于Charles Forgy's Rete算法研究的规则引擎实现。结合Rete到一个面向对象接口中,允许业务对象处理业务表达式。Drools由Java语言开发,但是可以运行在Java环境和.NET环境下。 Drools被...
流口水示例Drools 规则引擎示例。
Drools规则引擎是一种嵌套在应用程序中的组件, 是用Java语言编写的开放源码规则引擎,使用Rete算法对所编写的规则求值。 它实现了将业务规则从程序代码忠分离出来,规则引擎使用特定的语法编写业务规则,规则引擎...
drools7.5.0和spring 4.3.12集成示例,替换了最新的包,借鉴了csdn另外一个网友的代码,再次基础上做了调整。主要调整了pom.xml,升级了版本
Drools4 insurance例子 j2ee环境!用jboss会更好
很不错的drools6学习的例子 里面有入门的实例及一些语法的例子。
储存库初始化内容 您的项目描述在这里。
不关闭服务,修改规则文件条件,使得原来不能通过的条件通过,实现规则的动态加载。
drools
JPA+DROOLS4整合的例子,JPA+DROOLS4整合的例子,
这个是本人在研究drools flow总结的报告,希望对大家有帮助