activiti5/6 系列之--BpmnModel使用

BpmnModel对象,是activiti动态部署中很重要的一个对象,如果BpmnModel对象不能深入的理解,那可能如果自己需要开发一套流程设计器,使用bpmn-js使用前端或者C/S展现流程流转而不是使用工作流引擎,就显得力不从心。例如,web设计器:

当activiti web设计器设计的时候,存储格式是自定义的json对象,那现在问题来了,我们怎么把我们自己的json格式转化为标准的bpmn需要的xml文件呢?这一点很重要?所以这也是本节课需要重点讲解的地方,大家实际开发可以举一反三。灵活的运用到项目中。

1.1.1. BpmnModel使用
因为平时我们在使用的时候,展示流程图没有使用是默认的流程生成的这种方式,所以这里坐标信息,暂时不演示,主要演示节点等的核心功能。

1.1.1.1. eclipse绘制流程
为了方便演示,这里我们先在eclipse中绘制一个简单的流程。具体的流程图如下:

流程图的xml文件如下:直接用文本打开bpmn文件即可:

<?xml version="1.0" encoding="UTF-8"?>
<definitions xmlns="http://www.omg.org/spec/BPMN/20100524/MODEL" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:activiti="http://activiti.org/bpmn" xmlns:bpmndi="http://www.omg.org/spec/BPMN/20100524/DI" xmlns:omgdc="http://www.omg.org/spec/DD/20100524/DC" xmlns:omgdi="http://www.omg.org/spec/DD/20100524/DI" typeLanguage="http://www.w3.org/2001/XMLSchema" expressionLanguage="http://www.w3.org/1999/XPath" targetNamespace="http://www.activiti.org/test">
  <process id="process1" isExecutable="true">
    <startEvent id="start1shareniu" name="start1shareniu"></startEvent>
    <sequenceFlow id="starttouserTask" name="starttouserTask" sourceRef="start1shareniu" targetRef="userTask1shareniu"></sequenceFlow>
    <userTask id="userTask1shareniu" name="userTask1shareniu"></userTask>
    <sequenceFlow id="userTasktoend" name="userTasktoend" sourceRef="userTask1shareniu" targetRef="endEventshareniu"></sequenceFlow>
    <endEvent id="endEventshareniu" name="endEventshareniu"></endEvent>
  </process>
  <bpmndi:BPMNDiagram id="BPMNDiagram_process1">
    <bpmndi:BPMNPlane bpmnElement="process1" id="BPMNPlane_process1">
      <bpmndi:BPMNShape bpmnElement="start1shareniu" id="BPMNShape_start1shareniu">
        <omgdc:Bounds height="35.0" width="35.0" x="70.0" y="150.0"></omgdc:Bounds>
      </bpmndi:BPMNShape>
      <bpmndi:BPMNShape bpmnElement="userTask1shareniu" id="BPMNShape_userTask1shareniu">
        <omgdc:Bounds height="60.0" width="100.0" x="180.0" y="110.0"></omgdc:Bounds>
      </bpmndi:BPMNShape>
      <bpmndi:BPMNShape bpmnElement="endEventshareniu" id="BPMNShape_endEventshareniu">
        <omgdc:Bounds height="35.0" width="35.0" x="380.0" y="76.0"></omgdc:Bounds>
      </bpmndi:BPMNShape>
      <bpmndi:BPMNEdge bpmnElement="starttouserTask" id="BPMNEdge_starttouserTask">
        <omgdi:waypoint x="87.0" y="150.0"></omgdi:waypoint>
        <omgdi:waypoint x="100.0" y="139.0"></omgdi:waypoint>
        <omgdi:waypoint x="180.0" y="140.0"></omgdi:waypoint>
        <bpmndi:BPMNLabel>
          <omgdc:Bounds height="14.0" width="100.0" x="87.0" y="150.0"></omgdc:Bounds>
        </bpmndi:BPMNLabel>
      </bpmndi:BPMNEdge>
      <bpmndi:BPMNEdge bpmnElement="userTasktoend" id="BPMNEdge_userTasktoend">
        <omgdi:waypoint x="280.0" y="140.0"></omgdi:waypoint>
        <omgdi:waypoint x="324.0" y="129.0"></omgdi:waypoint>
        <omgdi:waypoint x="324.0" y="93.0"></omgdi:waypoint>
        <omgdi:waypoint x="380.0" y="93.0"></omgdi:waypoint>
        <bpmndi:BPMNLabel>
          <omgdc:Bounds height="14.0" width="100.0" x="414.0" y="126.0"></omgdc:Bounds>
        </bpmndi:BPMNLabel>
      </bpmndi:BPMNEdge>
    </bpmndi:BPMNPlane>
  </bpmndi:BPMNDiagram>
</definitions>

1.1.1.2. 自己生成

下面的代码,就是生成这个bpmnmodel 核心代码,代码如下所示:

//实例化BpmnModel对象
BpmnModel bpmnModel=new BpmnModel();
//开始节点的属性
StartEvent startEvent=new StartEvent();
startEvent.setId("start1shareniu");
startEvent.setName("start1shareniu");
//普通的UserTask节点
UserTask userTask=new UserTask();
userTask.setId("userTask1shareniu");
userTask.setName("userTask1shareniu");
//结束节点属性
EndEvent endEvent=new EndEvent();
endEvent.setId("endEventshareniu");
endEvent.setName("endEventshareniu");
//连线信息
List<SequenceFlow> sequenceFlows=new ArrayList<SequenceFlow>();
List<SequenceFlow> toEnd=new ArrayList<SequenceFlow>();
SequenceFlow s1=new SequenceFlow();
s1.setId("starttouserTask");
s1.setName("starttouserTask");
s1.setSourceRef("start1shareniu");
s1.setTargetRef("userTask1shareniu");
sequenceFlows.add(s1);
SequenceFlow s2=new SequenceFlow();
s2.setId("userTasktoend");
s2.setName("userTasktoend");
s2.setSourceRef("userTask1shareniu");
s2.setTargetRef("endEventshareniu");
toEnd.add(s2);
startEvent.setOutgoingFlows(sequenceFlows);
userTask.setOutgoingFlows(toEnd);
userTask.setIncomingFlows(sequenceFlows);
endEvent.setIncomingFlows(toEnd);
//Process对象
Process process=new Process();
process.setId("process1");
process.addFlowElement(startEvent);
process.addFlowElement(s1);
process.addFlowElement(userTask);
process.addFlowElement(s2);
process.addFlowElement(endEvent);
bpmnModel.addProcess(process);

上面的代码,我们已经写好了bpmnmodel绘制的流程,那我们怎么知道对还是不对呢?下面就开始将我们的bpmnmodel对象转化为标准的xml文件看一下。

1.1.2. BpmnModel转化xml
将上面的对象转化为标准的xml代码如下所示:

//bpmnModel 转换为标准的bpmn xml文件

BpmnXMLConverter bpmnXMLConverter=new BpmnXMLConverter();

byte[] convertToXML = bpmnXMLConverter.convertToXML(bpmnModel);

String bytes=new String(convertToXML);

System.out.println(bytes);

运行程序,看一下程序的输出如下:

<?xml version="1.0" encoding="UTF-8"?>
<definitions xmlns="http://www.omg.org/spec/BPMN/20100524/MODEL" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:activiti="http://activiti.org/bpmn" xmlns:bpmndi="http://www.omg.org/spec/BPMN/20100524/DI" xmlns:omgdc="http://www.omg.org/spec/DD/20100524/DC" xmlns:omgdi="http://www.omg.org/spec/DD/20100524/DI" typeLanguage="http://www.w3.org/2001/XMLSchema" expressionLanguage="http://www.w3.org/1999/XPath" targetNamespace="http://www.activiti.org/test">
  <process id="process1" isExecutable="true">
    <startEvent id="start1shareniu" name="start1shareniu"></startEvent>
    <sequenceFlow id="starttouserTask" name="starttouserTask" sourceRef="start1shareniu" targetRef="userTask1shareniu"></sequenceFlow>
    <userTask id="userTask1shareniu" name="userTask1shareniu"></userTask>
    <sequenceFlow id="userTasktoend" name="userTasktoend" sourceRef="userTask1shareniu" targetRef="endEventshareniu"></sequenceFlow>
    <endEvent id="endEventshareniu" name="endEventshareniu"></endEvent>
  </process>
  <bpmndi:BPMNDiagram id="BPMNDiagram_process1">
    <bpmndi:BPMNPlane bpmnElement="process1" id="BPMNPlane_process1"></bpmndi:BPMNPlane>
  </bpmndi:BPMNDiagram>
</definitions>

我们看到转化的xml文件,对比eclipse绘制流程的xml,除了坐标没有,其他的都是正确的。那我们怎么验证我们生成的xml是正确的呢?因为转化成功,也不一定可以使用的。接下来看一下bpmnmodel如何验证。

1.1.3. BpmnModel验证

验证的方法代码如下所示:

//验证bpmnModel 是否是正确的bpmn xml文件

ProcessValidatorFactory processValidatorFactory=new ProcessValidatorFactory();

ProcessValidator defaultProcessValidator = processValidatorFactory.createDefaultProcessValidator();

//验证失败信息的封装ValidationError

List<ValidationError> validate = defaultProcessValidator.validate(bpmnModel);

System.out.println(validate.size());

需要说明的是:ValidationError封装的是验证信息,如果size为0说明,bpmnmodel正确,大于0,说明自定义的bpmnmodel是错误的,不可以使用的。

验证还是很有必要使用的,因为流程部署的时候,我们最好验证一次,没有问题在部署。

原文地址:https://www.cnblogs.com/zhjh256/p/10021352.html