This is a declarative approach, it does not require to have any extra implementation, the interaction with decisions is executed out-of-the-box by the engine. The information needed to execute the decision evaluation should be set in the Data Assignments in the Business Rule Task.
The BPMN file where this process is declared is traffic-rules-dmn.bpmn.
These are the properties defined for the process, the most important one in this section to pay attention is the ID because it is used in the REST endpoint generation referring to the path to interact with this process.
The variables used in the process itself, but the focus in this example are the classes that are used to define the POJOs to interact the process with decisions, that are the Violation, Driver, Fine.
Mapping data from Process to/from DMN
It is important to mention DMN for instance can define the Data Type in its structure, but we can align all attributes names in a Java class that is used as process variables, in case the attribute names contain spaces or are not following java conventions we can use Jackson annotations to make the process variable POJOs aligned with DMN data types, for instance in the Violation class, where it is mapped the speedLimit
attribute as Speed Limit
using @JsonProperty
annotation, in this case, this attribute from the process variable with Violation can be seamlessly integrated Violation Data Type defined in DMN.
Violation Data Type in DMN
Fetch for driver information, in this implementation it is just mocking a result, that simply fill with an expired license date in case the driverId
is an odd number and with a valid date in case of an even number. In a real use case, it could be performing an external call to a service or a database to get this information.
The service task implementation is done in the DriverService class.
In the data assignment the input is the driverId
and output is the driver
variable, filled with all driver information.
Represents the task to do the call to the DRL service.
The properties to be set are mainly the Rule Language
that should be set as DRL
and the Rule Flow Group
with unit:
+ [the FQCN of the Rule Unit Data class]
, in this case org.kie.kogito.traffic.LicenseValidationService.
The input and output mapping for this task is just the driver variable that is filled with license validation information.
Similar to the License Validation Task, but it represents the task to do the call to the DMN service.
The properties to be set are mainly the Rule Language
that should be set as DMN
and the Namespace
and DMN Model Name
must be set with the values defined in in the DMN, in this case TrafficViolation.dmn.
The input for this task is the Driver
and Violation
variables, and the outputs are the Suspended
and Fine
.
Just an example task where it could be performed any action based on the condition in which the driver is suspended. In the current implementation, it is just logging the information in the console.
Just an example task where it could be performed any action based on the condition in which the driver is not suspended. In the current implementation, it is just logging the information in the console.
This decision consistis in rules which are evaluated to check if a driver's license is expired or not according to the expiration date and thus populating the result in the information in the driver variable.
The DRL file where this Rule Unit is declared is LicenseValidationService.drl and the the Java class that contains the Rule Unit Data is LicenseValidationService.
This decision consists in a DMN that basically checks if a driver is suspended or not according to the violation and current driver points in its license.
The DMN file where this decision is declared is TrafficViolation.dmn
You will need:
mvn clean spring-boot:run
mvn clean package java -jar target/process-decisions-springboot.jar
or on windows
mvn clean package java -jar target\process-decisions-springboot.jar
You can take a look at the OpenAPI definition - automatically generated and included in this service - to determine all available operations exposed by this service. For easy readability you can visualize the OpenAPI definition file using a UI tool like for example available Swagger UI.
In addition, various clients to interact with this service can be easily generated using this OpenAPI definition.
Once the service is up and running we can invoke the REST endpoints and examine the logic.
To make use of this application it is as simple as putting a sending request to http://localhost:8080/traffic
with appropriate contents. See the following cases:
You can play with different attributes. The driver-id
format is {days}-{points}
. Days value > 0 will originate a driver
with valid license. In this case, the evaluation will proceed taking in account the given points. Days value = 0 will originate a driver
with invalid license. In this case, the evaluation will stop after first node and will return a null
validation.
Given data:
{ "driverId": "12-345", "violation":{ "Type":"speed", "Speed Limit": 100, "Actual Speed":140 } }
Submit the JSON object from above:
curl -X POST -H 'Content-Type:application/json' -H 'Accept:application/json' -d '{"driverId": "12-345","violation":{"Type":"speed","Speed Limit": 100,"Actual Speed":140}}' http://localhost:8080/traffic
After the Curl command you should see a similar console log
{ "id": "06ecbbb0-4972-431d-9b37-355d83bb1092", "driverId": "12-345", "driver": { "licenseExpiration": "2021-08-13T17:59:08.589+00:00", "validLicense": true, "Name": "Arthur", "State": "SP", "City": "Campinas", "Points": 13, "Age": 30 }, "validated": { "ValidLicense": true, "Suspended": "no" }, "fine": { "Amount": 1000.0, "Points": 7 }, "violation": { "Code": null, "Date": null, "Type": "speed", "Speed Limit": 100, "Actual Speed": 140 } }
Given data:
{ "driverId": "12-15", "violation":{ "Type":"speed", "Speed Limit": 100, "Actual Speed":110 } }
Submit the JSON object from above:
curl -X POST -H 'Content-Type:application/json' -H 'Accept:application/json' -d '{"driverId": "12-15","violation":{"Type":"speed","Speed Limit": 100,"Actual Speed":110}}' http://localhost:8080/traffic
After the Curl command, you should see a similar console log
{ "id": "887d8f39-93ec-43cc-96e5-df8e9bb199f8", "driverId": "12-15", "driver": { "licenseExpiration": "2021-08-12T17:59:37.703+00:00", "validLicense": false, "Name": "Arthur", "State": "SP", "City": "Campinas", "Points": 13, "Age": 30 }, "validated": { "Suspended": "yes", "ValidLicense": false }, "fine": null, "violation": { "Code": null, "Date": null, "Type": "speed", "Speed Limit": 100, "Actual Speed": 110 } }
Given data:
{ "driverId": "0-150", "violation":{ "Type":"speed", "Speed Limit": 100, "Actual Speed":110 } }
Submit the JSON object from above:
curl -X POST -H 'Content-Type:application/json' -H 'Accept:application/json' -d '{"driverId": "0-150","violation":{"Type":"speed","Speed Limit": 100,"Actual Speed":110}}' http://localhost:8080/traffic
After the Curl command, you should see a similar console log
{ "id": "887d8f39-93ec-43cc-96e5-df8e9bb199f8", "driverId": "0-150", "driver": { "licenseExpiration": "2021-08-12T17:59:37.703+00:00", "validLicense": false, "Name": "Arthur", "State": "SP", "City": "Campinas", "Points": 13, "Age": 30 }, "validated": null, "fine": null, "violation": { "Code": null, "Date": null, "Type": "speed", "Speed Limit": 100, "Actual Speed": 110 } }
In this case the driver license is expired when the DRL is evaluated because the DriverService generated an expired date for the driver's license thus DMN is not evaluated, so the validLicense
is false
, suspended
and fine
are null
.