Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » UML2 » Simply API for saving UML2 models and associated Profile/Stereotype values
Simply API for saving UML2 models and associated Profile/Stereotype values [message #1075938] Tue, 30 July 2013 11:11 Go to next message
Ronan B is currently offline Ronan BFriend
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 Go to previous messageGo to next message
Camille Letavernier is currently offline Camille LetavernierFriend
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 Go to previous messageGo to next message
Christian Damus is currently offline Christian DamusFriend
Messages: 1270
Registered: July 2009
Location: Canada
Senior Member

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
Re: Simply API for saving UML2 models and associated Profile/Stereotype values [message #1076058 is a reply to message #1075999] Tue, 30 July 2013 16:13 Go to previous messageGo to next message
Ronan B is currently offline Ronan BFriend
Messages: 273
Registered: July 2009
Senior Member
Thanks guys!

Your right Camille it could never work without the references from the Model. It is unfortunate the API doesn't mention that there is a certain procedure required for ensuring your model stays intact i.e. with stereotype values. It is super easy to forgot the model resource assignment and be left wondering why the stereotype values have vanished.

Thanks again!
Ronan
Re: Simply API for saving UML2 models and associated Profile/Stereotype values [message #1076086 is a reply to message #1076058] Tue, 30 July 2013 17:30 Go to previous message
Ed Willink is currently offline Ed WillinkFriend
Messages: 7655
Registered: July 2009
Senior Member
Hi

It's not really a UML issue.

There is a limit to what you can do with EMF in general if you try to
live without resources.

URIs and proxies are just some of the areas where things can easily go
confusingly astray.

Regards

Ed Willink

On 30/07/2013 17:13, Ronan B wrote:
> Thanks guys!
>
> Your right Camille it could never work without the references from the
> Model. It is unfortunate the API doesn't mention that there is a
> certain procedure required for ensuring your model stays intact i.e.
> with stereotype values. It is super easy to forgot the model resource
> assignment and be left wondering why the stereotype values have vanished.
>
> Thanks again!
> Ronan
Previous Topic:XMI attribute lost
Next Topic:Importing/Opening xmi file from OMG
Goto Forum:
  


Current Time: Fri Mar 29 08:12:04 GMT 2024

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

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

Back to the top