Чтение модели

Готовую DMN-модель можно загрузить в API DMN-модели для обработки следующими методами:

// прочитать модель из файла
File file = new File("PATH/TO/MODEL.dmn");
DmnModelInstance modelInstance = Dmn.readModelFromFile(file);

// прочитать модель из потока
InputStream stream = [...]
DmnModelInstance modelInstance = Dmn.readModelFromStream(stream);

В загруженной модели можно искать элементы по идентификатору или по типу:

// найти экземпляр элемента по идентификатору
DecisionTable decisionTable = modelInstance.getModelElementById("decisionTable1");

// найти все элементы типа DecisionTable
ModelElementType decisionTableType = modelInstance.getModel()
  .getType(DecisionTable.class);
Collection<ModelElementInstance> decisionTableInstances =
  modelInstance.getModelElementsByType(decisionTableType);

У каждого экземпляра элемента можно читать и изменять значения атрибутов: через вспомогательные методы или через общий API XML-модели. Пользовательские атрибуты, добавленные к DMN-элементам, всегда доступны через общий API XML-модели.

DecisionTable decisionTable = modelInstance.getModelElementById("decisionTable1");

// прочитать атрибуты вспомогательными методами
String outputLabel = decisionTable.getOutputLabel();
Collection<Input> inputs = decisionTable.getInputs();

// изменить атрибуты вспомогательными методами
decisionTable.setOutputLabel("new-label");

// прочитать атрибуты через общий API XML-модели (пространство имён необязательно)
String custom1 = decisionTable.getAttributeValue("custom-attribute");
String custom2 = decisionTable.getAttributeValueNs("http://openbpm.ru/custom",
  "custom-attribute-2");

// изменить атрибуты через общий API XML-модели (пространство имён необязательно)
decisionTable.setAttributeValue("custom-attribute", "new value");
decisionTable.setAttributeValueNs("http://openbpm.ru/custom",
  "custom-attribute", "new value");

Можно также обращаться к дочерним элементам и к ссылкам на другие элементы. Например, решение может зависеть от другого, требуемого решения. Требуемое решение задаётся элементом requiredDecision внутри XML-элемента informationRequirement.

Возьмём простую DMN-модель:

<?xml version="1.0" encoding="UTF-8"?>
<definitions xmlns="https://www.omg.org/spec/DMN/20191111/MODEL/"
             id="dish" name="Dish" namespace="test-drd-2">

  <decision id="dish-decision" name="Dish Decision">
    <informationRequirement>
      <requiredDecision href="#season" />
    </informationRequirement>
    <decisionTable id="dishDecisionTable">
      <input id="seasonInput" label="Season">
        <inputExpression id="seasonInputExpresion" typeRef="string">
          <text>season</text>
        </inputExpression>
      </input>
      <output id="output" label="Dish" name="desiredDish" typeRef="string"/>
      <rule id="dishRule1">
        <inputEntry id="dishInputEntry1">
          <text><![CDATA["Spring"]]></text>
        </inputEntry>
        <outputEntry id="dishOutputEntry1">
          <text><![CDATA["Salad"]]></text>
        </outputEntry>
      </rule>
      ...
    </decisionTable>
  </decision>

  <decision id="season" name="Season decision">
    <decisionTable id="seasonDecisionTable">
      <input id="temperatureInput" label="Weather in Celsius">
        <inputExpression id="temperatureInputExpression" typeRef="integer">
          <text>temperature</text>
        </inputExpression>
      </input>
      <output id="seasonOutput" label="season" name="season" typeRef="string" />
      <rule id="seasonRule1">
        <inputEntry id="seasonInputEntry1">
          <text><![CDATA[<10]]></text>
        </inputEntry>
        <outputEntry id="seasonOutputEntry1">
          <text><![CDATA["Winter"]]></text>
        </outputEntry>
      </rule>
      ...
    </decisionTable>
  </decision>

</definitions>

Получим через API DMN-модели входы требуемых решений:

// прочитать DMN-модель из файла
File file = new File("PATH/TO/MODEL.dmn");
DmnModelInstance modelInstance = Dmn.readModelFromFile(file);

// найти основное решение по идентификатору
Decision decision = modelInstance.getModelElementById("dish-decision");

// получить информационные требования
Collection<InformationRequirement> informationRequirements =
  decision.getInformationRequirements();

// получить входы требуемых решений
for (InformationRequirement informationRequirement : informationRequirements) {
  Decision requiredDecision = informationRequirement.getRequiredDecision();
  DecisionTable decisionTable =
    (DecisionTable) requiredDecision.getUniqueChildElementByType(DecisionTable.class);
  Collection<Input> inputs = decisionTable.getInputs();
  ...
}

Документация создана на основе Camunda 7 Docs (Camunda, docs.camunda.org) и распространяется по лицензии Creative Commons Attribution-ShareAlike 3.0 Unported.