activiti5/6 系列之--Activiti 读取并转换BPMN2文件

统一的BPMN标准,对工作流的流程定义采用BPMN统一格式。BPMN流程文件可以使用eclipse bpmn2插件开发比如eclipse bpmn2 modeler或者idea activiti插件。

<?xml version="1.0" encoding="UTF-8"?>
<!-- origin at X=0.0 Y=0.0 -->
<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:java="http://www.java.com/javaTypes" 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" exporter="org.eclipse.bpmn2.modeler.core" exporterVersion="1.1.6.Final-v20160330-1631-B1306" expressionLanguage="http://www.mvel.org/2.0" targetNamespace="http://www.jboss.org/drools" typeLanguage="http://www.java.com/javaTypes">
  <bpmn2:itemDefinition id="ItemDefinition_1" isCollection="false" structureRef="String"/>
  <bpmn2:itemDefinition id="ItemDefinition_2" isCollection="false" structureRef="Integer"/>
  <bpmn2:itemDefinition id="ItemDefinition_3" isCollection="false" structureRef="Boolean"/>
  <bpmn2:process id="net.hs.cw.bpme.ta.flow_index_mainflow" tns:packageName="net.hs.cw.bpme.ta" name="flow_index_mainflow" isExecutable="true" processType="Private">
    <bpmn2:startEvent id="StartEvent_1" name="StartProcess">
      <bpmn2:extensionElements>
        <tns:metaData name="elementname">
          <tns:metaValue><![CDATA[StartProcess]]></tns:metaValue>
        </tns:metaData>
      </bpmn2:extensionElements>
      <bpmn2:outgoing>SequenceFlow_1</bpmn2:outgoing>
    </bpmn2:startEvent>
    <bpmn2:userTask id="UserTask_1" name="指数行情导入">
      <bpmn2:extensionElements>
        <tns:metaData name="elementname">
          <tns:metaValue><![CDATA[指数行情导入]]></tns:metaValue>
        </tns:metaData>
      </bpmn2:extensionElements>
      <bpmn2:incoming>SequenceFlow_1</bpmn2:incoming>
      <bpmn2:outgoing>SequenceFlow_2</bpmn2:outgoing>
      <bpmn2:ioSpecification id="InputOutputSpecification_2">
        <bpmn2:dataInput id="DataInput_8" itemSubjectRef="ItemDefinition_1" name="TaskName"/>
        <bpmn2:dataInput id="DataInput_9" itemSubjectRef="ItemDefinition_2" name="Priority"/>
        <bpmn2:dataInput id="DataInput_10" itemSubjectRef="ItemDefinition_1" name="Comment"/>
        <bpmn2:dataInput id="DataInput_11" itemSubjectRef="ItemDefinition_1" name="GroupId"/>
        <bpmn2:dataInput id="DataInput_12" itemSubjectRef="ItemDefinition_3" name="Skippable"/>
        <bpmn2:dataInput id="DataInput_13" itemSubjectRef="ItemDefinition_1" name="Content"/>
        <bpmn2:dataInput id="DataInput_14" itemSubjectRef="ItemDefinition_1" name="Locale"/>
        <bpmn2:inputSet id="_InputSet_209">
          <bpmn2:dataInputRefs>DataInput_8</bpmn2:dataInputRefs>
          <bpmn2:dataInputRefs>DataInput_9</bpmn2:dataInputRefs>
          <bpmn2:dataInputRefs>DataInput_10</bpmn2:dataInputRefs>
          <bpmn2:dataInputRefs>DataInput_11</bpmn2:dataInputRefs>
          <bpmn2:dataInputRefs>DataInput_12</bpmn2:dataInputRefs>
          <bpmn2:dataInputRefs>DataInput_13</bpmn2:dataInputRefs>
          <bpmn2:dataInputRefs>DataInput_14</bpmn2:dataInputRefs>
        </bpmn2:inputSet>
        <bpmn2:outputSet id="OutputSet_2" name="Output Set"/>
      </bpmn2:ioSpecification>
      <bpmn2:dataInputAssociation id="DataInputAssociation_8">
        <bpmn2:targetRef>DataInput_8</bpmn2:targetRef>
        <bpmn2:assignment id="Assignment_8">
          <bpmn2:from xsi:type="bpmn2:tFormalExpression" id="FormalExpression_15">ImpIndexFile</bpmn2:from>
          <bpmn2:to xsi:type="bpmn2:tFormalExpression" id="FormalExpression_16">DataInput_8</bpmn2:to>
        </bpmn2:assignment>
      </bpmn2:dataInputAssociation>
      <bpmn2:dataInputAssociation id="DataInputAssociation_9">
        <bpmn2:targetRef>DataInput_9</bpmn2:targetRef>
        <bpmn2:assignment id="Assignment_9">
          <bpmn2:from xsi:type="bpmn2:tFormalExpression" id="FormalExpression_17">1</bpmn2:from>
          <bpmn2:to xsi:type="bpmn2:tFormalExpression" id="FormalExpression_18">DataInput_9</bpmn2:to>
        </bpmn2:assignment>
      </bpmn2:dataInputAssociation>
      <bpmn2:dataInputAssociation id="DataInputAssociation_10">
        <bpmn2:targetRef>DataInput_10</bpmn2:targetRef>
      </bpmn2:dataInputAssociation>
      <bpmn2:dataInputAssociation id="DataInputAssociation_11">
        <bpmn2:targetRef>DataInput_11</bpmn2:targetRef>
      </bpmn2:dataInputAssociation>
      <bpmn2:dataInputAssociation id="DataInputAssociation_12">
        <bpmn2:targetRef>DataInput_12</bpmn2:targetRef>
        <bpmn2:assignment id="Assignment_12">
          <bpmn2:from xsi:type="bpmn2:tFormalExpression" id="FormalExpression_23">true</bpmn2:from>
          <bpmn2:to xsi:type="bpmn2:tFormalExpression" id="FormalExpression_24">DataInput_12</bpmn2:to>
        </bpmn2:assignment>
      </bpmn2:dataInputAssociation>
      <bpmn2:dataInputAssociation id="DataInputAssociation_13">
        <bpmn2:targetRef>DataInput_13</bpmn2:targetRef>
        <bpmn2:assignment id="Assignment_13">
          <bpmn2:from xsi:type="bpmn2:tFormalExpression" id="FormalExpression_25">{&quot;executeMode&quot;:&quot;manual&quot;}</bpmn2:from>
          <bpmn2:to xsi:type="bpmn2:tFormalExpression" id="FormalExpression_26">DataInput_13</bpmn2:to>
        </bpmn2:assignment>
      </bpmn2:dataInputAssociation>
      <bpmn2:dataInputAssociation id="DataInputAssociation_14">
        <bpmn2:targetRef>DataInput_14</bpmn2:targetRef>
        <bpmn2:assignment id="Assignment_14">
          <bpmn2:from xsi:type="bpmn2:tFormalExpression" id="FormalExpression_27">en-UK</bpmn2:from>
          <bpmn2:to xsi:type="bpmn2:tFormalExpression" id="FormalExpression_28">DataInput_14</bpmn2:to>
        </bpmn2:assignment>
      </bpmn2:dataInputAssociation>
    </bpmn2:userTask>
    <bpmn2:sequenceFlow id="SequenceFlow_1" tns:priority="1" sourceRef="StartEvent_1" targetRef="UserTask_1"/>
    <bpmn2:endEvent id="EndEvent_1" name="End Event 1">
      <bpmn2:extensionElements>
        <tns:metaData name="elementname">
          <tns:metaValue><![CDATA[End Event 1]]></tns:metaValue>
        </tns:metaData>
      </bpmn2:extensionElements>
      <bpmn2:incoming>SequenceFlow_2</bpmn2:incoming>
    </bpmn2:endEvent>
    <bpmn2:sequenceFlow id="SequenceFlow_2" tns:priority="1" sourceRef="UserTask_1" targetRef="EndEvent_1"/>
  </bpmn2:process>
  <bpmndi:BPMNDiagram id="BPMNDiagram_1">
    <bpmndi:BPMNPlane id="BPMNPlane_Process_1" bpmnElement="net.hs.cw.bpme.ta.flow_index_mainflow">
      <bpmndi:BPMNShape id="BPMNShape_StartEvent_1" bpmnElement="StartEvent_1">
        <dc:Bounds height="36.0" width="36.0" x="377.0" y="30.0"/>
        <bpmndi:BPMNLabel id="BPMNLabel_1">
          <dc:Bounds height="15.0" width="71.0" x="360.0" y="66.0"/>
        </bpmndi:BPMNLabel>
      </bpmndi:BPMNShape>
      <bpmndi:BPMNShape id="BPMNShape_UserTask_1" bpmnElement="UserTask_1">
        <dc:Bounds height="47.0" width="231.0" x="280.0" y="119.0"/>
        <bpmndi:BPMNLabel id="BPMNLabel_2">
          <dc:Bounds height="15.0" width="72.0" x="359.0" y="135.0"/>
        </bpmndi:BPMNLabel>
      </bpmndi:BPMNShape>
      <bpmndi:BPMNShape id="BPMNShape_EndEvent_1" bpmnElement="EndEvent_1">
        <dc:Bounds height="36.0" width="36.0" x="377.0" y="217.0"/>
        <bpmndi:BPMNLabel id="BPMNLabel_3">
          <dc:Bounds height="15.0" width="65.0" x="363.0" y="253.0"/>
        </bpmndi:BPMNLabel>
      </bpmndi:BPMNShape>
      <bpmndi:BPMNEdge id="BPMNEdge_SequenceFlow_1" bpmnElement="SequenceFlow_1" sourceElement="BPMNShape_StartEvent_1" targetElement="BPMNShape_UserTask_1">
        <di:waypoint xsi:type="dc:Point" x="395.0" y="66.0"/>
        <di:waypoint xsi:type="dc:Point" x="395.0" y="85.0"/>
        <di:waypoint xsi:type="dc:Point" x="395.0" y="119.0"/>
        <bpmndi:BPMNLabel id="BPMNLabel_4"/>
      </bpmndi:BPMNEdge>
      <bpmndi:BPMNEdge id="BPMNEdge_SequenceFlow_2" bpmnElement="SequenceFlow_2" sourceElement="BPMNShape_UserTask_1" targetElement="BPMNShape_EndEvent_1">
        <di:waypoint xsi:type="dc:Point" x="395.0" y="166.0"/>
        <di:waypoint xsi:type="dc:Point" x="395.0" y="191.0"/>
        <di:waypoint xsi:type="dc:Point" x="395.0" y="191.0"/>
        <di:waypoint xsi:type="dc:Point" x="395.0" y="217.0"/>
        <bpmndi:BPMNLabel id="BPMNLabel_5"/>
      </bpmndi:BPMNEdge>
    </bpmndi:BPMNPlane>
  </bpmndi:BPMNDiagram>
</bpmn2:definitions>

如下是对该类型文件的解析工具类和方法。

    public void init() throws XMLStreamException, IOException {
        for (int i = 0; i < bpmnLocation.length; i++) {
            InputStream is = bpmnLocation[i].getInputStream();
            byte[] encoded = IOUtils.toByteArray(is);
            String bpmnResource = new String(encoded, Charset.forName("UTF-8"));
            BpmnXMLConverter converter = new BpmnXMLConverter();
            XMLInputFactory factory = XMLInputFactory.newInstance();
            @Cleanup XMLStreamReader reader = factory.createXMLStreamReader(bpmnLocation[i].getInputStream());
            BpmnModel bpmnModel = converter.convertToBpmnModel(reader);
            Process process = bpmnModel.getProcesses().get(0);
            ProcessDefinition processDefinition = new ProcessDefinition();
            processDefinition.setProcessSource(bpmnResource);
            processDefinition.setProcessId(process.getId());
            processDefinition.setProcessName(process.getName());
            List<FlowElement> flowElementList = (List<FlowElement>) process.getFlowElements();
            Map<String,FlowElement> flowElementMap = new HashMap();
            for (FlowElement flowElement : flowElementList) {
                flowElementMap.put(flowElement.getId(),flowElement);
            }
            processDefinition.setFlowElementMap(flowElementMap);
            ProcessDataUtil.processDefinitionMap.put(process.getId(), processDefinition);
        }
    }

 

posted @ 2018-11-26 16:39  zhjh256  阅读(2564)  评论(0编辑  收藏  举报