1 package org.simantics.district.network.ui.function;
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;
12 import java.util.stream.Collectors;
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;
74 public class Functions {
76 private static final Logger LOGGER = LoggerFactory.getLogger(Functions.class);
81 private static class HasMappingEnumerationModifier extends GraphEnumerationModifier {
83 public HasMappingEnumerationModifier(Session session, Resource subject, Resource relation, Enumeration<Resource> enumeration, Resource value) {
84 super(session, subject, relation, enumeration, value);
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);
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);
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);
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);
117 @SCLValue(type = "ReadGraph -> Resource -> Variable -> b")
118 public static Object mappingModifier(ReadGraph graph, Resource resource, final Variable context) throws DatabaseException {
120 Resource element = resolveElement(graph, context);
121 Resource mappingType = resolveMappingType(graph, element);
122 return baseMappingModifier(graph, element, DistrictNetworkResource.getInstance(graph).HasMapping, mappingType, context);
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);
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);
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);
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 -> {
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);
152 } catch (DatabaseException e) {
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()));
165 Resource currentMapping = graph.getSingleObject(element, property);
167 return new HasMappingEnumerationModifier(Simantics.getSession(), element, property, enums, currentMapping);
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));
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));
184 return variable.getRepresents(graph);
187 private static EnumeratedValue<Resource> createEnumeratedValue(ReadGraph graph, Resource resource) {
189 String label = NameLabelUtil.modalName(graph, resource);
190 return new EnumeratedValue<Resource>(label, resource);
191 } catch (DatabaseException e) {
192 throw new RuntimeDatabaseException(e);
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();
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);
207 label = graph.getRelatedValue(resource, L0.HasName, Bindings.STRING);
212 @SCLValue(type = "Resource -> String -> Resource -> Resource")
213 public static Resource compositeInstantiator(final Resource compositeType, final String defaultName, final Resource target) throws DatabaseException {
215 return TypicalUtil.syncExec(procedure -> {
216 if (!SWTUtils.asyncExec(PlatformUI.getWorkbench().getDisplay(), () -> {
218 queryInitialValuesAndCreateComposite(compositeType, target, defaultName, procedure);
219 } catch (Throwable t) {
220 procedure.exception(t);
223 procedure.execute(null);
228 private static class DefaultMappingsDialog extends SelectionStatusDialog {
230 private Combo vertexMappingCombo;
231 private Combo rightClickMappingCombo;
232 private Combo leftClickMappingCombo;
233 private Combo edgeMappingCombo;
234 //private Combo crsCombo;
235 private Composite composite;
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<>();
243 private Resource defaultVertexMapping;
244 private Resource defaultEdgeMapping;
245 private Resource rightClickVertexMapping;
246 private Resource leftClickVertexMapping;
247 //private Resource defaultCRS;
249 //private Combo compositeMappingCombo;
250 //private Combo componentMappingCombo;
252 protected DefaultMappingsDialog(Shell parentShell, Resource configuration) {
254 this.configuration = configuration;
255 setTitle("Select mappings for new DN diagram");
258 public Resource getDefaultVertexMapping() {
259 return defaultVertexMapping;
262 public Resource getRightClickVertexMapping() {
263 return rightClickVertexMapping;
266 public Resource getLeftClickVertexMapping() {
267 return leftClickVertexMapping;
270 public Resource getDefaultEdgeMapping() {
271 return defaultEdgeMapping;
275 protected Control createDialogArea(Composite parent) {
276 composite = (Composite) super.createDialogArea(parent);
278 createMappingsGroup(composite);
279 //createExistingCompositeGroup(composite);
280 //createCRSSettingsGroup(composite);
282 // compute default values
283 Simantics.getSession().asyncRequest(new ReadRequest() {
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);
291 composites = getComposites(graph, configuration);
293 crss = getCRSs(graph, configuration);
295 composite.getDisplay().asyncExec(() -> {
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()]));
302 //crsCombo.setItems(crss.keySet().toArray(new String[crss.size()]));
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);
310 //crsCombo.select(0);
312 //if (!composites.isEmpty())
313 // compositeMappingCombo.select(0);
321 protected Map<String, Resource> getComposites(ReadGraph graph, Resource element) throws DatabaseException {
322 List<Resource> nonDistrictComposites = composites.values().stream().filter(comp -> {
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");
329 }).collect(Collectors.toList());
330 Map<String, Resource> result = new HashMap<>(nonDistrictComposites.size());
331 Layer0 L0 = Layer0.getInstance(graph);
332 nonDistrictComposites.forEach(mapping -> {
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);
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));
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));
354 Label vertexMappingLabel = new Label(cmposite, SWT.NONE);
355 vertexMappingLabel.setText("Default vertex mapping");
357 vertexMappingCombo = new Combo(cmposite, SWT.READ_ONLY | SWT.BORDER);
358 GridDataFactory.fillDefaults().grab(true, false).applyTo(vertexMappingCombo);
360 Label rightClickMappingLabel = new Label(cmposite, SWT.NONE);
361 rightClickMappingLabel.setText("Default right click mapping");
363 rightClickMappingCombo = new Combo(cmposite, SWT.READ_ONLY | SWT.BORDER);
364 GridDataFactory.fillDefaults().grab(true, false).applyTo(vertexMappingCombo);
366 Label leftClickMappingLabel = new Label(cmposite, SWT.NONE);
367 leftClickMappingLabel.setText("Default left click mapping");
369 leftClickMappingCombo = new Combo(cmposite, SWT.READ_ONLY | SWT.BORDER);
370 GridDataFactory.fillDefaults().grab(true, false).applyTo(vertexMappingCombo);
372 Label edgeMappingLabel = new Label(cmposite, SWT.NONE);
373 edgeMappingLabel.setText("Default edge mapping");
375 edgeMappingCombo = new Combo(cmposite, SWT.READ_ONLY | SWT.BORDER);
376 GridDataFactory.fillDefaults().grab(true, false).applyTo(edgeMappingCombo);
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));
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));
390 Label compositeMappingLabel = new Label(cmposite, SWT.NONE);
391 compositeMappingLabel.setText("Select composite");
393 // compositeMappingCombo = new Combo(cmposite, SWT.READ_ONLY | SWT.BORDER);
394 // GridDataFactory.fillDefaults().grab(true, false).applyTo(compositeMappingCombo);
395 // compositeMappingCombo.addSelectionListener(new SelectionAdapter() {
398 // public void widgetSelected(SelectionEvent e) {
399 // super.widgetSelected(e);
400 // recalculateMappapleComponents();
404 // Label compojnentMappingLabel = new Label(cmposite, SWT.NONE);
405 // compojnentMappingLabel.setText("Select component");
407 // componentMappingCombo = new Combo(cmposite, SWT.READ_ONLY | SWT.BORDER);
408 // GridDataFactory.fillDefaults().grab(true, false).applyTo(componentMappingCombo);
411 protected void recalculateMappapleComponents() {
412 Simantics.getSession().asyncRequest(new ReadRequest() {
415 public void run(ReadGraph graph) throws DatabaseException {
418 composite.getDisplay().asyncExec(() -> {
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));
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));
436 Label vertexMappingLabel = new Label(cmposite, SWT.NONE);
437 vertexMappingLabel.setText("Default CRS");
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);
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()));
454 public Resource getCRS() {
455 return crss.get("EPSG_4326"); // this is only supported
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);
464 if (dialog.open() != Dialog.OK) {
465 procedure.execute(null);
468 Simantics.getSession().asyncRequest(new WriteResultRequest<Resource>() {
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(),
482 }, new Procedure<Resource>() {
485 public void execute(Resource composite) {
486 DefaultActions.asyncPerformDefaultAction(Simantics.getSession(), composite, false, false, true);
487 procedure.execute(composite);
491 public void exception(Throwable t) {
492 LOGGER.error("Failed to create composite, see exception for details.", t);
493 procedure.exception(t);
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);
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());
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);
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);
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);
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);
540 private static final Function1<Resource, Double> ONE = new FunctionImpl1<Resource, Double>() {
541 private final Double ONE = 1.0;
543 public Double apply(Resource edge) {
547 public String toString() {
552 @SCLValue(type = "ReadGraph -> Resource -> Variable -> b")
553 public static Function1<Resource, Double> constantOne(ReadGraph graph, Resource resource, Variable context) throws DatabaseException {
557 private static Function1<Resource, Double> directPropertyValueFunction(Resource property, double defaultValue) throws DatabaseException {
558 Double def = defaultValue;
559 return new FunctionImpl1<Resource, Double>() {
561 public Double apply(Resource edge) {
562 ReadGraph graph = (ReadGraph) SCLContext.getCurrent().get("graph");
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);
574 private static class RangeValidator implements Function1<String, String> {
577 public RangeValidator(double min, double max) {
582 public String apply(String s) {
584 double d = Double.parseDouble(s);
586 return "Value must be greater than or equal to " + min;
588 return "Value must be less than or equal to " + max;
590 } catch (NumberFormatException e) {
591 return "Specified value is not a number";
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);
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);
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);
611 Resource sclmain = Layer0Utils.getPossibleChild(graph, root, "SCLMain");
612 if (sclmain != null) {
613 importEntry = graph.getPossibleURI(sclmain);
616 SCLContext ctx = SCLContext.getCurrent();
617 Object oldGraph = ctx.put("graph", graph);
619 return new BrightnessExpressionValidator(
621 ? Arrays.asList(importEntry)
622 : Collections.emptyList());
624 ctx.put("graph", oldGraph);
628 private static class BrightnessExpressionValidator implements Function1<String, String> {
629 private CommandSession session;
631 public BrightnessExpressionValidator(List<String> importEntries) {
632 this.session = new CommandSession(SCLOsgi.MODULE_REPOSITORY, SCLReportingHandler.DEFAULT);
633 this.session.setImportEntries(imports(importEntries));
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"));
645 public String apply(String s) {
647 if (!s.startsWith("="))
648 return "Expression expected, must start with '='";
649 CompilationError[] errors = session.validate(s.substring(1));
650 if(errors.length == 0)
652 return errors[0].description;