package com.camunda.consulting.bpmndi_example; import java.io.File; import java.io.IOException; import org.camunda.bpm.model.bpmn.Bpmn; import org.camunda.bpm.model.bpmn.BpmnModelInstance; import org.camunda.bpm.model.bpmn.instance.BaseElement; import org.camunda.bpm.model.bpmn.instance.BpmnModelElementInstance; import org.camunda.bpm.model.bpmn.instance.Definitions; import org.camunda.bpm.model.bpmn.instance.EndEvent; import org.camunda.bpm.model.bpmn.instance.FlowNode; import org.camunda.bpm.model.bpmn.instance.Process; import org.camunda.bpm.model.bpmn.instance.SequenceFlow; import org.camunda.bpm.model.bpmn.instance.StartEvent; import org.camunda.bpm.model.bpmn.instance.UserTask; import org.camunda.bpm.model.bpmn.instance.bpmndi.BpmnDiagram; import org.camunda.bpm.model.bpmn.instance.bpmndi.BpmnEdge; import org.camunda.bpm.model.bpmn.instance.bpmndi.BpmnLabel; import org.camunda.bpm.model.bpmn.instance.bpmndi.BpmnPlane; import org.camunda.bpm.model.bpmn.instance.bpmndi.BpmnShape; import org.camunda.bpm.model.bpmn.instance.dc.Bounds; import org.camunda.bpm.model.bpmn.instance.di.Waypoint; public class ProcessGenerator { public BpmnModelInstance modelInstance; public static void main(String[] args) throws IOException { ProcessGenerator createProcess = new ProcessGenerator(); createProcess.generateProcess(); } public void generateProcess() throws IOException { modelInstance = Bpmn.createEmptyModel(); Definitions definitions = modelInstance.newInstance(Definitions.class); definitions.setTargetNamespace("http://camunda.org/examples"); modelInstance.setDefinitions(definitions); // create the process org.camunda.bpm.model.bpmn.instance.Process process = modelInstance.newInstance(Process.class); process.setAttributeValue("id", "process-one-task", true); definitions.addChildElement(process); BpmnDiagram diagram = modelInstance.newInstance(BpmnDiagram.class); BpmnPlane plane = modelInstance.newInstance(BpmnPlane.class); plane.setBpmnElement(process); diagram.setBpmnPlane(plane); definitions.addChildElement(diagram); // create start event, user task and end event StartEvent startEvent = createElement(process, "start", "Di generation wanted", StartEvent.class, plane, 15, 15, 50, 50, true); UserTask userTask = createElement(process, "userTask", "Generate Model with DI", UserTask.class, plane, 100, 0, 80, 100, false); createSequenceFlow(process, startEvent, userTask, plane, 65, 40, 100, 40); EndEvent endEvent = createElement(process, "end", "DI generation completed", EndEvent.class, plane, 250, 15, 50, 50, true); createSequenceFlow(process, userTask, endEvent, plane, 200, 40, 250, 40); // validate and write model to file Bpmn.validateModel(modelInstance); File file = File.createTempFile("bpmn-model-api-", ".bpmn"); Bpmn.writeModelToFile(file, modelInstance); } protected T createElement(BpmnModelElementInstance parentElement, String id, String name, Class elementClass, BpmnPlane plane, double x, double y, double heigth, double width, boolean withLabel) { T element = modelInstance.newInstance(elementClass); element.setAttributeValue("id", id, true); element.setAttributeValue("name", name, false); parentElement.addChildElement(element); BpmnShape bpmnShape = modelInstance.newInstance(BpmnShape.class); bpmnShape.setBpmnElement((BaseElement) element); Bounds bounds = modelInstance.newInstance(Bounds.class); bounds.setX(x); bounds.setY(y); bounds.setHeight(heigth); bounds.setWidth(width); bpmnShape.setBounds(bounds); if (withLabel) { BpmnLabel bpmnLabel = modelInstance.newInstance(BpmnLabel.class); Bounds labelBounds = modelInstance.newInstance(Bounds.class); labelBounds.setX(x); labelBounds.setY(y + heigth); labelBounds.setHeight(heigth); labelBounds.setWidth(width); bpmnLabel.addChildElement(labelBounds); bpmnShape.addChildElement(bpmnLabel); } plane.addChildElement(bpmnShape); return element; } public SequenceFlow createSequenceFlow(org.camunda.bpm.model.bpmn.instance.Process process, FlowNode from, FlowNode to, BpmnPlane plane, int... waypoints) { String identifier = from.getId() + "-" + to.getId(); SequenceFlow sequenceFlow = modelInstance.newInstance(SequenceFlow.class); sequenceFlow.setAttributeValue("id", identifier, true); process.addChildElement(sequenceFlow); sequenceFlow.setSource(from); from.getOutgoing().add(sequenceFlow); sequenceFlow.setTarget(to); to.getIncoming().add(sequenceFlow); BpmnEdge bpmnEdge = modelInstance.newInstance(BpmnEdge.class); bpmnEdge.setBpmnElement(sequenceFlow); for (int i = 0; i < waypoints.length / 2; i++) { double waypointX = waypoints[i*2]; double waypointY = waypoints[i*2+1]; Waypoint wp = modelInstance.newInstance(Waypoint.class); wp.setX(waypointX); wp.setY(waypointY); bpmnEdge.addChildElement(wp); } plane.addChildElement(bpmnEdge); return sequenceFlow; } }