To create a new CMMN model from scratch, you have to create an empty CMMN model instance with the following method:
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.
Definitions definitions = modelInstance.newInstance(Definitions.class);
definitions.setTargetNamespace("http://camunda.org/examples");
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
Case caseElement = modelInstance.newInstance(Case.class);
caseElement.setId("a-case");
definitions.addChildElement(caseElement);
To simplify this repeating procedure, you can use a helper method like this one.
protected <T extends CmmnModelElementInstance> T createElement(CmmnModelElementInstance parentElement, String id, Class<T> elementClass) {
T element = modelInstance.newInstance(elementClass);
element.setAttributeValue("id", id, true);
parentElement.addChildElement(element);
return element;
}
Validate the model against the CMMN 1.1 specification and convert it toan XML string or save it to a file or stream.
// validate the model
Cmmn.validateModel(modelInstance);
// convert to string
String xmlString = Cmmn.convertToString(modelInstance);
// write to output stream
OutputStream outputStream = new OutputStream(...);
Cmmn.writeModelToStream(outputStream, modelInstance);
// write to file
File file = new File(...);
Cmmn.writeModelToFile(file, modelInstance);
原文: https://docs.camunda.org/manual/7.9/user-guide/model-api/cmmn-model-api/create-a-model/