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

    1. CmmnModelInstance modelInstance = Cmmn.createEmptyModel();

    The next step is to create a CMMN 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.setTargetNamespace("http://camunda.org/examples");
    3. modelInstance.setDefinitions(definitions);

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

    • Create a new instance of the CMMN element
    • Set attributes and child elements of the element instance
    • Add the newly created element instance to the corresponding parent element
      1. Case caseElement = modelInstance.newInstance(Case.class);
      2. caseElement.setId("a-case");
      3. definitions.addChildElement(caseElement);

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

    1. protected <T extends CmmnModelElementInstance> T createElement(CmmnModelElementInstance 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 CMMN 1.1 specification and convert it toan XML string or save it to a file or stream.

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

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