A decision table has a hit policy that specifies what theresults of the evaluation of a decision table consist of.
The hit policy is set in the hitPolicy
attribute on the decisionTable
XMLelement. If no hit policy is set, then the default hit policy UNIQUE
is used.
<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="dish" name="Dish">
<decisionTable id="decisionTable" hitPolicy="RULE ORDER">
<!-- .. -->
</decisionTable>
</decision>
</definitions>
In the visual representation of the decision table, the hit policy is specifiedby the initial letter of the hit policy. The following hit policies aresupported by the Camunda DMN engine:
Visual representation | XML representation |
---|---|
U | UNIQUE |
A | ANY |
F | FIRST |
R | RULE ORDER |
C | COLLECT |
The Role of a Hit Policy
A hit policy specifies how many rules of a decision table can be satisfied andwhich of the satisfied rules are included in the decision table result. Thehit policies Unique, Any and First will always return a maximum of onesatisfied rule. The hit policies Rule Order and Collect can returnmultiple satisfied rules.
Unique Hit Policy
Only a single rule can be satisfied. The decision table result contains theoutput entries of the satisfied rule.
If more than one rule is satisfied, the Unique hit policy is violated.
See the following decision table.
Depending on the current season the dish should be chosen.Only one dish can be chosen, since only one season can exist at the same time.
Any Hit Policy
Multiple rules can be satisfied. However, all satisfied rules must generate the sameoutput. The decision table result contains only the output of one of thesatisfied rules.
If multiple rules are satisfied which generate different outputs, the hit policyis violated.
See the following example:
This is a decision table for the leave application. If the applierhas no vacation days left or is currently in the probation period, the application will be refused.Otherwise the application is applied.
First Hit Policy
Multiple rules can be satisfied. The decision table result contains onlythe output of the first satisfied rule.
See the above decistion table for advertisement. Regarding the current age of the user, whichadvertisement should be shown is decided. For example, the user is 19 years old. All the rules will match, but sincethe hit policy is set to first only, the advertisement for Cars is used.
Rule Order Hit Policy
Multiple rules can be satisfied. The decision table result contains the outputof all satisfied rules in the order of the rules in the decision table.
Again, see the advertisement example with the rule order policy. Say we have a user at the age of 19 again.All rules are satisfied so all outputs are given, ordered by the rule ordering.It can perhaps be used to indicate the priority of the displayed advertisements.
Collect Hit Policy
Multiple rules can be satisfied. The decision table result contains the outputof all satisfied rules in an arbitrary order as a list.
With this hit policy, the output list has no ordering. So the advertisement will be arbitraryif, for example, the age is 19.
Additionally, an aggregator can be specified for the Collect hit policy. If anaggregator is specified, the decision table result will only contain a singleoutput entry. The aggregator will generate the output entry from all satisfiedrules. Note if the Collect hit policy is used with an aggregator, thedecision table can only have one output.
The aggregator is set as the aggregation
attribute of the decisionTable
XML element.
<decisionTable id="decisionTable" hitPolicy="COLLECT" aggregation="SUM">
<!-- .. -->
</decisionTable>
Aggregators for Collect Hit Policy
In the visual representation of the decision table the aggregator is specifiedby a marker after the hit policy. The following aggregators are supported bythe Camunda DMN engine:
Visual representation | XML representation | Result of the aggregation |
---|---|---|
+ | SUM | the sum of all output values |
< | MIN | the smallest value of all output values |
> | MAX | the largest value of all output values |
# | COUNT | the number of output values |
SUM aggregator
The SUM aggregator sums up all outputs from the satisfied rules.
The showed decision table can be used to sum up the salary bonus for an employee. For example, the employee has been working in the company for3.5 years. So the first, second and third rule will match and the result of the decision table is 600, since the output is summed up.
MIN aggregator
The MIN aggregator can be used to return the smallest output value of all satisfied rules.See the following example of a car insurance. After years without a car crash the insurance fee will be reduced.
For example, if the input for the decision table is 3.5 years, the result will be 98.83, since the first three rules matchbut the third rule has the minimal output.
MAX aggregator
The MAX aggregator can be used to return the largest output value of all satisfied rules.
This decision table represents the decision for the amount of pocket money for a child.Depending of the age, the amount grows. For example, an input of 9 will satisfy the first and second rules.The output of the second rule is larger then the output of the first rule, so the outputwill be 5. A child at the age of 9 will get 5 as pocket money.
COUNT aggregator
The COUNT aggregator can be use to return the count of satisfied rules.
For example, see the salary bonus decision table again, this time with the COUNT aggregator.With an input of 4, the first three rules will be satisfied. Therefore, the result from the decision table will be 3, which meansthat after 4 years the result of the decision table is 3 salary bonuses.
原文: https://docs.camunda.org/manual/7.9/reference/dmn11/decision-table/hit-policy/