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.

Goal

  • Implement a DMN model using the online DMN editor

  • Test DMN model

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:

    1. Only employees younger than 18 or at least 60 years, or employees with at least 30 years of service will receive 5 extra days;

    2. 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;

    3. 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/

  1. 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.

dmn model title

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:

  1. Add an Input node to the diagram by clicking on the Input node icon and placing it in the DRD.

    add drd input node
  2. Double-click on the node to set the name. We will name this node Age.

  3. With the Age node selected, open the property panel. Set the data type to number.

    drd input node property output data type
  4. In the same way, create an Input node for Years of Service. This node should also have its data type set to number.

    drd decision nodes complete

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:

  1. Add a Decision node to the DRD

    add drd decision node
  2. Give the node the name Base Vacation Days.

  3. Click on the node to select it and open the property panel. Set the node’s data type to number.

  4. Click on the node and click on the Edit icon to open the expression editor.

    drd decision node edit
  5. In the expression editor, click on the box that says Select expression and select Literal expression.

    select expression
  6. Simply set the Literal Expression to 22, the number of base vacation days defined in the problem statement.

    base vacation days literal expression
  7. 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.

  1. In your DRD, create 3 decision nodes with these given names. Set their data types to number.

  2. 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.

    add drd three decision nodes
  3. Select the Extra days case 1 node and open its expression editor by clicking on the Edit button.

  4. Select the expression Decision Table to create a boxed expression implemented as a decision table.

    drd decision node expression
  5. The first case defines 2 decisions which can be modelled with 2 rows in our decision table as such:

  6. employees younger than 18 or at least 60 years will receive 5 extra days, or …

  7. employees with at least 30 years of service will receive 5 extra days

    decision table case 1
  8. To add new lines to your table, right click the first column and select "Insert below"

    decision table new 1 new line
  9. Note that the hit-policy of the decision table is by default set to U, which means Unique. This implies that only one rule is expected to fire for a given input. In this case however, we would like to set it to Collect 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 the U in the upper-left corner of the decision table. Now, set the Hit Policy to Collect and the Builtin Aggregator to MAX.

    decision table hit policy
  10. 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 the Default output property to 0.

    decision table default output
  11. Save the model

  12. The other two decisions can be implemented in the same way. Now, implement the following two decision tables:

    • Case 2:

      decision table case 2
    • Case 3:

      decision table 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:

  1. Create a new Decision node in the model. Give the node the name Total Vacation Days and set its data type to number.

  2. 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.

    drd complete
  3. Click on the Total Vacation Days node and click on Edit to open the expression editor. Configure the expression as a literal expression.

  4. We need to configure the following logic:

  5. Everyone gets the Base Vacation Days.

  6. 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.

  7. If case 2 adds extra days, add them to the total.

  8. The above logic can be implemented with the following FEEL expression:

    total vacation days expression

Deploy and test your decision

You can test your decision using the KIE Extended services with KIE Sandbox "run" option or you can deploy it on OpenShift. You can also create a new kogito project with quarkus using maven locally and add your decision to it.