为什么在使用org.activiti.bpmn.BpmnAutoLayout为没有它的bpmn文件生成DI信息时出现NullPointerException?

时间:2018-08-13 14:56:30

标签: java xml activiti bpmn

我可以问您一个关于在Maven依赖项activiti-bpmn-layout中使用BpmnAutoLayout类的问题吗?

我使用activiti-bpmn-model api以编程方式创建了一个不含DI信息(bpmnWithoutDI.bpmn)的XML格式.bpmn文件。我想使用BpmnAutoLayout类为bpmnWithoutDI.bpmn xml文件自动生成DI信息。但是当我运行程序时,出现以下异常:

The Exception Info

有人可以告诉我如何解决这个问题吗?

您可以从以下URL下载我的源代码:

https://drive.google.com/file/d/1V9LUZLLZ1c-3JWzcSqDyM7IyywKB7Bvj/view?usp=sharing

这是Eclipse maven项目的zip文件,其中包含我的Java源代码和输入文件bpmnWithoutDI.bpmn。您只需导入maven项目并运行AutoLayoutDemo.java即可重现异常。

我还在下面添加了我的代码:

AutoLayoutDemo.java

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;

import javax.xml.stream.XMLInputFactory;
import javax.xml.stream.XMLStreamException;
import javax.xml.stream.XMLStreamReader;

import org.activiti.bpmn.BpmnAutoLayout;
import org.activiti.bpmn.converter.BpmnXMLConverter;
import org.activiti.bpmn.model.BpmnModel;


public class AutoLayoutDemo {

    private BpmnModel convertXmlFileWithoutDIToBpmnModel(String fileName) throws FileNotFoundException, UnsupportedEncodingException, XMLStreamException {
        InputStream xmlStream = new FileInputStream(new File(fileName));
        XMLInputFactory xif = XMLInputFactory.newInstance();
        InputStreamReader in = new InputStreamReader(xmlStream, "UTF-8");
        XMLStreamReader xtr = xif.createXMLStreamReader(in);
        return new BpmnXMLConverter().convertToBpmnModel(xtr);
    }

    private void autoLayoutBpmnModel(BpmnModel model) {
        BpmnAutoLayout autoLayout = new BpmnAutoLayout(model);
        autoLayout.execute();
    }

    private void generateXmlFileWithDI(BpmnModel model, String fileName) {
        File file = new File(fileName);



        try (FileOutputStream fos = new FileOutputStream(file)) {

            if (!file.exists()) {
                file.createNewFile();
            }
            byte[] byteArray = new BpmnXMLConverter().convertToXML(model);
            fos.write(byteArray);
            fos.flush();
            fos.close();

            System.out.println("Done");

        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public static void main(String[] args) throws FileNotFoundException, UnsupportedEncodingException, XMLStreamException {
        AutoLayoutDemo demo = new AutoLayoutDemo();
        BpmnModel model = demo.convertXmlFileWithoutDIToBpmnModel("bpmnWithoutDI.bpmn");
        demo.autoLayoutBpmnModel(model);
        demo.generateXmlFileWithDI(model, "bpmnWithDI.bpmn");
    }

}

bpmnWithoutDI.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="process_dispute_extractor" name="dispute_extractor" isExecutable="true">
    <subProcess id="process_dispute_extractor-extract_urls" name="STEP extract_urls: This step opens the AVC portal and gathers all the unresolved chargeback urls">
      <startEvent id="process_dispute_extractor-extract_urls-start" name="start"></startEvent>
      <endEvent id="process_dispute_extractor-extract_urls-end" name="end"></endEvent>
      <exclusiveGateway id="process_dispute_extractor-extract_urls-0"></exclusiveGateway>
      <subProcess id="process_dispute_extractor-extract_urls-1" name="get the chargeback id">
        <startEvent id="process_dispute_extractor-extract_urls-1-0" name="Start"></startEvent>
        <exclusiveGateway id="process_dispute_extractor-extract_urls-1-1"></exclusiveGateway>
        <exclusiveGateway id="process_dispute_extractor-extract_urls-1-2"></exclusiveGateway>
        <serviceTask id="process_dispute_extractor-extract_urls-1-3" name="add to the list of urls to process" activiti:delegateExpression="process_dispute_extractor-extract_urls-1-3implementation"></serviceTask>
        <endEvent id="process_dispute_extractor-extract_urls-1-4" name="End"></endEvent>
      </subProcess>
      <exclusiveGateway id="process_dispute_extractor-extract_urls-2"></exclusiveGateway>
      <subProcess id="process_dispute_extractor-extract_urls-3" name="Send 150 urls to image analyzer process module">
        <startEvent id="process_dispute_extractor-extract_urls-3-0" name="Start"></startEvent>
        <serviceTask id="process_dispute_extractor-extract_urls-3-1" name="JUMP TO image_analyzer" activiti:delegateExpression="process_dispute_extractor-extract_urls-3-1implementation"></serviceTask>
        <endEvent id="process_dispute_extractor-extract_urls-3-2" name="End"></endEvent>
      </subProcess>
    </subProcess>
    <sequenceFlow id="process_dispute_extractor-extract_urls-1-1--process_dispute_extractor-extract_urls-1-2" name="Else" sourceRef="process_dispute_extractor-extract_urls-1-1" targetRef="process_dispute_extractor-extract_urls-1-2"></sequenceFlow>
    <sequenceFlow id="process_dispute_extractor-extract_urls-1-1--process_dispute_extractor-extract_urls-1-3" name="If the id hasn't been processed" sourceRef="process_dispute_extractor-extract_urls-1-1" targetRef="process_dispute_extractor-extract_urls-1-3"></sequenceFlow>
    <sequenceFlow id="process_dispute_extractor-extract_urls-1-3--process_dispute_extractor-extract_urls-1-2" sourceRef="process_dispute_extractor-extract_urls-1-3" targetRef="process_dispute_extractor-extract_urls-1-2"></sequenceFlow>
    <sequenceFlow id="process_dispute_extractor-extract_urls-1-0--process_dispute_extractor-extract_urls-1-1" sourceRef="process_dispute_extractor-extract_urls-1-0" targetRef="process_dispute_extractor-extract_urls-1-1"></sequenceFlow>
    <sequenceFlow id="process_dispute_extractor-extract_urls-1-2--process_dispute_extractor-extract_urls-1-4" sourceRef="process_dispute_extractor-extract_urls-1-2" targetRef="process_dispute_extractor-extract_urls-1-4"></sequenceFlow>
    <sequenceFlow id="process_dispute_extractor-extract_urls-0--process_dispute_extractor-extract_urls-1" name="For each url" sourceRef="process_dispute_extractor-extract_urls-0" targetRef="process_dispute_extractor-extract_urls-1"></sequenceFlow>
    <sequenceFlow id="process_dispute_extractor-extract_urls-1--process_dispute_extractor-extract_urls-0" sourceRef="process_dispute_extractor-extract_urls-1" targetRef="process_dispute_extractor-extract_urls-0"></sequenceFlow>
    <sequenceFlow id="process_dispute_extractor-extract_urls-3-0--process_dispute_extractor-extract_urls-3-1" sourceRef="process_dispute_extractor-extract_urls-3-0" targetRef="process_dispute_extractor-extract_urls-3-1"></sequenceFlow>
    <sequenceFlow id="process_dispute_extractor-extract_urls-3-1--process_dispute_extractor-extract_urls-3-2" sourceRef="process_dispute_extractor-extract_urls-3-1" targetRef="process_dispute_extractor-extract_urls-3-2"></sequenceFlow>
    <sequenceFlow id="process_dispute_extractor-extract_urls-2--process_dispute_extractor-extract_urls-3" name="For each time" sourceRef="process_dispute_extractor-extract_urls-2" targetRef="process_dispute_extractor-extract_urls-3"></sequenceFlow>
    <sequenceFlow id="process_dispute_extractor-extract_urls-3--process_dispute_extractor-extract_urls-2" sourceRef="process_dispute_extractor-extract_urls-3" targetRef="process_dispute_extractor-extract_urls-2"></sequenceFlow>
    <sequenceFlow id="process_dispute_extractor-extract_urls-0--process_dispute_extractor-extract_urls-2" name="Loop over" sourceRef="process_dispute_extractor-extract_urls-0" targetRef="process_dispute_extractor-extract_urls-2"></sequenceFlow>
    <sequenceFlow id="process_dispute_extractor-extract_urls-start--process_dispute_extractor-extract_urls-0" sourceRef="process_dispute_extractor-extract_urls-start" targetRef="process_dispute_extractor-extract_urls-0"></sequenceFlow>
    <sequenceFlow id="process_dispute_extractor-extract_urls-2--process_dispute_extractor-extract_urls-end" sourceRef="process_dispute_extractor-extract_urls-2" targetRef="process_dispute_extractor-extract_urls-end"></sequenceFlow>
    <startEvent id="process_dispute_extractor-start" name="Start"></startEvent>
    <endEvent id="process_dispute_extractor-end" name="End"></endEvent>
    <sequenceFlow id="process_dispute_extractor-start--process_dispute_extractor-extract_urls" sourceRef="process_dispute_extractor-start" targetRef="process_dispute_extractor-extract_urls"></sequenceFlow>
    <sequenceFlow id="process_dispute_extractor-extract_urls--process_dispute_extractor-end" sourceRef="process_dispute_extractor-extract_urls" targetRef="process_dispute_extractor-end"></sequenceFlow>
  </process>
</definitions>

谢谢!

2 个答案:

答案 0 :(得分:4)

您的正确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="process_dispute_extractor" name="dispute_extractor" isExecutable="true">
    <subProcess id="process_dispute_extractor-extract_urls" name="STEP extract_urls: This step opens the AVC portal and gathers all the unresolved chargeback urls">
      <startEvent id="process_dispute_extractor-extract_urls-start" name="start"></startEvent>
      <endEvent id="process_dispute_extractor-extract_urls-end" name="end"></endEvent>
      <exclusiveGateway id="process_dispute_extractor-extract_urls-0"></exclusiveGateway>
      <subProcess id="process_dispute_extractor-extract_urls-1" name="get the chargeback id">
        <startEvent id="process_dispute_extractor-extract_urls-1-0" name="Start"></startEvent>
        <exclusiveGateway id="process_dispute_extractor-extract_urls-1-1"></exclusiveGateway>
        <exclusiveGateway id="process_dispute_extractor-extract_urls-1-2"></exclusiveGateway>
        <serviceTask id="process_dispute_extractor-extract_urls-1-3" name="add to the list of urls to process" activiti:delegateExpression="process_dispute_extractor-extract_urls-1-3implementation"></serviceTask>
        <endEvent id="process_dispute_extractor-extract_urls-1-4" name="End"></endEvent>
        <sequenceFlow id="xprocess_dispute_extractor-extract_urls-1-2" sourceRef="process_dispute_extractor-extract_urls-1-1" targetRef="process_dispute_extractor-extract_urls-1-2"></sequenceFlow>
        <sequenceFlow id="process_dispute_extractor-extract_urls-1-1--process_dispute_extractor-extract_urls-1-3" name="If the id hasn't been processed" sourceRef="process_dispute_extractor-extract_urls-1-1" targetRef="process_dispute_extractor-extract_urls-1-3"></sequenceFlow>
        <sequenceFlow id="process_dispute_extractor-extract_urls-1-3--process_dispute_extractor-extract_urls-1-2" sourceRef="process_dispute_extractor-extract_urls-1-3" targetRef="process_dispute_extractor-extract_urls-1-2"></sequenceFlow>     
        <sequenceFlow id="process_dispute_extractor-extract_urls-1-0--process_dispute_extractor-extract_urls-1-1" sourceRef="process_dispute_extractor-extract_urls-1-0" targetRef="process_dispute_extractor-extract_urls-1-1"></sequenceFlow>     
        <sequenceFlow id="process_dispute_extractor-extract_urls-1-2--process_dispute_extractor-extract_urls-1-4" sourceRef="process_dispute_extractor-extract_urls-1-2" targetRef="process_dispute_extractor-extract_urls-1-4"></sequenceFlow>     
      </subProcess>
      <exclusiveGateway id="process_dispute_extractor-extract_urls-2"></exclusiveGateway>
      <sequenceFlow id="process_dispute_extractor-extract_urls-2--process_dispute_extractor-extract_urls-3" name="For each time" sourceRef="process_dispute_extractor-extract_urls-2" targetRef="process_dispute_extractor-extract_urls-3"></sequenceFlow>
      <sequenceFlow id="process_dispute_extractor-extract_urls-3--process_dispute_extractor-extract_urls-2" sourceRef="process_dispute_extractor-extract_urls-3" targetRef="process_dispute_extractor-extract_urls-2"></sequenceFlow>
      <sequenceFlow id="process_dispute_extractor-extract_urls-0--process_dispute_extractor-extract_urls-2" name="Loop over" sourceRef="process_dispute_extractor-extract_urls-0" targetRef="process_dispute_extractor-extract_urls-2"></sequenceFlow>
      <sequenceFlow id="process_dispute_extractor-extract_urls-start--process_dispute_extractor-extract_urls-0" sourceRef="process_dispute_extractor-extract_urls-start" targetRef="process_dispute_extractor-extract_urls-0"></sequenceFlow>
      <sequenceFlow id="process_dispute_extractor-extract_urls-2--process_dispute_extractor-extract_urls-end" sourceRef="process_dispute_extractor-extract_urls-2" targetRef="process_dispute_extractor-extract_urls-end"></sequenceFlow>     
      <subProcess id="process_dispute_extractor-extract_urls-3" name="Send 150 urls to image analyzer process module">
        <startEvent id="process_dispute_extractor-extract_urls-3-0" name="Start"></startEvent>
        <serviceTask id="process_dispute_extractor-extract_urls-3-1" name="JUMP TO image_analyzer" activiti:delegateExpression="process_dispute_extractor-extract_urls-3-1implementation"></serviceTask>
        <endEvent id="process_dispute_extractor-extract_urls-3-2" name="End"></endEvent>
        <sequenceFlow id="process_dispute_extractor-extract_urls-3-0--process_dispute_extractor-extract_urls-3-1" sourceRef="process_dispute_extractor-extract_urls-3-0" targetRef="process_dispute_extractor-extract_urls-3-1"></sequenceFlow>
        <sequenceFlow id="process_dispute_extractor-extract_urls-3-1--process_dispute_extractor-extract_urls-3-2" sourceRef="process_dispute_extractor-extract_urls-3-1" targetRef="process_dispute_extractor-extract_urls-3-2"></sequenceFlow>
      </subProcess>
      <sequenceFlow id="process_dispute_extractor-extract_urls-0--process_dispute_extractor-extract_urls-1" name="For each url" sourceRef="process_dispute_extractor-extract_urls-0" targetRef="process_dispute_extractor-extract_urls-1"></sequenceFlow>       
      <sequenceFlow id="process_dispute_extractor-extract_urls-1--process_dispute_extractor-extract_urls-0" sourceRef="process_dispute_extractor-extract_urls-1" targetRef="process_dispute_extractor-extract_urls-0"></sequenceFlow>
    </subProcess>
    <startEvent id="process_dispute_extractor-start" name="Start"></startEvent>
    <endEvent id="process_dispute_extractor-end" name="End"></endEvent>
    <sequenceFlow id="process_dispute_extractor-start--process_dispute_extractor-extract_urls" sourceRef="process_dispute_extractor-start" targetRef="process_dispute_extractor-extract_urls"></sequenceFlow>
    <sequenceFlow id="process_dispute_extractor-extract_urls--process_dispute_extractor-end" sourceRef="process_dispute_extractor-extract_urls" targetRef="process_dispute_extractor-end"></sequenceFlow>

  </process>
</definitions>

BPMN导出的图像:

BPMN exported image

答案 1 :(得分:3)

我是您的代码,一切正常。 问题是您的bpmn文件。

我用xml内容创建了一个新的bpmn文件(bpmnWithoutDI2):

    <?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="myProcess" name="My process" isExecutable="true">
    <startEvent id="startevent1" name="Start"></startEvent>
    <userTask id="usertask1" name="User Task"></userTask>
    <sequenceFlow id="flow1" sourceRef="startevent1" targetRef="usertask1"></sequenceFlow>
    <endEvent id="endevent1" name="End"></endEvent>
    <sequenceFlow id="flow2" sourceRef="usertask1" targetRef="endevent1"></sequenceFlow>
  </process>
</definitions>

一切正常,并在运行代码后生成 bpmnWithDI.bpmn 。 但是我认为问题出在 sequenceFlow 元素中,因为没有它们,代码将无法成功运行。