[JAVA] Run a DMN with the Camunda DMN Engine

What is DMN (Decision Modeling and Notation)?

It is a decision-making modeling notation specified by OMG (Object Managemnt Group). In recent years, it has been attracting attention as a decision-making automation method in the knowledge part of RPA.

https://www.omg.org/spec/DMN/About-DMN/

The biggest feature of DMN is that you can execute the decision-making model written in a way that is easy for humans to understand. You can test the operation of your DMN with Camunda's DMN Simulator (https://camunda.com/dmn/simulator/). In this sample, the optimal dish (Dish) is determined based on the season and the number of guests, and the optimal drink (Beverages) is determined based on the dish and the guests with children. To do. "Optimal" here means based on predetermined logic (knowledge of companies and individuals). DMN gives this logic in a table called a decision table. For more information, please see here.

Try setting Decision Table to Beverages, Guests with Children to true, Season to Fall, and How Many Guests to 2. Two drinks, Aecht Schlenkerla Rauchbier and Apple Juice, were output as below. From the screen, you can see that Spareribs was selected as the dish that meets the entered conditions. The German beer Aecht Schlenkerla Rauchbier was chosen as the drink for Spareribs. Apple Juice is for kids, isn't it? This is the result of selecting the most suitable drink from the "knowledge" called the decision table based on the entered season and the number of guests.

image.png

Behind the scenes of this simulator is the Camunda DMN Engine running the DMN. This time, run this Camunda DMN Engine locally to run the DMN.

DMN file preparation

This time, we will use Camunda's sample DMN file as the DMN for verification. Download the sample DMN file from the "Download DMN Table" on the DMN Simulator Page. A DMN design tool is required to view and edit DMN files. Personally, I recommend Camunda Modeler, which is standalone and easy to operate. By the way, if you just want to run DMN, you don't need any design tools.

Acquiring Camunda DMN Engine

The Camunda DMN Engine is published on Maven Central. Add the following dependency to the POM.

<dependency>
    <groupId>org.camunda.bpm.dmn</groupId>
    <artifactId>camunda-engine-dmn</artifactId>
</dependency>

Running a DMN with the Camunda DMN Engine

Below is a sample Java code that executes a DMN.

//DMN file path and Decision Id settings
String dmnFilePath = "./simulation.dmn";
String decisionId = "beverages";

//Input data settings
Map<String, Object> input = new HashMap<String, Object>();
input.put("season", "Fall"); //autumn
input.put("guestCount", 2);  //2 guests
input.put("guestsWithChildren",true); //With children

//DMN engine generation
DmnEngine dmnEngine = DmnEngineConfiguration.createDefaultDmnEngineConfiguration().buildEngine();

//Generation of Decision
DmnDecision decision = dmnEngine.parseDecision(decisionId,new FileInputStream(dmnFilePath));

//Run DMN
DmnDecisionResult result = dmnEngine.evaluateDecision(decision,input);

//Get execution result
for(Map<String,Object> entry : result.getResultList()) {
    System.out.println(entry.get("beverages"));
}

Here, the decisionId is specified in the decision table Id, which is the value set in the following location on the decision table displayed by Camunda Modeler.

image.png

The input is given the same value as the example of running the DMN simulator earlier. When I run the above code, I get the same result as the DMN simulator as below.

Aecht Schlenkerla Rauchbier
Apple Juice

Add Output

Since the output of the decision table "beverages" is only beverage, you can only get drinks at this decision table. But anyway I want to get both the food and drink of choice. So edit the decision table to increase the output. In the example below, Camunda Modeler is used to edit the decision table.

Add a column to the output of beverages as shown below. Then, the value of Dish, which is one of the inputs of this decision table, is used as the output. The "desired Dish" in this output column means a reference to the Dish Expression (variable name).

image.png

The Input item of the decision table has Label, which is a display label, Expression, which sets the variable name or expression of the value, and Type, which indicates the data type of the value. In Camunda Modeler, Expression is not displayed unless you click the Input header. In the sample DMN, the Expression of the Dish column is desiredDish, and use this value when referencing from other columns.

image.png

Next, rewrite "Get Execution Result" in the above Java code as follows so that both food and drink can be displayed.

//Get execution result
for(Map<String,Object> entry : result.getResultList()) {
    System.out.println(entry.get("desiredDish") + " with " + entry.get("beverages"));
}

The execution result is shown below. You can now get both food and drinks.

Spareribs with Aecht Schlenkerla Rauchbier
Spareribs with Apple Juice

Recommended Posts

Run a DMN with the Camunda DMN Engine
Create a jar file with the command
I tried running the route search engine (OSRM) easily with a container
Until you run a Java program with the AWS SDK local to Windows
Come out with a suffix on the method 2
Create a multi-key map with the standard library
Refactor the Decorator pattern implementation with a functional interface
How to run the SpringBoot app as a service
[Tutorial] Download Eclipse → Run the application with Java (Pleiades)
The story of making a reverse proxy with ProxyServlet
Run Pico with docker
Run Payara with Docker
How to take a screenshot with the Android Studio emulator
Create a Docker image with the Oracle JDK installed (yum
The first WEB application with Spring Boot-Making a Pomodoro timer-
A story packed with the basics of Spring Boot (solved)
Check the operation of two roles with a chat application
Run PHP-FPM with OPcache enabled in a Read Only container
Run multiple applications with the same logic under Struts environment
[Swift 5] Select a date with the IDate Picker on iOS14
Run analysis with OpenJDK11 Java Flight Recorder + Google Kubernetes Engine
[Docker] Delete only the volume associated with a specific container
Explain the benefits of the State pattern with a movie rating
About the behavior when doing a file map with java
Find the number of days in a month with Kotlin
With the software I've been making for a long time ...
Modeling a Digimon with DDD for the first time Part 1