To create a new BPMN model from scratch you have to create an empty BPMN model instance with the following method:

  1. BpmnModelInstance modelInstance = Bpmn.createEmptyModel();

The next step is to create a BPMN definitions element. Set the target namespace on it and add itto the newly created empty model instance.

  1. Definitions definitions = modelInstance.newInstance(Definitions.class);
  2. definitions.setTargetNamespace("http://camunda.org/examples");
  3. modelInstance.setDefinitions(definitions);

Usually you want to add a process to your model. This followsthe same 3 steps as the creation of the BPMN definitions element:

  • Create a new instance of the BPMN element
  • Set attributes and child elements of the element instance
  • Add the newly created element instance to the corresponding parent element
    1. Process process = modelInstance.newInstance(Process.class);
    2. process.setId("process");
    3. definitions.addChildElement(process);

To simplify this repeating procedure, you can use a helper method like this one.

  1. protected <T extends BpmnModelElementInstance> T createElement(BpmnModelElementInstance parentElement, String id, Class<T> elementClass) {
  2. T element = modelInstance.newInstance(elementClass);
  3. element.setAttributeValue("id", id, true);
  4. parentElement.addChildElement(element);
  5. return element;
  6. }

After you created the elements of your process like start event, tasks, gateways and end event, you have to connectthe elements with sequence flows. Again, this follows the same 3 steps of element creation and can be simplified by thefollowing helper method.

  1. public SequenceFlow createSequenceFlow(Process process, FlowNode from, FlowNode to) {
  2. String identifier = from.getId() + "-" + to.getId();
  3. SequenceFlow sequenceFlow = createElement(process, identifier, SequenceFlow.class);
  4. process.addChildElement(sequenceFlow);
  5. sequenceFlow.setSource(from);
  6. from.getOutgoing().add(sequenceFlow);
  7. sequenceFlow.setTarget(to);
  8. to.getIncoming().add(sequenceFlow);
  9. return sequenceFlow;
  10. }

Validate the model against the BPMN 2.0 specification and convert it toan XML string or save it to a file or stream.

  1. // validate the model
  2. Bpmn.validateModel(modelInstance);
  3. // convert to string
  4. String xmlString = Bpmn.convertToString(modelInstance);
  5. // write to output stream
  6. OutputStream outputStream = new OutputStream(...);
  7. Bpmn.writeModelToStream(outputStream, modelInstance);
  8. // write to file
  9. File file = new File(...);
  10. Bpmn.writeModelToFile(file, modelInstance);

Example 1: Create a Simple Process With One User Task

With the basic helper methods from above it is very easy and straightforward to create simple processes. First, create aprocess with a start event, user task and an end event.

Create a Model - 图1

The following code creates this process using the helper methods from above (without the DI elements).

  1. // create an empty model
  2. BpmnModelInstance modelInstance = Bpmn.createEmptyModel();
  3. Definitions definitions = modelInstance.newInstance(Definitions.class);
  4. definitions.setTargetNamespace("http://camunda.org/examples");
  5. modelInstance.setDefinitions(definitions);
  6. // create the process
  7. Process process = createElement(definitions, "process-with-one-task", Process.class);
  8. // create start event, user task and end event
  9. StartEvent startEvent = createElement(process, "start", StartEvent.class);
  10. UserTask task1 = createElement(process, "task1", UserTask.class);
  11. task1.setName("User Task");
  12. EndEvent endEvent = createElement(process, "end", EndEvent.class);
  13. // create the connections between the elements
  14. createSequenceFlow(process, startEvent, task1);
  15. createSequenceFlow(process, task1, endEvent);
  16. // validate and write model to file
  17. Bpmn.validateModel(modelInstance);
  18. File file = File.createTempFile("bpmn-model-api-", ".bpmn");
  19. Bpmn.writeModelToFile(file, modelInstance);

Example 2: Create a Simple Process With Two Parallel Tasks

Even more complex processes can be created with a few lines of code with the standard BPMN model API.

Create a Model - 图2

  1. // create an empty model
  2. BpmnModelInstance modelInstance = Bpmn.createEmptyModel();
  3. Definitions definitions = modelInstance.newInstance(Definitions.class);
  4. definitions.setTargetNamespace("http://camunda.org/examples");
  5. modelInstance.setDefinitions(definitions);
  6. // create elements
  7. StartEvent startEvent = createElement(process, "start", StartEvent.class);
  8. ParallelGateway fork = createElement(process, "fork", ParallelGateway.class);
  9. ServiceTask task1 = createElement(process, "task1", ServiceTask.class);
  10. task1.setName("Service Task");
  11. UserTask task2 = createElement(process, "task2", UserTask.class);
  12. task2.setName("User Task");
  13. ParallelGateway join = createElement(process, "join", ParallelGateway.class);
  14. EndEvent endEvent = createElement(process, "end", EndEvent.class);
  15. // create flows
  16. createSequenceFlow(process, startEvent, fork);
  17. createSequenceFlow(process, fork, task1);
  18. createSequenceFlow(process, fork, task2);
  19. createSequenceFlow(process, task1, join);
  20. createSequenceFlow(process, task2, join);
  21. createSequenceFlow(process, join, endEvent);
  22. // validate and write model to file
  23. Bpmn.validateModel(modelInstance);
  24. File file = File.createTempFile("bpmn-model-api-", ".bpmn");
  25. Bpmn.writeModelToFile(file, modelInstance);

原文: https://docs.camunda.org/manual/7.9/user-guide/model-api/bpmn-model-api/create-a-model/