The DMN engine interface exposes methodsfor parsing and evaluating DMN Decisions.

Parse Decisions

Decisions can be parsed from an InputStream or transformed from a DmnModelInstance.

This example shows how to parse a decision from an input stream:

  1. // create a default DMN engine
  2. DmnEngine dmnEngine = DmnEngineConfiguration
  3. .createDefaultDmnEngineConfiguration()
  4. .buildEngine();
  5. InputStream inputStream = ...
  6. // parse all decision from the input stream
  7. List<DmnDecision> decisions = dmnEngine.parseDecisions(inputStream);

The next example uses the DMN Model API to first create aDmnModelInstance and then transform the decisions:

  1. // create a default DMN engine
  2. DmnEngine dmnEngine = DmnEngineConfiguration
  3. .createDefaultDmnEngineConfiguration()
  4. .buildEngine();
  5. // read a DMN model instance from a file
  6. DmnModelInstance dmnModelInstance = Dmn.readModelFromFile(...);
  7. // parse the decisions
  8. List<DmnDecision> decisions = dmnEngine.parseDecisions(dmnModelInstance);

The Decision Key

A DMN XML file can contain multiple decisions - grouped by the decision requirements graph. To distinguish the decisions,every decision must have an id attribute.

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <definitions xmlns="http://www.omg.org/spec/DMN/20151101/dmn.xsd" id="definitions" name="definitions" namespace="http://camunda.org/schema/1.0/dmn">
  3. <decision id="first-decision" name="First Decision">
  4. <decisionTable>
  5. <output id="output1"/>
  6. </decisionTable>
  7. </decision>
  8. <decision id="second-decision" name="Second Decision">
  9. <decisionTable>
  10. <output id="output2"/>
  11. </decisionTable>
  12. </decision>
  13. </definitions>

The id of a decision in the XML is called key in the context of the DMNengine. To only parse a specific decision from a DMN file, you specify the decisionkey which corresponds to the id attribute in the XML file.

  1. // create a default DMN engine
  2. DmnEngine dmnEngine = DmnEngineConfiguration
  3. .createDefaultDmnEngineConfiguration()
  4. .buildEngine();
  5. // read the DMN XML file as input stream
  6. InputStream inputStream = ...
  7. // parse only the decision with the key "second-decision"
  8. DmnDecision decision = dmnEngine.parseDecision("second-decision", inputStream);

Parse Decision Requirements Graph

In addition to parsing all contained decisions of a decision requirements graph (DRG), the DMN engine can also parse the DRG itself from an InputStream or a DmnModelInstance.

  1. // parse the drg from an input stream
  2. DmnDecisionRequirementsGraph drg = dmnEngine.parseDecisionRequirementsGraph(inputStream);
  3. // get the keys of all containing decisions
  4. Set<String> decisionKeys = drg.getDecisionKeys();
  5. // get a containing decision by key
  6. DmnDecision decision = drg.getDecision("decision");
  7. // get all containing decisions
  8. Collection<DmnDecision> decisions = drg.getDecisions();

The DRG is represented in the XML by the definitions element. The id of the DRG in the XML is called key in the context of the DMN engine.

Decision Tables only

It is possible to check if a parsed decision is implemented as decision table by using the method isDecisionTable().

  1. // create a default DMN engine
  2. DmnEngine dmnEngine = DmnEngineConfiguration
  3. .createDefaultDmnEngineConfiguration()
  4. .buildEngine();
  5. // read the DMN XML file as input stream
  6. InputStream inputStream = ...
  7. // parse all decision from the input stream
  8. List<DmnDecision> decisions = dmnEngine.parseDecisions(inputStream);
  9. // get the first decision
  10. DmnDecision decision = decisions.get(0);
  11. // do something if it is a decision table
  12. if (decision.isDecisionTable()) {
  13. // ...
  14. }

Evaluate Decisions

To evaluate (or “execute”) a decision, either pass an already transformed DmnDecision or use a DMN model instance or input stream in combination with a decision key.

As input to the evaluation, a set of input variables must be provided.

  1. // create a default DMN engine
  2. DmnEngine dmnEngine = DmnEngineConfiguration
  3. .createDefaultDmnEngineConfiguration()
  4. .buildEngine();
  5. // read the DMN XML file as input stream
  6. InputStream inputStream = ...;
  7. // parse the DMN decision from the input stream
  8. DmnDecision decision = dmnEngine.parseDecision("decisionKey", inputStream);
  9. // create the input variables
  10. VariableMap variables = ...;
  11. // evaluate the decision
  12. result = dmnEngine.evaluateDecision(decision, variables);
  13. // or if the decision is implemented as decision table then you can also use
  14. result = dmnEngine.evaluateDecisionTable(decision, variables);

Pass Variables

To provide the input variables for a decision evaluation you can use aJava Map<String, Object>, resp. a VariableMap or a VariableContext.

The following example shows how to use a VariableMap.

  1. // create the input variables
  2. VariableMap variables = Variables.createVariables()
  3. .putValue("x", "camunda")
  4. .putValue("y", 2015);
  5. // evaluate the decision with the input variables
  6. result = dmnEngine.evaluateDecision(decision, variables);

Alternatively, a VariableContext can be used.Use the VariableContext to support lazy-loading of variables.

Interpret the Decision Result

The evaluation of a DMN decision returns a DmnDecisionResult. If the decision is implemented as decision table then the result is a list of thematching decision rule results. These results represent a mapping from an output name to an output value.

If the decision is instead implemented as decision literal expression then the result is a listwhich contains only one entry. This entry represents the expression value and is mapped to the variable name.

Assume the following example of making a decision to select a dish.

Evaluate Decisions - 图1

The decision table returns the desiredDish as the output.

Assume that the decision table is executed with the following input variables:

  • season: “Spring”
  • guestCount: 14
    There is a matching rule in the table for the given inputs.The DmnDecisionResult thus consists of one DmnDecisionResultEntries which contains the key desiredDish.

To access the output value, get() method of DmnDecisionResultEntries is used:

  1. DmnDecisionResult decisionResult = dmnEngine.evaluateDecision(decision, variables);
  2. // the size will be 1
  3. int size = decisionResult.size();
  4. // get the matching rule
  5. DmnDecisionResultEntries ruleResult = decisionResult.get(0);
  6. // get output values by name
  7. Object result = ruleResult.get("desiredDish");

The result objects expose additional convenience methods:

  1. DmnDecisionResult decisionResult = dmnEngine.evaluateDecision(decision, variables);
  2. // returns the first rule result
  3. DmnDecisionResultEntries ruleResult = decisionResult.getFirstResult();
  4. // returns first rule result
  5. // but asserts that only a single one exists
  6. decisionResult.getSingleResult();
  7. // collects only the entries for an output column
  8. decisionResult.collectEntries("desiredDish");
  9. // returns the first output entry
  10. ruleResult.getFirstEntry();
  11. // also returns the first output entry
  12. // but asserts that only a single one exists
  13. ruleResult.getSingleEntry();
  14. // shortcut to returns the single output entry of the single rule result
  15. // - combine getSingleResult() and getSingleEntry()
  16. decisionResult.getSingleEntry();

Note that the decision can also be evaluated using theevaluateDecisionTable() method if it is implemented as decision table. In this case, evaluation returns a DmnDecisionTableResult which is semantically equal and provides the same methods as aDmnDecisionResult.

Decisions with Required Decisions

If a decision has one or more required decisions, then the required decisions are evaluated first. The results of this evaluations are passed as input for the evaluation of the decision.

Assume the following example of making a decision to select beverages.

Evaluate Decisions - 图2

The following decision requirements diagram shows that the Beverages decision requires the Dish decision (from the previous example).

Evaluate Decisions - 图3

When the Beverages decision is evaluated then the DMN engine evaluates the Dish decision first.

Assume that the decision is evaluated with the following input variables:

  • season: “Spring”
  • guestCount: 14
  • guestsWithChildren: false
    With the above inputs, Dish decision table has one matching rule and generates the output value Stew that is mapped to the output variable desiredDish.

The output result of the Dish decision is used as input of the Beverages decision. That means that the input expression desiredDish of the Beverages decision returns the output value Stew of the Dish decision. In general, a decision can access the results (i.e., the output values) of required decisions by there output name.

As result, the Beverages decision has two matching rules and generates the output values Guiness and Water.

  1. DmnDecision decision = dmnEngine.parseDecision("beverages", inputStream);
  2. DmnDecisionResult decisionResult = dmnEngine.evaluateDecision(decision, variables);
  3. List<String> beverages = decisionResult.collectEntries("beverages");

Hit Policy of Required Decisions

The hit policy of a required decision can affect the result that is passed as input to the requiring decision. If the required decision has a COLLECT hit policy with aggregator then the decision result (i.e. output value) is only the aggregated value.

In case of a hit policy with multiple matched rules (i.e., COLLECT without aggregator or RULE ORDER), the output variable is mapped to a list of output values, even if only one rule matched.

原文: https://docs.camunda.org/manual/7.9/user-guide/dmn-engine/evaluate-decisions/