19.9. 生命周期决策的整合方法(ruleflow 组和 GUI 集成)


Conway's Game of Life example 决策集基于 John Conway 的 famous cellular automaton,演示了如何在规则中使用 ruleflow 组来控制规则执行。这个示例还演示了如何将 Red Hat Process Automation Manager 规则与图形用户界面(GUI)集成,在本例中基于 Swing's Game of Life。

以下是 Conway's Game of Life (Conway)示例的概述:

  • Name:conway
  • 主类org.drools.examples.conway.ConwayRuleFlowGroupRun,org.drools.examples.conway.ConwayAgendaGroupRun (in src/main/java)
  • 模块droolsjbpm-integration-examples
  • 类型 :Java 应用程序
  • 规则文件:org.drools.examples.conway114.drl (在 src/main/resources中)
  • 目标 :演示 ruleflow 组和 GUI 集成
注意

The Conway's Game of Life example of the regularols jbpm-integration-examples in the Red Hat Process Automation Manager 7.9.1 source distribution from the Red Hat Customer Portal.

在 Conway's Game 的生命周期内,用户通过创建初始配置或具有定义属性的高级模式,然后观察初始状态的演进方式与游戏交互。游戏的目的是显示填充的开发,生成到生成。以上每个生成结果都基于所有单元的同时评估。

以下基本规则管理下一代内容,如下所示:

  • 如果实时单元少于两个实时邻居,它将划分掉 loneliness。
  • 如果实时单元有超过三个实时邻居,它将被过度分离。
  • 如果死单元具有三个实时邻居,则相当于生命周期。

任何不符合这些条件的单元都保留为在下一代位置中。

The Conway 的 Game of Life example 使用 Red Hat Process Automation Manager 规则和 ruleflow-group 属性来定义在游戏中实施的模式。该示例还包含一组决策集的版本,该版本通过电缆组实现相同的行为。通过电缆组,您可以对决策引擎人员进行分区,以提供对规则组的执行控制。默认情况下,所有规则都位于 products 组 MAIN 中。您可以使用 sales -group 属性为规则指定不同的 sales 组。

此概述不会探索使用电缆组的对比示例版本。有关销售组的更多信息,请参阅 Red Hat Process Automation Manager 决策集合,该设置专门解决电缆组。

执行和交互示例

与其他 Red Hat Process Automation Manager 决策示例类似,您可以通过在 IDE 中作为 Java 应用程序运行 org.drools.examples.conwayRuleFlowGroupRun 类来执行 Conway ruleflow 示例。

当您执行 Conway 示例时,会出现 Conway's Game of Life GUI 窗口。此窗口包含一个空的网格,或"arena",其中执行生命周期模拟。最初网格为空,因为系统上还没有实时单元。

图 19.24. 启动后的 GUI 示例

Pattern 下拉菜单中选择预定义的模式,然后单击 Next Generation 以点每个填充生成。每个单元都是 live 或 dead,其中实时单元包含一个绿色 ball。随着填充从初始模式增长时,根据游戏的规则,从初始模式增长或相对于邻居单元的单元格。

图 19.25. 一致性示例中的生成演进

邻居不仅包括左侧、右、顶部和底部连接的单元格,以及连接了 diagonly 的单元,因此每个单元格都有总计 8 个邻居。例外是角单元,其中只有三个邻居,以及四个边框的单元格,每个方都有五个邻居。

您可以通过单击单元来手动干预来创建或终止单元。

要从初始模式下运行,请单击 Start

使用带有 ruleflow 组的示例规则

ConwayRuleFlowGroupRun 示例中的规则使用 ruleflow 组来控制规则执行。ruleflow 组是由 rule flow-group rule 属性关联的一组规则。这些规则只能在组激活时触发。只有当 ruleflow 图的 Elaboration 到达代表组的节点时,组本身才能变为活动状态。

Conway 示例将以下 ruleflow 组用于规则:

  • "register neighbor"
  • "evaluate"
  • "calculate"
  • "reset calculate"
  • "birth"
  • "kill"
  • "kill all"

所有 Cell 对象都插入到 KIE 会话中,ruleflow 组 "register neighbor" 中的 "register …​" 规则可以被 ruleflow 进程执行。这个四个规则组会在某些单元格及其 northeastern、northern、northwestern 和 western neighbors 之间创建邻居关系。

这种关系是双向的,处理其他四个方向。边框单元不需要任何特殊处理。这些单元不会与没有邻居单元的邻居单元配对。

通过为这些规则触发所有激活的时间,所有单元都与所有邻居单元相关。

rules "register …​"

rule "register north east"
    ruleflow-group "register neighbor"
  when
    $cell: Cell( $row : row, $col : col )
    $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, $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, $col : col )
    $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, $col : col )
    $west : Cell( row  == $row, col == ( $col - 1 ) )
  then
    insert( new Neighbor( $cell, $west ) );
    insert( new Neighbor( $west, $cell ) );
end
Copy to Clipboard Toggle word wrap

插入完所有单元后,一些 Java 代码会将模式应用到网格,将某些单元设置为 Live。然后,当用户单击 StartNext Generation 时,示例会执行 Generation ruleflow。此 ruleflow 管理每个生成周期中的单元的所有更改。

图 19.26. 生成 ruleflow

ruleflow 进程输入 "evaluate" 规则流组,组中的任何活动规则都可以触发。这个组中的规则 "Kill the …​""Give Birth" 将游戏规则应用到 birth 或 kill 单元。这个示例使用 phase 属性根据特定规则组驱动 Cell 对象的原因。通常,阶段与 ruleflow 进程定义中的 ruleflow 组关联。

请注意,这个示例不会更改任何 Cell 对象的状态,因为它必须在应用更改前完成完整评估。示例将单元设置为 Phase.KILLPhase.114RTH阶段,稍后用于控制应用到 Cell 对象的操作。

规则 "Kill the …​" 和 "Give Birth"

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
    modify( theCell ){
        setPhase( Phase.KILL );
    }
end

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
    modify( theCell ){
        setPhase( Phase.KILL );
    }
end

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
    modify( theCell ){
        theCell.setPhase( Phase.BIRTH );
    }
end
Copy to Clipboard Toggle word wrap

评估网格中的所有 Cell 对象后,示例使用 "reset compute" 规则清除 " calculate" ruleflow 组中的任何激活。然后,在 ruleflow 中输入分割,如果 ruleflow 组被激活,使规则 "kill""birth" 触发。这些规则应用状态更改。

规则 "reset calculate", "kill", 和 "birth"

rule "reset calculate"
    ruleflow-group "reset calculate"
  when
  then
    WorkingMemory wm = drools.getWorkingMemory();
    wm.clearRuleFlowGroup( "calculate" );
end

rule "kill"
    ruleflow-group "kill"
    no-loop
  when
    theCell: Cell( phase == Phase.KILL )
  then
    modify( theCell ){
        setCellState( CellState.DEAD ),
        setPhase( Phase.DONE );
    }
end

rule "birth"
    ruleflow-group "birth"
    no-loop
  when
    theCell: Cell( phase == Phase.BIRTH )
  then
    modify( theCell ){
        setCellState( CellState.LIVE ),
        setPhase( Phase.DONE );
    }
end
Copy to Clipboard Toggle word wrap

在这个阶段,将几个 Cell 对象更改为 LIVEDEAD 状态。当单元格变为 live 或 dead 时,示例使用规则 "Calculate …​" 中的 邻居 关系来迭代所有周围的单元,增加或减少 liveNeighbor 数。更改了计数的任何单元都设置为 EVALUATE 阶段,以确保在 ruleflow 过程评估阶段将其包含在原因中。

在为所有单元决定和设置实时数后,ruleflow 进程将结束。如果用户最初点击 Start,则决策引擎会在此时重新启动 ruleflow。如果用户最初单击 Next Generation,用户可以请求另一个生成。

规则 "Calculate …​"

rule "Calculate Live"
    ruleflow-group "calculate"
    lock-on-active
  when
    theCell: Cell( cellState == CellState.LIVE )
    Neighbor( cell == theCell, $neighbor : neighbor )
  then
    modify( $neighbor ){
        setLiveNeighbors( $neighbor.getLiveNeighbors() + 1 ),
        setPhase( Phase.EVALUATE );
    }
end

rule "Calculate Dead"
    ruleflow-group "calculate"
    lock-on-active
  when
    theCell: Cell( cellState == CellState.DEAD )
    Neighbor( cell == theCell, $neighbor : neighbor )
  then
    modify( $neighbor ){
        setLiveNeighbors( $neighbor.getLiveNeighbors() - 1 ),
        setPhase( Phase.EVALUATE );
    }
end
Copy to Clipboard Toggle word wrap

返回顶部
Red Hat logoGithubredditYoutubeTwitter

学习

尝试、购买和销售

社区

关于红帽文档

通过我们的产品和服务,以及可以信赖的内容,帮助红帽用户创新并实现他们的目标。 了解我们当前的更新.

让开源更具包容性

红帽致力于替换我们的代码、文档和 Web 属性中存在问题的语言。欲了解更多详情,请参阅红帽博客.

關於紅帽

我们提供强化的解决方案,使企业能够更轻松地跨平台和环境(从核心数据中心到网络边缘)工作。

Theme

© 2025 Red Hat