Activiti使用Java代码创建Bpmn流程信息

这些操作,是对activiti入门之后的扩展,先了解了activiti的七大Service与23张表,理解这里会非常的迅速。
java代码

package com.activiti;

import org.activiti.bpmn.BpmnAutoLayout;
import org.activiti.bpmn.converter.BpmnXMLConverter;
import org.activiti.bpmn.model.*;
import org.activiti.bpmn.model.Process;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;


/**
 * @author zhudunfeng
 * @date 2020/11/9 15:14
 */
@SpringBootTest
public class GenerateBpmnTest {
    @Test
    public void generateBpmn(){
        //创建bpmn模型
        BpmnModel model = new BpmnModel();
        Process process = new Process();
        model.addProcess(process);
        process.setId("my-process");

        //创建bpmn元素
        process.addFlowElement(createStartEvent());
        process.addFlowElement(createUserTask("task1", "First task", "fred"));
        process.addFlowElement(createUserTask("task2", "Second task", "john"));
        process.addFlowElement(createEndEvent());


        process.addFlowElement(createSequenceFlow("start", "task1"));
        process.addFlowElement(createSequenceFlow("task1", "task2"));
        process.addFlowElement(createSequenceFlow("task2", "end"));


        // 2.生成BPMN自动布局
        new BpmnAutoLayout(model).execute();

        BpmnXMLConverter bpmnXMLConverter=new BpmnXMLConverter();
        byte[] convertToXML = bpmnXMLConverter.convertToXML(model);
        String bytes=new String(convertToXML);
        bytes = bytes.replaceAll("&lt;","<").replaceAll("&gt;",">");
        System.out.println(bytes);



    }

    //创建task
    protected UserTask createUserTask(String id, String name, String assignee) {
        UserTask userTask = new UserTask();
        userTask.setName(name);
        userTask.setId(id);
        userTask.setAssignee(assignee);
        return userTask;
    }


    //创建箭头
    protected SequenceFlow createSequenceFlow(String from, String to) {
        SequenceFlow flow = new SequenceFlow();
        flow.setSourceRef(from);
        flow.setTargetRef(to);
        return flow;
    }


    protected StartEvent createStartEvent() {
        StartEvent startEvent = new StartEvent();
        startEvent.setId("start");
        return startEvent;
    }


    protected EndEvent createEndEvent() {
        EndEvent endEvent = new EndEvent();
        endEvent.setId("end");
        return endEvent;
    }



}

生成的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="my-process" isExecutable="true">
    <startEvent id="start"></startEvent>
    <userTask id="task1" name="First task" activiti:assignee="fred"></userTask>
    <userTask id="task2" name="Second task" activiti:assignee="john"></userTask>
    <endEvent id="end"></endEvent>
    <sequenceFlow id="sequenceFlow-535a8ab6-16a4-4502-a53a-2ef7d05da7a3" sourceRef="start" targetRef="task1"></sequenceFlow>
    <sequenceFlow id="sequenceFlow-471a7e10-06c3-4077-89bc-0e6064fc3c04" sourceRef="task1" targetRef="task2"></sequenceFlow>
    <sequenceFlow id="sequenceFlow-3456ceb4-a4ae-4804-bbef-dbddaa90da10" sourceRef="task2" targetRef="end"></sequenceFlow>
  </process>
  <bpmndi:BPMNDiagram id="BPMNDiagram_my-process">
    <bpmndi:BPMNPlane bpmnElement="my-process" id="BPMNPlane_my-process">
      <bpmndi:BPMNShape bpmnElement="start" id="BPMNShape_start">
        <omgdc:Bounds height="30.0" width="30.0" x="0.0" y="15.0"></omgdc:Bounds>
      </bpmndi:BPMNShape>
      <bpmndi:BPMNShape bpmnElement="end" id="BPMNShape_end">
        <omgdc:Bounds height="30.0" width="30.0" x="380.0" y="15.0"></omgdc:Bounds>
      </bpmndi:BPMNShape>
      <bpmndi:BPMNShape bpmnElement="task1" id="BPMNShape_task1">
        <omgdc:Bounds height="60.0" width="100.0" x="80.0" y="0.0"></omgdc:Bounds>
      </bpmndi:BPMNShape>
      <bpmndi:BPMNShape bpmnElement="task2" id="BPMNShape_task2">
        <omgdc:Bounds height="60.0" width="100.0" x="230.0" y="0.0"></omgdc:Bounds>
      </bpmndi:BPMNShape>
      <bpmndi:BPMNEdge bpmnElement="sequenceFlow-3456ceb4-a4ae-4804-bbef-dbddaa90da10" id="BPMNEdge_sequenceFlow-3456ceb4-a4ae-4804-bbef-dbddaa90da10">
        <omgdi:waypoint x="330.0" y="30.0"></omgdi:waypoint>
        <omgdi:waypoint x="342.0" y="30.0"></omgdi:waypoint>
        <omgdi:waypoint x="342.0" y="30.000000000000004"></omgdi:waypoint>
        <omgdi:waypoint x="380.0" y="30.000000000000004"></omgdi:waypoint>
      </bpmndi:BPMNEdge>
      <bpmndi:BPMNEdge bpmnElement="sequenceFlow-471a7e10-06c3-4077-89bc-0e6064fc3c04" id="BPMNEdge_sequenceFlow-471a7e10-06c3-4077-89bc-0e6064fc3c04">
        <omgdi:waypoint x="180.0" y="30.0"></omgdi:waypoint>
        <omgdi:waypoint x="192.0" y="30.0"></omgdi:waypoint>
        <omgdi:waypoint x="192.0" y="30.000000000000007"></omgdi:waypoint>
        <omgdi:waypoint x="230.0" y="30.000000000000007"></omgdi:waypoint>
      </bpmndi:BPMNEdge>
      <bpmndi:BPMNEdge bpmnElement="sequenceFlow-535a8ab6-16a4-4502-a53a-2ef7d05da7a3" id="BPMNEdge_sequenceFlow-535a8ab6-16a4-4502-a53a-2ef7d05da7a3">
        <omgdi:waypoint x="30.0" y="30.0"></omgdi:waypoint>
        <omgdi:waypoint x="42.0" y="30.0"></omgdi:waypoint>
        <omgdi:waypoint x="42.0" y="30.000000000000007"></omgdi:waypoint>
        <omgdi:waypoint x="80.0" y="30.000000000000007"></omgdi:waypoint>
      </bpmndi:BPMNEdge>
    </bpmndi:BPMNPlane>
  </bpmndi:BPMNDiagram>
</definitions>
posted @ 2020-11-09 16:38  debug的勇士  阅读(2749)  评论(0编辑  收藏  举报