Copying an entire UML-RT model into the runtime instance [message #1775593] |
Wed, 01 November 2017 21:33 |
Sudharshan Gopikrishnan Messages: 15 Registered: April 2017 |
Junior Member |
|
|
Hello all,
I am developing a Plug-In Project, and within the project I have an UML-RT model. I would like this model to be imported/replicated in the run-time instance of my application progamaticaly.
I have the code which copies the UML file to the run-time instance. However, I do not know how to copy the notation file (containing the capsule diagrams, state machine diagrams) into the run-time instance. The code to copy the UML file is below. Any help to do the same in case of a notation file will be really helpful. Any help will be greatly appreciated.
Kind Regards,
Sudharshan
The code :
package ca.queensu.cs.rover.configuration.internal.commands;
import java.util.Collection;
import org.eclipse.core.commands.AbstractHandler;
import org.eclipse.core.commands.ExecutionEvent;
import org.eclipse.core.commands.ExecutionException;
import org.eclipse.core.commands.IHandler;
import org.eclipse.core.commands.IHandlerListener;
import org.eclipse.core.runtime.Platform;
import org.eclipse.emf.common.util.EList;
import org.eclipse.emf.common.util.URI;
import org.eclipse.emf.ecore.EAnnotation;
import org.eclipse.emf.ecore.EObject;
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.emf.ecore.util.EcoreUtil;
import org.eclipse.emf.transaction.RecordingCommand;
import org.eclipse.emf.transaction.TransactionalEditingDomain;
import org.eclipse.emf.transaction.util.TransactionUtil;
import org.eclipse.papyrus.editor.PapyrusMultiDiagramEditor;
import org.eclipse.papyrus.infra.core.resource.ModelSet;
import org.eclipse.papyrus.infra.core.sashwindows.di.util.DiUtils;
import org.eclipse.papyrus.infra.core.services.ServiceException;
import org.eclipse.papyrus.infra.core.services.ServicesRegistry;
import org.eclipse.papyrus.infra.emf.utils.EMFHelper;
import org.eclipse.papyrus.uml.tools.model.UmlUtils;
import org.eclipse.papyrusrt.umlrt.core.utils.CapsuleUtils;
import org.eclipse.ui.IEditorPart;
import org.eclipse.ui.handlers.HandlerUtil;
import org.eclipse.uml2.uml.Class;
import org.eclipse.uml2.uml.Classifier;
import org.eclipse.uml2.uml.Element;
import org.eclipse.uml2.uml.Model;
import org.eclipse.uml2.uml.Package;
import org.eclipse.uml2.uml.PackageableElement;
import org.eclipse.uml2.uml.Profile;
import ca.queensu.cs.rover.configuration.Activator;
public class LoadRoverPackage extends AbstractHandler {
@Override
public Object execute(ExecutionEvent event) throws ExecutionException {
IEditorPart editor = HandlerUtil.getActiveEditor(event);
ModelSet modelSet = null;
final ResourceSet resourceSet = new ResourceSetImpl();
if (!(editor instanceof PapyrusMultiDiagramEditor)) {
return null;
}
ServicesRegistry services = ((PapyrusMultiDiagramEditor)editor).getServicesRegistry();
try {
modelSet = services.getService(ModelSet.class);
} catch (ServiceException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
TransactionalEditingDomain editingDomain = TransactionUtil.getEditingDomain(modelSet);
try {
Resource UmlResource = null;
String path = "/Package/DetectionSensor/DetectionSensor.uml";
// String pathNotation = "/Package/DetectionSensor/DetectionSensor.notation";
UmlResource = loadTemplateResource(path, resourceSet);
// Resource notationResource = loadTemplateResource(pathNotation, resourceSet);
EcoreUtil.resolveAll(UmlResource);
// EcoreUtil.resolveAll(notationResource);
// copy all elements
EcoreUtil.Copier copier = new EcoreUtil.Copier();
Collection<EObject> umlObjects = copier.copyAll(UmlResource.getContents());
copier.copyReferences();
// Collection<EObject> notationObjects = copier.copyAll(notationResource.getContents());
// copier.copyReferences();
// set copied elements in goods resources
final EList<EObject> contents = UmlUtils.getUmlResource(modelSet).getContents();
final EList<EObject> notationContents = UmlUtils.getUmlResource(modelSet).getContents();
final Model model = (Model)UmlUtils.getUmlResource(modelSet).getContents().get(0);
final Package uml = model.getNestedPackage("UMLRT-Rover");
editingDomain.getCommandStack().execute(new RecordingCommand(editingDomain) {
@Override
protected void doExecute() {
for (EObject umlObject: umlObjects) {
if (umlObject instanceof Package)
uml.getPackagedElements().add((PackageableElement) umlObject);
else
contents.add(umlObject);
}
}
});
}
finally {
EMFHelper.unload(resourceSet);
}
return null;
}
private PackageableElement findTopCapsule(String topCapsuleName, Package root) {
PackageableElement pe = root.getPackagedElement(topCapsuleName);
if (pe != null && pe instanceof Classifier && CapsuleUtils.isCapsule((Classifier)pe)) {
return pe;
}
EList<Package> nestedPackages = root.getNestedPackages();
for (int i = 0; i < nestedPackages.size(); i++) {
Package nestedPackage = nestedPackages.get(i);
pe = findTopCapsule(topCapsuleName, nestedPackage);
if (pe != null)
return pe;
}
return null;
}
/**
* Load template resource.
*
* @param path
* the path
* @return the resource
*/
private Resource loadTemplateResource(String path, ResourceSet resourceSet) {
java.net.URL templateURL = Platform.getBundle(Activator.PLUGIN_ID).getResource(path);
if(templateURL != null) {
String fullUri = templateURL.getPath();
URI uri = URI.createPlatformPluginURI(Activator.PLUGIN_ID + fullUri, true);
Resource resource = resourceSet.getResource(uri, true);
if(resource.isLoaded()) {
return resource;
}
}
return null;
}
/**
* Obtains the name of the Top capsule.
*
* @param root
* - The model's root {@link Element}.
* @return The name of the Top capsule.
*/
public static String getTopCapsuleName(Element root) {
String retVal = null;
EAnnotation anno = root.getEAnnotation("UMLRT_Default_top");
if (anno != null) {
retVal = anno.getDetails().get("top_name");
}
return retVal != null ? retVal : "Top";
}
}
|
|
|
Re: Copying an entire UML-RT model into the runtime instance [message #1775807 is a reply to message #1775593] |
Mon, 06 November 2017 17:27 |
Ernesto Posse Messages: 438 Registered: March 2011 |
Senior Member |
|
|
Hi. First a couple of suggestions about posting in the forum: if you are posting a short bit of code, you should use the [ code ] [ / code ] tags (the last icon in the "Formatting Tools" bar just above the Body editor. Otherwise you loose all the formatting/indentation in your code and it is very hard to read. If you are posting a larger piece of code, you should attach it as a file. See the "File Attachements" section below the Body editor.
Anyway, regarding your question, maybe I'm missing something, but why not just copy the file manually to the workspace where you need it? And if you need to do it programmatically, why go through the UML/EMF/Papyrus API instead of using the basic Java library for file-system operations?
[Updated on: Mon, 06 November 2017 17:28] Report message to a moderator
|
|
|
Powered by
FUDForum. Page generated in 0.03656 seconds