How to modify and save a BPMN model? [message #1861889] |
Fri, 10 November 2023 06:48  |
Eclipse User |
|
|
|
I want to make changes to a BPMN model and save the modified model as a new one. I have the below code as a demo which tries to remove the last element of the model, but it seems it doesn't do it properly. When opening the modified model, the diagram info seems to have gone. I'm also getting error about unresolved reference for a sequence flow. I'd appreciate your help in this?
private static void removeLastNode() {
String modelPath = "process_2.bpmn";
URI uri = URI.createURI(modelPath);
Bpmn2ResourceFactoryImpl resFactory = new Bpmn2ResourceFactoryImpl();
Resource resource = resFactory.createResource(uri);
HashMap<Object, Object> options = new HashMap<Object, Object>();
options.put(XMLResource.OPTION_DEFER_IDREF_RESOLUTION, true);
try {
// Load the resource
resource.load(options);
}
catch(Exception e){
}
// This is the root element of the XML document
Definitions d = (Definitions) resource.getContents().get(0).eContents().get(0);
//if more than 1 node in the process, then extract and remove the last node
for(RootElement re: d.getRootElements()) {
if(re instanceof Process) {
Process p = (Process)re;
FlowElement node_toRemove = null;
List<SequenceFlow> seqflowList_toRemove = new ArrayList();
for(FlowElement fe: p.getFlowElements()) {
if(fe instanceof FlowNode) {
FlowNode fn = (FlowNode)fe;
if(fn.getOutgoing().size()==0) {//no outgoing node
node_toRemove = fe;
//get the list of the incoming sequence flows of the element
for(SequenceFlow sf: fn.getIncoming()) {
seqflowList_toRemove.add(sf);
}
break;
}
}
}
//remove node and its seq flows
for(SequenceFlow sf: seqflowList_toRemove) {
p.getFlowElements().remove(sf);
}
p.getFlowElements().remove(node_toRemove);
}
}
String newFileName = "newModel" + ".bpmn";
try {
File f = new File(newFileName);
URI new_uri = URI.createURI(f.getPath());
resource.setURI(new_uri);
resource.save(options);
System.out.println("model " + newFileName + " saved!");
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
|
|
|
|
|
Powered by
FUDForum. Page generated in 0.03723 seconds