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.setTargetNamespace("http://camunda.org/examples");
    2. 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

    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. }

    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. // write to output stream
    5. Bpmn.writeModelToStream(outputStream, modelInstance);
    6. // write to file
    7. File file = new File(...);
    8. 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.

    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 - 图1

    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. UserTask task2 = createElement(process, "task2", UserTask.class);
    10. task2.setName("User Task");
    11. ParallelGateway join = createElement(process, "join", ParallelGateway.class);
    12. EndEvent endEvent = createElement(process, "end", EndEvent.class);
    13. // create flows
    14. createSequenceFlow(process, startEvent, fork);
    15. createSequenceFlow(process, fork, task1);
    16. createSequenceFlow(process, fork, task2);
    17. createSequenceFlow(process, task1, join);
    18. createSequenceFlow(process, task2, join);
    19. createSequenceFlow(process, join, endEvent);
    20. // validate and write model to file
    21. Bpmn.validateModel(modelInstance);