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

    1. // read a model from a file
    2. File file = new File("PATH/TO/MODEL.dmn");
    3. DmnModelInstance modelInstance = Dmn.readModelFromFile(file);
    4. // read a model from a stream
    5. InputStream stream = [...]
    6. DmnModelInstance modelInstance = Dmn.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. DecisionTable decisionTable = modelInstance.getModelElementById("decisionTable1");
    3. // find all elements of the type DecisionTable
    4. ModelElementType decisionTableType = modelInstance.getModel()
    5. .getType(DecisionTable.class);
    6. Collection<ModelElementInstance> decisionTableInstances =
    7. modelInstance.getModelElementsByType(decisionTableType);

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

    1. DecisionTable decisionTable = modelInstance.getModelElementById("decisionTable1");
    2. // read attributes by helper methods
    3. String outputLabel = decisionTable.getOutputLabel();
    4. Collection<Input> inputs = decisionTable.getInputs();
    5. // edit attributes by helper methods
    6. decisionTable.setOutputLabel("new-label");
    7. // read attributes by generic XML model API (with optional namespace)
    8. String custom1 = decisionTable.getAttributeValue("custom-attribute");
    9. String custom2 = decisionTable.getAttributeValueNs("custom-attribute-2",
    10. "http://camunda.org/custom");
    11. // edit attributes by generic XML model API (with optional namespace)
    12. decisionTable.setAttributeValue("custom-attribute", "new value");
    13. decisionTable.setAttributeValueNs("custom-attribute",
    14. "http://camunda.org/custom", "new value");

    You can also access the child elements of an element or references to other elements. For example,a decision has a required decision which it depends on.A required decision is represented by a requiredDecision element inside an informationRequirement XML element.

    Consider the following simple DMN model:

    1. <?xml version="1.0" encoding="UTF-8"?>
    2. <definitions xmlns="http://www.omg.org/spec/DMN/20151101/dmn.xsd"
    3. id="dish" name="Dish" namespace="test-drd-2">
    4. <decision id="dish-decision" name="Dish Decision">
    5. <informationRequirement>
    6. <requiredDecision href="#season" />
    7. </informationRequirement>
    8. <decisionTable id="dishDecisionTable">
    9. <input id="seasonInput" label="Season">
    10. <inputExpression id="seasonInputExpresion" typeRef="string">
    11. <text>season</text>
    12. </inputExpression>
    13. </input>
    14. <output id="output" label="Dish" name="desiredDish" typeRef="string"/>
    15. <rule id="dishRule1">
    16. <inputEntry id="dishInputEntry1">
    17. <text><![CDATA["Spring"]]></text>
    18. </inputEntry>
    19. <outputEntry id="dishOutputEntry1">
    20. <text><![CDATA["Salad"]]></text>
    21. </outputEntry>
    22. </rule>
    23. ...
    24. </decisionTable>
    25. </decision>
    26. <decision id="season" name="Season decision">
    27. <decisionTable id="seasonDecisionTable">
    28. <input id="temperatureInput" label="Weather in Celsius">
    29. <inputExpression id="temperatureInputExpression" typeRef="integer">
    30. <text>temperature</text>
    31. </inputExpression>
    32. </input>
    33. <output id="seasonOutput" label="season" name="season" typeRef="string" />
    34. <rule id="seasonRule1">
    35. <inputEntry id="seasonInputEntry1">
    36. <text><![CDATA[<10]]></text>
    37. </inputEntry>
    38. <outputEntry id="seasonOutputEntry1">
    39. <text><![CDATA["Winter"]]></text>
    40. </outputEntry>
    41. </rule>
    42. ...
    43. </decisionTable>
    44. </decision>
    45. </definitions>

    You can now use the DMN model API to get the input elements of the required decisions.

    1. // read dmn model from file
    2. File file = new File("PATH/TO/MODEL.dmn");
    3. DmnModelInstance modelInstance = Dmn.readModelFromFile(file);
    4. // find the main decision by ID
    5. Decision decision = modelInstance.getModelElementById("dish-decision");
    6. // get the information requirements
    7. Collection<InformationRequirement> informationRequirements =
    8. decision.getInformationRequirements();
    9. // get the input of the required decisions
    10. for (InformationRequirement informationRequirement : informationRequirements) {
    11. Decision requiredDecision = informationRequirement.getRequiredDecision();
    12. DecisionTable decisionTable =
    13. requiredDecision.getUniqueChildElementByType(DecisionTable.class);
    14. Collection<Input> inputs = decisionTable.getInputs();
    15. ...
    16. }

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