Papyrus Activity Diagram Creation via API [message #1821189] |
Thu, 06 February 2020 14:04  |
Eclipse User |
|
|
|
Hi @ all,
I am quite used to the IBM Rational Rhapsody Java API, but completely new to the Eclipse Papyrus API.
I am trying to implement a short method which loads the API, creates a new Activity Diagram via the API and finally adds one Activity to the diagram.
Can anyone provide me a compact example how to do that?
Sadly, I couldn't find a straight-forward example by searching the forum or the web.
Thanks in advance,
DyDe
|
|
|
|
|
|
Re: Papyrus Activity Diagram Creation via API [message #1822751 is a reply to message #1822530] |
Thu, 12 March 2020 15:28  |
Eclipse User |
|
|
|
Hi there,
I have put your code into a plugin project and tried to get it to run. Sadly I get a nullpointer exception when trying to get the command from the architecturedescriptionutils... What am I doing wrong?
The code is:
package modelcreator;
import org.osgi.framework.BundleActivator;
import org.osgi.framework.BundleContext;
import java.io.File;
import java.io.IOException;
import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IFolder;
import org.eclipse.core.resources.IProject;
import org.eclipse.core.resources.IProjectDescription;
import org.eclipse.core.resources.IResource;
import org.eclipse.core.resources.IWorkspace;
import org.eclipse.core.resources.IWorkspaceRoot;
import org.eclipse.core.resources.ResourcesPlugin;
import org.eclipse.core.runtime.NullProgressMonitor;
import org.eclipse.jdt.core.JavaCore;
import org.eclipse.papyrus.infra.architecture.ArchitectureDomainManager;
import org.eclipse.papyrus.infra.architecture.ArchitectureDescriptionUtils;
import org.eclipse.papyrus.infra.core.resource.ModelSet;
import org.eclipse.papyrus.infra.core.resource.ModelsReader;
import org.eclipse.emf.common.util.URI;
import org.eclipse.emf.transaction.TransactionalEditingDomain;
public class Activator implements BundleActivator {
private static BundleContext context;
static BundleContext getContext() {
return context;
}
public void start(BundleContext bundleContext) throws Exception {
Activator.context = bundleContext;
System.out.println("Starting generation:\n");
IWorkspace workspace = ResourcesPlugin.getWorkspace();
IWorkspaceRoot root = workspace.getRoot();
IProject project = root.getProject("sysml");
if (!project.exists()) project.create(null);
if (!project.isOpen()) project.open(null);
if (!project.hasNature(JavaCore.NATURE_ID)) {
IProjectDescription description = project.getDescription();
String[] prevNatures= description.getNatureIds();
String[] newNatures= new String[prevNatures.length + 1];
System.arraycopy(prevNatures, 0, newNatures, 0, prevNatures.length);
newNatures[prevNatures.length]= JavaCore.NATURE_ID;
description.setNatureIds(newNatures);
project.setDescription(description, null);
System.out.println("Added nature...\n");
}
IFolder folder = project.getFolder(project.getName()); // project has same name as folder
IFile file = folder.getFile(project.getName()+".di"); // creation of the .di file
//IFile notation = folder.getFile(project.getName()+".notation"); // creation of the .di file
//IFile uml = folder.getFile(project.getName()+".uml"); // creation of the .di file
if (!folder.exists()) folder.create(IResource.NONE, true, null);
if (!project.isOpen()) project.open(null);
System.out.println("Project folder: " + project.getFullPath()+"\n");
System.out.println("File: " + file.getFullPath().toString()+"\n");
Package pkg = createModelResource(file, "sysml");
}
public Package createModelResource(final IResource container, final String modelFileName) {
ArchitectureDomainManager manager = ArchitectureDomainManager.getInstance();
String contextId = manager.getDefaultArchitectureContextId();
String[] viewpointsIds = new String[2];
viewpointsIds[0] = "org.eclipse.papyrus.uml.analysis";
viewpointsIds[1] = "org.eclipse.papyrus.uml.design";
final ModelSet modelSet = new ModelSet();
ModelsReader reader = new ModelsReader(); //Standard ModelsReader for Di + UML + Notation
reader.readModel(modelSet);
String path = container.getFullPath().toOSString()+File.separator+modelFileName+".di";
URI uri = URI.createPlatformResourceURI(path, true);
modelSet.createModels(uri);
ArchitectureDescriptionUtils arc = new ArchitectureDescriptionUtils(modelSet);
org.eclipse.emf.common.command.Command command = arc.createNewModel(contextId, viewpointsIds);
final TransactionalEditingDomain ted = modelSet.getTransactionalEditingDomain();
ted.getCommandStack().execute(command);
try {
modelSet.save(new NullProgressMonitor());
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
public void stop(BundleContext bundleContext) throws Exception {
Activator.context = null;
}
}
|
|
|
Powered by
FUDForum. Page generated in 0.03856 seconds