1 package org.simantics.district.network.ui.function;
3 import java.util.Collections;
4 import java.util.HashMap;
7 import java.util.stream.Collectors;
9 import org.eclipse.jface.dialogs.Dialog;
10 import org.eclipse.jface.layout.GridDataFactory;
11 import org.eclipse.swt.SWT;
12 import org.eclipse.swt.events.SelectionAdapter;
13 import org.eclipse.swt.events.SelectionEvent;
14 import org.eclipse.swt.layout.GridData;
15 import org.eclipse.swt.layout.GridLayout;
16 import org.eclipse.swt.widgets.Combo;
17 import org.eclipse.swt.widgets.Composite;
18 import org.eclipse.swt.widgets.Control;
19 import org.eclipse.swt.widgets.Group;
20 import org.eclipse.swt.widgets.Label;
21 import org.eclipse.swt.widgets.Shell;
22 import org.eclipse.ui.PlatformUI;
23 import org.eclipse.ui.dialogs.SelectionStatusDialog;
24 import org.simantics.NameLabelUtil;
25 import org.simantics.Simantics;
26 import org.simantics.browsing.ui.common.modifiers.EnumeratedValue;
27 import org.simantics.browsing.ui.common.modifiers.Enumeration;
28 import org.simantics.browsing.ui.graph.impl.GraphEnumerationModifier;
29 import org.simantics.databoard.Bindings;
30 import org.simantics.db.ReadGraph;
31 import org.simantics.db.Resource;
32 import org.simantics.db.Session;
33 import org.simantics.db.WriteGraph;
34 import org.simantics.db.common.request.IndexRoot;
35 import org.simantics.db.common.request.ReadRequest;
36 import org.simantics.db.common.request.WriteRequest;
37 import org.simantics.db.exception.DatabaseException;
38 import org.simantics.db.exception.RuntimeDatabaseException;
39 import org.simantics.db.exception.ServiceException;
40 import org.simantics.db.layer0.variable.Variable;
41 import org.simantics.db.layer0.variable.Variables.Role;
42 import org.simantics.db.procedure.Procedure;
43 import org.simantics.district.network.ontology.DistrictNetworkResource;
44 import org.simantics.layer0.Layer0;
45 import org.simantics.modeling.ModelingResources;
46 import org.simantics.modeling.ModelingUtils;
47 import org.simantics.modeling.adapters.NewCompositeActionFactory;
48 import org.simantics.modeling.typicals.TypicalUtil;
49 import org.simantics.scl.reflection.annotations.SCLValue;
50 import org.simantics.structural.stubs.StructuralResource2;
51 import org.simantics.ui.workbench.action.DefaultActions;
52 import org.simantics.utils.ui.SWTUtils;
53 import org.slf4j.Logger;
54 import org.slf4j.LoggerFactory;
56 public class Functions {
58 private static final Logger LOGGER = LoggerFactory.getLogger(Functions.class);
63 private static class HasMappingEnumerationModifier extends GraphEnumerationModifier {
65 public HasMappingEnumerationModifier(Session session, Resource subject, Resource relation, Enumeration<Resource> enumeration, Resource value) {
66 super(session, subject, relation, enumeration, value);
71 @SCLValue(type = "ReadGraph -> Resource -> Variable -> b")
72 public static Object defaultEdgeMappingModifier(ReadGraph graph, Resource resource, final Variable context) throws DatabaseException {
73 Resource diagram = resolveElement(graph, context);
74 DistrictNetworkResource DN = DistrictNetworkResource.getInstance(graph);
75 return baseMappingModifier(graph, diagram, DN.EdgeDefaultMapping, DN.Mapping_EdgeMapping, context);
78 @SCLValue(type = "ReadGraph -> Resource -> Variable -> b")
79 public static Object defaultVertexMappingModifier(ReadGraph graph, Resource resource, final Variable context) throws DatabaseException {
80 System.out.println(graph.getURI(resource));
81 System.out.println(context.getURI(graph));
83 Resource diagram = resolveElement(graph, context);
84 DistrictNetworkResource DN = DistrictNetworkResource.getInstance(graph);
85 return baseMappingModifier(graph, diagram, DN.VertexDefaultMapping, DN.Mapping_VertexMapping, context);
88 @SCLValue(type = "ReadGraph -> Resource -> Variable -> b")
89 public static Object mappingModifier(ReadGraph graph, Resource resource, final Variable context) throws DatabaseException {
91 Resource element = resolveElement(graph, context);
92 Resource mappingType = resolveMappingType(graph, element);
93 return baseMappingModifier(graph, element, DistrictNetworkResource.getInstance(graph).HasMapping, mappingType, context);
96 public static Map<String, Resource> getVertexMappings(ReadGraph graph, Resource resource) throws DatabaseException {
97 return getNetworkMappingsByType(graph, resource , DistrictNetworkResource.getInstance(graph).Mapping_VertexMapping);
100 public static Map<String, Resource> getEdgeMappings(ReadGraph graph, Resource resource) throws DatabaseException {
101 return getNetworkMappingsByType(graph, resource , DistrictNetworkResource.getInstance(graph).Mapping_EdgeMapping);
104 public static Map<String, Resource> getNetworkMappingsByType(ReadGraph graph, Resource element, Resource mappingType) throws DatabaseException {
105 Resource indexRoot = graph.sync(new IndexRoot(element));
106 List<Resource> mappings = ModelingUtils.searchByType(graph, indexRoot, mappingType);
107 Map<String, Resource> result = new HashMap<>(mappings.size());
108 Layer0 L0 = Layer0.getInstance(graph);
109 mappings.forEach(mapping -> {
111 String name = graph.getRelatedValue2(mapping, L0.HasName);
112 result.put(name, mapping);
113 } catch (DatabaseException e) {
120 private static Object baseMappingModifier(ReadGraph graph, Resource element, Resource property, Resource mappingType, Variable context) throws DatabaseException {
121 Resource indexRoot = graph.sync(new IndexRoot(element));
122 List<Resource> mappings = ModelingUtils.searchByType(graph, indexRoot, mappingType);
123 Enumeration<Resource> enums = Enumeration
124 .make(mappings.stream().map(m -> createEnumeratedValue(graph, m)).collect(Collectors.toList()));
126 Resource currentMapping = graph.getSingleObject(element, property);
128 return new HasMappingEnumerationModifier(Simantics.getSession(), element, property, enums, currentMapping);
131 private static Resource resolveMappingType(ReadGraph graph, Resource element) throws DatabaseException {
132 DistrictNetworkResource DN = DistrictNetworkResource.getInstance(graph);
133 if (graph.isInstanceOf(element, DN.Edge))
134 return DN.Mapping_EdgeMapping;
135 else if (graph.isInstanceOf(element, DN.Vertex))
136 return DN.Mapping_VertexMapping;
137 throw new IllegalStateException("No mapping type found for element " + element + " : " + graph.getPossibleURI(element));
140 private static Resource resolveElement(ReadGraph graph, Variable variable) throws DatabaseException {
141 Role role = variable.getPossibleRole(graph);
142 if (role.equals(Role.PROPERTY))
143 return resolveElement(graph, variable.getParent(graph));
145 return variable.getRepresents(graph);
148 private static EnumeratedValue<Resource> createEnumeratedValue(ReadGraph graph, Resource resource) {
150 String label = NameLabelUtil.modalName(graph, resource);
151 return new EnumeratedValue<Resource>(label, resource);
152 } catch (DatabaseException e) {
153 throw new RuntimeDatabaseException(e);
157 @SCLValue(type = "ReadGraph -> Resource -> a -> b")
158 public static Object enumerationValues(ReadGraph graph, Resource resource, Object context) throws DatabaseException {
159 Variable var = (Variable) context;
160 System.out.println(graph.getURI(resource));
161 System.out.println(var.getURI(graph));
162 return Collections.emptyList();
165 @SCLValue(type = "ReadGraph -> Resource -> a -> b")
166 public static Object convertToValue(ReadGraph graph, Resource resource, Object context) throws DatabaseException {
167 return graph.getRelatedValue2(resource, Layer0.getInstance(graph).HasName, Bindings.STRING);
172 @SCLValue(type = "Resource -> String -> Resource -> Resource")
173 public static Resource compositeInstantiator(final Resource compositeType, final String defaultName, final Resource target) throws DatabaseException {
175 return TypicalUtil.syncExec(procedure -> {
176 if (!SWTUtils.asyncExec(PlatformUI.getWorkbench().getDisplay(), () -> {
178 queryInitialValuesAndCreateComposite(compositeType, target, defaultName, procedure);
179 } catch (Throwable t) {
180 procedure.exception(t);
183 procedure.execute(null);
188 private static class DefaultMappingsDialog extends SelectionStatusDialog {
190 private Combo vertexMappingCombo;
191 private Combo edgeMappingCombo;
192 private Combo crsCombo;
193 private Composite composite;
195 private Resource configuration;
196 private Map<String, Resource> vertexMappings = new HashMap<>();
197 private Map<String, Resource> edgeMappings = new HashMap<>();
198 private Map<String, Resource> composites = new HashMap<>();
199 private Map<String, Map<String, Resource>> components = new HashMap<>();
201 private Resource defaultVertexMapping;
202 private Resource defaultEdgeMapping;
203 private Combo compositeMappingCombo;
204 private Combo componentMappingCombo;
206 protected DefaultMappingsDialog(Shell parentShell, Resource configuration) {
208 this.configuration = configuration;
209 setTitle("Select mappings for new DN diagram");
212 public Resource getDefaultVertexMapping() {
213 return defaultVertexMapping;
216 public Resource getDefaultEdgeMapping() {
217 return defaultEdgeMapping;
221 protected Control createDialogArea(Composite parent) {
222 composite = (Composite) super.createDialogArea(parent);
224 createMappingsGroup(composite);
225 createExistingCompositeGroup(composite);
226 createCRSSettingsGroup(composite);
228 // compute default values
229 Simantics.getSession().asyncRequest(new ReadRequest() {
232 public void run(ReadGraph graph) throws DatabaseException {
234 vertexMappings = getVertexMappings(graph, configuration);
235 edgeMappings = getEdgeMappings(graph, configuration);
237 composites = getComposites(graph, configuration);
238 if (composites.size() > 0) {
239 components = getComponents(graph, composites.get(0));
243 composite.getDisplay().asyncExec(() -> {
245 vertexMappingCombo.setItems(vertexMappings.keySet().toArray(new String[vertexMappings.size()]));
246 edgeMappingCombo.setItems(edgeMappings.keySet().toArray(new String[edgeMappings.size()]));
247 compositeMappingCombo.setItems(composites.keySet().toArray(new String[composites.size()]));
248 vertexMappingCombo.select(0);
249 edgeMappingCombo.select(0);
250 if (!composites.isEmpty())
251 compositeMappingCombo.select(0);
259 protected Map<String, Map<String, Resource>> getComponents(ReadGraph graph, Resource resource) {
260 // TODO Auto-generated method stub
264 protected Map<String, Resource> getComposites(ReadGraph graph, Resource element) throws DatabaseException {
265 Resource indexRoot = graph.sync(new IndexRoot(element));
266 List<Resource> composites = ModelingUtils.searchByType(graph, indexRoot, StructuralResource2.getInstance(graph).Composite);
267 List<Resource> nonDistrictComposites = composites.stream().filter(comp -> {
269 return !graph.isInstanceOf(comp, DistrictNetworkResource.getInstance(graph).Composite);
270 } catch (ServiceException e1) {
271 LOGGER.error("Could not check if composite " + comp + " is instanceOf DistrictNetwork.composite");
274 }).collect(Collectors.toList());
275 Map<String, Resource> result = new HashMap<>(nonDistrictComposites.size());
276 Layer0 L0 = Layer0.getInstance(graph);
277 nonDistrictComposites.forEach(mapping -> {
279 String name = graph.getRelatedValue2(mapping, L0.HasName);
280 result.put(name, mapping);
281 } catch (DatabaseException e) {
288 private void createMappingsGroup(Composite parent) {
289 Group group= new Group(parent, SWT.NONE);
290 group.setFont(parent.getFont());
291 group.setText("Default mappings");
292 GridDataFactory.fillDefaults().grab(true, false).applyTo(group);
293 group.setLayout(new GridLayout(1, false));
295 Composite cmposite = new Composite(group, SWT.NONE);
296 cmposite.setLayoutData(new GridData(SWT.FILL, SWT.TOP, true, false));
297 cmposite.setLayout(new GridLayout(2, false));
299 Label vertexMappingLabel = new Label(cmposite, SWT.NONE);
300 vertexMappingLabel.setText("Default vertex mapping");
302 vertexMappingCombo = new Combo(cmposite, SWT.READ_ONLY | SWT.BORDER);
303 GridDataFactory.fillDefaults().grab(true, false).applyTo(vertexMappingCombo);
305 Label edgeMappingLabel = new Label(cmposite, SWT.NONE);
306 edgeMappingLabel.setText("Default edge mapping");
308 edgeMappingCombo = new Combo(cmposite, SWT.READ_ONLY | SWT.BORDER);
309 GridDataFactory.fillDefaults().grab(true, false).applyTo(edgeMappingCombo);
312 private void createExistingCompositeGroup(Composite parent) {
313 Group group= new Group(parent, SWT.NONE);
314 group.setFont(parent.getFont());
315 group.setText("Mapped composite");
316 GridDataFactory.fillDefaults().grab(true, false).applyTo(group);
317 group.setLayout(new GridLayout(1, false));
319 Composite cmposite = new Composite(group, SWT.NONE);
320 cmposite.setLayoutData(new GridData(SWT.FILL, SWT.TOP, true, false));
321 cmposite.setLayout(new GridLayout(2, false));
323 Label compositeMappingLabel = new Label(cmposite, SWT.NONE);
324 compositeMappingLabel.setText("Select composite");
326 compositeMappingCombo = new Combo(cmposite, SWT.READ_ONLY | SWT.BORDER);
327 GridDataFactory.fillDefaults().grab(true, false).applyTo(compositeMappingCombo);
328 compositeMappingCombo.addSelectionListener(new SelectionAdapter() {
331 public void widgetSelected(SelectionEvent e) {
332 super.widgetSelected(e);
333 recalculateMappapleComponents();
337 Label compojnentMappingLabel = new Label(cmposite, SWT.NONE);
338 compojnentMappingLabel.setText("Select component");
340 componentMappingCombo = new Combo(cmposite, SWT.READ_ONLY | SWT.BORDER);
341 GridDataFactory.fillDefaults().grab(true, false).applyTo(componentMappingCombo);
344 protected void recalculateMappapleComponents() {
345 Simantics.getSession().asyncRequest(new ReadRequest() {
348 public void run(ReadGraph graph) throws DatabaseException {
351 composite.getDisplay().asyncExec(() -> {
358 private void createCRSSettingsGroup(Composite parent) {
359 Group group= new Group(parent, SWT.NONE);
360 group.setFont(parent.getFont());
361 group.setText("CRS settings");
362 GridDataFactory.fillDefaults().grab(true, false).applyTo(group);
363 group.setLayout(new GridLayout(1, false));
365 Composite cmposite = new Composite(group, SWT.NONE);
366 cmposite.setLayoutData(new GridData(SWT.FILL, SWT.TOP, true, false));
367 cmposite.setLayout(new GridLayout(2, false));
369 Label vertexMappingLabel = new Label(cmposite, SWT.NONE);
370 vertexMappingLabel.setText("Default CRS");
372 crsCombo = new Combo(cmposite, SWT.READ_ONLY | SWT.BORDER);
373 GridData textData = new GridData(SWT.FILL, SWT.CENTER, true, false);
374 crsCombo.setLayoutData(textData);
379 protected void computeResult() {
380 defaultVertexMapping = vertexMappings.get(vertexMappingCombo.getItem(vertexMappingCombo.getSelectionIndex()));
381 defaultEdgeMapping = edgeMappings.get(edgeMappingCombo.getItem(edgeMappingCombo.getSelectionIndex()));
386 private static void queryInitialValuesAndCreateComposite(final Resource compositeType, final Resource target,
387 String defaultName, final Procedure<Resource> procedure) {
388 DefaultMappingsDialog dialog = new DefaultMappingsDialog(PlatformUI.getWorkbench().getActiveWorkbenchWindow().getShell(), target);
390 if (dialog.open() != Dialog.OK) {
391 procedure.execute(null);
394 Simantics.getSession().asyncRequest(
395 NewCompositeActionFactory.createCompositeRequest(target, defaultName, compositeType),
396 new Procedure<Resource>() {
398 public void execute(Resource composite) {
399 Simantics.getSession().asyncRequest(new WriteRequest() {
402 public void perform(WriteGraph graph) throws DatabaseException {
403 DistrictNetworkResource DN = DistrictNetworkResource.getInstance(graph);
404 Resource diagram = graph.getSingleObject(composite, ModelingResources.getInstance(graph).CompositeToDiagram);
405 graph.claim(diagram, DN.EdgeDefaultMapping, dialog.getDefaultEdgeMapping());
406 graph.claim(diagram, DN.VertexDefaultMapping, dialog.getDefaultVertexMapping());
409 DefaultActions.asyncPerformDefaultAction(Simantics.getSession(), composite, false, false, true);
410 procedure.execute(composite);
414 public void exception(Throwable t) {
415 LOGGER.error("Failed to create composite, see exception for details.", t);
416 procedure.exception(t);