Vacation Days
In this lab you’ll try out the combination of DMN decision tables with literal expressions. You will also explore a number of different FEEL constructs and expressions like, for example, ranges. Finally, you’ll learn how to use the KIE Java Client to consume decisions.
Problem Statement
In this lab we will create a decision that determines the number of vacation days assigned to an employee. The number of vacation days depends on age and years of service.
-
Every employee receives at least 22 days.
-
Additional days are provided according to the following criteria:
-
Only employees younger than 18 or at least 60 years, or employees with at least 30 years of service will receive 5 extra days;
-
Employees with at least 30 years of service and also employees of age 60 or more, receive 3 extra days, on top of possible additional days already given;
-
If an employee has at least 15 but less than 30 years of service, 2 extra days are given. These 2 days are also provided for employees of age 45 or more. These 2 extra days can not be combined with the 5 extra days.
-
Create a new decision
To work in the decision model, we’ll use the KIE Sandbox DMN editor. 1. In your browser, navigate to http://dmn.new/
-
Click on the Untitled tile to define the DMN model name. Give it the name
vacation-days
. This will create the asset and open the DMN editor.
Next, let’s work on the decision model.
4.1. Define the input nodes
The problem statement describes a number of different inputs to our decision:
-
Age of the employee
-
Years of Service of the employee
Therefore, we should create two input nodes, one for each input:
-
Add an Input node to the diagram by clicking on the Input node icon and placing it in the DRD.
-
Double-click on the node to set the name. We will name this node
Age
. -
With the
Age
node selected, open the property panel. Set the data type tonumber
. -
In the same way, create an Input node for
Years of Service
. This node should also have its data type set tonumber
.
4.2. Constants
The problem statement describes that every employee receives at least 22 days. So, if no other decisions apply, an employee receives 22 days. This is can be seen as a constant input value into our decision model. In DMN we can model such constant inputs with a Decision node with a Literal boxed expression that defines the constant value:
-
Add a Decision node to the DRD
-
Give the node the name
Base Vacation Days
. -
Click on the node to select it and open the property panel. Set the node’s data type to
number
. -
Click on the node and click on the Edit icon to open the expression editor.
-
In the expression editor, click on the box that says Select expression and select Literal expression.
-
Simply set the Literal Expression to
22
, the number of base vacation days defined in the problem statement. -
Save the model.
4.3. Decisions
The problem statement defines 3 decisions which can cause extra days to be given to employees based on various criteria. Let’s simply call these decision:
-
Extra days case 1
-
Extra days case 2
-
Extra days case 3
Although these decisions could be implemented in a single decision node, we’ve decided, in order to improve maintainability of the solution, to define these decisions in 3 separate decision nodes.
-
In your DRD, create 3 decision nodes with these given names. Set their data types to
number
. -
We need to attach both input nodes, Age and Years of Service to all 3 decision nodes. We can do this by clicking on an Input node, clicking on its arrow icon, and attaching the arrow to the Decision node.
-
Select the Extra days case 1 node and open its expression editor by clicking on the Edit button.
-
Select the expression Decision Table to create a boxed expression implemented as a decision table.
-
The first case defines 2 decisions which can be modelled with 2 rows in our decision table as such:
-
employees younger than 18 or at least 60 years will receive 5 extra days, or …
-
employees with at least 30 years of service will receive 5 extra days
-
To add new lines to your table, right click the first column and select "Insert below"
-
Note that the hit-policy of the decision table is by default set to
U
, which meansUnique
. This implies that only one rule is expected to fire for a given input. In this case however, we would like to set it toCollect Max
, as, for a given input, multiple decisions might match, but we would like to collect the output from the rule with the highest number of additional vacation days. To do this, click on theU
in the upper-left corner of the decision table. Now, set the Hit Policy toCollect
and the Builtin Aggregator toMAX
. -
Finally, we need to set the default result of the decision. This is the result that will be returned when none of the rules match the given input. This is done as follows: .. Select the output/result column of the decision table. In this case this is the column
Extra days case 1
.. Open the properties panel on the right-side of the editor. .. Expand the Default output section. .. Set theDefault output property
to0
. -
Save the model
-
The other two decisions can be implemented in the same way. Now, implement the following two decision tables:
-
Case 2:
-
Case 3:
-
4.4. Total Vacation Days
The total vacation days needs to be determined from the base vacation days and the decisions taken by our 3 decision nodes. As such, we need to create a new Decision node, which takes the output of our 4 Decision nodes (3 decision tables and a literal expression) as input and determines the final output. To do this, we need to:
-
Create a new Decision node in the model. Give the node the name
Total Vacation Days
and set its data type tonumber
. -
Connect the 4 existing Decision nodes to the node. This defines that the output of these nodes will be the input of the next node.
-
Click on the
Total Vacation Days
node and click on Edit to open the expression editor. Configure the expression as a literal expression. -
We need to configure the following logic:
-
Everyone gets the Base Vacation Days.
-
If both case 1 and case 3 add extra days, only the extra days of one of this decision is added. So, in that case we take the maximum.
-
If case 2 adds extra days, add them to the total.
-
The above logic can be implemented with the following FEEL expression: