Прочтение модели
|
Этот раздел перенесён из документации Camunda 7 и в дальнейшем будет доработан с учётом особенностей OpenBPM Engine |
Если вы уже создали DMN-модель и хотите обработать ее через API DMN-моделей, вы можете импортировать ее, используя следующие методы:
// read a model from a file
File file = new File("PATH/TO/MODEL.dmn");
DmnModelInstance modelInstance = Dmn.readModelFromFile(file);
// read a model from a stream
InputStream stream = [...]
DmnModelInstance modelInstance = Dmn.readModelFromStream(stream);
После того как вы импортировали вашу модель, вы можете искать элементы по их id или по типу элемента.
// find element instance by ID
DecisionTable decisionTable = modelInstance.getModelElementById("decisionTable1");
// find all elements of the type DecisionTable
ModelElementType decisionTableType = modelInstance.getModel()
.getType(DecisionTable.class);
Collection<ModelElementInstance> decisionTableInstances =
modelInstance.getModelElementsByType(decisionTableType);
Для каждого экземпляра элемента вы теперь можете читать и редактировать значения атрибутов. Это можно сделать либо с использованием предоставленных вспомогательных методов, либо через API XML-моделей общего назначения. Если вы добавили кастомизированные атрибуты к DMN элементам, вы всегда можете обращаться к ним через API XML-моделей общего назначения.
DecisionTable decisionTable = modelInstance.getModelElementById("decisionTable1");
// read attributes by helper methods
String outputLabel = decisionTable.getOutputLabel();
Collection<Input> inputs = decisionTable.getInputs();
// edit attributes by helper methods
decisionTable.setOutputLabel("new-label");
// read attributes by generic XML model API (with optional namespace)
String custom1 = decisionTable.getAttributeValue("custom-attribute");
String custom2 = decisionTable.getAttributeValueNs("custom-attribute-2",
"http://camunda.org/custom");
// edit attributes by generic XML model API (with optional namespace)
decisionTable.setAttributeValue("custom-attribute", "new value");
decisionTable.setAttributeValueNs("custom-attribute",
"http://camunda.org/custom", "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-моделей, чтобы получить входящие элементы обязательных решений.
// read dmn model from file
File file = new File("PATH/TO/MODEL.dmn");
DmnModelInstance modelInstance = Dmn.readModelFromFile(file);
// find the main decision by ID
Decision decision = modelInstance.getModelElementById("dish-decision");
// get the information requirements
Collection<InformationRequirement> informationRequirements =
decision.getInformationRequirements();
// get the input of the required decisions
for (InformationRequirement informationRequirement : informationRequirements) {
Decision requiredDecision = informationRequirement.getRequiredDecision();
DecisionTable decisionTable =
requiredDecision.getUniqueChildElementByType(DecisionTable.class);
Collection<Input> inputs = decisionTable.getInputs();
...
}
Лицензия и атрибуция
Эта документация была создана на базе материала "Camunda 7 Docs" от Camunda, находится под лицензией Creative Commons Attribution-ShareAlike 3.0 Unported License .
Оригинал документации: https://docs.camunda.org