Load BPMN2 model using the API [message #1070295] |
Wed, 17 July 2013 12:53  |
Eclipse User |
|
|
|
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 10:56] by Moderator
|
|
|
|
|
Re: Load BPMN2 model using the API [message #1070399 is a reply to message #1070332] |
Wed, 17 July 2013 18:26   |
Eclipse User |
|
|
|
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.25075 seconds