Camunda 8 dynamic DMN table

Hello All,

Is there a way to generate DMN table using Java in Camunda 8?

I have a use case where I need to add, update, delete, execute rules from DMN table so I thought in creating the DMN table using the java and redeploy it after every change
So, if there is also better solution to solve this use case you can tell me with it.

@Mohamed_Ahmed_Mohame Let’s generalize this so you can:

  1. Load a DMN XML file
  2. Search for a rule by input entry value
  3. Update its output entry value
  4. Save the updated DMN
  5. Deploy it to Zeebe

Generalized Java Class: DynamicDmnUpdater.java

This example finds a rule by an input value and updates its corresponding output.

import io.camunda.zeebe.client.ZeebeClient;
import org.w3c.dom.*;
import javax.xml.parsers.*;
import javax.xml.transform.*;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import java.io.File;

public class DynamicDmnUpdater {

    public static void main(String[] args) throws Exception {
        String dmnPath = "path/to/your-decision.dmn";
        String updatedPath = "path/to/updated-decision.dmn";
        String targetInputValue = "\"Silver\"";      // The input value to match
        String newOutputValue = "\"10% discount\"";  // The output value to set

        // Load and modify DMN
        updateDmnRule(dmnPath, updatedPath, targetInputValue, newOutputValue);

        // Deploy updated DMN
        deployToCamunda(updatedPath);
    }

    public static void updateDmnRule(String inputPath, String outputPath, String matchInput, String newOutput) throws Exception {
        DocumentBuilder docBuilder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
        Document doc = docBuilder.parse(new File(inputPath));
        doc.getDocumentElement().normalize();

        NodeList ruleList = doc.getElementsByTagName("rule");

        for (int i = 0; i < ruleList.getLength(); i++) {
            Element rule = (Element) ruleList.item(i);
            NodeList inputEntries = rule.getElementsByTagName("inputEntry");

            for (int j = 0; j < inputEntries.getLength(); j++) {
                Element inputEntry = (Element) inputEntries.item(j);
                String inputText = inputEntry.getTextContent().trim();

                if (inputText.equals(matchInput)) {
                    NodeList outputEntries = rule.getElementsByTagName("outputEntry");
                    if (outputEntries.getLength() > 0) {
                        Element outputEntry = (Element) outputEntries.item(0);
                        outputEntry.setTextContent(newOutput);
                        System.out.println("Updated rule with input " + matchInput + " to output " + newOutput);
                    }
                }
            }
        }

        // Save the modified DMN
        Transformer transformer = TransformerFactory.newInstance().newTransformer();
        transformer.setOutputProperty(OutputKeys.INDENT, "yes");
        DOMSource source = new DOMSource(doc);
        StreamResult result = new StreamResult(new File(outputPath));
        transformer.transform(source, result);
    }

    public static void deployToCamunda(String dmnFilePath) {
        try (ZeebeClient client = ZeebeClient.newClientBuilder().build()) {
            client.newDeployResourceCommand()
                  .addResourceFile(dmnFilePath)
                  .send()
                  .join();
            System.out.println("Successfully deployed updated DMN.");
        }
    }
}

How to Use It

  • Drop in your DMN file.
  • Change the matchInput and newOutput values based on what rule you want to update.
  • Run the class—it will:
    • Update the rule
    • Save a new DMN XML
    • Deploy it to Camunda 8
1 Like

@Mohamed_Ahmed_Mohame, Make sure you have these dependencies in your pom.xml:

<dependency>
    <groupId>io.camunda</groupId>
    <artifactId>zeebe-client-java</artifactId>
    <version>${camunda.version}</version> <!-- Match your Camunda version -->
</dependency>

<dependency>
    <groupId>org.apache.commons</groupId>
    <artifactId>commons-io</artifactId>
    <version>x.yy.z</version>
</dependency>
1 Like