我正在按照官方文档提供的示例(在本例中为示例 2)构建一些测试 BPMN 2.0 模型并将它们保存到 Java 项目中的 xml 文件中。
我在我的 pom 中导入了 lib,如下所示:
<!-- https://mvnrepository.com/artifact/org.camunda.bpm.model/camunda-bpmn-model -->
<dependency>
<groupId>org.camunda.bpm.model</groupId>
<artifactId>camunda-bpmn-model</artifactId>
<version>7.10.0</version>
</dependency>
这是我的测试类,遵循示例 2:
import org.camunda.bpm.model.bpmn.Bpmn;
import org.camunda.bpm.model.bpmn.BpmnModelInstance;
import org.camunda.bpm.model.bpmn.instance.*;
import org.camunda.bpm.model.bpmn.instance.Process;
import java.io.File;
import java.io.IOException;
public class Test {
protected static <T extends BpmnModelElementInstance> T createElement(BpmnModelInstance modelInstance, BpmnModelElementInstance parentElement, String id, Class<T> elementClass) {
T element = modelInstance.newInstance(elementClass);
element.setAttributeValue("id", id, true);
parentElement.addChildElement(element);
return element;
}
public static SequenceFlow createSequenceFlow(BpmnModelInstance modelInstance, Process process, FlowNode from, FlowNode to) {
String identifier = from.getId() + "-" + to.getId();
SequenceFlow sequenceFlow = createElement(modelInstance, process, identifier, SequenceFlow.class);
process.addChildElement(sequenceFlow);
sequenceFlow.setSource(from);
from.getOutgoing().add(sequenceFlow);
sequenceFlow.setTarget(to);
to.getIncoming().add(sequenceFlow);
return sequenceFlow;
}
public static void main(String[] args) throws IOException {
// create an empty model
BpmnModelInstance modelInstance = Bpmn.createEmptyModel();
Definitions definitions = modelInstance.newInstance(Definitions.class);
definitions.setTargetNamespace("http://camunda.org/examples");
modelInstance.setDefinitions(definitions);
// create a process
Process process = modelInstance.newInstance(Process.class);
process.setId("process");
definitions.addChildElement(process);
// create elements
StartEvent startEvent = createElement(modelInstance, process, "start", StartEvent.class);
ParallelGateway fork = createElement(modelInstance, process, "fork", ParallelGateway.class);
ServiceTask task1 = createElement(modelInstance, process, "task1", ServiceTask.class);
task1.setName("Service Task");
UserTask task2 = createElement(modelInstance, process, "task2", UserTask.class);
task2.setName("User Task");
ParallelGateway join = createElement(modelInstance, process, "join", ParallelGateway.class);
EndEvent endEvent = createElement(modelInstance, process, "end", EndEvent.class);
// create flows
createSequenceFlow(modelInstance, process, startEvent, fork);
createSequenceFlow(modelInstance, process, fork, task1);
createSequenceFlow(modelInstance, process, fork, task2);
createSequenceFlow(modelInstance, process, task1, join);
createSequenceFlow(modelInstance, process, task2, join);
createSequenceFlow(modelInstance, process, join, endEvent);
// validate and write model to file
Bpmn.validateModel(modelInstance);
File file = new File("bpmn-model.bpmn.xml");
file.createNewFile();
Bpmn.writeModelToFile(file, modelInstance);
}
}
这里生成的 BPMN 2.0 xml
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<definitions id="definitions_7b2df680-1d9c-4897-ba8b-a83548ab937f" targetNamespace="http://camunda.org/examples" xmlns="http://www.omg.org/spec/BPMN/20100524/MODEL">
<process id="process">
<startEvent id="start">
<outgoing>start-fork</outgoing>
</startEvent>
<parallelGateway id="fork">
<incoming>start-fork</incoming>
<outgoing>fork-task1</outgoing>
<outgoing>fork-task2</outgoing>
</parallelGateway>
<serviceTask id="task1" name="Service Task">
<incoming>fork-task1</incoming>
<outgoing>task1-join</outgoing>
</serviceTask>
<userTask id="task2" name="User Task">
<incoming>fork-task2</incoming>
<outgoing>task2-join</outgoing>
</userTask>
<parallelGateway id="join">
<incoming>task1-join</incoming>
<incoming>task2-join</incoming>
<outgoing>join-end</outgoing>
</parallelGateway>
<endEvent id="end">
<incoming>join-end</incoming>
</endEvent>
<sequenceFlow id="start-fork" sourceRef="start" targetRef="fork"/>
<sequenceFlow id="fork-task1" sourceRef="fork" targetRef="task1"/>
<sequenceFlow id="fork-task2" sourceRef="fork" targetRef="task2"/>
<sequenceFlow id="task1-join" sourceRef="task1" targetRef="join"/>
<sequenceFlow id="task2-join" sourceRef="task2" targetRef="join"/>
<sequenceFlow id="join-end" sourceRef="join" targetRef="end"/>
</process>
</definitions>
通过验证方法,当尝试使用 Camunda Modeler 应用程序和bpmn.io显示 BPMN 时,生成的 xml 似乎无效。
这个例子有什么问题,因此我的代码有什么问题?如何使生成的 xml 有效?由于 bpmn 2.0 是一个标准,我也对这个问题感到有些惊讶。