While most advanced business process management tools implement a thing called decision tables to be used in a defined business process, MuleSoft’s flow editor is still missing this basic feature.
In Mule flows, simple decisions can be made with a “Choice Router ” instead, which mimicks a simple if-then-else rule logic.
Each branch in this router needs to define its own matching expression separately, so the criteria for the choices are not shared or being referred to in other choices.
Furthermore, a scenario involving more than two parameters for this decision tree can be rather tedious to set up.
Having to set up all the possible combinations with separate branches in the decision tree and checking all the same parameters all over again can introduce repeated (and unnecessary) code and code complexity.
You can either
- nest a choice into another choice, however then you need to repeat the criteria of the choices in every other branch.
- or put all the variants into one choice and combine all criteria with Boolean logic in some way.
But as you can see this gets more complicated the more criteria you need for a decision.
The very limited view of the decision in the flow editor (by just truncating the choice criteria with the characters “…”) only adds to this problem.
Consequently, when more parameters are added, it becomes more and more difficult to understand the variants and to oversee the choice in total.
In this case a comprehensive view is necessary – and this is where decision tables come in.
They can be introduced to see quickly what combinations should lead to which results (see example below).
Solution / implementation
The Decision Model Notation (DMN) is a standard by the OMG standards development organization which addresses this. In its format, decision tables can be defined.
As there is currently no editor known to the author of this article which can be used in the Eclipse IDE, he used VS Code and its DMN Editor Plugin released by RedHat.
Shown in the screenshots below are the overview of the DMN model and the decision table “Final Price” included.
dmn model overview
example of a decision table “Final Price”
Marked in red are the input parameters which should be considered for the outcome of the decision.
Also placed in the DMN model is a fixed integer value for Price, which can be used in the calculation of the final price (as the function call “Price()“).
Installation and use
At first you might need to add either https://repo1.maven.org/maven2/ or https://repository.mulesoft.org/nexus/content/repositories/public to your list of plugin repositories in either Maven’s configuration file (settings.xml) or your project configuration file (pom.xml). Make sure you have one of them added in the list of pluginRepositories (not repositories as it is a plugin…).
Then make sure to add this in your dependencies list:
<dependency> <groupId>io.github.obscure1910</groupId> <artifactId>mule-dmn</artifactId> <version>0.2.0</version> <classifier>mule-plugin</classifier> </dependency>
After the plugin is installed, in the Mulesoft configuration elements, a connector configuration can be added for the new module. The model name and the namespace need to be set in the connector configuration page according to the attributes “id” and “model” of the DMN file.
Open the DMN XML file in a text editor to see these values (in this case namespace=”https://kiegroup.org/dmn/_FEB19CAC-D6B6-45F7-9DCF-E3678AA6217D” and name=”price” ) seen below:
dmn adapter configuration
Once this is done, you can add the node “execute dmn” in your flow editor canvas and start filling in the parameters and use the results of the models.
To achieve this, you can add a transformer before and after the model. See the below screenshot where the same parameters are being filled which are defined in the DMN model seen in the previous screenshot.
Mule flow using dmn model – preparing transformer
If you start this flow in a debugging session, you can see the output of the model (in which the price had a fixed value of 100) as XML payload:
<?xml version='1.0' encoding='UTF-8'?><results><Final_Price><BigDecimal>600</BigDecimal></Final_Price></results>
For the model used in this example you can access the value of the XML with a transformer like this:
%dw 2.0 output application/json --- payload.results.Final_Price.BigDecimal
Notice that the space in the name of the result column is being replaced by an underscore in the XML.
The source code of this plugin is available at this GitHub repository . Pull requests and comments are very welcome.
You can also find a sample Mule project file called “spielwiese_dmn.jar” in the repos folder “anypointstudio ” which you can import in Anypoint Studio and which already configured the dmn file in a sample flow.
The sample is pretty similar to the example you see in this article.
Limitations, known issues and thanks
So far, the error handling for broken dmn models and when the model simply calculates no results is rather limited.
Also, the metadata propagation for the structure of the outgoing value structure is not implemented yet.
Thanks to my humble colleague Benjamin who made all this possible.
Your job at codecentric?
More articles in this subject area
Discover exciting further topics and let the codecentric world inspire you.