Generate Model API
Target audience: Developers
Overview
In this how-to guide you will learn how to use the model generation APIs available in the Flowable Java backend.
We use that API heavily when creating models for unit-testing purposes as an alternative to just use the JSON or XML (e.g. BPMN, CMMN or DMN) source format when generating and deploying models we need for unit testing.
Another use-case is to use that model API to generate models based on other formats like you might have from legacy systems where you want an automatic one-to-one migration from the old format to the Flowable models.
Example 1: Generate simple BPMN process model
In this simple example, we use the BPMN model API to create a very basic and simple BPMN process model to showcase how the API can be used to create deployable BPMN XML models.
public void testProcessModelGeneration() {
// create the BPMN model container and one process within (typically you will have one process
// per BPMN model, although the standard would allow multiple processes per BPMN xml file)
BpmnModel bpmnModel = new BpmnModel();
Process processModel = new Process();
processModel.setName("My Process");
processModel.setId("myProcess");
bpmnModel.addProcess(processModel);
// create and add the start event of the process
StartEvent startEvent = new StartEvent();
startEvent.setId("startEvent");
processModel.addFlowElement(startEvent);
// and the end event as well
EndEvent endEvent = new EndEvent();
endEvent.setId("endEvent");
processModel.addFlowElement(endEvent);
// create a first user task and add it to the process as an element
UserTask userTaskA = new UserTask();
userTaskA.setId("userTaskA");
userTaskA.setName("User Task A");
processModel.addFlowElement(userTaskA);
// do it again for a second user task
UserTask userTaskB = new UserTask();
userTaskB.setId("userTaskB");
userTaskB.setName("User Task B");
processModel.addFlowElement(userTaskB);
// now create a sequence flow connecting the start event and the first user task
SequenceFlow flow1 = new SequenceFlow(startEvent.getId(), userTaskA.getId());
flow1.setId("flow1");
processModel.addFlowElement(flow1);
// do the same to connect both user tasks
SequenceFlow flow2 = new SequenceFlow(userTaskA.getId(), userTaskB.getId());
flow2.setId("flow2");
processModel.addFlowElement(flow2);
// and finally, add a new sequence flow connecting the second user task with the end event
SequenceFlow flow3 = new SequenceFlow(userTaskB.getId(), endEvent.getId());
flow3.setId("flow3");
processModel.addFlowElement(flow3);
// now we can export the process model to BPMN XML
byte[] xml = new BpmnXMLConverter().convertToXML(bpmnModel);
String processModelXml = new String(xml, StandardCharsets.UTF_8);
}
And this is the BPMN XML which will be generated with the example above:
<?xml version='1.0' encoding='UTF-8'?>
<definitions xmlns="http://www.omg.org/spec/BPMN/20100524/MODEL" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:flowable="http://flowable.org/bpmn" xmlns:bpmndi="http://www.omg.org/spec/BPMN/20100524/DI" xmlns:omgdc="http://www.omg.org/spec/DD/20100524/DC" xmlns:omgdi="http://www.omg.org/spec/DD/20100524/DI" typeLanguage="http://www.w3.org/2001/XMLSchema" expressionLanguage="http://www.w3.org/1999/XPath" targetNamespace="http://www.flowable.org/test">
<process id="myProcess" name="My Process" isExecutable="true">
<startEvent id="startEvent"/>
<endEvent id="endEvent"/>
<userTask id="userTaskA" name="User Task A"/>
<userTask id="userTaskB" name="User Task B"/>
<sequenceFlow id="flow1" sourceRef="startEvent" targetRef="userTaskA"/>
<sequenceFlow id="flow2" sourceRef="userTaskA" targetRef="userTaskB"/>
<sequenceFlow id="flow3" sourceRef="userTaskB" targetRef="endEvent"/>
</process>
<bpmndi:BPMNDiagram id="BPMNDiagram_myProcess">
<bpmndi:BPMNPlane bpmnElement="myProcess" id="BPMNPlane_myProcess"/>
</bpmndi:BPMNDiagram>
</definitions>
You can directly deploy such a generated process model to the runtime of Flowable using this code snipped:
processEngine
.getRepositoryService()
.createDeployment()
.name("myProcess").addString("MyProcess.bpmn20.xml", new String(xml))
.deploy();
Of course, you can also directly import this generated BPMN model into Flowable Design, if you want to be able to modify the model before deploying it.
If you open the imported BPMN process model, it might look something like this:
Now you can either directly deploy and run the model, if you also defined all necessary technical details or you can use it as a starting point and further develop and define the model according your needs and requirements.
Example 2: More sophisticated BPMN process model
In this example, we are going to create another BPMN process model which has a bit more complexity, still very simple, but to just add some more elements like gateways and service tasks and even use conditional sequence flows to explain how every detail can be defined using the Java model API.