Search code examples
javaxmlcamundadmndecision-model-notation

Getting DMN decision variable list in java


Given an xml file containing a some DMN decisions, I need to retrieve all of to the variable names process them. If i can't do this then I can't know what the name of the variables given by the user are, just the value. I've been using the camunda java library and it doens't seem very straight forward


<?xml version="1.0" encoding="UTF-8"?>
<definitions xmlns="http://www.omg.org/spec/DMN/20151101/dmn.xsd" id="definitions" name="definitions" namespace="http://camunda.org/schema/1.0/dmn">
    <decision id="decision" name="Dish">
        <decisionTable id="decisionTable">
            <input id="input1" label="Season">
                <inputExpression id="inputExpression1" typeRef="string">
                    <text>season</text>
                </inputExpression>
            </input>
            <input id="InputClause_0hmkumv" label="How many guests">
                <inputExpression id="LiteralExpression_0m7s53h" typeRef="integer">
                    <text>guestCount</text>
                </inputExpression>
            </input>
            <output id="output1" label="Dish" name="desiredDish" typeRef="string" />
            <rule id="row-950612891-1">
                <inputEntry id="UnaryTests_0c1o054">
                    <text><![CDATA["Fall"]]></text>
                </inputEntry>
                <inputEntry id="UnaryTests_1lod0sz">
                    <text><![CDATA[<= 8]]></text>
                </inputEntry>
                <outputEntry id="LiteralExpression_065u3ym">
                    <text><![CDATA["Spareribs"]]></text>
                </outputEntry>
            </rule>
            <rule id="row-950612891-2">
                <inputEntry id="UnaryTests_0u1z4ho">
                    <text><![CDATA["Winter"]]></text>
                </inputEntry>
                <inputEntry id="UnaryTests_1euytqf">
                    <text><![CDATA[<= 8]]></text>
                </inputEntry>
                <outputEntry id="LiteralExpression_198frve">
                    <text><![CDATA["Roastbeef"]]></text>
                </outputEntry>
            </rule>
            <rule id="row-950612891-3">
                <inputEntry id="UnaryTests_1vn9t5c">
                    <text><![CDATA["Spring"]]></text>
                </inputEntry>
                <inputEntry id="UnaryTests_1bbbmvu">
                    <text><![CDATA[<= 4]]></text>
                </inputEntry>
                <outputEntry id="LiteralExpression_1bewepn">
                    <text><![CDATA["Dry Aged Gourmet Steak"]]></text>
                </outputEntry>
            </rule>
            <rule id="row-950612891-4">
                <description>Save money</description>
                <inputEntry id="UnaryTests_0ogofox">
                    <text><![CDATA["Spring"]]></text>
                </inputEntry>
                <inputEntry id="UnaryTests_0c60gjz">
                    <text>[5..8]</text>
                </inputEntry>
                <outputEntry id="LiteralExpression_1lahvj7">
                    <text><![CDATA["Steak"]]></text>
                </outputEntry>
            </rule>
            <rule id="row-950612891-5">
                <description>Less effort</description>
                <inputEntry id="UnaryTests_1774yme">
                    <text><![CDATA["Fall", "Winter", "Spring"]]></text>
                </inputEntry>
                <inputEntry id="UnaryTests_01rn17i">
                    <text><![CDATA[> 8]]></text>
                </inputEntry>
                <outputEntry id="LiteralExpression_0jpd7hr">
                    <text><![CDATA["Stew"]]></text>
                </outputEntry>
            </rule>
            <rule id="row-950612891-6">
                <description>Hey, why not!?</description>
                <inputEntry id="UnaryTests_0ifdx8k">
                    <text><![CDATA["Summer"]]></text>
                </inputEntry>
                <inputEntry id="UnaryTests_0c8ym7l">
                    <text></text>
                </inputEntry>
                <outputEntry id="LiteralExpression_08d4mb6">
                    <text><![CDATA["Light Salad and a nice Steak"]]></text>
                </outputEntry>
            </rule>
        </decisionTable>
    </decision>
</definitions>```


above is the following dmn file I took from the internet.
It contains 2 variables; Season and guestCount
I already knew this so in my java file I simply did 
        VariableMap variables = Variables
                .putValue("season", season)
                .putValue("guestCount", guestCount);```

But what if I didn't do this? Every example i come accross on camunda's github already has the names of the variables hard-coded into the example. No example I saw fetched the number and the name of these variables. Do you know if there is any built-in function for the camunda-engine in java?

Went online trying to see some examples of people fetching these automatically from a java file. Asked AI to help me only to be given List<DecisionInputImpl> decisionInputs = decisionDefinition.getInputs(); Where the path of their "DecisionInputImpl" was non-existent (example of non existent path : import org.camunda.bpm.engine.DecisionService;) scoured the forums and saw someone advise someone else to do a rest something but I didn't really understood


Solution

  • I have found an answer to my question. This works with camunda name schemes. I don't know if it is a definitive answer but it works well enough for me. Here is the signature of the java fuction

        public List<String> recupererInputs(InputStream inputStream){
        DmnModelInstance dmnModelInstance = Dmn.readModelFromStream(inputStream);
        DecisionTable decisionTable = dmnModelInstance.getModelElementById("decisionTable");
        List<String> expressions = new ArrayList<>();
        Collection<Input> inputs = decisionTable.getInputs();
        inputs.stream().forEach(input->{
            // chaque variable dans "input" sera mise dans expressions. Cela facilitera la manipulation après
            InputExpression inputExpression = input.getInputExpression();
            expressions.add(inputExpression.getText().getTextContent());
        });
        return expressions;
    }
    

    the 'inputStream' in question is the Stream made from the dmn file in my local machine. Once I had all the names of the variables, I only had to associate them with the values passed by the user and then it went smoothly enough.