Parse Decisions
Decisions can be parsed from an or transformed from a .
This example shows how to parse a decision from an input stream:
The next example uses the DMN Model API to first create aDmnModelInstance and then transform the decisions:
// create a default DMN engine
DmnEngine dmnEngine = DmnEngineConfiguration
.createDefaultDmnEngineConfiguration()
.buildEngine();
// read a DMN model instance from a file
DmnModelInstance dmnModelInstance = Dmn.readModelFromFile(...);
// parse the decisions
List<DmnDecision> decisions = dmnEngine.parseDecisions(dmnModelInstance);
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.
<?xml version="1.0" encoding="UTF-8"?>
<definitions xmlns="http://www.omg.org/spec/DMN/20151101/dmn.xsd" id="definitions" name="definitions" namespace="http://camunda.org/schema/1.0/dmn">
<decision id="first-decision" name="First Decision">
<decisionTable>
<output id="output1"/>
</decisionTable>
</decision>
<decision id="second-decision" name="Second Decision">
<decisionTable>
<output id="output2"/>
</decision>
</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.
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
.
// parse the drg from an input stream
DmnDecisionRequirementsGraph drg = dmnEngine.parseDecisionRequirementsGraph(inputStream);
// get the keys of all containing decisions
Set<String> decisionKeys = drg.getDecisionKeys();
// get a containing decision by key
// get all containing decisions
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.
It is possible to check if a parsed decision is implemented as by using the method isDecisionTable().
// create a default DMN engine
DmnEngine dmnEngine = DmnEngineConfiguration
.createDefaultDmnEngineConfiguration()
.buildEngine();
// read the DMN XML file as input stream
InputStream inputStream = ...
// parse all decision from the input stream
List<DmnDecision> decisions = dmnEngine.parseDecisions(inputStream);
// get the first decision
DmnDecision decision = decisions.get(0);
// do something if it is a decision table
// ...
}
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.
Pass Variables
The following example shows how to use a VariableMap
.
// create the input variables
VariableMap variables = Variables.createVariables()
.putValue("x", "camunda")
.putValue("y", 2015);
// evaluate the decision with the input variables
result = dmnEngine.evaluateDecision(decision, variables);
Alternatively, a VariableContext
can be used.Use the to support lazy-loading of variables.
The evaluation of a DMN decision returns a DmnDecisionResult. If the decision is implemented as 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.
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.TheDmnDecisionResult
thus consists of oneDmnDecisionResultEntries
which contains the keydesiredDish
.
To access the output value, get()
method of DmnDecisionResultEntries
is used:
DmnDecisionResult decisionResult = dmnEngine.evaluateDecision(decision, variables);
// the size will be 1
int size = decisionResult.size();
// get the matching rule
DmnDecisionResultEntries ruleResult = decisionResult.get(0);
// get output values by name
Object result = ruleResult.get("desiredDish");
The result objects expose additional convenience methods:
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.
The following shows that the Beverages
decision requires the Dish
decision (from the previous example).
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 valueStew
that is mapped to the output variabledesiredDish
.
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
.
DmnDecision decision = dmnEngine.parseDecision("beverages", inputStream);
DmnDecisionResult decisionResult = dmnEngine.evaluateDecision(decision, variables);
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 hit policy with aggregator then the decision result (i.e. output value) is only the aggregated value.