-
Notifications
You must be signed in to change notification settings - Fork 45
/
CompatibilityToolSectionsProvider.java
335 lines (307 loc) · 17.8 KB
/
CompatibilityToolSectionsProvider.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
/*******************************************************************************
* Copyright (c) 2022, 2023 Obeo.
* This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v2.0
* which accompanies this distribution, and is available at
* https://www.eclipse.org/legal/epl-2.0/
*
* SPDX-License-Identifier: EPL-2.0
*
* Contributors:
* Obeo - initial API and implementation
*******************************************************************************/
package org.eclipse.sirius.components.compatibility.services.diagrams;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Objects;
import java.util.Optional;
import java.util.function.Function;
import org.eclipse.emf.common.util.EList;
import org.eclipse.emf.common.util.URI;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.sirius.components.collaborative.diagrams.api.DiagramImageConstants;
import org.eclipse.sirius.components.collaborative.diagrams.api.IToolSectionsProvider;
import org.eclipse.sirius.components.compatibility.api.IAQLInterpreterFactory;
import org.eclipse.sirius.components.compatibility.api.IIdentifierProvider;
import org.eclipse.sirius.components.compatibility.services.api.IODesignRegistry;
import org.eclipse.sirius.components.core.api.Environment;
import org.eclipse.sirius.components.diagrams.Edge;
import org.eclipse.sirius.components.diagrams.Node;
import org.eclipse.sirius.components.diagrams.description.DiagramDescription;
import org.eclipse.sirius.components.diagrams.description.EdgeDescription;
import org.eclipse.sirius.components.diagrams.description.IDiagramElementDescription;
import org.eclipse.sirius.components.diagrams.description.NodeDescription;
import org.eclipse.sirius.components.diagrams.description.SynchronizationPolicy;
import org.eclipse.sirius.components.diagrams.tools.ITool;
import org.eclipse.sirius.components.diagrams.tools.SingleClickOnDiagramElementTool;
import org.eclipse.sirius.components.diagrams.tools.ToolSection;
import org.eclipse.sirius.components.interpreter.AQLInterpreter;
import org.eclipse.sirius.components.interpreter.Result;
import org.eclipse.sirius.components.interpreter.Status;
import org.eclipse.sirius.components.representations.IStatus;
import org.eclipse.sirius.components.representations.Success;
import org.eclipse.sirius.components.representations.VariableManager;
import org.eclipse.sirius.diagram.description.ContainerMapping;
import org.eclipse.sirius.diagram.description.DiagramElementMapping;
import org.eclipse.sirius.diagram.description.EdgeMapping;
import org.eclipse.sirius.diagram.description.Layer;
import org.eclipse.sirius.diagram.description.NodeMapping;
import org.eclipse.sirius.diagram.description.tool.ContainerCreationDescription;
import org.eclipse.sirius.diagram.description.tool.DeleteElementDescription;
import org.eclipse.sirius.diagram.description.tool.DirectEditLabel;
import org.eclipse.sirius.diagram.description.tool.EdgeCreationDescription;
import org.eclipse.sirius.diagram.description.tool.NodeCreationDescription;
import org.eclipse.sirius.viewpoint.description.tool.AbstractToolDescription;
import org.eclipse.sirius.viewpoint.description.tool.MappingBasedToolDescription;
import org.springframework.stereotype.Service;
/**
* The tool sections provider for the compatibility layer.
*
* @author arichard
*/
@Service
public class CompatibilityToolSectionsProvider implements IToolSectionsProvider {
private final IIdentifierProvider identifierProvider;
private final IODesignRegistry odesignRegistry;
private final IAQLInterpreterFactory interpreterFactory;
public CompatibilityToolSectionsProvider(IIdentifierProvider identifierProvider, IODesignRegistry odesignRegistry, IAQLInterpreterFactory interpreterFactory) {
this.identifierProvider = Objects.requireNonNull(identifierProvider);
this.odesignRegistry = Objects.requireNonNull(odesignRegistry);
this.interpreterFactory = Objects.requireNonNull(interpreterFactory);
}
@Override
public boolean canHandle(DiagramDescription diagramDescription) {
return this.identifierProvider.findVsmElementId(diagramDescription.getId()).isPresent();
}
@Override
public List<ToolSection> handle(Object targetElement, Object diagramElement, Object diagramElementDescription, DiagramDescription diagramDescription) {
var optionalVsmElementId = this.identifierProvider.findVsmElementId(diagramDescription.getId());
// @formatter:off
var optionalSiriusDiagramDescription = this.odesignRegistry.getODesigns().stream()
.map(EObject::eResource)
.map(resource -> resource.getResourceSet().getEObject(URI.createURI(optionalVsmElementId.get()), false))
.filter(Objects::nonNull)
.filter(org.eclipse.sirius.diagram.description.DiagramDescription.class::isInstance)
.map(org.eclipse.sirius.diagram.description.DiagramDescription.class::cast)
.findFirst();
// @formatter:on
List<ToolSection> toolSections = new ArrayList<>();
if (optionalSiriusDiagramDescription.isPresent()) {
org.eclipse.sirius.diagram.description.DiagramDescription siriusDiagramDescription = optionalSiriusDiagramDescription.get();
// @formatter:off
List<ToolSection> filteredToolSections = diagramDescription.getToolSections().stream()
.map(toolSection -> this.filteredTools(targetElement, diagramElement, toolSection, siriusDiagramDescription, diagramElementDescription))
.filter(toolSection -> !toolSection.getTools().isEmpty())
.toList();
// @formatter:on
toolSections.addAll(filteredToolSections);
toolSections.addAll(this.createExtraToolSections(diagramElementDescription));
}
return toolSections;
}
private ToolSection filteredTools(Object targetElement, Object diagramElement, ToolSection toolSection, org.eclipse.sirius.diagram.description.DiagramDescription siriusDiagramDescription,
Object diagramElementDescription) {
// @formatter:off
List<ITool> tools = toolSection.getTools().stream()
.filter(tool -> {
boolean keepTool = false;
var optionalVsmElementId = this.identifierProvider.findVsmElementId(tool.getId());
if (optionalVsmElementId.isPresent()) {
var optionalSiriusTool = this.odesignRegistry.getODesigns().stream()
.map(EObject::eResource)
.map(resource -> resource.getResourceSet().getEObject(URI.createURI(optionalVsmElementId.get()), false))
.filter(Objects::nonNull)
.filter(AbstractToolDescription.class::isInstance)
.map(AbstractToolDescription.class::cast)
.findFirst();
if (optionalSiriusTool.isPresent()) {
AbstractToolDescription siriusTool = optionalSiriusTool.get();
if (siriusTool instanceof MappingBasedToolDescription) {
keepTool = this.matchMapping(diagramElementDescription, siriusTool, siriusDiagramDescription);
} else {
keepTool = true;
}
if (keepTool) {
keepTool = this.checkPrecondition(targetElement, diagramElement, siriusTool, siriusDiagramDescription);
}
}
}
return keepTool;
})
.toList();
// @formatter:on
return ToolSection.newToolSection(toolSection).tools(tools).build();
}
private boolean matchMapping(Object diagramElementDescription, AbstractToolDescription siriusTool, org.eclipse.sirius.diagram.description.DiagramDescription siriusDiagramDescription) {
boolean matchMapping = false;
var optionalDiagramElementMapping = this.getDiagramElementMapping(diagramElementDescription);
if (optionalDiagramElementMapping.isPresent()) {
List<DiagramElementMapping> mappings = this.getSiriusToolMappings(siriusTool);
if (siriusTool instanceof EdgeCreationDescription) {
matchMapping = mappings.stream().anyMatch(mapping -> mapping.equals(optionalDiagramElementMapping.get()));
} else if (diagramElementDescription instanceof DiagramDescription) {
matchMapping = this.atLeastOneRootMapping(mappings);
} else {
matchMapping = this.getParentMappings(mappings).stream().anyMatch(mapping -> mapping.equals(optionalDiagramElementMapping.get()));
}
}
return matchMapping;
}
private Optional<EObject> getDiagramElementMapping(Object diagramElementDescription) {
String descriptionId = this.getDescriptionId(diagramElementDescription);
var optionalVsmElementId = this.identifierProvider.findVsmElementId(descriptionId);
if (optionalVsmElementId.isPresent()) {
// @formatter:off
return this.odesignRegistry.getODesigns().stream()
.map(EObject::eResource).map(r -> r.getResourceSet().getEObject(URI.createURI(optionalVsmElementId.get()), false))
.filter(Objects::nonNull)
.findFirst();
// @formatter:on
}
return Optional.empty();
}
private List<DiagramElementMapping> getSiriusToolMappings(AbstractToolDescription siriusTool) {
List<DiagramElementMapping> mappings = new ArrayList<>();
if (siriusTool instanceof ContainerCreationDescription) {
EList<ContainerMapping> containerMappings = ((ContainerCreationDescription) siriusTool).getContainerMappings();
mappings.addAll(containerMappings);
} else if (siriusTool instanceof NodeCreationDescription) {
EList<NodeMapping> nodeMappings = ((NodeCreationDescription) siriusTool).getNodeMappings();
mappings.addAll(nodeMappings);
} else if (siriusTool instanceof EdgeCreationDescription) {
EList<EdgeMapping> edgeMappings = ((EdgeCreationDescription) siriusTool).getEdgeMappings();
mappings.addAll(edgeMappings.stream().map(EdgeMapping::getSourceMapping).flatMap(Collection::stream).toList());
} else if (siriusTool instanceof DirectEditLabel) {
EList<DiagramElementMapping> eltMappings = ((DirectEditLabel) siriusTool).getMapping();
mappings.addAll(eltMappings);
} else if (siriusTool instanceof DeleteElementDescription) {
EList<DiagramElementMapping> eltMappings = ((DeleteElementDescription) siriusTool).getMappings();
mappings.addAll(eltMappings);
}
return mappings;
}
private List<DiagramElementMapping> getParentMappings(List<DiagramElementMapping> mappings) {
//@formatter:off
return mappings.stream()
.map(DiagramElementMapping::eContainer)
.filter(DiagramElementMapping.class::isInstance)
.map(DiagramElementMapping.class::cast)
.toList();
//@formatter:on
}
private boolean atLeastOneRootMapping(List<DiagramElementMapping> mappings) {
//@formatter:off
return mappings.stream()
.map(DiagramElementMapping::eContainer)
.anyMatch(Layer.class::isInstance);
//@formatter:on
}
private String getDescriptionId(Object diagramElementDescription) {
String descriptionId = null;
if (diagramElementDescription instanceof DiagramDescription) {
descriptionId = ((DiagramDescription) diagramElementDescription).getId();
} else if (diagramElementDescription instanceof NodeDescription) {
descriptionId = ((NodeDescription) diagramElementDescription).getId().toString();
} else if (diagramElementDescription instanceof EdgeDescription) {
descriptionId = ((EdgeDescription) diagramElementDescription).getId().toString();
}
return descriptionId;
}
private boolean checkPrecondition(Object targetElement, Object diagramElement, AbstractToolDescription siriusTool,
org.eclipse.sirius.diagram.description.DiagramDescription siriusDiagramDescription) {
boolean checkPrecondition = false;
if (!(siriusTool instanceof EdgeCreationDescription)) {
AQLInterpreter interpreter = this.interpreterFactory.create(siriusDiagramDescription);
String precondition = siriusTool.getPrecondition();
if (precondition != null && !precondition.isBlank()) {
VariableManager variableManager = new VariableManager();
variableManager.put(VariableManager.SELF, targetElement);
variableManager.put(Environment.ENVIRONMENT, Environment.SIRIUS_COMPONENTS);
if (diagramElement instanceof Node) {
variableManager.put(Node.SELECTED_NODE, diagramElement);
} else if (diagramElement instanceof Edge) {
variableManager.put(Edge.SELECTED_EDGE, diagramElement);
}
Result result = interpreter.evaluateExpression(variableManager.getVariables(), precondition);
checkPrecondition = result.getStatus().compareTo(Status.WARNING) <= 0 && result.asBoolean().orElse(Boolean.FALSE);
} else {
checkPrecondition = true;
}
} else {
checkPrecondition = true;
}
return checkPrecondition;
}
private List<ToolSection> createExtraToolSections(Object diagramElementDescription) {
List<ToolSection> extraToolSections = new ArrayList<>();
List<IDiagramElementDescription> targetDescriptions = new ArrayList<>();
boolean unsynchronizedMapping = false;
//@formatter:off
if (diagramElementDescription instanceof NodeDescription nodeDescription) {
targetDescriptions.add(nodeDescription);
unsynchronizedMapping = SynchronizationPolicy.UNSYNCHRONIZED.equals(nodeDescription.getSynchronizationPolicy());
} else if (diagramElementDescription instanceof EdgeDescription edgeDescription) {
var optionalDiagramElementMapping = this.getDiagramElementMapping(edgeDescription);
var domainEdgeMapping = optionalDiagramElementMapping
.filter(EdgeMapping.class::isInstance)
.map(EdgeMapping.class::cast)
.filter(edgeMapping -> edgeMapping.isUseDomainElement());
if (domainEdgeMapping.isPresent()) {
targetDescriptions.add(edgeDescription);
} else {
targetDescriptions.addAll(edgeDescription.getSourceNodeDescriptions());
}
unsynchronizedMapping = SynchronizationPolicy.UNSYNCHRONIZED.equals(((EdgeDescription) diagramElementDescription).getSynchronizationPolicy());
}
Function<VariableManager, IStatus> fakeHandler = variableManager -> new Success();
// Graphical Delete Tool for unsynchronized mapping only (the handler is never called)
if (diagramElementDescription instanceof NodeDescription || diagramElementDescription instanceof EdgeDescription) {
// Edit Tool (the handler is never called)
SingleClickOnDiagramElementTool editTool = SingleClickOnDiagramElementTool.newSingleClickOnDiagramElementTool("edit")
.label("Edit")
.imageURL(DiagramImageConstants.EDIT_SVG)
.targetDescriptions(targetDescriptions)
.handler(fakeHandler)
.appliesToDiagramRoot(false)
.build();
var editToolSection = ToolSection.newToolSection("edit-section")
.label("")
.imageURL("")
.tools(List.of(editTool))
.build();
extraToolSections.add(editToolSection);
if (unsynchronizedMapping) {
SingleClickOnDiagramElementTool graphicalDeleteTool = SingleClickOnDiagramElementTool.newSingleClickOnDiagramElementTool("graphical-delete")
.label("Delete from diagram")
.imageURL(DiagramImageConstants.GRAPHICAL_DELETE_SVG)
.targetDescriptions(targetDescriptions)
.handler(fakeHandler)
.appliesToDiagramRoot(false)
.build();
var graphicalDeleteToolSection = ToolSection.newToolSection("graphical-delete-section")
.label("")
.imageURL("")
.tools(List.of(graphicalDeleteTool))
.build();
extraToolSections.add(graphicalDeleteToolSection);
}
// Semantic Delete Tool (the handler is never called)
SingleClickOnDiagramElementTool semanticDeleteTool = SingleClickOnDiagramElementTool.newSingleClickOnDiagramElementTool("semantic-delete")
.label("Delete from model")
.imageURL(DiagramImageConstants.SEMANTIC_DELETE_SVG)
.targetDescriptions(targetDescriptions)
.handler(fakeHandler)
.appliesToDiagramRoot(false)
.build();
var semanticDeleteToolSection = ToolSection.newToolSection("semantic-delete-section")
.label("")
.imageURL("")
.tools(List.of(semanticDeleteTool))
.build();
extraToolSections.add(semanticDeleteToolSection);
}
return extraToolSections;
//@formatter:on
}
}