The decision service is a part of the process engine’s Services API. It allowsto evaluate a deployed decision definition independently from BPMN and CMMN.

Evaluating a Decision

To evaluate a deployed decision, reference it by id or a combination of key and version. Ifa key is used but no version is specified then the latest version of decisiondefinition with the given key is evaluated.

  1. DecisionService decisionService = processEngine.getDecisionService();
  2. VariableMap variables = Variables.createVariables()
  3. .putValue("status", "bronze")
  4. .putValue("sum", 1000);
  5. DmnDecisionResult decisionResult = decisionService
  6. .evaluateDecisionByKey("decision-key")
  7. .variables(variables)
  8. .evaluate();
  9. // alternatively for decision tables only
  10. DmnDecisionTableResult decisionResult = decisionService
  11. .evaluateDecisionTableByKey("decision-key")
  12. .variables(variables)
  13. .evaluate();

The Decision Key

The key of a decision definition is specified by the id attribute of thedecision element in the DMN XML. The different naming is related to theVersioning of Decisions. Since a key can reference multiple versions of adecision definition, the id specifies exactly one version.

Passing Data

A decision may reference one or more variables. For example, a variable can bereferenced in an input expression or an input entry of a decision table. Thevariables are passed to the decision service as key-value pairs. Each pairspecifies the name and the value of a variable.

For more information on the different expressions see the DMN 1.1 reference.

Authorizations for Evaluating Decisions

The user needs the permission CREATE_INSTANCE on the resourceDECISION_DEFINITION to evaluate decisions. The resource id of theauthorization is the decision definition key.

For more information about authorizations please refer to the AuthorizationService section.

Working with the Decision Result

The result of an evaluation is called decision result. The decision result is a complex objectof type DmnDecisionResult. Think of it as a list of key-value pairs.

If the decision is implemented as decision table then each entry in the list represents one matched rule. The output entries of thisrule are represented by the key-value pairs. The key of a pair is specified bythe name of the output.

Instead, if the decision is implemented as decision literal expression then the list contains only one entry. This entry represents the expression value and is mapped by the variable name.

The decision result provides methods from interface List<Map<String,
Object>>
and some convenience methods.

  1. DmnDecisionResult decisionResult = ...;
  2. // get the value of the single entry of the only matched rule
  3. String singleEntry = decisionResult.getSingleResult().getSingleEntry();
  4. // get the value of the result entry with name 'result' of the only matched rule
  5. String result = decisionResult.getSingleResult().getEntry("result");
  6. // get the value of the first entry of the second matched rule
  7. String firstValue = decisionResult.get(1).getFirstEntry();
  8. // get a list of all entries with the output name 'result' of all matched rules
  9. List<String> results = decisionResult.collectEntries("result");
  10. // shortcut to get the single output entry of the single rule result
  11. // - combine getSingleResult() and getSingleEntry()
  12. String result = decisionResult.getSingleEntry();

Note that the decision result also provides methods to get typed output entries.A complete list of all methods can be found in the Java Docs.

If the decision is implemented as decision table then it can also be evaluated using one of theevaluateDecisionTable methods. In this case, the evaluation returns a DmnDecisionTableResult which is semantically equal and provide the same methods as aDmnDecisionResult.

History of Evaluated Decisions

When a decision is evaluated, a new history entry of typeHistoricDecisionInstance is created which contains the inputs and outputs ofthe decision. The history can be queried by the history service.

  1. List<HistoricDecisionInstance> historicDecisions = processEngine
  2. .getHistoryService()
  3. .createHistoricDecisionInstanceQuery()
  4. .decisionDefinitionKey("decision-key")
  5. .includeInputs()
  6. .includeOutputs()
  7. .list();

For more information about this, please refer to the History for DMN Decisions.

原文: https://docs.camunda.org/manual/7.9/user-guide/process-engine/decisions/decision-service/