]> gerrit.simantics Code Review - simantics/district.git/blob - org.simantics.district.network.ui/src/org/simantics/district/network/ui/function/Functions.java
cc05a173b739e2b6d33fe5b823e3f1fa513c236b
[simantics/district.git] / org.simantics.district.network.ui / src / org / simantics / district / network / ui / function / Functions.java
1 package org.simantics.district.network.ui.function;
2
3 import java.util.ArrayList;
4 import java.util.Arrays;
5 import java.util.Collection;
6 import java.util.Collections;
7 import java.util.HashMap;
8 import java.util.HashSet;
9 import java.util.List;
10 import java.util.Map;
11 import java.util.Set;
12 import java.util.stream.Collectors;
13
14 import org.eclipse.jface.dialogs.Dialog;
15 import org.eclipse.jface.layout.GridDataFactory;
16 import org.eclipse.swt.SWT;
17 import org.eclipse.swt.events.SelectionAdapter;
18 import org.eclipse.swt.events.SelectionEvent;
19 import org.eclipse.swt.layout.GridData;
20 import org.eclipse.swt.layout.GridLayout;
21 import org.eclipse.swt.widgets.Combo;
22 import org.eclipse.swt.widgets.Composite;
23 import org.eclipse.swt.widgets.Control;
24 import org.eclipse.swt.widgets.Group;
25 import org.eclipse.swt.widgets.Label;
26 import org.eclipse.swt.widgets.Shell;
27 import org.eclipse.ui.PlatformUI;
28 import org.eclipse.ui.dialogs.SelectionStatusDialog;
29 import org.simantics.NameLabelUtil;
30 import org.simantics.Simantics;
31 import org.simantics.browsing.ui.common.modifiers.EnumeratedValue;
32 import org.simantics.browsing.ui.common.modifiers.Enumeration;
33 import org.simantics.browsing.ui.graph.impl.GraphEnumerationModifier;
34 import org.simantics.databoard.Bindings;
35 import org.simantics.db.ReadGraph;
36 import org.simantics.db.Resource;
37 import org.simantics.db.Session;
38 import org.simantics.db.WriteGraph;
39 import org.simantics.db.common.request.IndexRoot;
40 import org.simantics.db.common.request.ObjectsWithType;
41 import org.simantics.db.common.request.ReadRequest;
42 import org.simantics.db.common.request.WriteRequest;
43 import org.simantics.db.common.request.WriteResultRequest;
44 import org.simantics.db.exception.DatabaseException;
45 import org.simantics.db.exception.RuntimeDatabaseException;
46 import org.simantics.db.exception.ServiceException;
47 import org.simantics.db.layer0.QueryIndexUtils;
48 import org.simantics.db.layer0.util.Layer0Utils;
49 import org.simantics.db.layer0.variable.Variable;
50 import org.simantics.db.layer0.variable.Variables;
51 import org.simantics.db.layer0.variable.Variables.Role;
52 import org.simantics.db.procedure.Procedure;
53 import org.simantics.district.network.DistrictNetworkUtil;
54 import org.simantics.district.network.ontology.DistrictNetworkResource;
55 import org.simantics.layer0.Layer0;
56 import org.simantics.modeling.ModelingResources;
57 import org.simantics.modeling.adapters.NewCompositeActionFactory;
58 import org.simantics.modeling.typicals.TypicalUtil;
59 import org.simantics.operation.Layer0X;
60 import org.simantics.scl.compiler.commands.CommandSession;
61 import org.simantics.scl.compiler.commands.CommandSessionImportEntry;
62 import org.simantics.scl.compiler.errors.CompilationError;
63 import org.simantics.scl.osgi.SCLOsgi;
64 import org.simantics.scl.reflection.annotations.SCLValue;
65 import org.simantics.scl.runtime.SCLContext;
66 import org.simantics.scl.runtime.function.Function1;
67 import org.simantics.scl.runtime.function.FunctionImpl1;
68 import org.simantics.scl.runtime.reporting.SCLReportingHandler;
69 import org.simantics.ui.workbench.action.DefaultActions;
70 import org.simantics.utils.ui.SWTUtils;
71 import org.slf4j.Logger;
72 import org.slf4j.LoggerFactory;
73
74 public class Functions {
75
76     private static final Logger LOGGER = LoggerFactory.getLogger(Functions.class);
77     
78     private Functions() {
79     }
80
81     private static class HasMappingEnumerationModifier extends GraphEnumerationModifier {
82
83         public HasMappingEnumerationModifier(Session session, Resource subject, Resource relation, Enumeration<Resource> enumeration, Resource value) {
84             super(session, subject, relation, enumeration, value);
85         }
86
87     }
88
89     @SCLValue(type = "ReadGraph -> Resource -> Variable -> b")
90     public static Object defaultEdgeMappingModifier(ReadGraph graph, Resource resource, final Variable context) throws DatabaseException {
91         Resource diagram = resolveElement(graph, context);
92         DistrictNetworkResource DN = DistrictNetworkResource.getInstance(graph);
93         return baseMappingModifier(graph, diagram, DN.EdgeDefaultMapping, DN.Mapping_EdgeMapping, context);
94     }
95     
96     @SCLValue(type = "ReadGraph -> Resource -> Variable -> b")
97     public static Object defaultVertexMappingModifier(ReadGraph graph, Resource resource, final Variable context) throws DatabaseException {
98         Resource diagram = resolveElement(graph, context);
99         DistrictNetworkResource DN = DistrictNetworkResource.getInstance(graph);
100         return baseMappingModifier(graph, diagram, DN.VertexDefaultMapping, DN.Mapping_VertexMapping, context);
101     }
102
103     @SCLValue(type = "ReadGraph -> Resource -> Variable -> b")
104     public static Object rightClickVertexMappingModifier(ReadGraph graph, Resource resource, final Variable context) throws DatabaseException {
105         Resource diagram = resolveElement(graph, context);
106         DistrictNetworkResource DN = DistrictNetworkResource.getInstance(graph);
107         return baseMappingModifier(graph, diagram, DN.RightClickDefaultMapping, DN.Mapping_VertexMapping, context);
108     }
109
110     @SCLValue(type = "ReadGraph -> Resource -> Variable -> b")
111     public static Object leftClickVertexMappingModifier(ReadGraph graph, Resource resource, final Variable context) throws DatabaseException {
112         Resource diagram = resolveElement(graph, context);
113         DistrictNetworkResource DN = DistrictNetworkResource.getInstance(graph);
114         return baseMappingModifier(graph, diagram, DN.LeftClickDefaultMapping, DN.Mapping_VertexMapping, context);
115     }
116
117     @SCLValue(type = "ReadGraph -> Resource -> Variable -> b")
118     public static Object mappingModifier(ReadGraph graph, Resource resource, final Variable context) throws DatabaseException {
119         
120         Resource element = resolveElement(graph, context);
121         Resource mappingType = resolveMappingType(graph, element);
122         return baseMappingModifier(graph, element, DistrictNetworkResource.getInstance(graph).HasMapping, mappingType, context);
123     }
124
125     public static Map<String, Resource> getVertexMappings(ReadGraph graph, Resource indexRoot) throws DatabaseException {
126         Map<String, Resource> second = getNetworkMappingsByType(graph, indexRoot, DistrictNetworkResource.getInstance(graph).Mapping_VertexMapping);
127         return second;
128     }
129
130     public static Map<String, Resource> getEdgeMappings(ReadGraph graph, Resource indexRoot) throws DatabaseException {
131         Map<String, Resource> second = getNetworkMappingsByType(graph, indexRoot, DistrictNetworkResource.getInstance(graph).Mapping_EdgeMapping);
132         return second;
133     }
134     
135     public static Map<String, Resource> getCRSs(ReadGraph graph, Resource resource) throws DatabaseException {
136         Map<String, Resource> result = getNetworkMappingsByType(graph, graph.sync(new IndexRoot(resource)), DistrictNetworkResource.getInstance(graph).SpatialRefSystem);
137         return result;
138         
139     }
140
141     public static Map<String, Resource> getNetworkMappingsByType(ReadGraph graph, Resource indexRoot, Resource mappingType) throws DatabaseException {
142         List<Resource> mappings = QueryIndexUtils.searchByType(graph, indexRoot, mappingType);
143         Map<String, Resource> result = new HashMap<>(mappings.size());
144         Layer0 L0 = Layer0.getInstance(graph);
145         mappings.forEach(mapping -> {
146             try {
147                 String name = graph.getRelatedValue2(mapping, L0.HasName);
148                 Resource existing = result.put(name, mapping);
149                 if (existing != null) {
150                     LOGGER.warn("Duplicate mapping name! {} {} and existing is {}", name, mapping, existing);
151                 }
152             } catch (DatabaseException e) {
153                 e.printStackTrace();
154             }
155         });
156         return result;
157     }
158     
159     private static Object baseMappingModifier(ReadGraph graph, Resource element, Resource property, Resource mappingType, Variable context) throws DatabaseException {
160         Resource indexRoot = graph.sync(new IndexRoot(element));
161         List<Resource> mappings = QueryIndexUtils.searchByType(graph, indexRoot, mappingType);
162         Enumeration<Resource> enums = Enumeration
163                 .make(mappings.stream().map(m -> createEnumeratedValue(graph, m)).collect(Collectors.toList()));
164         
165         Resource currentMapping = graph.getSingleObject(element, property);
166         
167         return new HasMappingEnumerationModifier(Simantics.getSession(), element, property, enums, currentMapping);
168     }
169     
170     private static Resource resolveMappingType(ReadGraph graph, Resource element) throws DatabaseException {
171         DistrictNetworkResource DN = DistrictNetworkResource.getInstance(graph);
172         if (graph.isInstanceOf(element, DN.Edge))
173             return DN.Mapping_EdgeMapping;
174         else if (graph.isInstanceOf(element, DN.Vertex))
175             return DN.Mapping_VertexMapping;
176         throw new IllegalStateException("No mapping type found for element " + element + " : " + graph.getPossibleURI(element));
177     }
178
179     private static Resource resolveElement(ReadGraph graph, Variable variable) throws DatabaseException {
180         Role role = variable.getPossibleRole(graph);
181         if (role.equals(Role.PROPERTY))
182             return resolveElement(graph, variable.getParent(graph));
183         else
184             return variable.getRepresents(graph);
185     }
186
187     private static EnumeratedValue<Resource> createEnumeratedValue(ReadGraph graph, Resource resource) {
188         try {
189             String label = NameLabelUtil.modalName(graph, resource);
190             return new EnumeratedValue<Resource>(label, resource);
191         } catch (DatabaseException e) {
192             throw new RuntimeDatabaseException(e);
193         }
194     }
195     
196     @SCLValue(type = "ReadGraph -> Resource -> a -> b")
197     public static Object enumerationValues(ReadGraph graph, Resource resource, Object context) throws DatabaseException {
198         Variable var = (Variable) context;
199         return Collections.emptyList();
200     }
201     
202     @SCLValue(type = "ReadGraph -> Resource -> a -> b")
203     public static Object convertToValue(ReadGraph graph, Resource resource, Object context) throws DatabaseException {
204         Layer0 L0 = Layer0.getInstance(graph);
205         String label = graph.getPossibleRelatedValue2(resource, L0.HasLabel, Bindings.STRING);
206         if (label == null)
207             label = graph.getRelatedValue(resource, L0.HasName, Bindings.STRING);
208         return label;
209     }
210     
211     
212     @SCLValue(type = "Resource -> String -> Resource -> Resource")
213     public static Resource compositeInstantiator(final Resource compositeType, final String defaultName, final Resource target) throws DatabaseException {
214         
215         return TypicalUtil.syncExec(procedure -> {
216             if (!SWTUtils.asyncExec(PlatformUI.getWorkbench().getDisplay(), () -> {
217                 try {
218                     queryInitialValuesAndCreateComposite(compositeType, target, defaultName, procedure);
219                 } catch (Throwable t) {
220                     procedure.exception(t);
221                 }
222             })) {
223                 procedure.execute(null);
224             }
225         });
226     }
227
228     private static class DefaultMappingsDialog extends SelectionStatusDialog {
229
230         private Combo vertexMappingCombo;
231         private Combo rightClickMappingCombo;
232         private Combo leftClickMappingCombo;
233         private Combo edgeMappingCombo;
234         //private Combo crsCombo;
235         private Composite composite;
236         
237         private Resource configuration;
238         private Map<String, Resource> vertexMappings = new HashMap<>();
239         private Map<String, Resource> edgeMappings = new HashMap<>();
240         private Map<String, Resource> composites = new HashMap<>();
241         private Map<String, Resource> crss = new HashMap<>();
242         
243         private Resource defaultVertexMapping;
244         private Resource defaultEdgeMapping;
245         private Resource rightClickVertexMapping;
246         private Resource leftClickVertexMapping;
247         //private Resource defaultCRS;
248         
249         //private Combo compositeMappingCombo;
250         //private Combo componentMappingCombo;
251
252         protected DefaultMappingsDialog(Shell parentShell, Resource configuration) {
253             super(parentShell);
254             this.configuration = configuration;
255             setTitle("Select mappings for new DN diagram");
256         }
257
258         public Resource getDefaultVertexMapping() {
259             return defaultVertexMapping;
260         }
261
262                 public Resource getRightClickVertexMapping() {
263                         return rightClickVertexMapping;
264                 }
265
266                 public Resource getLeftClickVertexMapping() {
267                         return leftClickVertexMapping;
268                 }
269
270         public Resource getDefaultEdgeMapping() {
271             return defaultEdgeMapping;
272         }
273
274         @Override
275         protected Control createDialogArea(Composite parent) {
276             composite = (Composite) super.createDialogArea(parent);
277             
278             createMappingsGroup(composite);
279             //createExistingCompositeGroup(composite);
280             //createCRSSettingsGroup(composite);
281             
282             // compute default values
283             Simantics.getSession().asyncRequest(new ReadRequest() {
284
285                 @Override
286                 public void run(ReadGraph graph) throws DatabaseException {
287                     Resource indexRoot = graph.sync(new IndexRoot(configuration));
288                     vertexMappings = getVertexMappings(graph, indexRoot);
289                     edgeMappings = getEdgeMappings(graph, indexRoot);
290                     
291                     composites = getComposites(graph, configuration);
292                   
293                     crss = getCRSs(graph, configuration);
294                     
295                     composite.getDisplay().asyncExec(() -> {
296                         
297                         vertexMappingCombo.setItems(vertexMappings.keySet().toArray(new String[vertexMappings.size()]));
298                         rightClickMappingCombo.setItems(vertexMappings.keySet().toArray(new String[vertexMappings.size()]));
299                         leftClickMappingCombo.setItems(vertexMappings.keySet().toArray(new String[vertexMappings.size()]));
300                         edgeMappingCombo.setItems(edgeMappings.keySet().toArray(new String[edgeMappings.size()]));
301                         
302                         //crsCombo.setItems(crss.keySet().toArray(new String[crss.size()]));
303                         
304                         //compositeMappingCombo.setItems(composites.keySet().toArray(new String[composites.size()]));
305                         vertexMappingCombo.select(0);
306                         rightClickMappingCombo.select(0);
307                         leftClickMappingCombo.select(0);
308                         edgeMappingCombo.select(0);
309                         
310                         //crsCombo.select(0);
311                         
312                         //if (!composites.isEmpty())
313                         //    compositeMappingCombo.select(0);
314                     }); 
315                     
316                 }
317             });
318             return composite;
319         }
320         
321         protected Map<String, Resource> getComposites(ReadGraph graph, Resource element) throws DatabaseException {
322             List<Resource> nonDistrictComposites = composites.values().stream().filter(comp -> {
323                 try {
324                     return !graph.isInstanceOf(comp, DistrictNetworkResource.getInstance(graph).Composite);
325                 } catch (ServiceException e1) {
326                     LOGGER.error("Could not check if composite " + comp + " is instanceOf DistrictNetwork.composite");
327                     return false;
328                 }
329             }).collect(Collectors.toList());
330             Map<String, Resource> result = new HashMap<>(nonDistrictComposites.size());
331             Layer0 L0 = Layer0.getInstance(graph);
332             nonDistrictComposites.forEach(mapping -> {
333                 try {
334                     String name = graph.getRelatedValue2(mapping, L0.HasName);
335                     result.put(name, mapping);
336                 } catch (DatabaseException e) {
337                     LOGGER.error("Could not read name of " + mapping, e);
338                 }
339             });
340             return result;
341         }
342
343         private void createMappingsGroup(Composite parent) {
344             Group group= new Group(parent, SWT.NONE);
345             group.setFont(parent.getFont());
346             group.setText("Default mappings");
347             GridDataFactory.fillDefaults().grab(true, false).applyTo(group);
348             group.setLayout(new GridLayout(1, false));
349             
350             Composite cmposite = new Composite(group, SWT.NONE);
351             cmposite.setLayoutData(new GridData(SWT.FILL, SWT.TOP, true, false));
352             cmposite.setLayout(new GridLayout(2, false));
353             
354             Label vertexMappingLabel = new Label(cmposite, SWT.NONE);
355             vertexMappingLabel.setText("Default vertex mapping");
356
357             vertexMappingCombo = new Combo(cmposite, SWT.READ_ONLY | SWT.BORDER);
358             GridDataFactory.fillDefaults().grab(true, false).applyTo(vertexMappingCombo);
359
360             Label rightClickMappingLabel = new Label(cmposite, SWT.NONE);
361             rightClickMappingLabel.setText("Default right click mapping");
362
363             rightClickMappingCombo = new Combo(cmposite, SWT.READ_ONLY | SWT.BORDER);
364             GridDataFactory.fillDefaults().grab(true, false).applyTo(vertexMappingCombo);
365
366             Label leftClickMappingLabel = new Label(cmposite, SWT.NONE);
367             leftClickMappingLabel.setText("Default left click mapping");
368
369             leftClickMappingCombo = new Combo(cmposite, SWT.READ_ONLY | SWT.BORDER);
370             GridDataFactory.fillDefaults().grab(true, false).applyTo(vertexMappingCombo);
371
372             Label edgeMappingLabel = new Label(cmposite, SWT.NONE);
373             edgeMappingLabel.setText("Default edge mapping");
374
375             edgeMappingCombo = new Combo(cmposite, SWT.READ_ONLY | SWT.BORDER);
376             GridDataFactory.fillDefaults().grab(true, false).applyTo(edgeMappingCombo);
377         }
378         
379         private void createExistingCompositeGroup(Composite parent) {
380             Group group= new Group(parent, SWT.NONE);
381             group.setFont(parent.getFont());
382             group.setText("Mapped composite");
383             GridDataFactory.fillDefaults().grab(true, false).applyTo(group);
384             group.setLayout(new GridLayout(1, false));
385             
386             Composite cmposite = new Composite(group, SWT.NONE);
387             cmposite.setLayoutData(new GridData(SWT.FILL, SWT.TOP, true, false));
388             cmposite.setLayout(new GridLayout(2, false));
389             
390             Label compositeMappingLabel = new Label(cmposite, SWT.NONE);
391             compositeMappingLabel.setText("Select composite");
392
393 //            compositeMappingCombo = new Combo(cmposite, SWT.READ_ONLY | SWT.BORDER);
394 //            GridDataFactory.fillDefaults().grab(true, false).applyTo(compositeMappingCombo);
395 //            compositeMappingCombo.addSelectionListener(new SelectionAdapter() {
396 //                
397 //                @Override
398 //                public void widgetSelected(SelectionEvent e) {
399 //                    super.widgetSelected(e);
400 //                    recalculateMappapleComponents();
401 //                }
402 //            });
403             
404 //            Label compojnentMappingLabel = new Label(cmposite, SWT.NONE);
405 //            compojnentMappingLabel.setText("Select component");
406 //            
407 //            componentMappingCombo = new Combo(cmposite, SWT.READ_ONLY | SWT.BORDER);
408 //            GridDataFactory.fillDefaults().grab(true, false).applyTo(componentMappingCombo);
409         }
410         
411         protected void recalculateMappapleComponents() {
412             Simantics.getSession().asyncRequest(new ReadRequest() {
413                 
414                 @Override
415                 public void run(ReadGraph graph) throws DatabaseException {
416                     
417                     
418                     composite.getDisplay().asyncExec(() -> {
419                         
420                     }); 
421                 }
422             });
423         }
424
425         private void createCRSSettingsGroup(Composite parent) {
426             Group group= new Group(parent, SWT.NONE);
427             group.setFont(parent.getFont());
428             group.setText("CRS settings");
429             GridDataFactory.fillDefaults().grab(true, false).applyTo(group);
430             group.setLayout(new GridLayout(1, false));
431             
432             Composite cmposite = new Composite(group, SWT.NONE);
433             cmposite.setLayoutData(new GridData(SWT.FILL, SWT.TOP, true, false));
434             cmposite.setLayout(new GridLayout(2, false));
435             
436             Label vertexMappingLabel = new Label(cmposite, SWT.NONE);
437             vertexMappingLabel.setText("Default CRS");
438
439             //crsCombo = new Combo(cmposite, SWT.READ_ONLY | SWT.BORDER);
440             //GridData textData = new GridData(SWT.FILL, SWT.CENTER, true, false);
441             //crsCombo.setLayoutData(textData);
442         }
443         
444
445         @Override
446         protected void computeResult() {
447             defaultVertexMapping = vertexMappings.get(vertexMappingCombo.getItem(vertexMappingCombo.getSelectionIndex()));
448             defaultEdgeMapping = edgeMappings.get(edgeMappingCombo.getItem(edgeMappingCombo.getSelectionIndex()));
449             rightClickVertexMapping = vertexMappings.get(rightClickMappingCombo.getItem(rightClickMappingCombo.getSelectionIndex()));
450             leftClickVertexMapping = vertexMappings.get(leftClickMappingCombo.getItem(leftClickMappingCombo.getSelectionIndex()));
451             //defaultCRS = crss.get(crsCombo.getItem(crsCombo.getSelectionIndex()));
452         }
453
454         public Resource getCRS() {
455             return crss.get("EPSG_4326"); // this is only supported
456         }
457         
458     }
459     
460     private static void queryInitialValuesAndCreateComposite(final Resource compositeType, final Resource target,
461             String defaultName, final Procedure<Resource> procedure) {
462         DefaultMappingsDialog dialog = new DefaultMappingsDialog(PlatformUI.getWorkbench().getActiveWorkbenchWindow().getShell(), target);
463
464         if (dialog.open() != Dialog.OK) {
465             procedure.execute(null);
466             return;
467         }
468         Simantics.getSession().asyncRequest(new WriteResultRequest<Resource>() {
469
470             @Override
471             public Resource perform(WriteGraph graph) throws DatabaseException {
472                 return DistrictNetworkUtil.createNetworkDiagram(graph, target, compositeType, defaultName, 
473                         dialog.getDefaultEdgeMapping(),
474                         dialog.getDefaultVertexMapping(),
475                         dialog.getRightClickVertexMapping(),
476                         dialog.getLeftClickVertexMapping(),
477                         dialog.getCRS()
478                     );
479             }
480             
481
482         }, new Procedure<Resource>() {
483
484             @Override
485             public void execute(Resource composite) {
486                 DefaultActions.asyncPerformDefaultAction(Simantics.getSession(), composite, false, false, true);
487                 procedure.execute(composite);
488             }
489
490             @Override
491             public void exception(Throwable t) {
492                 LOGGER.error("Failed to create composite, see exception for details.", t);
493                 procedure.exception(t);
494             }
495         });
496     }
497
498     public static Collection<Resource> getDistrictDiagrams(ReadGraph graph) throws DatabaseException {
499         Layer0 L0 = Layer0.getInstance(graph);
500         Collection<Resource> indexRoots = graph.sync(new ObjectsWithType(Simantics.getProjectResource(), L0.ConsistsOf, L0.IndexRoot));
501         DistrictNetworkResource DN = DistrictNetworkResource.getInstance(graph);
502         Set<Resource> results = new HashSet<>();
503         for (Resource indexRoot : indexRoots) {
504             Collection<Resource> diagrams = QueryIndexUtils.searchByType(graph, indexRoot, DN.Diagram);
505             results.addAll(diagrams);
506         }
507         return results;
508     }
509
510     private static List<String> listInstanceNames(ReadGraph graph, Variable context, Resource type) throws DatabaseException {
511         Resource indexRoot = Variables.getIndexRoot(graph, context);
512         DistrictNetworkResource DN = DistrictNetworkResource.getInstance(graph);
513         List<Resource> properties = QueryIndexUtils.searchByType(graph, indexRoot, DN.Vertex_ScaleProperty);
514         return properties.stream()
515                 .map(m -> createEnumeratedValue(graph, m))
516                 .map(EnumeratedValue::getName)
517                 .collect(Collectors.toList());
518     }
519
520     @SCLValue(type = "ReadGraph -> Resource -> Variable -> b")
521     public static Object edgeThicknessPropertyEnumerationValues(ReadGraph graph, Resource resource, Variable context) throws DatabaseException {
522         DistrictNetworkResource DN = DistrictNetworkResource.getInstance(graph);
523         return listInstanceNames(graph, context, DN.Edge_ThicknessProperty);
524     }
525     
526     @SCLValue(type = "ReadGraph -> Resource -> Variable -> b")
527     public static Object arrowLengthPropertyEnumerationValues(ReadGraph graph, Resource resource, Variable context) throws DatabaseException {
528         DistrictNetworkResource DN = DistrictNetworkResource.getInstance(graph);
529         return listInstanceNames(graph, context, DN.Edge_ArrowLengthProperty);
530     }
531     
532     @SCLValue(type = "ReadGraph -> Resource -> Variable -> b")
533     public static Object nodeScalePropertyEnumerationValues(ReadGraph graph, Resource resource, Variable context) throws DatabaseException {
534         DistrictNetworkResource DN = DistrictNetworkResource.getInstance(graph);
535         return listInstanceNames(graph, context, DN.Vertex_ScaleProperty);
536     }
537
538     @SCLValue(type = "ReadGraph -> Resource -> Variable -> b")
539     public static Object edgeThicknessPropertyModifier(ReadGraph graph, Resource resource, Variable context) throws DatabaseException {
540         Resource diagram = resolveElement(graph, context);
541         DistrictNetworkResource DN = DistrictNetworkResource.getInstance(graph);
542         return baseMappingModifier(graph, diagram, DN.Diagram_edgeThicknessProperty, DN.Edge_ThicknessProperty, context);
543     }
544
545     @SCLValue(type = "ReadGraph -> Resource -> Variable -> b")
546     public static Object arrowLengthPropertyModifier(ReadGraph graph, Resource resource, Variable context) throws DatabaseException {
547         Resource diagram = resolveElement(graph, context);
548         DistrictNetworkResource DN = DistrictNetworkResource.getInstance(graph);
549         return baseMappingModifier(graph, diagram, DN.Diagram_arrowLengthProperty, DN.Edge_ArrowLengthProperty, context);
550     }
551     
552     @SCLValue(type = "ReadGraph -> Resource -> Variable -> b")
553     public static Object nodeScalePropertyModifier(ReadGraph graph, Resource resource, Variable context) throws DatabaseException {
554         Resource diagram = resolveElement(graph, context);
555         DistrictNetworkResource DN = DistrictNetworkResource.getInstance(graph);
556         return baseMappingModifier(graph, diagram, DN.Diagram_nodeScaleProperty, DN.Vertex_ScaleProperty, context);
557     }
558
559     @SCLValue(type = "ReadGraph -> Resource -> Variable -> b")
560     public static Function1<Resource, Double> hasDiameterValue(ReadGraph graph, Resource resource, Variable context) throws DatabaseException {
561         return directPropertyValueFunction(DistrictNetworkResource.getInstance(graph).Edge_HasDiameter, 0);
562     }
563
564     @SCLValue(type = "ReadGraph -> Resource -> Variable -> b")
565     public static Function1<Resource, Double> hasNominalMassFlowValue(ReadGraph graph, Resource resource, Variable context) throws DatabaseException {
566         return directPropertyValueFunction(DistrictNetworkResource.getInstance(graph).Edge_HasNominalMassFlow, 0);
567     }
568
569     @SCLValue(type = "ReadGraph -> Resource -> Variable -> b")
570     public static Function1<Resource, Double> hasNominalSupplyPressure(ReadGraph graph, Resource resource, Variable context) throws DatabaseException {
571         return directPropertyValueFunction(DistrictNetworkResource.getInstance(graph).Vertex_HasSupplyPressure, 0);
572     }
573
574     @SCLValue(type = "ReadGraph -> Resource -> Variable -> b")
575     public static Function1<Resource, Double> hasElevation(ReadGraph graph, Resource resource, Variable context) throws DatabaseException {
576         return directPropertyValueFunction(DistrictNetworkResource.getInstance(graph).Vertex_HasElevation, 0);
577     }
578
579     private static final Function1<Resource, Double> ONE = new FunctionImpl1<Resource, Double>() {
580         private final Double ONE = 1.0;
581         @Override
582         public Double apply(Resource edge) {
583             return ONE;
584         }
585         @Override
586         public String toString() {
587             return "1";
588         }
589     };
590
591     @SCLValue(type = "ReadGraph -> Resource -> Variable -> b")
592     public static Function1<Resource, Double> constantOne(ReadGraph graph, Resource resource, Variable context) throws DatabaseException {
593         return ONE;
594     }
595
596     private static Function1<Resource, Double> directPropertyValueFunction(Resource property, double defaultValue) throws DatabaseException {
597         Double def = defaultValue;
598         return new FunctionImpl1<Resource, Double>() {
599             @Override
600             public Double apply(Resource edge) {
601                 ReadGraph graph = (ReadGraph) SCLContext.getCurrent().get("graph");
602                 try {
603                     Double d = graph.getPossibleRelatedValue(edge, property, Bindings.DOUBLE);
604                     return d != null ? d : def;
605                 } catch (DatabaseException e) {
606                     LOGGER.error("Failed to evaluate property value", e);
607                     return def;
608                 }
609             }
610         };
611     }
612
613     private static class RangeValidator implements Function1<String, String> {
614         private double min;
615         private double max;
616         public RangeValidator(double min, double max) {
617             this.min = min;
618             this.max = max;
619         }
620         @Override
621         public String apply(String s) {
622             try {
623                 double d = Double.parseDouble(s);
624                 if (d < min)
625                     return "Value must be greater than or equal to " + min;
626                 if (d > max)
627                     return "Value must be less than or equal to " + max;
628                 return null;
629             } catch (NumberFormatException e) {
630                 return "Specified value is not a number";
631             }
632         }
633     }
634
635     @SCLValue(type = "ReadGraph -> Resource -> Variable -> b")
636     public static Object hueValidator(ReadGraph graph, Resource r, Variable context) throws DatabaseException {
637         return new RangeValidator(0, 360);
638     }
639
640     @SCLValue(type = "ReadGraph -> Resource -> Variable -> b")
641     public static Object saturationValidator(ReadGraph graph, Resource r, Variable context) throws DatabaseException {
642         return new RangeValidator(0, 100);
643     }
644
645     @SCLValue(type = "ReadGraph -> Resource -> Variable -> b")
646     public static Object brightnessValidator(ReadGraph graph, Resource r, Variable context) throws DatabaseException {
647         String importEntry = null;
648         Resource root = Variables.getPossibleIndexRoot(graph, context);
649         if (root != null) {
650             Resource sclmain = Layer0Utils.getPossibleChild(graph, root, "SCLMain");
651             if (sclmain != null) {
652                 importEntry = graph.getPossibleURI(sclmain);
653             }
654         }
655         SCLContext ctx = SCLContext.getCurrent();
656         Object oldGraph = ctx.put("graph", graph);
657         try {
658             return new BrightnessExpressionValidator(
659                     importEntry != null
660                     ? Arrays.asList(importEntry)
661                     : Collections.emptyList());
662         } finally {
663             ctx.put("graph", oldGraph);
664         }
665     }
666
667     private static class BrightnessExpressionValidator implements Function1<String, String> {
668         private CommandSession session;
669
670         public BrightnessExpressionValidator(List<String> importEntries) {
671             this.session = new CommandSession(SCLOsgi.MODULE_REPOSITORY, SCLReportingHandler.DEFAULT);
672             this.session.setImportEntries(imports(importEntries));
673         }
674
675         private ArrayList<CommandSessionImportEntry> imports(List<String> entries) {
676             ArrayList<CommandSessionImportEntry> result = new ArrayList<>();
677             entries.stream().map(CommandSessionImportEntry::new).forEach(result::add);
678             if (entries.isEmpty())
679                 result.add(new CommandSessionImportEntry("Simantics/District/SCLMain"));
680             return result;
681         }
682
683         @Override
684         public String apply(String s) {
685             s = s.trim();
686             if (!s.startsWith("="))
687                 return "Expression expected, must start with '='";
688             CompilationError[] errors = session.validate(s.substring(1));
689             if(errors.length == 0)
690                 return null;
691             return errors[0].description;
692         }
693     }
694
695 }