Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » Sirius » Create diagram programmatically(How to Create diagram programmatically ?)
Create diagram programmatically [message #1815626] Mon, 07 October 2019 12:43 Go to next message
Jean Pierre Clavery is currently offline Jean Pierre ClaveryFriend
Messages: 16
Registered: June 2019
Junior Member
Hi,

I am very new to Sirius.

I would like to know if is it possible to create a diagram programmatically and put in elements?

Thanks
Re: Create diagram programmatically [message #1815630 is a reply to message #1815626] Mon, 07 October 2019 13:34 Go to previous messageGo to next message
Emmanuel Chebbi is currently offline Emmanuel ChebbiFriend
Messages: 123
Registered: February 2018
Senior Member
Hi,

This is possible. As far as I know the API is not documented but you can start with taking a look at the following threads:

Regards,
Emmanuel
Re: Create diagram programmatically [message #1816011 is a reply to message #1815630] Thu, 17 October 2019 08:32 Go to previous messageGo to next message
Jean Pierre Clavery is currently offline Jean Pierre ClaveryFriend
Messages: 16
Registered: June 2019
Junior Member
Thank you very much, it help me.

Now I want to insert element from my model into the diagram. I can i do this ?
Re: Create diagram programmatically [message #1819658 is a reply to message #1816011] Mon, 20 January 2020 14:21 Go to previous messageGo to next message
Emmanuel Chebbi is currently offline Emmanuel ChebbiFriend
Messages: 123
Registered: February 2018
Senior Member
Sorry, I'm a bit late and hope you found the solution. In case someone stumble upon this thread I just posted a code snippet that shows how I managed to programmatically create a diagram for a given model here:
Re: Create diagram programmatically [message #1821021 is a reply to message #1815626] Tue, 04 February 2020 15:19 Go to previous messageGo to next message
Viet Dinh is currently offline Viet DinhFriend
Messages: 2
Registered: December 2019
Junior Member
Hi,
thank you for the answer and the hint to the other posts. I managed to create a session and a diagram.
However, I am stuck with one remaining thing. I want to create more than 1 representation for different objects.


What I get is that:
index.php/fa/37332/0/



My code should create a representation for "Global Context Diagram", but it does not. Why?
 


	// Add the initial model object to the contents.
	//
	EObject rootObject = createInitialModel();
					
	if (rootObject != null) {
							resource.getContents().add(rootObject);
	}

	// Save the contents of the resource to the file system.
	//
	Map<Object, Object> options = new HashMap<Object, Object>();
	options.put(XMLResource.OPTION_ENCODING, initialObjectCreationPage.getEncoding());
	resource.save(options);
						
	// create aird representation which is a session
	URI sessionResourceURI = URI.createPlatformResourceURI(modelFile.getFullPath().toString()+".aird", true);
	Session session = SessionManager.INSTANCE.getSession(sessionResourceURI, progressMonitor);
	session.open(progressMonitor);

	// add semantic resource
	session.getTransactionalEditingDomain().getCommandStack()
				           .execute(new RecordingCommand(session.getTransactionalEditingDomain()) {
				               @Override
				               protected void doExecute() {
				            	   SubMonitor subMonitor = SubMonitor.convert(progressMonitor);
				            	   subMonitor.setTaskName("Adding resource");
				                   SubMonitor commandMonitor = SubMonitor.convert(subMonitor);
				                   session.addSemanticResource(fileURI, commandMonitor);
				               }
				           });
						
						
						
         //select the correct Viewpoint and instantiate a representation
	UserSession userSession = UserSession.from(session);
	userSession.selectViewpoint("probframes");

for (RepresentationDescription rd: DialectManager.INSTANCE.
				getAvailableRepresentationDescriptions(ViewpointRegistry.getInstance().getViewpoints(), rootObject)) {
							DialectManager.INSTANCE.createRepresentation("GlobalCD", rootObject, rd, session, progressMonitor);
							break;
						}
						
						


update:
Strange thing here is, that dialectmanager seems not to create a representation. Everything else works . Any hints are welcome again! Thx
  • Attachment: curr.PNG
    (Size: 5.61KB, Downloaded 929 times)

[Updated on: Tue, 11 February 2020 09:42]

Report message to a moderator

Re: Create diagram programmatically [message #1822134 is a reply to message #1821021] Thu, 27 February 2020 16:54 Go to previous messageGo to next message
Emmanuel Chebbi is currently offline Emmanuel ChebbiFriend
Messages: 123
Registered: February 2018
Senior Member
Hi,

I just ran into a similar issue (and spent a lot of time in the debugger to find a workaround). Disclaimer: the following is true for Sirius 6.0.0 (which is a bit old), I didn't have the time to test a recent release yet.

So, first of all you have to know that when creating a Diagram Description (within a Viewpoint) you can set an Initialization flag (see the doc [1]). When a new View is created for a Diagram Description that has this flag set to true then Sirius automatically creates a Representation for the given EObject. That's why my initial code snippet was working for me.

When the Initialization flag is false we have to use the DialectManager as you did. BUT, this manager may actually silently refuse to create the representation. The reason is that, for some reason, Sirius creates several instances of a same Viewpoint. The instance returned by the ViewpointRegistry is not the same as the instance use by the DialectManager; that makes the DialectManager think that your request cannot be fulfilled (I think that could have been prevented by merely overriding an equals method).
The trick is thus to:

  1. Retrieve the Viewpoint instance used by the DialectManager
  2. Give this instance to the DialectManager when querying the Representation Description

In my case I managed to get the right instance from session's selected views. The corresponding code may be something like the following:
Collection<Viewpoint> actualViewpoints = session.getSelectedViews().stream()
                                                .map(DView::getViewpoint)
                                                .collect(Collectors.toList());

Collection<RepresentationDescription> descs = DialectManager.INSTANCE.getAvailableRepresentationDescriptions(actualViewpoints, rootObject);

// Otherwise Sirius is not able to find the session from the model
rootObject.eAdapters().add(new SessionTransientAttachment(session));

session.getTransactionalEditingDomain().getCommandStack()
   .execute(new RecordingCommand(session.getTransactionalEditingDomain()) {
	   @Override
	   protected void doExecute() {
                   for (RepresentationDescription desc : descs) {
		           DialectManager.INSTANCE.createRepresentation(
				           "GlobalCD", 
				           rootObject, 
				           desc, 
				           session, 
			    	           monitor
		           );
                  }
	   }
   });

Note: you can call DialectManager.INSTANCE.canCreate(rootObject, desc) to check whether Sirius allows the new representation.

To be honest that looks really strange so I may miss something important here.

Anyway, hope that helps. Good luck!

[1] https://www.eclipse.org/sirius/doc/specifier/diagrams/Diagrams.html#diagram_description

[Updated on: Thu, 27 February 2020 16:59]

Report message to a moderator

Re: Create diagram programmatically [message #1857168 is a reply to message #1822134] Fri, 20 January 2023 20:22 Go to previous message
Christoforos Zolotas is currently offline Christoforos ZolotasFriend
Messages: 10
Registered: March 2015
Junior Member
I just logged in to say a HUGE thanks for posting this code snippet. Literally, *all* other code snippets I could find with respect to creating programmatically a representation could miss the line

// Otherwise Sirius is not able to find the session from the model
rootObject.eAdapters().add(new SessionTransientAttachment(session));


and cause session exception.

Be blessed,
Chris
Previous Topic:[ANN] Sirius 7.0.7
Next Topic:Messy diagram layout when creating programmatically representations
Goto Forum:
  


Current Time: Thu Apr 25 21:47:44 GMT 2024

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

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

Back to the top