I am trying to add a End or Terminate event to a existing gateway in a BPMN diagram as another outgoing sequence flow pragmatically in java at the run time. What are the steps need to follow to achieve this? I am following these examples as a reference.

https://www.programcreek.com/java-api-examples/?class=org.camunda.bpm.model.bpmn.instance.camunda.CamundaExecutionListener&method=setCamundaEvent

BpmnModelInstance bpmn = execution.getBpmnModelInstance();
Collection<ExclusiveGateway> gateways = execution.getBpmnModelInstance().getModelElementsByType(ExclusiveGateway.class);

ExclusiveGateway gateway; // getting the instance of gateway from collection
SequenceFlow sequenceFlow = bpmn.newInstance(SequenceFlow.class);
sequenceFlow.setName("Sequence_flow_2");

ConditionExpression conditionExpression = bpmn.newInstance(ConditionExpression.class);
conditionExpression.setTextContent("${x == 2}");
sequenceFlow.setConditionExpression(conditionExpression);

CamundaExecutionListener executionListener = bpmn.newInstance(CamundaExecutionListener.class);
executionListener.setCamundaEvent("start");
executionListener.setCamundaDelegateExpression("Terminate");

EndEvent endEvent = bpmn.newInstance(EndEvent.class);
endEvent.setName("End error");
if (endEvent.getExtensionElements() == null) {
    ExtensionElements extensionElements =    bpmn.newInstance(ExtensionElements.class);
    endEvent.addChildElement(extensionElements);
}
endEvent.getExtensionElements().addChildElement(executionListener);

if (sequenceFlow.getExtensionElements() == null) {
    ExtensionElements extensionElements =     bpmn.newInstance(ExtensionElements.class);
    sequenceFlow.addChildElement(extensionElements);
}
sequenceFlow.getExtensionElements().addChildElement(endEvent);

if((gateway.getExtensionElements() == null) {
    ExtensionElements extensionElements =    bpmn.newInstance(ExtensionElements.class);
    gateway.addChildElement(extensionElements);
}
gateway.getExtensionElements().addChildElement(sequenceFlow);

//sequenceFlow.setSource(gateway);
//sequenceFlow.setTarget(endEvent);

bpmn.setDocumentElement(sequenceFlow);
gateway.getOutgoing().add(sequenceFlow);

I have attached an image for the bpmn diagram below.

bpmn diagram

0

There are 0 answers