Drools开源业务规则引擎(五)- jBPM流程图元素介绍

文章目录

Drools开源业务规则引擎(五)- jBPM流程图元素介绍

阅前须知:

​ 根据本文操作前,请先根据上一篇文章《Drools开源业务规则引擎(四)- 规则流(rule flow)及手把手教你构建jBPM项目》搭建好相关开发环境。博主会在空闲时间内持续更新本文

1.process

在 jBPM 中,process 是用于定义和配置业务流程的关键元素之一,通过BPMN 2.0规范中的<process>元素表示。process表示业务流程的起始和结束点。它定义了一个独立的业务流程,由一系列的活动、网关、事件和连接组成。每个<process>元素都有一个唯一的ID,用于在 jBPM 中唯一标识该流程。

主要属性如下:

  • Id:流程的唯一标识符。每个流程都必须有一个唯一的ID。
  • Name:流程的名称。
  • Version:流程的版本号。
  • Package Name:流程所属的包名称。(一般为流程文件所在包名)
  • Is Executable:是否可执行。

示例

2.startEvent

开始事件(例如消息、定时器、信号等),用于启动一个流程实例。它标记了流程的开始,作为执行流程的入口点。

示例

3.Connections

连接器,用于在不同的流程节点之间传递数据和信息

3.1.sequenceFlow

顺序流是连接流程中不同活动的一种方式,用于定义活动之间的顺序关系和流程控制。

示例

3.2.Association

关联(Association)是一种连接流程图元素和相关信息或文档的方式。它用于表示两个元素之间的关系,而不影响流程的控制流。关联与顺序流(Sequence Flow)不同,顺序流表示流程的顺序性和传递性,而关联则表示额外的信息和关系。关联不会影响流程的流转,只提供了一种可选的附加信息。

  • 数据关联(Data Association)用于在流程中传递和关联数据。它可以将输入数据传递给任务、子流程或事件,并将结果数据传递回流程中。

    • 输入数据关联:通过将数据关联到任务、子流程或事件中,可以将必要的输入数据传递给它们。

    • 输出数据关联:当任务、子流程或事件完成后,可以将结果数据关联回流程中。输出数据关联通常发生在任务、子流程或事件的完成之后,把结果数据传递给流程的下一个节点。

4.Activities

活动节点,如任务节点、自动节点等

4.1.businessRuleTask

jBPM 中的一种任务节点类型,用于执行业务规则。业务规则是指定义和描述业务逻辑的一组规则,它们可以在应用程序中灵活地配置和调整,而无需修改代码。业务规则通常用于根据一组预定义的条件和约束来进行决策和执行特定的操作。

在 jBPM 中的流程中,可以使用 "Business Rule Task" 来执行业务规则。这个任务节点类型可以与规则引擎(如Drools规则引擎)进行集成,以便在流程执行过程中应用和评估业务规则。通过配置 "Business Rule Task",可以指定要应用的规则集,以及规则的输入参数和输出结果。当流程执行到该任务节点时,规则引擎会根据预定义的规则集对输入参数进行计算和评估,并生成相应的输出结果。 "Business Rule Task" 的执行结果可以被其他任务节点使用,或者影响流程的走向和后续动作。它使业务逻辑得以动态调整和变化,从而提供了更大的灵活性和可扩展性。

主要属性如下:

  • Name:任务节点的名称,用于在流程定义中标识该任务节点。
  • Rule Flow Group:指定要使用的规则流组。规则流组是一组相关规则的集合,可以根据需要对规则进行组织和管理。
  • On Entry Script:任务节点进入时执行的脚本或表达式,支持Java,MVEL两种语言。这些脚本或表达式通常用于在任务节点执行前执行一些预处理逻辑、初始化变量、设置环境等。
  • On Exit Script:任务节点离开时执行的脚本或表达式,支持Java,MVEL两种语言。

示例

4.2.scriptTask

脚本任务(Script Task)表示在流程中执行自定义脚本或代码的操作。脚本任务通常用于执行一些简单的操作,而无需创建独立的服务或规则任务。

脚本任务通常用于以下几种情况:

  • 计算或转换数据:执行一些简单的计算或数据转换操作。
  • 校验或判断条件:脚本任务可以执行一些条件判断,根据不同的条件来决定流程的走向。
  • 调用外部系统或服务:例如,使用Java脚本调用RESTful API。

示例

5.Gateways

网关在流程图中用于控制流程的分支和合并,根据不同的条件或规则来选择不同的路径。

5.1.exclusiveGateway

排他网关,也称为分支网关。排他网关用于在流程中做出简单的选择,仅选择一个满足条件的路径。每个条件表达式都与一个出口连接相关联,只有满足条件的出口才会被选择。

主要属性如下:

  • 默认出口(Default Flow):排他网关可以有一个默认出口,当其他条件都不满足时,流程会选择默认出口。默认出口通常不需要设置条件,它只是用于在没有其他路径满足时作为备用路径。

  • 条件表达式(Condition Expression):对于排他网关的每个出口,可以设置一个条件表达式。条件表达式用于评估在特定情况下选择哪个出口。条件表达式可以是简单的布尔表达式,也可以是复杂的规则表达式。例如,条件表达式可以使用流程变量、业务规则或脚本语言来定义。

  • 优先级(Priority):表示出口顺序流的执行优先级。值越小,优先级越高。

  • 网关用法(Gateway Direction):

    • 分支(Diverging):分支网关用于流程的分支,将流程的执行分发到不同的路径上。在分支网关中,有一个入口和多个出口,根据条件或规则选择不同的出口路径。分支网关的方向指向多个出口,表示流程将沿着这些出口执行不同的路径。
    • 合并(Converging):合并网关用于流程的合并,将来自不同路径的流程实例汇聚到一个节点上。在合并网关中,有多个入口和一个出口,当前需要等待所有入口上的流程实例到达合并网关时,才会将流程实例汇聚到下一个节点。合并网关的方向指向一个出口,表示流程将汇聚到该出口继续执行。

    • 循环(Converging and Diverging):循环网关用于对流程进行循环执行,即将流程返回到之前的某个节点或循环体中重复执行。循环网关在进入循环体之前会进行条件的判断,如果满足条件,则流程会回到指定的节点或循环体继续执行。循环网关既有分支的方向,也有合并的方向。

示例

xml 复制代码
<?xml version="1.0" encoding="UTF-8"?>
<bpmn2:definitions xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:bpmn2="http://www.omg.org/spec/BPMN/20100524/MODEL" xmlns:bpmndi="http://www.omg.org/spec/BPMN/20100524/DI" xmlns:dc="http://www.omg.org/spec/DD/20100524/DC" xmlns:di="http://www.omg.org/spec/DD/20100524/DI" xmlns:g="http://www.jboss.org/drools/flow/gpd" xmlns:tns="http://www.jboss.org/drools" xmlns="http://www.jboss.org/drools" xsi:schemaLocation="http://www.omg.org/spec/BPMN/20100524/MODEL BPMN20.xsd http://www.jboss.org/drools drools.xsd http://www.bpsim.org/schemas/1.0 bpsim.xsd" id="Definition" expressionLanguage="http://www.mvel.org/2.0" targetNamespace="http://www.jboss.org/drools" typeLanguage="http://www.java.com/javaTypes">
  <bpmn2:process id="Process_2" tns:version="1" tns:packageName="drools.flow3" tns:adHoc="false" name="流程案例" isExecutable="true">
    <bpmn2:startEvent id="StartEvent_1" name="流程入口点">
      <bpmn2:outgoing>SequenceFlow_1</bpmn2:outgoing>
    </bpmn2:startEvent>
    <bpmn2:sequenceFlow id="SequenceFlow_1" tns:priority="1" name="c1" sourceRef="StartEvent_1" targetRef="ExclusiveGateway_1"/>
    <bpmn2:exclusiveGateway id="ExclusiveGateway_1" name="效验年龄" gatewayDirection="Diverging" default="SequenceFlow_4">
      <bpmn2:incoming>SequenceFlow_1</bpmn2:incoming>
      <bpmn2:outgoing>SequenceFlow_3</bpmn2:outgoing>
      <bpmn2:outgoing>SequenceFlow_4</bpmn2:outgoing>
    </bpmn2:exclusiveGateway>
    <bpmn2:sequenceFlow id="SequenceFlow_3" tns:priority="1" name="年龄小于20" sourceRef="ExclusiveGateway_1" targetRef="BusinessRuleTask_2">
      <bpmn2:conditionExpression xsi:type="bpmn2:tFormalExpression" id="FormalExpression_3" language="http://www.jboss.org/drools/rule">UserIn(age &lt; 20);</bpmn2:conditionExpression>
    </bpmn2:sequenceFlow>
    <bpmn2:sequenceFlow id="SequenceFlow_4" tns:priority="999" name="默认分支" sourceRef="ExclusiveGateway_1" targetRef="BusinessRuleTask_1"/>
    <bpmn2:businessRuleTask id="BusinessRuleTask_2" tns:ruleFlowGroup="rfg_1" name="规则流1">
      <bpmn2:extensionElements>
        <tns:onEntry-script scriptFormat="http://www.java.com/java">
          <tns:script>System.out.println(&quot;Entry:进入rfg_1&quot;);</tns:script>
        </tns:onEntry-script>
        <tns:onExit-script scriptFormat="http://www.java.com/java">
          <tns:script></tns:script>
        </tns:onExit-script>
      </bpmn2:extensionElements>
      <bpmn2:incoming>SequenceFlow_3</bpmn2:incoming>
      <bpmn2:outgoing>SequenceFlow_2</bpmn2:outgoing>
    </bpmn2:businessRuleTask>
    <bpmn2:sequenceFlow id="SequenceFlow_2" tns:priority="1" name="" sourceRef="BusinessRuleTask_2" targetRef="ExclusiveGateway_2"/>
    <bpmn2:businessRuleTask id="BusinessRuleTask_1" tns:ruleFlowGroup="bottom" name="规则流3">
      <bpmn2:extensionElements>
        <tns:onEntry-script scriptFormat="http://www.java.com/java">
          <tns:script>System.out.println(&quot;进入默认分支&quot;);</tns:script>
        </tns:onEntry-script>
        <tns:onExit-script scriptFormat="http://www.java.com/java">
          <tns:script></tns:script>
        </tns:onExit-script>
      </bpmn2:extensionElements>
      <bpmn2:incoming>SequenceFlow_4</bpmn2:incoming>
      <bpmn2:outgoing>SequenceFlow_5</bpmn2:outgoing>
    </bpmn2:businessRuleTask>
    <bpmn2:exclusiveGateway id="ExclusiveGateway_2" name="执行结果合并" gatewayDirection="Converging">
      <bpmn2:incoming>SequenceFlow_2</bpmn2:incoming>
      <bpmn2:incoming>SequenceFlow_5</bpmn2:incoming>
      <bpmn2:outgoing>SequenceFlow_6</bpmn2:outgoing>
    </bpmn2:exclusiveGateway>
    <bpmn2:endEvent id="EndEvent_1" name="流程结束点">
      <bpmn2:incoming>SequenceFlow_6</bpmn2:incoming>
    </bpmn2:endEvent>
    <bpmn2:sequenceFlow id="SequenceFlow_5" tns:priority="1" sourceRef="BusinessRuleTask_1" targetRef="ExclusiveGateway_2"/>
    <bpmn2:sequenceFlow id="SequenceFlow_6" tns:priority="1" sourceRef="ExclusiveGateway_2" targetRef="EndEvent_1"/>
  </bpmn2:process>
  <bpmndi:BPMNDiagram id="BPMNDiagram_1" name="流程案例">
    <bpmndi:BPMNPlane id="BPMNPlane_Process_1" bpmnElement="Process_2">
      <bpmndi:BPMNShape id="BPMNShape_StartEvent_1" bpmnElement="StartEvent_1">
        <dc:Bounds height="36.0" width="36.0" x="142.0" y="122.0"/>
      </bpmndi:BPMNShape>
      <bpmndi:BPMNShape id="BPMNShape_EndEvent_1" bpmnElement="EndEvent_1">
        <dc:Bounds height="36.0" width="36.0" x="790.0" y="170.0"/>
      </bpmndi:BPMNShape>
      <bpmndi:BPMNShape id="BPMNShape_ExclusiveGateway_1" bpmnElement="ExclusiveGateway_1" isMarkerVisible="true">
        <dc:Bounds height="50.0" width="50.0" x="310.0" y="190.0"/>
      </bpmndi:BPMNShape>
      <bpmndi:BPMNShape id="BPMNShape_ExclusiveGateway_2" bpmnElement="ExclusiveGateway_2" isMarkerVisible="true">
        <dc:Bounds height="50.0" width="50.0" x="650.0" y="200.0"/>
      </bpmndi:BPMNShape>
      <bpmndi:BPMNShape id="BPMNShape_BusinessRuleTask_2" bpmnElement="BusinessRuleTask_2">
        <dc:Bounds height="50.0" width="110.0" x="460.0" y="90.0"/>
      </bpmndi:BPMNShape>
      <bpmndi:BPMNShape id="BPMNShape_BusinessRuleTask_1" bpmnElement="BusinessRuleTask_1">
        <dc:Bounds height="50.0" width="110.0" x="450.0" y="310.0"/>
      </bpmndi:BPMNShape>
      <bpmndi:BPMNEdge id="BPMNEdge_SequenceFlow_1" bpmnElement="SequenceFlow_1" sourceElement="BPMNShape_StartEvent_1" targetElement="BPMNShape_ExclusiveGateway_1">
        <di:waypoint xsi:type="dc:Point" x="160.0" y="158.0"/>
        <di:waypoint xsi:type="dc:Point" x="160.0" y="215.0"/>
        <di:waypoint xsi:type="dc:Point" x="310.0" y="215.0"/>
      </bpmndi:BPMNEdge>
      <bpmndi:BPMNEdge id="BPMNEdge_SequenceFlow_2" bpmnElement="SequenceFlow_2" sourceElement="BPMNShape_BusinessRuleTask_2" targetElement="BPMNShape_ExclusiveGateway_2">
        <di:waypoint xsi:type="dc:Point" x="570.0" y="115.0"/>
        <di:waypoint xsi:type="dc:Point" x="675.0" y="115.0"/>
        <di:waypoint xsi:type="dc:Point" x="675.0" y="200.0"/>
      </bpmndi:BPMNEdge>
      <bpmndi:BPMNEdge id="BPMNEdge_SequenceFlow_3" bpmnElement="SequenceFlow_3" sourceElement="BPMNShape_ExclusiveGateway_1" targetElement="BPMNShape_BusinessRuleTask_2">
        <di:waypoint xsi:type="dc:Point" x="335.0" y="190.0"/>
        <di:waypoint xsi:type="dc:Point" x="335.0" y="115.0"/>
        <di:waypoint xsi:type="dc:Point" x="460.0" y="115.0"/>
      </bpmndi:BPMNEdge>
      <bpmndi:BPMNEdge id="BPMNEdge_SequenceFlow_4" bpmnElement="SequenceFlow_4" sourceElement="BPMNShape_ExclusiveGateway_1" targetElement="BPMNShape_BusinessRuleTask_1">
        <di:waypoint xsi:type="dc:Point" x="335.0" y="241.0"/>
        <di:waypoint xsi:type="dc:Point" x="335.0" y="335.0"/>
        <di:waypoint xsi:type="dc:Point" x="450.0" y="335.0"/>
      </bpmndi:BPMNEdge>
      <bpmndi:BPMNEdge id="BPMNEdge_SequenceFlow_5" bpmnElement="SequenceFlow_5" sourceElement="BPMNShape_BusinessRuleTask_1" targetElement="BPMNShape_ExclusiveGateway_2">
        <di:waypoint xsi:type="dc:Point" x="560.0" y="335.0"/>
        <di:waypoint xsi:type="dc:Point" x="675.0" y="335.0"/>
        <di:waypoint xsi:type="dc:Point" x="675.0" y="251.0"/>
      </bpmndi:BPMNEdge>
      <bpmndi:BPMNEdge id="BPMNEdge_SequenceFlow_6" bpmnElement="SequenceFlow_6" sourceElement="BPMNShape_ExclusiveGateway_2" targetElement="BPMNShape_EndEvent_1">
        <di:waypoint xsi:type="dc:Point" x="701.0" y="225.0"/>
        <di:waypoint xsi:type="dc:Point" x="808.0" y="225.0"/>
        <di:waypoint xsi:type="dc:Point" x="808.0" y="206.0"/>
      </bpmndi:BPMNEdge>
    </bpmndi:BPMNPlane>
  </bpmndi:BPMNDiagram>
</bpmn2:definitions>

6.endEvent

定义流程的结束事件。常见的结束事件类型有完成、错误和取消。

示例

相关推荐
架构文摘JGWZ2 小时前
Java 23 的12 个新特性!!
java·开发语言·学习
拾光师3 小时前
spring获取当前request
java·后端·spring
aPurpleBerry3 小时前
neo4j安装启动教程+对应的jdk配置
java·neo4j
我是苏苏3 小时前
Web开发:ABP框架2——入门级别的增删改查Demo
java·开发语言
xujinwei_gingko3 小时前
Spring IOC容器Bean对象管理-Java Config方式
java·spring
2301_789985943 小时前
Java语言程序设计基础篇_编程练习题*18.29(某个目录下的文件数目)
java·开发语言·学习
IT学长编程3 小时前
计算机毕业设计 教师科研信息管理系统的设计与实现 Java实战项目 附源码+文档+视频讲解
java·毕业设计·springboot·毕业论文·计算机毕业设计选题·计算机毕业设计开题报告·教师科研管理系统
m0_571957583 小时前
Java | Leetcode Java题解之第406题根据身高重建队列
java·leetcode·题解
程序猿小D4 小时前
第二百三十五节 JPA教程 - JPA Lob列示例
java·数据库·windows·oracle·jdk·jpa
Zd085 小时前
14.其他流(下篇)
java·前端·数据库