How to manipulate UML Diagram Elements [message #1733031] |
Mon, 23 May 2016 16:20  |
Eclipse User |
|
|
|
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 151 times)
|
|
|
|
|
|
|
|
Powered by
FUDForum. Page generated in 0.16694 seconds