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 06:35  |
Eclipse User |
|
|
|
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 07:24] by Moderator
|
|
|
Re: Issue when adding programmatically new diagrams to a representation [message #1822734 is a reply to message #1822730] |
Thu, 12 March 2020 08:37   |
Eclipse User |
|
|
|
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:
- checks whether the save job is over
- if yes, then it closes the session
- 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
|
|
| | | | |
Goto Forum:
Current Time: Wed Jun 18 18:23:15 EDT 2025
Powered by FUDForum. Page generated in 0.03926 seconds
|