Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » BPMN 2.0 Modeler » How to modify and save a BPMN model?(modify and save a BPMN model properly)
How to modify and save a BPMN model? [message #1861889] Fri, 10 November 2023 11:48 Go to next message
Eclipse UserFriend
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 Go to previous messageGo to next message
Eclipse UserFriend
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
Re: How to modify and save a BPMN model? [message #1862171 is a reply to message #1861903] Thu, 16 November 2023 09:15 Go to previous message
Eclipse UserFriend
Hi Ralph,

Thanks for the reply. That looks very promising. I'm going to check the project.
Previous Topic:Question
Next Topic:Update error
Goto Forum:
  


Current Time: Sun Feb 16 23:06:57 GMT 2025

Powered by FUDForum. Page generated in 0.03372 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software

Back to the top