Chapter 17. Invoking a Decision Model and Notation (DMN) service in a business process

You can use Decision Model and Notation (DMN) to model a decision service graphically in a decision requirements diagram (DRD) in Business Central and then invoke that DMN service as part of a business process in Business Central. Business processes interact with DMN services by identifying the DMN service and mapping business data between DMN inputs and the business process properties.

As an illustration, this procedure uses an example TrainStation project that defines train routing logic. This example project contains the following data object and DMN components designed in Business Central for the routing decision logic:

Example Train object

public class Train {

     private String departureStation;

     private String destinationStation;

     private BigDecimal railNumber;

     // Getters and setters
}

Figure 17.1. Example Compute Rail DMN model

dmn execution graph

Figure 17.2. Example Rail DMN decision table

dmn execution expression

Figure 17.3. Example tTrain DMN data type

dmn execution data type

For more information about creating DMN models in Business Central, see Designing a decision service using DMN models.

Prerequisites

  • All required data objects and DMN model components are defined in the project.

Procedure

  1. In Business Central, go to MenuDesignProjects and click the project name.
  2. Select or create the business process asset in which you want to invoke the DMN service.
  3. In the process designer, use the left toolbar to drag and drop BPMN components as usual to define your overall business process logic, connections, events, tasks, or other elements.
  4. To incorporate a DMN service in the business process, add a Business Rule task from the left toolbar or from the start-node options and insert the task in the relevant location in the process flow.

    For this example, the following Accept Train business process incorporates the DMN service in the Route To Rail node:

    Figure 17.4. Example Accept Train business process with a DMN service

    dmn execution business process
  5. Select the business rule task node that you want to use for the DMN service, click Diagram properties in the upper-right corner of the process designer, and under Implementation/Execution, define the following fields:

    • Rule Language: Select DMN.
    • Namespace: Enter the unique namespace from the DMN model file. Example: https://www.drools.org/kie-dmn
    • Decision Name: Enter the name of the DMN decision node that you want to invoke in the selected process node. Example: Rail
    • DMN Model Name: Enter the DMN model name. Example: Compute Rail

      Important

      When you explore the root node, ensure that the Namespace and DMN Model Name fields consist of the same value in BPMN as DMN diagram.

  6. Under Data AssignmentsAssignments, click the Edit icon and add the DMN input and output data to define the mapping between the DMN service and the process data.

    For the Route To Rail DMN service node in this example, you add an input assignment for Train that corresponds to the input node in the DMN model, and add an output assignment for Rail that corresponds to the decision node in the DMN model. The Data Type must match the type that you set for that node in the DMN model, and the Source and Target definition is the relevant variable or field for the specified object.

    Figure 17.5. Example input and output mapping for the Route To Rail DMN service node

    dmn execution io mapping
  7. Click Save to save the data input and output data.
  8. Define the remainder of your business process according to how you want the completed DMN service to be handled.

    For this example, the Diagram propertiesImplementation/ExecutionOn Exit Action value is set to the following code to store the rail number after the Route To Rail DMN service is complete:

    Example code for On Exit Action

    train.setRailNumber(rail);

    If the rail number is not computed, the process reaches a No Appropriate Rail end error node that is defined with the following condition expression:

    Figure 17.6. Example condition for No Appropriate Rail end error node

    dmn execution negative condition

    If the rail number is computed, the process reaches an Accept Train script task that is defined with the following condition expression:

    Figure 17.7. Example condition for Accept Train script task node

    dmn execution positive condition

    The Accept Train script task also uses the following script in Diagram propertiesImplementation/ExecutionScript to print a message about the train route and current rail:

    com.myspace.trainstation.Train t =
        (com.myspace.trainstation.Train) kcontext.getVariable("train");
    System.out.println("Train from: " + t.getDepartureStation() +
                       ", to: " + t.getDestinationStation() +
                       ",  is on rail: " + t.getRailNumber());
  9. After you define your business process with the incorporated DMN service, save your process in the process designer, deploy the project, and run the corresponding process definition to invoke the DMN service.

    For this example, when you deploy the TrainStation project and run the corresponding process definition, you open the process instance form for the Accept Train process definition and set the departure station and destination station fields to test the execution:

    Figure 17.8. Example process instance form for the Accept Train process definition

    dmn execution process instance form

    After the process is executed, a message appears in the server log with the train route that you specified:

    Example server log output for the Accept Train process

    Train from: Zagreb, to: Belgrade,  is on rail: 1