Remove Activity from model via camunda model api

Dear Community,

currently, I am trying to remove an activity from a model using the model api.

I am trying to remove the activity and connect the incoming sequence flows with the subsequent flow node and remove the outgoing sequence flows as they are no longer necessary.

    Collection<Definitions> definitions = modelInstance.getModelElementsByType(Definitions.class);
	definitions.forEach(definition -> {

		FlowNode activityA = (FlowNode) modelInstance.getModelElementById("Activity_A");

		Collection<SequenceFlow> outgoing = activityA.getOutgoing();
		Collection<SequenceFlow> incoming = activityA.getIncoming();

		for (SequenceFlow incomingFlow : incoming) {
			for (SequenceFlow outgoingFlow : outgoing) {
				FlowNode target = outgoingFlow.getTarget();
				incomingFlow.setTarget(target);
				target.getIncoming().add(incomingFlow);
				target.getIncoming().remove(outgoingFlow);

				activityA.getOutgoing().remove(outgoingFlow);

				System.out.println("remove sequence flow " + outgoingFlow.getId());
				System.out.println(definition.removeChildElement(outgoingFlow));
			}
		}

		definition.removeChildElement(activityA);
	});

However, the outgoing sequence flow cannot be removed. And as a result I get an error:
Caused by: org.xml.sax.SAXParseException; cvc-complex-type.4: Attribute ‘sourceRef’ must appear on element ‘bpmn:sequenceFlow’.

I think I get the error because the sequence flow is still there and wants to reference the activity that shall be removed.

So, the question is, why can’t I remove the sequence flow and is my assumption right that I geht the error because of that?

Any hints are appreciated.

For all those who are facing the same challenge, I found the solution:

You have to get the process element and remove the Edge from the process element.

Collection<Process> processes = definition.getChildElementsByType(Process.class);

for (Process process : processes) {
	System.out.println(process.removeChildElement(outgoingFlow));
	System.out.println(process.removeChildElement(activityA));
}

However, I don’t understand why I can remove the activity by removing it from the definition but I need to remove the sequence flow from the process element.

I think in that regard the API is not very intuitive.

2 Likes

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.