Load BPMN2 model using the API [message #1070295] |
Wed, 17 July 2013 16:53 |
Poissy B. Messages: 31 Registered: June 2012 |
Member |
|
|
Hello everyone,
Sorry this is a stupid question but I can't seem to be able to load a .bpmn2 model created with the modeler using the bpmn2 ecore api. Could someone share a code snippet on how to do this.
Sorry I am not giving more details about the errors because I am convinced it is stupidity on my end, but now I've spent too much time trying to load the damn model. Please help!
Thanks.
[Updated on: Mon, 29 July 2013 14:56] Report message to a moderator
|
|
|
|
Re: Load BPMN2 model using the API [message #1070332 is a reply to message #1070306] |
Wed, 17 July 2013 18:58 |
Poissy B. Messages: 31 Registered: June 2012 |
Member |
|
|
Hi Bob,
I've managed to load it. Could you confirm this is a correct way please:
Resource.Factory.Registry reg = Resource.Factory.Registry.INSTANCE;
Map<String, Object> m = reg.getExtensionToFactoryMap();
m.put("bpmn2", new Bpmn2ResourceFactoryImpl());
ResourceSet resSet = new ResourceSetImpl();
Resource resource = resSet.getResource(URI.createURI("My.bpmn2"), true);
DocumentRoot dr = (DocumentRoot)resource.getContents().get(0);
Process p = dr.getProcess();
Also the Process object is null even though I have a process element in the bpmn file. Am I doing something wrong?
Thanks!
[Updated on: Wed, 17 July 2013 19:03] Report message to a moderator
|
|
|
Re: Load BPMN2 model using the API [message #1070399 is a reply to message #1070332] |
Wed, 17 July 2013 22:26 |
|
Close, but not quite right
Here's how you should load a bpmn process file from a workspace project:
// Create a URI from a file string, workspace project string, URL, whatever...
URI uri = URI.createFileURI("/MyEclipseProject/process_1.bpmn");
Resource.Factory.Registry reg = Resource.Factory.Registry.INSTANCE;
// get the Resource Factory for this URI (it should be the Bpmn2ResourceFactory!)
Factory f = reg.getFactory(uri);
// create the resource object (note that this does not actually LOAD the file)
Resource res = f.createResource(uri);
try {
// load it
res.load(null);
}
catch (IOException e) {
// handle "file not found" exceptions, HTTP timeouts, etc. here
e.printStackTrace();
}
To access the model elements, you need to start with the Definitions element obtained from the DocumentRoot. Then, to find the first Process object (or any other BPMN2 RootElement object) use this:
DocumentRoot root = (DocumentRoot) res.getContents().get(0);
Definitions definitions = root.getDefinitions();
for (RootElement re : definitions.getRootElements()) {
if (re instanceof Process) {
Process p = (Process)re;
break;
}
}
Yes, I know it's convoluted, but that's how it works
HTH,
Bob
|
|
|
|
|
|
|
Powered by
FUDForum. Page generated in 0.03262 seconds