If you already created a BPMN model and want to process it through the BPMN model API, you can import it with thefollowing methods.

    1. // read a model from a file
    2. File file = new File("PATH/TO/MODEL.bpmn");
    3. BpmnModelInstance modelInstance = Bpmn.readModelFromFile(file);
    4. // read a model from a stream
    5. InputStream stream = [...]
    6. BpmnModelInstance modelInstance = Bpmn.readModelFromStream(stream);

    After you imported your model you can search for elements by their id or by the type of element.

    1. // find element instance by ID
    2. StartEvent start = (StartEvent) modelInstance.getModelElementById("start");
    3. // find all elements of the type task
    4. ModelElementType taskType = modelInstance.getModel().getType(Task.class);
    5. Collection<ModelElementInstance> taskInstances = modelInstance.getModelElementsByType(taskType);

    For every element instance you can now read and edit the attribute values. You can do this by either using the providedhelper methods or the generic XML model API. If you added custom attributes to the BPMN elements, you canalways access them with the generic XML model API.

    1. StartEvent start = (StartEvent) modelInstance.getModelElementById("start");
    2. // read attributes by helper methods
    3. String id = start.getId();
    4. String name = start.getName();
    5. // edit attributes by helper methods
    6. start.setId("new-id");
    7. start.setName("new name");
    8. // read attributes by generic XML model API (with optional namespace)
    9. String custom1 = start.getAttributeValue("custom-attribute");
    10. String custom2 = start.getAttributeValueNs("custom-attribute-2", "http://camunda.org/custom");
    11. // edit attributes by generic XML model API (with optional namespace)
    12. start.setAttributeValue("custom-attribute", "new value");
    13. start.setAttributeValueNs("custom-attribute", "http://camunda.org/custom", "new value");

    You can also access the child elements of an element or references to other elements. For example, a sequence flowreferences a source and a target element while a flow node (like start event, tasks, etc.) has child elementsfor incoming and outgoing sequence flows.

    For example, the following BPMN model was created by the BPMN model API as an example for a simple process.

    1. <?xml version="1.0" encoding="UTF-8" standalone="no"?>
    2. <definitions targetNamespace="http://camunda.org/examples" xmlns="http://www.omg.org/spec/BPMN/20100524/MODEL">
    3. <process id="process-with-one-task">
    4. <startEvent id="start">
    5. <outgoing>start-task1</outgoing>
    6. </startEvent>
    7. <userTask id="task1">
    8. <incoming>start-task1</incoming>
    9. <outgoing>task1-end</outgoing>
    10. </userTask>
    11. <endEvent id="end">
    12. <incoming>task1-end</incoming>
    13. </endEvent>
    14. <sequenceFlow id="start-task1" sourceRef="start" targetRef="task1"/>
    15. <sequenceFlow id="task1-end" sourceRef="task1" targetRef="end"/>
    16. </process>
    17. </definitions>

    You can now use the BPMN model API to get the source and target flow node of the sequence flow with the ID start-task1.

    1. // read bpmn model from file
    2. BpmnModelInstance modelInstance = Bpmn.readModelFromFile(new File("/PATH/TO/MODEL.bpmn"));
    3. // find sequence flow by id
    4. SequenceFlow sequenceFlow = (SequenceFlow) modelInstance.getModelElementById("start-task1");
    5. // get the source and target element
    6. FlowNode source = sequenceFlow.getSource();
    7. FlowNode target = sequenceFlow.getTarget();
    8. // get all outgoing sequence flows of the source
    9. Collection<SequenceFlow> outgoing = source.getOutgoing();
    10. assert(outgoing.contains(sequenceFlow));

    With these references you can easily create helper methods for different use cases. For example, if you want tofind the following flow nodes of a task or a gateway you can use a helper method like the following.

    1. public Collection<FlowNode> getFlowingFlowNodes(FlowNode node) {
    2. Collection<FlowNode> followingFlowNodes = new ArrayList<FlowNode>();
    3. for (SequenceFlow sequenceFlow : node.getOutgoing()) {
    4. followingFlowNodes.add(sequenceFlow.getTarget());
    5. }
    6. return followingFlowNodes;
    7. }

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