To create a new DMN model from scratch, you have to create an empty DMN model instance with the following method:

    1. DmnModelInstance modelInstance = Dmn.createEmptyModel();

    The next step is to create a DMN definitions element. Set the target namespace on it and add itto the newly created empty model instance.

    1. Definitions definitions = modelInstance.newInstance(Definitions.class);
    2. definitions.setNamespace("http://camunda.org/schema/1.0/dmn");
    3. definitions.setName("definitions");
    4. definitions.setId("definitions");
    5. modelInstance.setDefinitions(definitions);

    Usually you want to add a decision to your model. This followsthe same 3 steps as the creation of the DMN definitions element:

    • Create a new instance of the DMN element
    • Set attributes and child elements of the element instance
    • Add the newly created element instance to the corresponding parent element
      1. Decision decision = modelInstance.newInstance(Decision.class);
      2. decision.setId("testGenerated");
      3. decision.setName("generationtest");
      4. definitions.addChildElement(decision);

    To simplify this repeating procedure, you can use a helper method like this one:

    1. protected <T extends DmnModelElementInstance> T createElement(DmnModelElementInstance parentElement, String id, Class<T> elementClass) {
    2. T element = modelInstance.newInstance(elementClass);
    3. element.setAttributeValue("id", id, true);
    4. parentElement.addChildElement(element);
    5. return element;
    6. }

    Validate the model against the DMN 1.1 specification and convert it toan XML string or save it to a file or stream.

    1. // validate the model
    2. Dmn.validateModel(modelInstance);
    3. // convert to string
    4. String xmlString = Dmn.convertToString(modelInstance);
    5. // write to output stream
    6. OutputStream outputStream = new OutputStream(...);
    7. Dmn.writeModelToStream(outputStream, modelInstance);
    8. // write to file
    9. File file = new File(...);
    10. Dmn.writeModelToFile(file, modelInstance);

    原文: https://docs.camunda.org/manual/7.9/user-guide/model-api/dmn-model-api/create-a-model/