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

    1. // read a model from a file
    2. File file = new File("PATH/TO/MODEL.cmmn");
    3. CmmnModelInstance modelInstance = Cmmn.readModelFromFile(file);
    4. // read a model from a stream
    5. InputStream stream = [...]
    6. CmmnModelInstance modelInstance = Cmmn.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. HumanTask humanTask = (HumanTask) modelInstance.getModelElementById("HumanTask_1");
    3. // find all elements of the type HumanTask
    4. ModelElementType humanTaskType = modelInstance.getModel().getType(HumanTask.class);
    5. Collection<ModelElementInstance> humanTaskInstances = modelInstance.getModelElementsByType(humanTaskType);

    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 CMMN elements, you canalways access them with the generic XML model API.

    1. HumanTask humanTask = (HumanTask) modelInstance.getModelElementById("HumanTask_1");
    2. // read attributes by helper methods
    3. String id = humanTask.getId();
    4. String name = humanTask.getName();
    5. // edit attributes by helper methods
    6. humanTask.setId("new-id");
    7. humanTask.setName("new name");
    8. // read attributes by generic XML model API (with optional namespace)
    9. String custom1 = humanTask.getAttributeValue("custom-attribute");
    10. String custom2 = humanTask.getAttributeValueNs("custom-attribute-2", "http://camunda.org/custom");
    11. // edit attributes by generic XML model API (with optional namespace)
    12. humanTask.setAttributeValue("custom-attribute", "new value");
    13. humanTask.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 plan itemreferences a definition element (like human task, process task, etc.) while the referenced definition element containsdefault control.

    Consider the following simple CMMN model:

    1. <?xml version="1.0" encoding="UTF-8" standalone="no"?>
    2. <definitions targetNamespace="http://camunda.org/examples"
    3. xmlns="http://www.omg.org/spec/CMMN/20151109/MODEL">
    4. <case id="case-with-one-task">
    5. <casePlanModel id="CasePlanModel_1">
    6. <planItem id="PI_HumanTask_1" definitionRef="HumanTask_1" name="A Task" />
    7. <humanTask id="HumanTask_1">
    8. <defaultControl>
    9. <manualActivationRule>
    10. <condition>${false}</condition>
    11. </manualActivationRule>
    12. </defaultControl>
    13. </humanTask>
    14. </casePlanModel>
    15. </case>
    16. </definitions>

    You can now use the CMMN model API to get the definition of the plan item with the ID PI_HumanTask_1.

    1. // read cmmn model from file
    2. File file = new File("PATH/TO/MODEL.cmmn");
    3. CmmnModelInstance modelInstance = Cmmn.readModelFromFile(file);
    4. // find plan item by id
    5. PlanItem planItem = (PlanItem) modelInstance.getModelElementById("PI_HumanTask_1");
    6. // get the definition element
    7. PlanItemDefinition definition = planItem.getDefinition();
    8. // get the default control
    9. DefaultControl defaultControl = definition.getDefaultControl();
    10. ManualActivationRule rule = defaultControl.getManualActivationRule();

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