6. Vacation Days - Consuming Decisions
The first thing we should do is deploy the project. We'll deploy it in KIE Server using Business Central.
6.1 Deploying the Decision Service
With our decision model completed, we can now package our DMN model in a Deployment Unit (KJAR) and deploy it on the Execution Server. To do this:
-
In the bread-crumb navigation in the upper-left corner, click on
vacation-days-decisions
to go back to the project’s Library View. -
Click on the Deploy button in the upper-right corner of the screen. This will package our DMN mode in a Deployment Unit (KJAR) and deploy it onto the Execution Server (KIE-Server).
-
Go to the Execution Servers perspective by clicking on "Menu → Deploy → Execution Servers". You will see the Deployment Unit deployed on the Execution Server.
6.2 Testing DMN Solution
In this section, you will test the DMN solution with Execution Server’s Swagger interface and via Java KIE Client API.
6.3 Testing the solution via REST API
In this section, you will test the DMN solution with KIE Server’s Swagger interface.
The Swagger interface provides the description and documentation of the Execution Server’s RESTful API. At the same time, it allows the APIs to be called from the UI. This enables developers and users to quickly test, in this case, a deployed DMN Service.
-
Navigate to KIE Server
-
Locate the DMN Models section. The DMN API provides the DMN model as a RESTful resources, which accepts 2 operations:
-
GET
: Retrieves the DMN model. -
POST
: Evaluates the decisions for a given input.
-
-
Expand the
GET
operation by clicking on it. -
Click on the Try it out button.
-
Set the containerId field to
vacation-days-decisions
and set the Response content type toapplication/json
and click on Execute -
If requested, provide the username and password of your Business Central and KIE-Server user.
-
The response will be the model-description of your DMN model.
Next, we will evaluate our model with some input data. We need to provide our model with the age of an employee and the number of years of service. Let’s try a number of different values to test our deicions.
-
Expand the
POST
operation and click on the Try it out button -
Set the containerId field to
vacation-days-decisions
. Set the Parameter content type and Response content type fields toapplication/json
. -
Pass the following request to lookup the number of vacation days for an employee of 16 years old with 1 year of service (note that the namespace of your model is probably different as it is generated. You can lookup the namespace of your model in the response/result of the
GET
operation you executed ealier, which returned the model description).
-
Click on Execute. The result value of the
Total Vacation Days
should be 27. -
Test the service with a number of other values. See the following table for some sample values and expected output.
Age |
Years of Service |
Total Vacation Days |
16 |
1 |
27 |
25 |
5 |
22 |
44 |
20 |
24 |
44 |
30 |
30 |
50 |
20 |
24 |
50 |
30 |
30 |
60 |
20 |
30 |
6.4 Using the KIE Java Client
Red Hat Decision Manager provides a KIE Java Client API that allows the user to interact with the KIE-Server from a Java client using a higher level API. It abstracts the data marshalling and unmarshalling and the creation and execution of the RESTful commands from the developer, allowing him/her to focus on developing business logic.
In this section we will create a simple Java client for our DMN model.
IMPORTANT: If your KIE Server is exposed via https you need to configure the `javax.net.ssl.trustStore and
javax.net.ssl.trustStorePasswordin the Java client code using the Remote Java API. If not, you may get a
rest.NoEndpointFoundException`. For more information check this solution Red Hat's knowledge base.
-
Create a new Maven Java JAR project in your favourite IDE (e.g. IntelliJ, Eclipse, Visual Studio Code).
-
Add the following dependency to your project:
<dependency>
<groupId>org.kie.server</groupId>
<artifactId>kie-server-client</artifactId>
<version>7.48.0.Final-redhat-00006</version>
<scope>compile</scope>
</dependency>
-
Create a Java package in your
src/main/java
folder with the nameorg.kie.dmn.lab
. -
In the package you’ve just created, create a Java class called
Main.java
. -
Add a
public static void main(String[] args)
method to your main class. -
Before we implement our method, we first define a number of constants that we will need when implementing our method (note that the values of your constants can be different depending on your environment, model namespace, etc.):
private static final String KIE_SERVER_URL = "http://localhost:8080/kie-server/services/rest/server";
private static final String CONTAINER_ID = "vacation-days-decisions";
private static final String USERNAME = "pamAdmin";
private static final String PASSWORD = "redhatpam1!";
- KIE-Server client API classes can mostly be retrieved from the
KieServicesFactory
class. We first need to create aKieServicesConfiguration
instance that will hold our credentials and defines how we want our client to communicate with the server:
KieServicesConfiguration kieServicesConfig = KieServicesFactory.newRestConfiguration(KIE_SERVER_URL, new EnteredCredentialsProvider(USERNAME, PASSWORD));
- Next, we create the
KieServicesClient
:
- From this client we retrieve our DMNServicesClient:
-
To pass the input values to our model to the Execution Server, we need to create a
DMNContext
: -
We now have defined all the required instances needed to send a DMN evaluation request to the server:
-
Finally we can retrieve the DMN evaluation result and print it in the console:
-
Compile your project and run it. Observe the output in the console, which should say: Total vacation days: 27
The complete project can be found here: https://github.com/kmacedovarela/dmn-workshop-labs/tree/master/vacation-days-dmn-lab-client