Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » Papyrus » Papyrus Activity Diagram Creation via API(Papyrus API)
Papyrus Activity Diagram Creation via API [message #1821189] Thu, 06 February 2020 19:04 Go to next message
Dying Devil is currently offline Dying DevilFriend
Messages: 3
Registered: February 2020
Junior Member
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 #1821456 is a reply to message #1821189] Thu, 13 February 2020 11:07 Go to previous messageGo to next message
Thomas Wiman is currently offline Thomas WimanFriend
Messages: 83
Registered: June 2012
Location: Stockholm , Sweden
Member

Here is a code snippet that creates an activity diagram within a given activity element and adds the activity itself to the diagram


TransactionalEditingDomain ted = TransactionUtil.getEditingDomain(activity);
RecordingCommand cmd = new RecordingCommand(editingDomain, "Create Activity Diagram", "") {
@Override
protected void doExecute() {

TransactionalEditingDomain ted = TransactionUtil.getEditingDomain(activity);
final Resource diagramResource = NotationUtils.getNotationResourceForDiagram(activity, ted);
Diagram activityDiagram = ViewService.createDiagram(activity,"PapyrusUMLActivityDiagram", UMLDiagramEditorPlugin.DIAGRAM_PREFERENCES_HINT);

activityDiagram.setName("My Activity Diagram");
activityDiagram.setElement(activity);
diagramResource.getContents().add(activityDiagram);

PapyrusDiagramStyle style =(new StyleFactoryImpl()).createPapyrusDiagramStyle();
style.setOwner(activity);
style.setDiagramKindId("org.eclipse.papyrus.uml.diagram.activity");
activityDiagram.getStyles().add(style);
String visualId = UMLVisualIDRegistry.getType(org.eclipse.papyrus.uml.diagram.activity.edit.parts.ActivityEditPart.VISUAL_ID);
String type = org.eclipse.papyrus.uml.diagram.activity.part.UMLVisualIDRegistry.getType(visualId);

Node activityNode = ViewService.createNode(activityDiagram, activity, type, PreferencesHint.USE_DEFAULTS);
}
};
editingDomain.getCommandStack().execute(cmd);



Thomas Wiman
MetaModelAgent Product Manager
Re: Papyrus Activity Diagram Creation via API [message #1822455 is a reply to message #1821456] Fri, 06 March 2020 20:39 Go to previous messageGo to next message
Dying Devil is currently offline Dying DevilFriend
Messages: 3
Registered: February 2020
Junior Member
Hey there,

thanks for the response, that looks quite good.

Sadly I just don't get it to run:
- First I can't find the correct package to import for StyleFactoryImpl(). What package should I include?
- Secondly what infrastructure (or files, or models) do there have to be in order to execute that code? I have been trying several days now to just put a small class together that programmatically creates a new project in the workspace, initialize that properly as papyrus project, adds an activity diagram and adds one activity to that. I got so far that the project is created, but neither can I find out how to initialize that as papyrus project, nor can I find out what code I need to add to embed your code and get the thing done.

If it is not too harsh to ask for help again, it would be very great to get some assistance there...

Thanks!

DyDe
Re: Papyrus Activity Diagram Creation via API [message #1822530 is a reply to message #1822455] Mon, 09 March 2020 14:00 Go to previous messageGo to next message
Thomas Wiman is currently offline Thomas WimanFriend
Messages: 83
Registered: June 2012
Location: Stockholm , Sweden
Member

Hi,

My code snippet unfortunately contained a reference to an implementation class constructor StyleFactoryImpl()

A better solution would be to exchange that line to the following:
PapyrusDiagramStyle style = org.eclipse.papyrus.infra.gmfdiag.style.StyleFactory.createPapyrusDiagramStyle()

To create model resources (.di, .uml, .notation-files) given a project you can use the following snippet which return the root element of the model:

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);

Command command = arc.createNewModel(contextId, viewpointsIds);
TransactionalEditingDomain ted = modelSet.getTransactionalEditingDomain();
ted.getCommandStack().execute(command);

try {
modelSet.save(new NullProgressMonitor());
} catch (IOException e) {
e.printStackTrace();
}
//Opens the new model in an editor

IFile file = null;
if (container instanceof IFolder) {
file = ((IFolder)container).getFile(modelFileName);
} else if (container instanceof IProject) {
file = ((IProject)container).getFile(modelFileName);
}
if (file == null) {
return null;
} else {
Package rootPackage = openEditor(file);
return rootPackage;
}
}




Thomas Wiman
MetaModelAgent Product Manager
Re: Papyrus Activity Diagram Creation via API [message #1822751 is a reply to message #1822530] Thu, 12 March 2020 19:28 Go to previous message
Dying Devil is currently offline Dying DevilFriend
Messages: 3
Registered: February 2020
Junior Member
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;
}
}
Previous Topic:Template class attribute of a template class
Next Topic:Papyrus Compare problems with RCP based on 2019-12
Goto Forum:
  


Current Time: Thu Mar 28 16:36:47 GMT 2024

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

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

Back to the top