Hi, I’m trying to use Camunda API to manipulate my BPMN models, but I don’t find enough documentation and code samples to do what I want. The guid below is useful but I couldn’t use it to do the task.
I’m actually trying to implement a mutation generator for BPMN models, so that given a BPMN model, the tool should generate new models with changes (e.g. addition/removal of elements) to the original model.
Now I want to remove a random node within the given model but I don’t succeed. After the node is removed, the nodes before and after the deleted node need to be reconnected.
Here’s what I’ve got so far:
public static void main(String[] args) {
try {
removeMiddleNode();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
private static void removeMiddleNode() {
File f = new File("data/mutation/input/103082.bpmn");
BpmnModelInstance modelInstance = Bpmn.readModelFromFile(f);
//this is not the right way. I use this to access the parent node to add the sequenceFlow to it but I think it needs to be added to the parent process instead. Not sure how to access it.
Task task = modelInstance.getModelElementById("sourceNodeID");
ModelElementInstance parentElement = task.getParentElement();
//these are the nodes before and after the node to be deleted, but they need to be set based on the chosen node to be deleted
FlowNode sourceNode = modelInstance.getModelElementById("sourceNodeID");
FlowNode targetNode = modelInstance.getModelElementById("targetNodeID");
//the code for deleting the node and its incoming and outgoing sequence flows should go here.
//reconnect the nodes that are before and after the deletecd node
createSequenceFlow(modelInstance, parentElement, sourceNode, targetNode);
// write to file
File file = new File("data/mutation/output/mutant.bpmn");
Bpmn.writeModelToFile(file, modelInstance);
System.out.print("done");
}
public static SequenceFlow createSequenceFlow(BpmnModelInstance modelInstance, ModelElementInstance parentElement, FlowNode from, FlowNode to) {
String identifier = from.getId() + "-" + to.getId();
SequenceFlow sequenceFlow = createElement(modelInstance, parentElement, identifier, SequenceFlow.class);
parentElement.addChildElement(sequenceFlow);
sequenceFlow.setSource(from);
from.getOutgoing().add(sequenceFlow);
sequenceFlow.setTarget(to);
to.getIncoming().add(sequenceFlow);
return sequenceFlow;
}
protected static <T extends BpmnModelElementInstance> T createElement(BpmnModelInstance modelInstance, ModelElementInstance parentElement, String id, Class<T> elementClass) {
T element = modelInstance.newInstance(elementClass);
element.setAttributeValue("id", id, true);
parentElement.addChildElement(element);
return element;
}