]> gerrit.simantics Code Review - simantics/district.git/blob - ui/function/Functions.java
Hide "enabled" column for non-component type tech type tables
[simantics/district.git] / 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 = Collections.emptyList();//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 Function1<Resource, Double> hasDiameterValue(ReadGraph graph, Resource resource, Variable context) throws DatabaseException {
522         return directPropertyValueFunction(DistrictNetworkResource.getInstance(graph).Edge_HasDiameter, 0);
523     }
524
525     @SCLValue(type = "ReadGraph -> Resource -> Variable -> b")
526     public static Function1<Resource, Double> hasNominalMassFlowValue(ReadGraph graph, Resource resource, Variable context) throws DatabaseException {
527         return directPropertyValueFunction(DistrictNetworkResource.getInstance(graph).Edge_HasNominalMassFlow, 0);
528     }
529
530     @SCLValue(type = "ReadGraph -> Resource -> Variable -> b")
531     public static Function1<Resource, Double> hasNominalSupplyPressure(ReadGraph graph, Resource resource, Variable context) throws DatabaseException {
532         return directPropertyValueFunction(DistrictNetworkResource.getInstance(graph).Vertex_HasSupplyPressure, 0);
533     }
534
535     @SCLValue(type = "ReadGraph -> Resource -> Variable -> b")
536     public static Function1<Resource, Double> hasElevation(ReadGraph graph, Resource resource, Variable context) throws DatabaseException {
537         return directPropertyValueFunction(DistrictNetworkResource.getInstance(graph).Vertex_HasElevation, 0);
538     }
539
540     private static final Function1<Resource, Double> ONE = new FunctionImpl1<Resource, Double>() {
541         private final Double ONE = 1.0;
542         @Override
543         public Double apply(Resource edge) {
544             return ONE;
545         }
546         @Override
547         public String toString() {
548             return "1";
549         }
550     };
551
552     @SCLValue(type = "ReadGraph -> Resource -> Variable -> b")
553     public static Function1<Resource, Double> constantOne(ReadGraph graph, Resource resource, Variable context) throws DatabaseException {
554         return ONE;
555     }
556
557     private static Function1<Resource, Double> directPropertyValueFunction(Resource property, double defaultValue) throws DatabaseException {
558         Double def = defaultValue;
559         return new FunctionImpl1<Resource, Double>() {
560             @Override
561             public Double apply(Resource edge) {
562                 ReadGraph graph = (ReadGraph) SCLContext.getCurrent().get("graph");
563                 try {
564                     Double d = graph.getPossibleRelatedValue(edge, property, Bindings.DOUBLE);
565                     return d != null ? d : def;
566                 } catch (DatabaseException e) {
567                     LOGGER.error("Failed to evaluate property value", e);
568                     return def;
569                 }
570             }
571         };
572     }
573
574     private static class RangeValidator implements Function1<String, String> {
575         private double min;
576         private double max;
577         public RangeValidator(double min, double max) {
578             this.min = min;
579             this.max = max;
580         }
581         @Override
582         public String apply(String s) {
583             try {
584                 double d = Double.parseDouble(s);
585                 if (d < min)
586                     return "Value must be greater than or equal to " + min;
587                 if (d > max)
588                     return "Value must be less than or equal to " + max;
589                 return null;
590             } catch (NumberFormatException e) {
591                 return "Specified value is not a number";
592             }
593         }
594     }
595
596     @SCLValue(type = "ReadGraph -> Resource -> Variable -> b")
597     public static Object hueValidator(ReadGraph graph, Resource r, Variable context) throws DatabaseException {
598         return new RangeValidator(0, 360);
599     }
600
601     @SCLValue(type = "ReadGraph -> Resource -> Variable -> b")
602     public static Object saturationValidator(ReadGraph graph, Resource r, Variable context) throws DatabaseException {
603         return new RangeValidator(0, 100);
604     }
605
606     @SCLValue(type = "ReadGraph -> Resource -> Variable -> b")
607     public static Object brightnessValidator(ReadGraph graph, Resource r, Variable context) throws DatabaseException {
608         String importEntry = null;
609         Resource root = Variables.getPossibleIndexRoot(graph, context);
610         if (root != null) {
611             Resource sclmain = Layer0Utils.getPossibleChild(graph, root, "SCLMain");
612             if (sclmain != null) {
613                 importEntry = graph.getPossibleURI(sclmain);
614             }
615         }
616         SCLContext ctx = SCLContext.getCurrent();
617         Object oldGraph = ctx.put("graph", graph);
618         try {
619             return new BrightnessExpressionValidator(
620                     importEntry != null
621                     ? Arrays.asList(importEntry)
622                     : Collections.emptyList());
623         } finally {
624             ctx.put("graph", oldGraph);
625         }
626     }
627
628     private static class BrightnessExpressionValidator implements Function1<String, String> {
629         private CommandSession session;
630
631         public BrightnessExpressionValidator(List<String> importEntries) {
632             this.session = new CommandSession(SCLOsgi.MODULE_REPOSITORY, SCLReportingHandler.DEFAULT);
633             this.session.setImportEntries(imports(importEntries));
634         }
635
636         private ArrayList<CommandSessionImportEntry> imports(List<String> entries) {
637             ArrayList<CommandSessionImportEntry> result = new ArrayList<>();
638             entries.stream().map(CommandSessionImportEntry::new).forEach(result::add);
639             if (entries.isEmpty())
640                 result.add(new CommandSessionImportEntry("Simantics/District/SCLMain"));
641             return result;
642         }
643
644         @Override
645         public String apply(String s) {
646             s = s.trim();
647             if (!s.startsWith("="))
648                 return "Expression expected, must start with '='";
649             CompilationError[] errors = session.validate(s.substring(1));
650             if(errors.length == 0)
651                 return null;
652             return errors[0].description;
653         }
654     }
655
656 }