diff --git a/src/main/java/org/prestoncabe/generators/SimpleKogitoDMNExample.java b/src/main/java/org/prestoncabe/generators/SimpleKogitoDMNExample.java new file mode 100644 index 0000000..f6f66d4 --- /dev/null +++ b/src/main/java/org/prestoncabe/generators/SimpleKogitoDMNExample.java @@ -0,0 +1,146 @@ +package org.prestoncabe.generators; + +import org.kie.dmn.model.v1_2.*; +import org.kie.dmn.model.v1_2.dmndi.*; +import org.kie.dmn.backend.marshalling.v1x.DMNMarshallerFactory; +import javax.xml.namespace.QName; + +import java.io.File; +import java.io.FileWriter; +import java.io.IOException; +import java.nio.file.Files; +import java.nio.file.Path; +import java.nio.file.Paths; +import java.util.UUID; + +public class SimpleKogitoDMNExample { + public static void main(String[] args) { + // Load the sample file to see how it structures QNames + try { + // Generate unique IDs + String modelId = "_" + UUID.randomUUID().toString().toUpperCase(); + String modelName = "discount_calculator"; + String namespaceURI = "https://kiegroup.org/dmn/" + UUID.randomUUID().toString(); + + // Create the DMN model definition + TDefinitions definitions = new TDefinitions(); + definitions.setId(modelId); + definitions.setName(modelName); + definitions.setNamespace(namespaceURI); + + // Set required namespaces + definitions.setTypeLanguage("http://www.omg.org/spec/DMN/20180521/FEEL/"); + definitions.getNsContext().put("dmn", "http://www.omg.org/spec/DMN/20180521/MODEL/"); + definitions.getNsContext().put("feel", "http://www.omg.org/spec/DMN/20180521/FEEL/"); + definitions.getNsContext().put("kie", "http://www.drools.org/kie/dmn/1.2"); + definitions.getNsContext().put("dmndi", "http://www.omg.org/spec/DMN/20180521/DMNDI/"); + definitions.getNsContext().put("di", "http://www.omg.org/spec/DMN/20180521/DI/"); + definitions.getNsContext().put("dc", "http://www.omg.org/spec/DMN/20180521/DC/"); + + // Create input data node + TInputData purchaseAmountInput = new TInputData(); + String purchaseInputId = "_" + UUID.randomUUID().toString().toUpperCase(); + purchaseAmountInput.setId(purchaseInputId); + purchaseAmountInput.setName("Purchase Amount"); + + // Create input variable + TInformationItem purchaseVariable = new TInformationItem(); + purchaseVariable.setId("_" + UUID.randomUUID().toString().toUpperCase()); + purchaseVariable.setName("Purchase Amount"); + purchaseVariable.setTypeRef(new QName("number")); + purchaseAmountInput.setVariable(purchaseVariable); + + // Create decision node + TDecision discountDecision = new TDecision(); + String decisionId = "_" + UUID.randomUUID().toString().toUpperCase(); + discountDecision.setId(decisionId); + discountDecision.setName("Discount Amount"); + + // Create decision variable + TInformationItem decisionVariable = new TInformationItem(); + decisionVariable.setId("_" + UUID.randomUUID().toString().toUpperCase()); + decisionVariable.setName("Discount Amount"); + decisionVariable.setTypeRef(new QName("number")); + discountDecision.setVariable(decisionVariable); + + // Create literal expression + TLiteralExpression expression = new TLiteralExpression(); + String expressionId = "_" + UUID.randomUUID().toString().toUpperCase(); + expression.setId(expressionId); + expression.setText("if Purchase Amount > 100 then Purchase Amount * 0.1 else 0"); + discountDecision.setExpression(expression); + + // Create information requirement + TInformationRequirement requirement = new TInformationRequirement(); + String requirementId = "_" + UUID.randomUUID().toString().toUpperCase(); + requirement.setId(requirementId); + TDMNElementReference reference = new TDMNElementReference(); + reference.setHref("#" + purchaseInputId); + requirement.setRequiredInput(reference); + discountDecision.getInformationRequirement().add(requirement); + + // Add nodes to definitions + definitions.getDrgElement().add(purchaseAmountInput); + definitions.getDrgElement().add(discountDecision); + + // Marshal to XML + String dmnXML = DMNMarshallerFactory.newDefaultMarshaller().marshal(definitions); + + // Add DMNDI using string manipulation + String dmndiXml = createDMNDI(purchaseInputId, decisionId, requirementId); + dmnXML = dmnXML.replace("", dmndiXml + "\n"); + + // Save the file + Path resourcesPath = Paths.get("src", "main", "resources"); + Files.createDirectories(resourcesPath); + File dmnFile = new File(resourcesPath.toFile(), "discount-calculator.dmn"); + + try (FileWriter writer = new FileWriter(dmnFile)) { + writer.write(dmnXML); + System.out.println("DMN file saved to: " + dmnFile.getAbsolutePath()); + } + } catch (Exception e) { + e.printStackTrace(); + } + } + + private static String createDMNDI(String inputId, String decisionId, String requirementId) { + StringBuilder dmndi = new StringBuilder(); + + dmndi.append(" \n"); + dmndi.append(" \n"); + + // Input shape + dmndi.append(" \n"); + dmndi.append(" \n"); + dmndi.append(" \n"); + dmndi.append(" \n"); + dmndi.append(" \n"); + dmndi.append(" \n"); + dmndi.append(" \n"); + dmndi.append(" \n"); + dmndi.append(" \n"); + + // Decision shape + dmndi.append(" \n"); + dmndi.append(" \n"); + dmndi.append(" \n"); + dmndi.append(" \n"); + dmndi.append(" \n"); + dmndi.append(" \n"); + dmndi.append(" \n"); + dmndi.append(" \n"); + dmndi.append(" \n"); + + // Edge + dmndi.append(" \n"); + dmndi.append(" \n"); + dmndi.append(" \n"); + dmndi.append(" \n"); + + dmndi.append(" \n"); + dmndi.append(" "); + + return dmndi.toString(); + } +} \ No newline at end of file diff --git a/src/main/resources/addFive.dmn b/src/main/resources/addFive.dmn new file mode 100644 index 0000000..52d57c1 --- /dev/null +++ b/src/main/resources/addFive.dmn @@ -0,0 +1,18 @@ + + + + + + + + + + + + + + + inputNumber + 5 + + + \ No newline at end of file diff --git a/src/main/resources/discount-calculator.dmn b/src/main/resources/discount-calculator.dmn new file mode 100644 index 0000000..9269914 --- /dev/null +++ b/src/main/resources/discount-calculator.dmn @@ -0,0 +1,41 @@ + + + + + + + + + + + + if Purchase Amount > 100 then Purchase Amount * 0.1 else 0 + + + + + + + + + + + + + + + + + + + + + + + + + + + + +