Skip to main content



      Home
Home » Modeling » Papyrus » Create Papyrus diagram programmatically
Create Papyrus diagram programmatically [message #1748002] Thu, 17 November 2016 10:24 Go to next message
Eclipse UserFriend
Hi,
I'm trying to create a papyrus diagram programmatically.
The creation of the model is ok (*.di, *.notation, *.uml):

final IModelCreationCommand creationCommand = new CreateUMLModelCommand();
			creationCommand.createModel(modelset);
			
			final CreateSequenceDiagramCommand diagramCommand = new CreateSequenceDiagramCommand();
			diagramCommand.createDiagram(modelset, null, diagramName);


but the second bloc throw an exception when I create the diagram:

Caused by: java.lang.NullPointerException
	at org.eclipse.papyrus.infra.gmfdiag.common.AbstractPapyrusGmfCreateDiagramCommandHandler.attachModelToResource(AbstractPapyrusGmfCreateDiagramCommandHandler.java:304)
	at org.eclipse.papyrus.infra.gmfdiag.common.AbstractPapyrusGmfCreateDiagramCommandHandler.access$1(AbstractPapyrusGmfCreateDiagramCommandHandler.java:303)
	at org.eclipse.papyrus.infra.gmfdiag.common.AbstractPapyrusGmfCreateDiagramCommandHandler$Creator.createDiagram(AbstractPapyrusGmfCreateDiagramCommandHandler.java:142)
	at org.eclipse.papyrus.infra.gmfdiag.common.AbstractPapyrusGmfCreateDiagramCommandHandler$Creator.access$0(AbstractPapyrusGmfCreateDiagramCommandHandler.java:127)
	at org.eclipse.papyrus.infra.gmfdiag.common.AbstractPapyrusGmfCreateDiagramCommandHandler$1.doExecuteWithResult(AbstractPapyrusGmfCreateDiagramCommandHandler.java:443)
	at org.eclipse.gmf.runtime.emf.commands.core.command.AbstractTransactionalCommand.doExecute(AbstractTransactionalCommand.java:247)
	at org.eclipse.emf.workspace.AbstractEMFOperation.execute(AbstractEMFOperation.java:150)
	at org.eclipse.gmf.runtime.common.core.command.CompositeCommand.doExecuteWithResult(CompositeCommand.java:403)
	at org.eclipse.gmf.runtime.common.core.command.AbstractCommand.execute(AbstractCommand.java:134)
	at org.eclipse.core.commands.operations.DefaultOperationHistory.execute(DefaultOperationHistory.java:488)
	... 11 more


Did someone have any idea?
Re: Create Papyrus diagram programmatically [message #1748085 is a reply to message #1748002] Fri, 18 November 2016 06:44 Go to previous messageGo to next message
Eclipse UserFriend
I have not used the command before, but it's pretty likely that you cannot pass "null" for the "owner" parameter of the createDiagram method. Choose an element in the semantic model instead (e.g. a class that you have to create first)
Re: Create Papyrus diagram programmatically [message #1748100 is a reply to message #1748085] Fri, 18 November 2016 08:44 Go to previous messageGo to next message
Eclipse UserFriend
Thanks a lot!
I pass a
Now the error line change. Seems like it comme from my modelSet.

org.eclipse.core.commands.ExecutionException: While executing the operation, an exception occurred
	at org.eclipse.core.commands.operations.DefaultOperationHistory.execute(DefaultOperationHistory.java:496)
	at org.eclipse.papyrus.infra.emf.gmf.command.CheckedOperationHistory.doExecute(CheckedOperationHistory.java:206)
	at org.eclipse.papyrus.infra.emf.gmf.command.CheckedOperationHistory.execute(CheckedOperationHistory.java:195)
	at org.eclipse.papyrus.commands.CheckedOperationHistory.execute(CheckedOperationHistory.java:86)
	at org.eclipse.papyrus.infra.gmfdiag.common.AbstractPapyrusGmfCreateDiagramCommandHandler.createDiagram(AbstractPapyrusGmfCreateDiagramCommandHandler.java:360)
	at org.eclipse.papyrus.infra.gmfdiag.common.AbstractPapyrusGmfCreateDiagramCommandHandler.createDiagram(AbstractPapyrusGmfCreateDiagramCommandHandler.java:392)
	at org.eclipse.papyrus.infra.gmfdiag.common.AbstractPapyrusGmfCreateDiagramCommandHandler.createDiagram(AbstractPapyrusGmfCreateDiagramCommandHandler.java:345)

	at org.eclipse.jface.operation.ModalContext$ModalContextThread.run(ModalContext.java:119)
Caused by: java.lang.NullPointerException
	at org.eclipse.papyrus.infra.gmfdiag.common.AbstractPapyrusGmfCreateDiagramCommandHandler.getNotationResource(AbstractPapyrusGmfCreateDiagramCommandHandler.java:237)
	at org.eclipse.papyrus.infra.gmfdiag.common.AbstractPapyrusGmfCreateDiagramCommandHandler$Creator.createDiagram(AbstractPapyrusGmfCreateDiagramCommandHandler.java:128)
	at org.eclipse.papyrus.infra.gmfdiag.common.AbstractPapyrusGmfCreateDiagramCommandHandler$Creator.access$0(AbstractPapyrusGmfCreateDiagramCommandHandler.java:127)
	at org.eclipse.papyrus.infra.gmfdiag.common.AbstractPapyrusGmfCreateDiagramCommandHandler$1.doExecuteWithResult(AbstractPapyrusGmfCreateDiagramCommandHandler.java:443)
	at org.eclipse.gmf.runtime.emf.commands.core.command.AbstractTransactionalCommand.doExecute(AbstractTransactionalCommand.java:247)
	at org.eclipse.emf.workspace.AbstractEMFOperation.execute(AbstractEMFOperation.java:150)
	at org.eclipse.gmf.runtime.common.core.command.CompositeCommand.doExecuteWithResult(CompositeCommand.java:403)
	at org.eclipse.gmf.runtime.common.core.command.AbstractCommand.execute(AbstractCommand.java:134)
	at org.eclipse.core.commands.operations.DefaultOperationHistory.execute(DefaultOperationHistory.java:488)
	... 11 more


I create it this way:
final ModelSet modelset = new ModelSet();
final ModelsReader reader = new ModelsReader();
reader.readModel(modelset);

final URI diPlateformResourceURI = URI.createPlatformResourceURI(file.getFullPath().toOSString()+".di", Boolean.TRUE);

modelset.createModels(diPlateformResourceURI);
final UmlModel umlModel = UmlUtils.getUmlModel(modelset);
			
umlModel.init(modelset);
...
final IModelCreationCommand creationCommand = new CreateUMLModelCommand();
creationCommand.createModel(modelset);
			
final CreateSequenceDiagramCommand diagramCommand = new CreateSequenceDiagramCommand();
diagramCommand.createDiagram(modelset, UMLFactory.eINSTANCE.createSequenceNode(), diagramName);


Perhaps the initialization is wrong?
Re: Create Papyrus diagram programmatically [message #1748102 is a reply to message #1748100] Fri, 18 November 2016 08:54 Go to previous messageGo to next message
Eclipse UserFriend
If you create an element using the UML factory, the element is not associated with any resource. You need to add it to an element of the created model first (e.g. if you already have an existing package or model, you need to call
org.eclipse.uml2.uml.Package root = (org.eclipse.uml2.uml.Package) umlModel.lookupRoot();
root.getPackagedElements().add(new element);
(btw. I don't think, you can directly add a sequenceNode to a package).
Re: Create Papyrus diagram programmatically [message #1748109 is a reply to message #1748102] Fri, 18 November 2016 09:24 Go to previous messageGo to next message
Eclipse UserFriend
For
Package root = (Package) umlModel.lookupRoot();


I got
org.eclipse.papyrus.infra.core.resource.NotFoundException: No root defined in the model
	at org.eclipse.papyrus.uml.tools.model.UmlModel.lookupRoot(UmlModel.java:112)


How did I create the model, or link it to the modelSet?
Re: Create Papyrus diagram programmatically [message #1748245 is a reply to message #1748109] Mon, 21 November 2016 10:00 Go to previous messageGo to next message
Eclipse UserFriend
Hi Ismael,

I know I tried to do something alike some time ago and here is what I came up with.
// Create the new Component diagram associated to the newly created package
		Collection<ViewPrototype> availablePrototypes = PolicyChecker.getCurrent().getPrototypesFor(newContextPackage);
		for (ViewPrototype viewPrototype : availablePrototypes) {
			if (viewPrototype.getLabel().equals(UML_COMPONENT_DIAGRAM)) {
				viewPrototype.instantiateOn(newContextPackage, decomposeDialog.getDiagramName());
			}
		}

// Create the package used to populate the new diagram, rename 
		final CreateElementRequest requestFunctionPackage = new CreateElementRequest(currentDomain, newContextPackage,
				ElementTypeRegistry.getInstance().getType(ORG_ECLIPSE_PAPYRUS_UML_PACKAGE));
		IElementEditService providerFunctionPackage = ElementEditServiceUtils.getCommandProvider(newContextPackage);
		ICommand createGMFCommandFunctionPackage = providerFunctionPackage.getEditCommand(requestFunctionPackage);
		final Command emfCommandFunctionPackage = GMFtoEMFCommandWrapper.wrap(createGMFCommandFunctionPackage);
		currentDomain.getCommandStack().execute(emfCommandFunctionPackage);

		final EObject newFunctionPackage = requestFunctionPackage.getNewElement();

		RenameElementCommand renameFunctionPackageCommand = new RenameElementCommand(currentDomain,
				(NamedElement) UMLUtil.resolveUMLElement(newFunctionPackage), decomposeDialog.getFunctionName());
		renameFunctionPackageCommand.execute();


Then you have to drop the newly created package/element in the new diagram.

HTH
Re: Create Papyrus diagram programmatically [message #1749059 is a reply to message #1748245] Thu, 01 December 2016 09:11 Go to previous messageGo to next message
Eclipse UserFriend
Thanks a lot Quentin,
But it seams like I have a problem with my ModelSet. It's correctly created, but on the UmlModel object I can't get the root.
final UmlModel umlModel = (UmlModel) modelset.getModel(UmlModel.MODEL_ID);
umlModel.lookupRoot();

throw
org.eclipse.papyrus.infra.core.resource.NotFoundException: No root defined in the model
	at org.eclipse.papyrus.uml.tools.model.UmlModel.lookupRoot(UmlModel.java:112)


Without it, I can' instantiate the diagram.
Did I miss something?
Re: Create Papyrus diagram programmatically [message #1749065 is a reply to message #1749059] Thu, 01 December 2016 09:29 Go to previous messageGo to next message
Eclipse UserFriend
So, how to create the newContextPackage wih my ModelSet?
Because, for:
viewPrototype.instantiateOn(UMLFactory.eINSTANCE.createActivity(), diagramName);

I get:
org.eclipse.papyrus.infra.core.services.ServiceNotFoundException: Can't find the ResourceSet needed retrieve the ServiceRegistry.
	at org.eclipse.papyrus.infra.emf.utils.ServiceUtilsForResource.getServiceRegistry(ServiceUtilsForResource.java:63)
	at org.eclipse.papyrus.infra.emf.utils.ServiceUtilsForEObject.getServiceRegistry(ServiceUtilsForEObject.java:43)
	at org.eclipse.papyrus.infra.gmfdiag.common.helper.DiagramPrototype.instantiateOn(DiagramPrototype.java:54)
Re: Create Papyrus diagram programmatically [message #1749614 is a reply to message #1748245] Thu, 08 December 2016 11:23 Go to previous message
Eclipse UserFriend
I've found the solution.
What was missing: the asynchrone execution of the transactional editing domain
editingDomain.getCommandStack().execute(new RecordingCommand(editingDomain) {
					public void doExecute(){
                                            //my stuff
                                        }
}


Thanks a lot guys.
Previous Topic:open programmatically generated model by papyrus again
Next Topic:Hide Welcome page
Goto Forum:
  


Current Time: Mon Jul 07 16:00:59 EDT 2025

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

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

Back to the top