Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » Epsilon » How to manipulate UML Diagram Elements
How to manipulate UML Diagram Elements [message #1733031] Mon, 23 May 2016 20:20 Go to next message
Mohammadreza Sharbaf is currently offline Mohammadreza SharbafFriend
Messages: 22
Registered: February 2016
Junior Member

Hi all,

I want to modify an UML Class Diagram by java code, for this purpose first, I load an UML model by "InMemoryEmfModel" class in java code, But I don't know how to change elements of "UMLModel" in run method. (for example change name of "Person" class to "Human").

Can one help me?

My java code is shown below:
package example ; 

import java.io.File;
import java.util.ArrayList;
import java.util.Collection;

import org.eclipse.emf.common.util.URI;
import org.eclipse.emf.ecore.EPackage;
import org.eclipse.emf.ecore.resource.Resource;
import org.eclipse.emf.ecore.resource.ResourceSet;
import org.eclipse.emf.ecore.resource.impl.ResourceSetImpl;
import org.eclipse.epsilon.emc.emf.InMemoryEmfModel;
import org.eclipse.uml2.uml.UMLPackage;
import org.eclipse.uml2.uml.resource.UMLResource;
import org.eclipse.uml2.uml.resources.util.UMLResourcesUtil;

public class MinimalExample {
	
	public static void main(String[] args) throws Exception {
		new MinimalExample().run();
	}
	
	public void run() throws Exception {
		InMemoryEmfModel Model = getUmlModel("UMLModel", new File("src/models/Dev1.uml"));
		
	}
	
	public InMemoryEmfModel getUmlModel(String name, File file) throws Exception {
		ResourceSet set = new ResourceSetImpl();
		UMLResourcesUtil.init(set);
		set.getPackageRegistry().put(UMLPackage.eNS_URI, UMLPackage.eINSTANCE);
		set.getResourceFactoryRegistry().getExtensionToFactoryMap().
		         put(UMLResource.FILE_EXTENSION, UMLResource.Factory.INSTANCE);
		Resource r = set.getResource(URI.createFileURI(file.getAbsolutePath()), true);
		r.load(null);
		
		Collection<EPackage> ePackages = new ArrayList<EPackage>();
		for (Object ePackage : set.getPackageRegistry().values()) {
			ePackages.add((EPackage) ePackage);
		}
		return new InMemoryEmfModel(name, r, ePackages);
	}
}



I attached a minimal example to this post.

Best regards,
Mohammadreza
  • Attachment: Minimal.rar
    (Size: 5.22KB, Downloaded 128 times)
Re: How to manipulate UML Diagram Elements [message #1733083 is a reply to message #1733031] Tue, 24 May 2016 02:34 Go to previous messageGo to next message
Antonio Garcia-Dominguez is currently offline Antonio Garcia-DominguezFriend
Messages: 594
Registered: January 2010
Location: Birmingham, UK
Senior Member

Just curious - why are you using only the EMC part of Epsilon and not EOL itself? Normally I'd go either full Java (use plain EMF APIs) or full Epsilon (use EMC + EOL).

In any case, EMC does provide some convenience functions that might be helpful. The EMC API has a method to get all the instances of the UML "Class" EClass: you can use it and then downcast the EObject to org.eclipse.uml2.uml.Class and use the regular getName/setName methods. If you want to save the changes, make sure you enable storage on disposal and make sure you dispose of the model when you're done.

I've attached the resulting Java program, in case it helps.
Re: How to manipulate UML Diagram Elements [message #1733115 is a reply to message #1733083] Tue, 24 May 2016 13:34 Go to previous messageGo to next message
Mohammadreza Sharbaf is currently offline Mohammadreza SharbafFriend
Messages: 22
Registered: February 2016
Junior Member

Hi Antonio,

Thanks for your helpful reply,

About your question:
Quote:

why are you using only the EMC part of Epsilon and not EOL itself? Normally I'd go either full Java (use plain EMF APIs) or full Epsilon (use EMC + EOL).


I will use other Epsilon languages such as ECL, EML and ETL, but in this minimal example to simplify, I didn't use them.

Kind regards,
Mohammadreza
Re: How to manipulate UML Diagram Elements [message #1733243 is a reply to message #1733083] Wed, 25 May 2016 16:38 Go to previous messageGo to next message
Mohammadreza Sharbaf is currently offline Mohammadreza SharbafFriend
Messages: 22
Registered: February 2016
Junior Member

Hi Antonio,

As you said I can use methods of EMC API to get all the instances of the UML "Class" and the downcast the EObject to org.eclipse.uml2.uml.Class and use the regular getName/setName methods and it worked correctly.

But if before run, the UML element's type was not specified, for example once was "Class" and once again was "Association", do you know any Interface that I can use it and downcast the EObject to it?

Best regards,
Mohammadreza
Re: How to manipulate UML Diagram Elements [message #1733443 is a reply to message #1733243] Fri, 27 May 2016 07:26 Go to previous messageGo to next message
Antonio Garcia-Dominguez is currently offline Antonio Garcia-DominguezFriend
Messages: 594
Registered: January 2010
Location: Birmingham, UK
Senior Member

You'd have to find a common supertype of Class and Association with the proper getName/setName methods. I guess that would be NamedElement.
Re: How to manipulate UML Diagram Elements [message #1733465 is a reply to message #1733443] Fri, 27 May 2016 18:43 Go to previous message
Mohammadreza Sharbaf is currently offline Mohammadreza SharbafFriend
Messages: 22
Registered: February 2016
Junior Member

Thanks a lot
Previous Topic:How to access enumeration literals defined in UML Profiles?
Next Topic:Generation EMF code from splited ecore model
Goto Forum:
  


Current Time: Thu Apr 25 21:15:24 GMT 2024

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

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

Back to the top