|
|
|
|
Re: Create diagram programmatically [message #1821021 is a reply to message #1815626] |
Tue, 04 February 2020 15:19 |
Viet Dinh 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:
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 1030 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 |
Emmanuel Chebbi 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:
- Retrieve the Viewpoint instance used by the DialectManager
- 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
|
|
|
|
Powered by
FUDForum. Page generated in 0.03716 seconds