How to modify and save a BPMN model? [message #1861889] |
Fri, 10 November 2023 11: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();
}
}
|
|
|
Re: How to modify and save a BPMN model? [message #1861903 is a reply to message #1861889] |
Sat, 11 November 2023 14:16   |
Eclipse User |
|
|
|
Hi,
the meta model ob Eclipse BPMN is not perfect and in general deleting or adding a node to a BPMN model can affect several parts of the BPMN file which makes a simple action complicated.
For this reason I started a new project Open-BPMN. This project includes a subproject Open-BPMN Metamodel : https://github.com/imixs/open-bpmn/tree/master/open-bpmn.metamodel
I think you can solve your problem more easily with this meta model project
For example, to delete a task element - with in and outgoing sequence flows - would look like this:
@Test
public void testDeleteTask() {
BPMNModel model = null;
String out = "src/test/resources/output/delete-element_1.bpmn";
try {
model = BPMNModelFactory.read("/refmodel-1.bpmn");
BPMNProcess process = model.openProcess(null); // opens default process
process.deleteTask("Task_1");
} catch (BPMNModelException e) {
e.printStackTrace();
fail();
}
model.save(out);
logger.info("...model update sucessful: " + out);
}
To can rise a question regarding Open-BPMN here: https://github.com/imixs/open-bpmn/discussions
|
|
|
|
Powered by
FUDForum. Page generated in 0.03372 seconds