Dynamically update process with java model api

Hi, I want to dynamically update the process and gerenate a new bpmn file, the original bpmn is here:


I want to delete some of the user tasks except the first user task. Right now I just try to delete the second user task and the gateway next to it and then write to a new bpmn file, the expected result is the first user task’s out going connect to the third user task, but the new bpmn just as same as the original one (I know my code is not completed), but it should have some changes, where is the problem ?

Thanks.
here is my code:

    //clone model
	BpmnModelInstance newInst = modelInstance.clone();
	
	//user task want to delete from process(second user task in process)
	UserTask task2delete = newInst.getModelElementById("task1");
	
	//get the next gateway
	Collection<SequenceFlow> outs = task2delete.getOutgoing();
	for(SequenceFlow flow:outs){
		FlowNode node = flow.getTarget();
		System.out.println("node name :  " + node.getName());
		
		Collection<SequenceFlow> incoming = node.getIncoming();
		//delete all incoming of gateway
		incoming.removeAll(incoming);
		
		Collection<SequenceFlow> outgoing = node.getOutgoing();
		//delete all outgoing of gateway
		outgoing.removeAll(outgoing);
	}
	//
	UserTask task0 = newInst.getModelElementById("task0");// first user task
	UserTask task2 = newInst.getModelElementById("task2");// third user task
	Collection<SequenceFlow> incoming = task2delete.getIncoming();
	for(SequenceFlow flow:incoming){
		flow.setSource(task0);
		flow.setTarget(task2);
	}
	
	Bpmn.validateModel(newInst);
	
	try {
		OutputStream outputStream = new FileOutputStream("F:/new.bpmn");
		Bpmn.writeModelToStream(outputStream, newInst);
	} catch (FileNotFoundException e) {
		e.printStackTrace();
	}
2 Likes