Home » Modeling » Papyrus » Creating models (uml,notation and di) programatically tutorial
|
Re: Creating models (uml,notation and di) programatically tutorial [message #1424271 is a reply to message #1404359] |
Mon, 15 September 2014 18:54 |
András Dobreff Messages: 9 Registered: September 2014 |
Junior Member |
|
|
Hi!
I'm also trying with this. After lots of search on this forum and in the papyrus repository:
eclipse.googlesource.com/papyrus/org.eclipse.papyrus/+/master
I've managed to build up an eclipse plugin that creates a papyrus project, inserts an existing .uml file, creates the Class Diagrams and drops the Classes on the diagrams.
I think the two links that you found are the best helps.
Here is a code, that creates a papyrus model
(path is a string and stands for Projectname/Modelname e.g. MyPapyrusProject/mypapyrusmodel, where you have a folder MyPapyrusProject with the 3 files: mypapyrusmodel.uml, mypapyrusmodel.di, mypapyrusmodel.notation )
I'm not saying that it is the best way but works
ModelSet modelSet = new ModelSet();
ModelsReader reader = new ModelsReader(); //Standard ModelsReader for Di + UML + Notation
reader.readModel(modelSet);
String sourceUmlPath = "C:\\uml.uml";
String diFilePath = path+".di";
String umlFilePath = path+".uml";
URI diFileURI = URI.createFileURI(diFilePath);
URI umlFileURI = URI.createFileURI(umlFilePath);
URI diPResURI = URI.createPlatformResourceURI(diFilePath, true);
URI umlPResURI = URI.createPlatformResourceURI(umlFilePath, true);
URI resolvedFile = CommonPlugin.resolve(diFileURI);
IFile ifile = ResourcesPlugin.getWorkspace().getRoot().getFile(new Path(resolvedFile.toFileString()));
modelSet.createModels(diPResURI);
ServicesRegistry registry = new ExtensionServicesRegistry(org.eclipse.papyrus.infra.core.Activator.PLUGIN_ID);
try {
registry.add(ModelSet.class, Integer.MAX_VALUE, modelSet);
registry.startRegistry();
} catch (ServiceException ex) {
//Ignore
}
modelSet.save(new NullProgressMonitor());
modelSet.loadModels(umlPResURI);
URI UmlFileResURI = CommonPlugin.resolve(umlFileURI);
IFile UmlFile = ResourcesPlugin.getWorkspace().getRoot().getFile(new Path(UmlFileResURI.toFileString()));
InputStream is = new FileInputStream(sourceUmlPath);
modelSet.save(new NullProgressMonitor());
UmlFile.setContents(is, true, false, new NullProgressMonitor());
modelSet.loadModels(umlPResURI);
IModelCreationCommand creationCommand = new CreateUMLModelCommand();
creationCommand.createModel(modelSet);
|
|
| |
Re: Creating models (uml,notation and di) programatically tutorial [message #1494848 is a reply to message #1451154] |
Mon, 01 December 2014 20:20 |
András Dobreff Messages: 9 Registered: September 2014 |
Junior Member |
|
|
Hello Alexandre,
first of all the 3 files don't need to be created. These files will be created as the ModelSet.save() function is called.
For the imports: In my code I use the followings:
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IProject;
import org.eclipse.core.resources.ResourcesPlugin;
import org.eclipse.core.runtime.NullProgressMonitor;
import org.eclipse.core.runtime.Path;
import org.eclipse.jface.action.IAction;
import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.papyrus.infra.core.editor.IMultiDiagramEditor;
import org.eclipse.papyrus.infra.core.extension.commands.IModelCreationCommand;
import org.eclipse.papyrus.infra.core.resource.ModelSet;
import org.eclipse.papyrus.infra.core.resource.ModelsReader;
import org.eclipse.papyrus.infra.core.services.ExtensionServicesRegistry;
import org.eclipse.papyrus.infra.core.services.ServiceException;
import org.eclipse.papyrus.infra.core.services.ServicesRegistry;
import org.eclipse.ui.IEditorPart;
import org.eclipse.ui.IWorkbenchPage;
import org.eclipse.ui.IWorkbenchWindow;
import org.eclipse.ui.IWorkbenchWindowActionDelegate;
import org.eclipse.ui.PartInitException;
import org.eclipse.ui.PlatformUI;
(I'm not sure that all of them are necessary)
For debugging I think these imports will be enough. But if you want to export your plugin you have to add some plugin dependencies. (Open the plugin.xml and switch to the dependencies tab)
I use the followings:
org.eclipse.ui,
org.eclipse.core.runtime,
org.eclipse.papyrus.infra.gmfdiag.common,
org.eclipse.papyrus.infra.core,
org.eclipse.papyrus.uml.tools,
org.eclipse.papyrus.uml.diagram.wizards,
org.eclipse.papyrus.uml.diagram.common,
org.eclipse.ui.ide,
org.eclipse.papyrus.infra.gmfdiag.commands,
org.eclipse.papyrus.uml.diagram.clazz,
org.eclipse.papyrus.uml.diagram.activity,
org.eclipse.papyrus.editor,
org.eclipse.jdt.core,
org.eclipse.core.expressions,
org.eclipse.ui.workbench,
org.junit,
org.eclipse.gef,
org.eclipse.gmf.runtime.diagram.ui
(and again I think you won't need all of these)
András
|
|
| |
Re: Creating models (uml,notation and di) programatically tutorial [message #1694286 is a reply to message #1496085] |
Mon, 04 May 2015 08:02 |
András Dobreff Messages: 9 Registered: September 2014 |
Junior Member |
|
|
During the last few Months I've found a better way to achieve model creation in Papyrus:
Watching the org.eclipse.papyrus.uml.diagram.wizards.CreateModelWizard class I've found out that there is a Command for Model Creation:
It's called: NewPapyrusModelCommand
The wizard calls this command, initializes the registry and creates a model considering the selected diagrams. (In our case it's an empty model)
The following code should do the work:
//Starting the registry
ServicesRegistry registry = new ExtensionServicesRegistry(org.eclipse.papyrus.infra.core.Activator.PLUGIN_ID);
try {
// have to create the model set and populate it with the DI model
// before initializing other services that actually need the DI
// model, such as the SashModel Manager service
registry.startServicesByClassKeys(ModelSet.class);
} catch (ServiceException ex) {
// Ignore this exception: some services may not have been loaded,
// which is probably normal at this point
}
ModelSet modelSet = registry.getService(ModelSet.class);
//The modelname is the name of your model
URI diPResURI = URI.createPlatformResourceURI(modelname+".di", true);
RecordingCommand command = new NewPapyrusModelCommand(modelSet, diPResURI);
modelSet.getTransactionalEditingDomain().getCommandStack().execute(command);
//Initializing the registry
try {
registry.startRegistry();
} catch (ServiceException ex) {
// Ignore this exception: some services may not have been loaded,
// which is probably normal at this point
}
registry.getService(IPageManager.class);
modelSet.save(new NullProgressMonitor());
//Creating specific model
Map<String, DiagramCategoryDescriptor> categories = DiagramCategoryRegistry.getInstance().getDiagramCategoryMap();
String key = "sysml" //the three possibilities are: uml, sysml and profile
IModelCreationCommand creationCommand = categories.get("uml").getCommand();
creationCommand.createModel(modelSet);
|
|
|
Goto Forum:
Current Time: Thu Sep 26 04:11:32 GMT 2024
Powered by FUDForum. Page generated in 0.03079 seconds
|