Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » Sirius » Issue when adding programmatically new diagrams to a representation
Issue when adding programmatically new diagrams to a representation [message #1822730] Thu, 12 March 2020 10:35 Go to next message
Matias Vara is currently offline Matias VaraFriend
Messages: 15
Registered: October 2014
Junior Member
Hello everyone,
I saw that this topic is quite trending. Apologies if it is duplicated. My context is I am extending my model editor by creating a graphical representation when user double clicks on an element. It works fine for the first diagram. But then, the followers diagrams are never displayed. The initialisation flag in on. This problem looks similar to https://www.eclipse.org/forums/index.php/t/1102698/. I am clearly missing something but I do not what. Each time a user double clicks on an element of the editor, the following code is executed:
// create sirius session
ModelType modeltype = (ModelType ) selection.getFirstElement();
URI airdURI = URI.createPlatformResourceURI("/prueba/model/representation.aird", true);
Session siriusSession = SessionManager.INSTANCE.getSession(airdURI, new NullProgressMonitor());
						
siriusSession.open(new NullProgressMonitor());

URI modeluri = modeltype.eResource().getURI();
siriusSession.getTransactionalEditingDomain().getCommandStack()
.execute(new RecordingCommand(siriusSession.getTransactionalEditingDomain()) {
				               @Override
				               protected void doExecute() {
				                   siriusSession.addSemanticResource(modeluri, new NullProgressMonitor());
				               }
				           });
										
// select the Viewpoint and instantiates a representation
UserSession userSession = UserSession.from(siriusSession);
userSession.selectViewpoint("MyViewerView");
						
final Collection<DRepresentation> rd_fr = DialectManager.INSTANCE.getAllRepresentations(siriusSession);
Iterator<DRepresentation> s = rd_fr.iterator();
while (s.hasNext()) {
    DRepresentation repre = s.next();
    DialectUIManager.INSTANCE.openEditor(siriusSession, repre, new NullProgressMonitor());	
}

I also tried by disabling the initialisation flag, and then , I followed https://www.eclipse.org/forums/index.php?t=msg&th=1100864&goto=1821021&#msg_1821021. At first glance, it works well, but when I create EObjects from the Palette, Sirius complains about EObjects that are not in the Session. This is the source code:
Collection<Viewpoint> actualViewpoints = siriusSession.getSelectedViews().stream()
   .map(DView::getViewpoint)
   .collect(Collectors.toList());

Collection<RepresentationDescription> descs = DialectManager.INSTANCE.getAvailableRepresentationDescriptions(actualViewpoints, modeltype);
modeltype.eAdapters().add(new SessionTransientAttachment(siriusSession));
siriusSession.getTransactionalEditingDomain().getCommandStack()
.execute(new RecordingCommand(siriusSession.getTransactionalEditingDomain()) {
@Override
    protected void doExecute() {
      for (RepresentationDescription desc : descs) {
           DRepresentation DRepresentation = DialectManager.INSTANCE.createRepresentation(
		modeltype.getName(), 
		modeltype, 
		desc, 
		siriusSession, 
		new NullProgressMonitor()
		);
DialectUIManager.INSTANCE.openEditor(siriusSession, DRepresentation, new NullProgressMonitor());
				        		}
				            }
				        });

When I use the Palette to instantiate a "child EObject", I got the message:
Impossible to find an interpreter - Could not find a session for model element : 

Any thoughts?

Regards, Matias.

[Updated on: Thu, 12 March 2020 11:24]

Report message to a moderator

Re: Issue when adding programmatically new diagrams to a representation [message #1822734 is a reply to message #1822730] Thu, 12 March 2020 12:37 Go to previous messageGo to next message
Emmanuel Chebbi is currently offline Emmanuel ChebbiFriend
Messages: 123
Registered: February 2018
Senior Member
Hi,

I'm the one who answered the threads you linked above and I faced the same issue as you. I'll dig into the details below, but to sum up I solved the issue by closing the Sirius session right after creating the representation (again, my solution works with Sirius 6.0.0). Here is the corresponding code:

new Job("Close Sirius session of project " + project.getName()) {

    @Override
    protected IStatus run(IProgressMonitor monitor) {
        boolean sessionIsSaved = Job.getJobManager().find(SaveSessionJob.FAMILY).length == 0;

        if (! sessionIsSaved) {
            // The session has not been saved yet, we cannot close
            // Reschedule the job so we can try later
            schedule(5);
        }
        else {
            session.close(new NullProgressMonitor());
        }
        return Status.OK_STATUS;
    }
	    	
}.schedule();

So in my case I noticed that when I close the project then re-open it everything works as expected so I suspected that the issue was related to the Sirius session and maybe some cache it may keep with it.

I don't know what the exact issue is, but according to [1], when Sirius adds the external Ecore model as a semantic resource of the session it actually adds kind of a copy of the resource. If true, it would suggest that Sirius then mistakenly believes that Ecore classes are not part of the session's semantic resources and throws a exception. Anyway, I decided to close the session right after creating the representation to see what would happen.

I first tried a bare

session.close(new NullProgressMonitor());

but it leaded to exceptions being randomly thrown by Sirius shortly afterwards and basically saying that it was unable to save the representation. As a matter of fact, the .aird file was almost empty. It came out that Sirius does not persist the representation as we execute commands, but instead launches a background save job, at some point in time. If this job attempts to save the representation whereas the session is closed, an exception is thrown. I tried to overcome the issue by waiting for this save job:

Job.getJobManager().join(SaveSessionJob.FAMILY);

but for some reason it was leading to an infinite loop: the save job was never ending.

In this end I launched my own background job that:

  1. checks whether the save job is over
  2. if yes, then it closes the session
  3. if no, then it re-schedules itself so that it can try again later

And that resulted in the first snippet. It works but to be honest it feels awfully clumsy so I may miss something here; I'd love to have some insights from a Sirius developer about it.

[1] https://www.eclipse.org/forums/index.php?t=msg&th=1085757&goto=1760173&#msg_1760173
Re: Issue when adding programmatically new diagrams to a representation [message #1822739 is a reply to message #1822734] Thu, 12 March 2020 14:33 Go to previous messageGo to next message
Matias Vara is currently offline Matias VaraFriend
Messages: 15
Registered: October 2014
Junior Member
Thanks Emmanuel. If I understand well, your solution is for my second snippet, i.e., when the "Initialization" flag is not set, Am I right? I will try that later.
In the case that the Initialisation flag is set (my first snippet), I am not invoking createRepresentation(). The XMI changes but it seems that Sirius is missing the change and the diagram is never re-initialised. Maybe I am not understanding when initialisation is happening.
Re: Issue when adding programmatically new diagrams to a representation [message #1822740 is a reply to message #1822739] Thu, 12 March 2020 14:40 Go to previous messageGo to next message
Emmanuel Chebbi is currently offline Emmanuel ChebbiFriend
Messages: 123
Registered: February 2018
Senior Member
Matias Vara wrote on Thu, 12 March 2020 15:33
Thanks Emmanuel. If I understand well, your solution is for my second snippet, i.e., when the "Initialization" flag is not set, Am I right?

Yes.

Matias Vara wrote on Thu, 12 March 2020 15:33
The XMI changes but it seems that Sirius is missing the change and the diagram is never re-initialised. Maybe I am not understanding when initialisation is happening.

I think you still need to create a View (session.createView), see https://www.eclipse.org/forums/index.php?t=msg&th=1102078&goto=1819657&#msg_1819657
Re: Issue when adding programmatically new diagrams to a representation [message #1822744 is a reply to message #1822740] Thu, 12 March 2020 15:41 Go to previous messageGo to next message
Matias Vara is currently offline Matias VaraFriend
Messages: 15
Registered: October 2014
Junior Member
Hello Emmanuel,

I tried your solution and it seems to work. If I tried to open the session just after I close it, I go the exception "Uncaught exception in life-cycle listener". I do not get this exception if I am in debug mode and I put a breakpoint when session is going to be closed. This makes me think there is still something executing in background. Based on your solution, I did something like.

 if (! sessionIsSaved) {
   schedule(5);
 } else {
   siriusSession.close(new NullProgressMonitor());
   final Session siriusSession2 = SessionManager.INSTANCE.getSession(airdURI, new NullProgressMonitor());
    final Collection<DRepresentation> rd_fr = DialectManager.INSTANCE
				        			         .getAllRepresentations(siriusSession2);
Iterator<DRepresentation> s = rd_fr.iterator();

while (s.hasNext()) {
    DRepresentation repre = s.next();
    DialectUIManager.INSTANCE.openEditor(siriusSession2, repre, new 
    NullProgressMonitor());	
  }
}


Regards, Matias.

[Updated on: Thu, 12 March 2020 15:42]

Report message to a moderator

Re: Issue when adding programmatically new diagrams to a representation [message #1822808 is a reply to message #1822744] Fri, 13 March 2020 17:26 Go to previous message
Matias Vara is currently offline Matias VaraFriend
Messages: 15
Registered: October 2014
Junior Member
I made it work by defining the job outside of:
siriusSession.getTransactionalEditingDomain().getCommandStack()
				        .execute(new RecordingCommand(siriusSession.getTransactionalEditingDomain()) {

}

I defined a new job after this, and I opened the Session just after it is closed.

Regards, Matias.
Previous Topic:Rotate Diagram Element
Next Topic:Is there something like a file selector in Sirius?
Goto Forum:
  


Current Time: Wed Apr 24 18:04:03 GMT 2024

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

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

Back to the top