Simply API for saving UML2 models and associated Profile/Stereotype values [message #1075938] |
Tue, 30 July 2013 11:11 |
Ronan B Messages: 273 Registered: July 2009 |
Senior Member |
|
|
Hi,
I have written a save method for my UML2 to save the model and the associated profiles values. However, I was surprised that there wasn't a more obvious way of doing it than iterating over the whole model and calling contents.addAll(((Element) eObject).getStereotypeApplications());
Did I missing soemthing?
My method is as follows:
public void saveModel(File file) throws IOException {
if (file == null)
return;
URI modelURI = URI.createFileURI(file.getAbsolutePath());
Resource modelResource = new ResourceSetImpl().createResource(modelURI);
EList<EObject> contents = modelResource.getContents();
contents.add(myModel);
//by default UML2 does not save the stereotypes so we must do that ourselves
for (Iterator<EObject> allContents = UMLUtil.getAllContents(myModel, true, false); allContents.hasNext();)
{
EObject eObject = (EObject) allContents.next();
if (eObject instanceof Element)
{
contents.addAll(((Element) eObject).getStereotypeApplications());
}
}
try
{
modelResource.save(null);
}
catch (IOException ioe)
{
throw new IOException("Could not save UML 2 Model to file: "+modelURI);
}
}
It would have been niee if the modelResource.save(null); call did the saving for me.
Thanks!
Ronan
[Updated on: Tue, 30 July 2013 11:36] Report message to a moderator
|
|
|
Re: Simply API for saving UML2 models and associated Profile/Stereotype values [message #1075944 is a reply to message #1075938] |
Tue, 30 July 2013 11:32 |
Camille Letavernier Messages: 952 Registered: February 2011 |
Senior Member |
|
|
Hi Ronan,
If you add the model to the resource from the beginning, the StereotypeApplications will be added to the resource when you apply the stereotypes on the model elements. Then you just need to save the Resource when you're done (Or give it a new path/URI, then save).
Always having a ResourceSet and a Resource is the most common use case, so I'm not sure there is really a need to retrieve all stereotype applications afterwards. Actually, I don't even know how the UML Model could retrieve its stereotype applications without a resource, because there is no reference from the Model to the Stereotype Applications.
So, basically, it should probably look like the following:
public void init(){
Resource resource = new ResourceSetImpl().createResource(anyURI);
resource.getContents().add(myModel);
manipulateModel(myModel); //Add contents, apply stereotypes...
saveModel(anyFile);
}
public void saveModel(File file) throws IOException{
Resource resource = myModel.eResource();
URI modelURI = URI.createFileURI(file.getAbsolutePath());
resource.setURI(modelURI); //Set the target URI afterwards
try {
resource.save(null);
} catch (IOException e) {
throw new IOException("Could not save UML 2 Model to file: "+modelURI);
}
}
Note that, if you know the target File from the beginning, you don't need to change it afterwards, and you can simply call resource.save(null).
Regards,
Camille
Camille Letavernier
|
|
|
Re: Simply API for saving UML2 models and associated Profile/Stereotype values [message #1075999 is a reply to message #1075944] |
Tue, 30 July 2013 13:42 |
|
Hi, Camille,
See comments in-line, below.
cW
On 2013-07-30 11:32:27 +0000, Camille Letavernier said:
> Hi Ronan,
>
>
> If you add the model to the resource from the beginning, the
> StereotypeApplications will be added to the resource when you apply the
> stereotypes on the model elements. Then you just need to save the
> Resource when you're done (Or give it a new path/URI, then save).
>
> Always having a ResourceSet and a Resource is the most common use case,
> so I'm not sure there is really a need to retrieve all stereotype
> applications afterwards. Actually, I don't even know how the UML Model
> could retrieve its stereotype applications without a resource, because
> there is no reference from the Model to the Stereotype Applications.
That's one of the reasons why the CacheAdapter is a singleton instance:
in order to be able to track inverse cross-references between objects
that aren't in resources, especially for finding stereotype
applications. It's also the reason why the UML API explicitly adds the
CacheAdapter to stereotype applications, because they may not end up
getting it from a resource.
Somebody somewhere in the history of UML users needed to be able to
work with free-floating (non-resource-contained) UML models ...
>
> So, basically, it should probably look like the following:
>
> public void init(){
> Resource resource = new ResourceSetImpl().createResource(anyURI);
> resource.getContents().add(myModel);
> manipulateModel(myModel); //Add contents, apply stereotypes...
> saveModel(anyFile);
> }
>
> public void saveModel(File file) throws IOException{
> Resource resource = myModel.eResource();
> URI modelURI = URI.createFileURI(file.getAbsolutePath());
> resource.setURI(modelURI); //Set the target URI afterwards
> try {
> resource.save(null);
> } catch (IOException e) {
> throw new IOException("Could not save UML 2 Model to file: "+modelURI);
> }
> }
>
>
> Note that, if you know the target File from the beginning, you don't
> need to change it afterwards, and you can simply call
> resource.save(null).
>
>
> Regards,
> Camille
|
|
|
|
|
Powered by
FUDForum. Page generated in 0.05959 seconds