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.exception.DatabaseException;
44 import org.simantics.db.exception.RuntimeDatabaseException;
45 import org.simantics.db.exception.ServiceException;
46 import org.simantics.db.layer0.QueryIndexUtils;
47 import org.simantics.db.layer0.util.Layer0Utils;
48 import org.simantics.db.layer0.variable.Variable;
49 import org.simantics.db.layer0.variable.Variables;
50 import org.simantics.db.layer0.variable.Variables.Role;
51 import org.simantics.db.procedure.Procedure;
52 import org.simantics.district.network.ontology.DistrictNetworkResource;
53 import org.simantics.layer0.Layer0;
54 import org.simantics.modeling.ModelingResources;
55 import org.simantics.modeling.adapters.NewCompositeActionFactory;
56 import org.simantics.modeling.typicals.TypicalUtil;
57 import org.simantics.operation.Layer0X;
58 import org.simantics.scl.compiler.commands.CommandSession;
59 import org.simantics.scl.compiler.commands.CommandSessionImportEntry;
60 import org.simantics.scl.compiler.errors.CompilationError;
61 import org.simantics.scl.osgi.SCLOsgi;
62 import org.simantics.scl.reflection.annotations.SCLValue;
63 import org.simantics.scl.runtime.SCLContext;
64 import org.simantics.scl.runtime.function.Function1;
65 import org.simantics.scl.runtime.function.FunctionImpl1;
66 import org.simantics.scl.runtime.reporting.SCLReportingHandler;
67 import org.simantics.ui.workbench.action.DefaultActions;
68 import org.simantics.utils.ui.SWTUtils;
69 import org.slf4j.Logger;
70 import org.slf4j.LoggerFactory;
72 public class Functions {
74 private static final Logger LOGGER = LoggerFactory.getLogger(Functions.class);
79 private static class HasMappingEnumerationModifier extends GraphEnumerationModifier {
81 public HasMappingEnumerationModifier(Session session, Resource subject, Resource relation, Enumeration<Resource> enumeration, Resource value) {
82 super(session, subject, relation, enumeration, value);
87 @SCLValue(type = "ReadGraph -> Resource -> Variable -> b")
88 public static Object defaultEdgeMappingModifier(ReadGraph graph, Resource resource, final Variable context) throws DatabaseException {
89 Resource diagram = resolveElement(graph, context);
90 DistrictNetworkResource DN = DistrictNetworkResource.getInstance(graph);
91 return baseMappingModifier(graph, diagram, DN.EdgeDefaultMapping, DN.Mapping_EdgeMapping, context);
94 @SCLValue(type = "ReadGraph -> Resource -> Variable -> b")
95 public static Object defaultVertexMappingModifier(ReadGraph graph, Resource resource, final Variable context) throws DatabaseException {
96 System.out.println(graph.getURI(resource));
97 System.out.println(context.getURI(graph));
99 Resource diagram = resolveElement(graph, context);
100 DistrictNetworkResource DN = DistrictNetworkResource.getInstance(graph);
101 return baseMappingModifier(graph, diagram, DN.VertexDefaultMapping, DN.Mapping_VertexMapping, context);
104 @SCLValue(type = "ReadGraph -> Resource -> Variable -> b")
105 public static Object mappingModifier(ReadGraph graph, Resource resource, final Variable context) throws DatabaseException {
107 Resource element = resolveElement(graph, context);
108 Resource mappingType = resolveMappingType(graph, element);
109 return baseMappingModifier(graph, element, DistrictNetworkResource.getInstance(graph).HasMapping, mappingType, context);
112 public static Map<String, Resource> getVertexMappings(ReadGraph graph, Resource resource) throws DatabaseException {
113 Map<String, Resource> second = getNetworkMappingsByType(graph, resource , DistrictNetworkResource.getInstance(graph).Mapping_VertexMapping);
117 public static Map<String, Resource> getEdgeMappings(ReadGraph graph, Resource resource) throws DatabaseException {
118 Map<String, Resource> second = getNetworkMappingsByType(graph, resource , DistrictNetworkResource.getInstance(graph).Mapping_EdgeMapping);
122 public static Map<String, Resource> getCRSs(ReadGraph graph, Resource resource) throws DatabaseException {
123 Map<String, Resource> result = getNetworkMappingsByType(graph, resource, DistrictNetworkResource.getInstance(graph).SpatialRefSystem);
128 public static Map<String, Resource> getNetworkMappingsByType(ReadGraph graph, Resource element, Resource mappingType) throws DatabaseException {
129 Resource indexRoot = graph.sync(new IndexRoot(element));
130 List<Resource> mappings = QueryIndexUtils.searchByType(graph, indexRoot, mappingType);
131 Map<String, Resource> result = new HashMap<>(mappings.size());
132 Layer0 L0 = Layer0.getInstance(graph);
133 mappings.forEach(mapping -> {
135 String name = graph.getRelatedValue2(mapping, L0.HasName);
136 Resource existing = result.put(name, mapping);
137 if (existing != null) {
138 LOGGER.warn("Duplicate mapping name! {} {} and existing is {}", name, mapping, existing);
140 } catch (DatabaseException e) {
147 private static Object baseMappingModifier(ReadGraph graph, Resource element, Resource property, Resource mappingType, Variable context) throws DatabaseException {
148 Resource indexRoot = graph.sync(new IndexRoot(element));
149 List<Resource> mappings = QueryIndexUtils.searchByType(graph, indexRoot, mappingType);
150 Enumeration<Resource> enums = Enumeration
151 .make(mappings.stream().map(m -> createEnumeratedValue(graph, m)).collect(Collectors.toList()));
153 Resource currentMapping = graph.getSingleObject(element, property);
155 return new HasMappingEnumerationModifier(Simantics.getSession(), element, property, enums, currentMapping);
158 private static Resource resolveMappingType(ReadGraph graph, Resource element) throws DatabaseException {
159 DistrictNetworkResource DN = DistrictNetworkResource.getInstance(graph);
160 if (graph.isInstanceOf(element, DN.Edge))
161 return DN.Mapping_EdgeMapping;
162 else if (graph.isInstanceOf(element, DN.Vertex))
163 return DN.Mapping_VertexMapping;
164 throw new IllegalStateException("No mapping type found for element " + element + " : " + graph.getPossibleURI(element));
167 private static Resource resolveElement(ReadGraph graph, Variable variable) throws DatabaseException {
168 Role role = variable.getPossibleRole(graph);
169 if (role.equals(Role.PROPERTY))
170 return resolveElement(graph, variable.getParent(graph));
172 return variable.getRepresents(graph);
175 private static EnumeratedValue<Resource> createEnumeratedValue(ReadGraph graph, Resource resource) {
177 String label = NameLabelUtil.modalName(graph, resource);
178 return new EnumeratedValue<Resource>(label, resource);
179 } catch (DatabaseException e) {
180 throw new RuntimeDatabaseException(e);
184 @SCLValue(type = "ReadGraph -> Resource -> a -> b")
185 public static Object enumerationValues(ReadGraph graph, Resource resource, Object context) throws DatabaseException {
186 Variable var = (Variable) context;
187 System.out.println(graph.getURI(resource));
188 System.out.println(var.getURI(graph));
189 return Collections.emptyList();
192 @SCLValue(type = "ReadGraph -> Resource -> a -> b")
193 public static Object convertToValue(ReadGraph graph, Resource resource, Object context) throws DatabaseException {
194 Layer0 L0 = Layer0.getInstance(graph);
195 String label = graph.getPossibleRelatedValue2(resource, L0.HasLabel, Bindings.STRING);
197 label = graph.getRelatedValue(resource, L0.HasName, Bindings.STRING);
202 @SCLValue(type = "Resource -> String -> Resource -> Resource")
203 public static Resource compositeInstantiator(final Resource compositeType, final String defaultName, final Resource target) throws DatabaseException {
205 return TypicalUtil.syncExec(procedure -> {
206 if (!SWTUtils.asyncExec(PlatformUI.getWorkbench().getDisplay(), () -> {
208 queryInitialValuesAndCreateComposite(compositeType, target, defaultName, procedure);
209 } catch (Throwable t) {
210 procedure.exception(t);
213 procedure.execute(null);
218 private static class DefaultMappingsDialog extends SelectionStatusDialog {
220 private Combo vertexMappingCombo;
221 private Combo edgeMappingCombo;
222 private Combo crsCombo;
223 private Composite composite;
225 private Resource configuration;
226 private Map<String, Resource> vertexMappings = new HashMap<>();
227 private Map<String, Resource> edgeMappings = new HashMap<>();
228 private Map<String, Resource> composites = new HashMap<>();
229 private Map<String, Resource> crss = new HashMap<>();
231 private Resource defaultVertexMapping;
232 private Resource defaultEdgeMapping;
233 private Resource defaultCRS;
235 private Combo compositeMappingCombo;
236 private Combo componentMappingCombo;
238 protected DefaultMappingsDialog(Shell parentShell, Resource configuration) {
240 this.configuration = configuration;
241 setTitle("Select mappings for new DN diagram");
244 public Resource getDefaultVertexMapping() {
245 return defaultVertexMapping;
248 public Resource getDefaultEdgeMapping() {
249 return defaultEdgeMapping;
253 protected Control createDialogArea(Composite parent) {
254 composite = (Composite) super.createDialogArea(parent);
256 createMappingsGroup(composite);
257 createExistingCompositeGroup(composite);
258 createCRSSettingsGroup(composite);
260 // compute default values
261 Simantics.getSession().asyncRequest(new ReadRequest() {
264 public void run(ReadGraph graph) throws DatabaseException {
266 vertexMappings = getVertexMappings(graph, configuration);
267 edgeMappings = getEdgeMappings(graph, configuration);
269 composites = getComposites(graph, configuration);
271 crss = getCRSs(graph, configuration);
273 composite.getDisplay().asyncExec(() -> {
275 vertexMappingCombo.setItems(vertexMappings.keySet().toArray(new String[vertexMappings.size()]));
276 edgeMappingCombo.setItems(edgeMappings.keySet().toArray(new String[edgeMappings.size()]));
278 crsCombo.setItems(crss.keySet().toArray(new String[crss.size()]));
280 compositeMappingCombo.setItems(composites.keySet().toArray(new String[composites.size()]));
281 vertexMappingCombo.select(0);
282 edgeMappingCombo.select(0);
286 if (!composites.isEmpty())
287 compositeMappingCombo.select(0);
295 protected Map<String, Resource> getComposites(ReadGraph graph, Resource element) throws DatabaseException {
296 List<Resource> nonDistrictComposites = composites.values().stream().filter(comp -> {
298 return !graph.isInstanceOf(comp, DistrictNetworkResource.getInstance(graph).Composite);
299 } catch (ServiceException e1) {
300 LOGGER.error("Could not check if composite " + comp + " is instanceOf DistrictNetwork.composite");
303 }).collect(Collectors.toList());
304 Map<String, Resource> result = new HashMap<>(nonDistrictComposites.size());
305 Layer0 L0 = Layer0.getInstance(graph);
306 nonDistrictComposites.forEach(mapping -> {
308 String name = graph.getRelatedValue2(mapping, L0.HasName);
309 result.put(name, mapping);
310 } catch (DatabaseException e) {
311 LOGGER.error("Could not read name of " + mapping, e);
317 private void createMappingsGroup(Composite parent) {
318 Group group= new Group(parent, SWT.NONE);
319 group.setFont(parent.getFont());
320 group.setText("Default mappings");
321 GridDataFactory.fillDefaults().grab(true, false).applyTo(group);
322 group.setLayout(new GridLayout(1, false));
324 Composite cmposite = new Composite(group, SWT.NONE);
325 cmposite.setLayoutData(new GridData(SWT.FILL, SWT.TOP, true, false));
326 cmposite.setLayout(new GridLayout(2, false));
328 Label vertexMappingLabel = new Label(cmposite, SWT.NONE);
329 vertexMappingLabel.setText("Default vertex mapping");
331 vertexMappingCombo = new Combo(cmposite, SWT.READ_ONLY | SWT.BORDER);
332 GridDataFactory.fillDefaults().grab(true, false).applyTo(vertexMappingCombo);
334 Label edgeMappingLabel = new Label(cmposite, SWT.NONE);
335 edgeMappingLabel.setText("Default edge mapping");
337 edgeMappingCombo = new Combo(cmposite, SWT.READ_ONLY | SWT.BORDER);
338 GridDataFactory.fillDefaults().grab(true, false).applyTo(edgeMappingCombo);
341 private void createExistingCompositeGroup(Composite parent) {
342 Group group= new Group(parent, SWT.NONE);
343 group.setFont(parent.getFont());
344 group.setText("Mapped composite");
345 GridDataFactory.fillDefaults().grab(true, false).applyTo(group);
346 group.setLayout(new GridLayout(1, false));
348 Composite cmposite = new Composite(group, SWT.NONE);
349 cmposite.setLayoutData(new GridData(SWT.FILL, SWT.TOP, true, false));
350 cmposite.setLayout(new GridLayout(2, false));
352 Label compositeMappingLabel = new Label(cmposite, SWT.NONE);
353 compositeMappingLabel.setText("Select composite");
355 compositeMappingCombo = new Combo(cmposite, SWT.READ_ONLY | SWT.BORDER);
356 GridDataFactory.fillDefaults().grab(true, false).applyTo(compositeMappingCombo);
357 compositeMappingCombo.addSelectionListener(new SelectionAdapter() {
360 public void widgetSelected(SelectionEvent e) {
361 super.widgetSelected(e);
362 recalculateMappapleComponents();
366 Label compojnentMappingLabel = new Label(cmposite, SWT.NONE);
367 compojnentMappingLabel.setText("Select component");
369 componentMappingCombo = new Combo(cmposite, SWT.READ_ONLY | SWT.BORDER);
370 GridDataFactory.fillDefaults().grab(true, false).applyTo(componentMappingCombo);
373 protected void recalculateMappapleComponents() {
374 Simantics.getSession().asyncRequest(new ReadRequest() {
377 public void run(ReadGraph graph) throws DatabaseException {
380 composite.getDisplay().asyncExec(() -> {
387 private void createCRSSettingsGroup(Composite parent) {
388 Group group= new Group(parent, SWT.NONE);
389 group.setFont(parent.getFont());
390 group.setText("CRS settings");
391 GridDataFactory.fillDefaults().grab(true, false).applyTo(group);
392 group.setLayout(new GridLayout(1, false));
394 Composite cmposite = new Composite(group, SWT.NONE);
395 cmposite.setLayoutData(new GridData(SWT.FILL, SWT.TOP, true, false));
396 cmposite.setLayout(new GridLayout(2, false));
398 Label vertexMappingLabel = new Label(cmposite, SWT.NONE);
399 vertexMappingLabel.setText("Default CRS");
401 crsCombo = new Combo(cmposite, SWT.READ_ONLY | SWT.BORDER);
402 GridData textData = new GridData(SWT.FILL, SWT.CENTER, true, false);
403 crsCombo.setLayoutData(textData);
408 protected void computeResult() {
409 defaultVertexMapping = vertexMappings.get(vertexMappingCombo.getItem(vertexMappingCombo.getSelectionIndex()));
410 defaultEdgeMapping = edgeMappings.get(edgeMappingCombo.getItem(edgeMappingCombo.getSelectionIndex()));
411 defaultCRS = crss.get(crsCombo.getItem(crsCombo.getSelectionIndex()));
414 public Resource getCRS() {
420 private static void queryInitialValuesAndCreateComposite(final Resource compositeType, final Resource target,
421 String defaultName, final Procedure<Resource> procedure) {
422 DefaultMappingsDialog dialog = new DefaultMappingsDialog(PlatformUI.getWorkbench().getActiveWorkbenchWindow().getShell(), target);
424 if (dialog.open() != Dialog.OK) {
425 procedure.execute(null);
428 Simantics.getSession().asyncRequest(
429 NewCompositeActionFactory.createCompositeRequest(target, defaultName, compositeType),
430 new Procedure<Resource>() {
432 public void execute(Resource composite) {
433 Simantics.getSession().asyncRequest(new WriteRequest() {
436 public void perform(WriteGraph graph) throws DatabaseException {
437 DistrictNetworkResource DN = DistrictNetworkResource.getInstance(graph);
438 Resource diagram = graph.getSingleObject(composite, ModelingResources.getInstance(graph).CompositeToDiagram);
439 graph.claim(diagram, DN.EdgeDefaultMapping, dialog.getDefaultEdgeMapping());
440 graph.claim(diagram, DN.VertexDefaultMapping, dialog.getDefaultVertexMapping());
441 graph.claim(diagram, DN.HasSpatialRefSystem, dialog.getCRS());
443 // Generated name prefix from composite name
444 String compositeName = graph.getRelatedValue2(composite, Layer0.getInstance(graph).HasName, Bindings.STRING);
445 graph.claimLiteral(diagram, Layer0X.getInstance(graph).HasGeneratedNamePrefix, "N" + compositeName.substring(compositeName.length() - 1, compositeName.length()));
448 DefaultActions.asyncPerformDefaultAction(Simantics.getSession(), composite, false, false, true);
449 procedure.execute(composite);
453 public void exception(Throwable t) {
454 LOGGER.error("Failed to create composite, see exception for details.", t);
455 procedure.exception(t);
460 public static Collection<Resource> getDistrictDiagrams(ReadGraph graph) throws DatabaseException {
461 Layer0 L0 = Layer0.getInstance(graph);
462 Collection<Resource> indexRoots = graph.sync(new ObjectsWithType(Simantics.getProjectResource(), L0.ConsistsOf, L0.IndexRoot));
463 DistrictNetworkResource DN = DistrictNetworkResource.getInstance(graph);
464 Set<Resource> results = new HashSet<>();
465 for (Resource indexRoot : indexRoots) {
466 Collection<Resource> diagrams = QueryIndexUtils.searchByType(graph, indexRoot, DN.Diagram);
467 results.addAll(diagrams);
472 private static List<String> listInstanceNames(ReadGraph graph, Variable context, Resource type) throws DatabaseException {
473 Resource indexRoot = Variables.getIndexRoot(graph, context);
474 DistrictNetworkResource DN = DistrictNetworkResource.getInstance(graph);
475 List<Resource> properties = QueryIndexUtils.searchByType(graph, indexRoot, DN.Vertex_ScaleProperty);
476 return properties.stream()
477 .map(m -> createEnumeratedValue(graph, m))
478 .map(EnumeratedValue::getName)
479 .collect(Collectors.toList());
482 @SCLValue(type = "ReadGraph -> Resource -> Variable -> b")
483 public static Object edgeThicknessPropertyEnumerationValues(ReadGraph graph, Resource resource, Variable context) throws DatabaseException {
484 DistrictNetworkResource DN = DistrictNetworkResource.getInstance(graph);
485 return listInstanceNames(graph, context, DN.Edge_ThicknessProperty);
488 @SCLValue(type = "ReadGraph -> Resource -> Variable -> b")
489 public static Object nodeScalePropertyEnumerationValues(ReadGraph graph, Resource resource, Variable context) throws DatabaseException {
490 DistrictNetworkResource DN = DistrictNetworkResource.getInstance(graph);
491 return listInstanceNames(graph, context, DN.Vertex_ScaleProperty);
494 @SCLValue(type = "ReadGraph -> Resource -> Variable -> b")
495 public static Object edgeThicknessPropertyModifier(ReadGraph graph, Resource resource, Variable context) throws DatabaseException {
496 Resource diagram = resolveElement(graph, context);
497 DistrictNetworkResource DN = DistrictNetworkResource.getInstance(graph);
498 return baseMappingModifier(graph, diagram, DN.Diagram_edgeThicknessProperty, DN.Edge_ThicknessProperty, context);
501 @SCLValue(type = "ReadGraph -> Resource -> Variable -> b")
502 public static Object nodeScalePropertyModifier(ReadGraph graph, Resource resource, Variable context) throws DatabaseException {
503 Resource diagram = resolveElement(graph, context);
504 DistrictNetworkResource DN = DistrictNetworkResource.getInstance(graph);
505 return baseMappingModifier(graph, diagram, DN.Diagram_nodeScaleProperty, DN.Vertex_ScaleProperty, context);
508 @SCLValue(type = "ReadGraph -> Resource -> Variable -> b")
509 public static Function1<Resource, Double> hasDiameterValue(ReadGraph graph, Resource resource, Variable context) throws DatabaseException {
510 return directPropertyValueFunction(DistrictNetworkResource.getInstance(graph).Edge_HasDiameter, 0);
513 @SCLValue(type = "ReadGraph -> Resource -> Variable -> b")
514 public static Function1<Resource, Double> hasNominalMassFlowValue(ReadGraph graph, Resource resource, Variable context) throws DatabaseException {
515 return directPropertyValueFunction(DistrictNetworkResource.getInstance(graph).Edge_HasNominalMassFlow, 0);
518 @SCLValue(type = "ReadGraph -> Resource -> Variable -> b")
519 public static Function1<Resource, Double> hasNominalSupplyPressure(ReadGraph graph, Resource resource, Variable context) throws DatabaseException {
520 return directPropertyValueFunction(DistrictNetworkResource.getInstance(graph).Vertex_HasSupplyPressure, 0);
523 @SCLValue(type = "ReadGraph -> Resource -> Variable -> b")
524 public static Function1<Resource, Double> hasElevation(ReadGraph graph, Resource resource, Variable context) throws DatabaseException {
525 return directPropertyValueFunction(DistrictNetworkResource.getInstance(graph).Vertex_HasElevation, 0);
528 private static final Function1<Resource, Double> ONE = new FunctionImpl1<Resource, Double>() {
529 private final Double ONE = 1.0;
531 public Double apply(Resource edge) {
535 public String toString() {
540 @SCLValue(type = "ReadGraph -> Resource -> Variable -> b")
541 public static Function1<Resource, Double> constantOne(ReadGraph graph, Resource resource, Variable context) throws DatabaseException {
545 private static Function1<Resource, Double> directPropertyValueFunction(Resource property, double defaultValue) throws DatabaseException {
546 Double def = defaultValue;
547 return new FunctionImpl1<Resource, Double>() {
549 public Double apply(Resource edge) {
550 ReadGraph graph = (ReadGraph) SCLContext.getCurrent().get("graph");
552 Double d = graph.getPossibleRelatedValue(edge, property, Bindings.DOUBLE);
553 return d != null ? d : def;
554 } catch (DatabaseException e) {
555 LOGGER.error("Failed to evaluate property value", e);
562 private static class RangeValidator implements Function1<String, String> {
565 public RangeValidator(double min, double max) {
570 public String apply(String s) {
572 double d = Double.parseDouble(s);
574 return "Value must be greater than or equal to " + min;
576 return "Value must be less than or equal to " + max;
578 } catch (NumberFormatException e) {
579 return "Specified value is not a number";
584 @SCLValue(type = "ReadGraph -> Resource -> Variable -> b")
585 public static Object hueValidator(ReadGraph graph, Resource r, Variable context) throws DatabaseException {
586 return new RangeValidator(0, 360);
589 @SCLValue(type = "ReadGraph -> Resource -> Variable -> b")
590 public static Object saturationValidator(ReadGraph graph, Resource r, Variable context) throws DatabaseException {
591 return new RangeValidator(0, 100);
594 @SCLValue(type = "ReadGraph -> Resource -> Variable -> b")
595 public static Object brightnessValidator(ReadGraph graph, Resource r, Variable context) throws DatabaseException {
596 String importEntry = null;
597 Resource root = Variables.getPossibleIndexRoot(graph, context);
599 Resource sclmain = Layer0Utils.getPossibleChild(graph, root, "SCLMain");
600 if (sclmain != null) {
601 importEntry = graph.getPossibleURI(sclmain);
604 SCLContext ctx = SCLContext.getCurrent();
605 Object oldGraph = ctx.put("graph", graph);
607 return new BrightnessExpressionValidator(
609 ? Arrays.asList(importEntry)
610 : Collections.emptyList());
612 ctx.put("graph", oldGraph);
616 private static class BrightnessExpressionValidator implements Function1<String, String> {
617 private CommandSession session;
619 public BrightnessExpressionValidator(List<String> importEntries) {
620 this.session = new CommandSession(SCLOsgi.MODULE_REPOSITORY, SCLReportingHandler.DEFAULT);
621 this.session.setImportEntries(imports(importEntries));
624 private ArrayList<CommandSessionImportEntry> imports(List<String> entries) {
625 ArrayList<CommandSessionImportEntry> result = new ArrayList<>();
626 entries.stream().map(CommandSessionImportEntry::new).forEach(result::add);
627 if (entries.isEmpty())
628 result.add(new CommandSessionImportEntry("Simantics/District/SCLMain"));
633 public String apply(String s) {
635 if (!s.startsWith("="))
636 return "Expression expected, must start with '='";
637 CompilationError[] errors = session.validate(s.substring(1));
638 if(errors.length == 0)
640 return errors[0].description;