X-Git-Url: https://gerrit.simantics.org/r/gitweb?a=blobdiff_plain;f=org.simantics.district.network.ui%2Fsrc%2Forg%2Fsimantics%2Fdistrict%2Fnetwork%2Fui%2Ffunction%2FFunctions.java;h=14c75030e019d7598bc2e8b08db82302b8af38c1;hb=refs%2Fchanges%2F54%2F3354%2F1;hp=d80082ec65279d8281c2f29590897fddf2a65e1b;hpb=36d6d10e40cf262fd59ec67351f8e5d966af0fb6;p=simantics%2Fdistrict.git diff --git a/org.simantics.district.network.ui/src/org/simantics/district/network/ui/function/Functions.java b/org.simantics.district.network.ui/src/org/simantics/district/network/ui/function/Functions.java index d80082ec..14c75030 100644 --- a/org.simantics.district.network.ui/src/org/simantics/district/network/ui/function/Functions.java +++ b/org.simantics.district.network.ui/src/org/simantics/district/network/ui/function/Functions.java @@ -1,5 +1,7 @@ package org.simantics.district.network.ui.function; +import java.util.ArrayList; +import java.util.Arrays; import java.util.Collection; import java.util.Collections; import java.util.HashMap; @@ -38,21 +40,32 @@ import org.simantics.db.common.request.IndexRoot; import org.simantics.db.common.request.ObjectsWithType; import org.simantics.db.common.request.ReadRequest; import org.simantics.db.common.request.WriteRequest; +import org.simantics.db.common.request.WriteResultRequest; import org.simantics.db.exception.DatabaseException; import org.simantics.db.exception.RuntimeDatabaseException; import org.simantics.db.exception.ServiceException; +import org.simantics.db.layer0.QueryIndexUtils; +import org.simantics.db.layer0.util.Layer0Utils; import org.simantics.db.layer0.variable.Variable; +import org.simantics.db.layer0.variable.Variables; import org.simantics.db.layer0.variable.Variables.Role; import org.simantics.db.procedure.Procedure; -import org.simantics.diagram.stubs.DiagramResource; +import org.simantics.district.network.DistrictNetworkUtil; import org.simantics.district.network.ontology.DistrictNetworkResource; import org.simantics.layer0.Layer0; import org.simantics.modeling.ModelingResources; -import org.simantics.modeling.ModelingUtils; import org.simantics.modeling.adapters.NewCompositeActionFactory; import org.simantics.modeling.typicals.TypicalUtil; import org.simantics.operation.Layer0X; +import org.simantics.scl.compiler.commands.CommandSession; +import org.simantics.scl.compiler.commands.CommandSessionImportEntry; +import org.simantics.scl.compiler.errors.CompilationError; +import org.simantics.scl.osgi.SCLOsgi; import org.simantics.scl.reflection.annotations.SCLValue; +import org.simantics.scl.runtime.SCLContext; +import org.simantics.scl.runtime.function.Function1; +import org.simantics.scl.runtime.function.FunctionImpl1; +import org.simantics.scl.runtime.reporting.SCLReportingHandler; import org.simantics.ui.workbench.action.DefaultActions; import org.simantics.utils.ui.SWTUtils; import org.slf4j.Logger; @@ -82,14 +95,25 @@ public class Functions { @SCLValue(type = "ReadGraph -> Resource -> Variable -> b") public static Object defaultVertexMappingModifier(ReadGraph graph, Resource resource, final Variable context) throws DatabaseException { - System.out.println(graph.getURI(resource)); - System.out.println(context.getURI(graph)); - Resource diagram = resolveElement(graph, context); DistrictNetworkResource DN = DistrictNetworkResource.getInstance(graph); return baseMappingModifier(graph, diagram, DN.VertexDefaultMapping, DN.Mapping_VertexMapping, context); } - + + @SCLValue(type = "ReadGraph -> Resource -> Variable -> b") + public static Object rightClickVertexMappingModifier(ReadGraph graph, Resource resource, final Variable context) throws DatabaseException { + Resource diagram = resolveElement(graph, context); + DistrictNetworkResource DN = DistrictNetworkResource.getInstance(graph); + return baseMappingModifier(graph, diagram, DN.RightClickDefaultMapping, DN.Mapping_VertexMapping, context); + } + + @SCLValue(type = "ReadGraph -> Resource -> Variable -> b") + public static Object leftClickVertexMappingModifier(ReadGraph graph, Resource resource, final Variable context) throws DatabaseException { + Resource diagram = resolveElement(graph, context); + DistrictNetworkResource DN = DistrictNetworkResource.getInstance(graph); + return baseMappingModifier(graph, diagram, DN.LeftClickDefaultMapping, DN.Mapping_VertexMapping, context); + } + @SCLValue(type = "ReadGraph -> Resource -> Variable -> b") public static Object mappingModifier(ReadGraph graph, Resource resource, final Variable context) throws DatabaseException { @@ -98,25 +122,24 @@ public class Functions { return baseMappingModifier(graph, element, DistrictNetworkResource.getInstance(graph).HasMapping, mappingType, context); } - public static Map getVertexMappings(ReadGraph graph, Resource resource) throws DatabaseException { - Map second = getNetworkMappingsByType(graph, resource , DistrictNetworkResource.getInstance(graph).Mapping_VertexMapping); + public static Map getVertexMappings(ReadGraph graph, Resource indexRoot) throws DatabaseException { + Map second = getNetworkMappingsByType(graph, indexRoot, DistrictNetworkResource.getInstance(graph).Mapping_VertexMapping); return second; } - public static Map getEdgeMappings(ReadGraph graph, Resource resource) throws DatabaseException { - Map second = getNetworkMappingsByType(graph, resource , DistrictNetworkResource.getInstance(graph).Mapping_EdgeMapping); + public static Map getEdgeMappings(ReadGraph graph, Resource indexRoot) throws DatabaseException { + Map second = getNetworkMappingsByType(graph, indexRoot, DistrictNetworkResource.getInstance(graph).Mapping_EdgeMapping); return second; } public static Map getCRSs(ReadGraph graph, Resource resource) throws DatabaseException { - Map result = getNetworkMappingsByType(graph, resource, DistrictNetworkResource.getInstance(graph).SpatialRefSystem); + Map result = getNetworkMappingsByType(graph, graph.sync(new IndexRoot(resource)), DistrictNetworkResource.getInstance(graph).SpatialRefSystem); return result; } - public static Map getNetworkMappingsByType(ReadGraph graph, Resource element, Resource mappingType) throws DatabaseException { - Resource indexRoot = graph.sync(new IndexRoot(element)); - List mappings = ModelingUtils.searchByType(graph, indexRoot, mappingType); + public static Map getNetworkMappingsByType(ReadGraph graph, Resource indexRoot, Resource mappingType) throws DatabaseException { + List mappings = QueryIndexUtils.searchByType(graph, indexRoot, mappingType); Map result = new HashMap<>(mappings.size()); Layer0 L0 = Layer0.getInstance(graph); mappings.forEach(mapping -> { @@ -135,7 +158,7 @@ public class Functions { private static Object baseMappingModifier(ReadGraph graph, Resource element, Resource property, Resource mappingType, Variable context) throws DatabaseException { Resource indexRoot = graph.sync(new IndexRoot(element)); - List mappings = ModelingUtils.searchByType(graph, indexRoot, mappingType); + List mappings = QueryIndexUtils.searchByType(graph, indexRoot, mappingType); Enumeration enums = Enumeration .make(mappings.stream().map(m -> createEnumeratedValue(graph, m)).collect(Collectors.toList())); @@ -173,15 +196,16 @@ public class Functions { @SCLValue(type = "ReadGraph -> Resource -> a -> b") public static Object enumerationValues(ReadGraph graph, Resource resource, Object context) throws DatabaseException { Variable var = (Variable) context; - System.out.println(graph.getURI(resource)); - System.out.println(var.getURI(graph)); return Collections.emptyList(); } @SCLValue(type = "ReadGraph -> Resource -> a -> b") public static Object convertToValue(ReadGraph graph, Resource resource, Object context) throws DatabaseException { - return graph.getRelatedValue2(resource, Layer0.getInstance(graph).HasName, Bindings.STRING); -// return null; + Layer0 L0 = Layer0.getInstance(graph); + String label = graph.getPossibleRelatedValue2(resource, L0.HasLabel, Bindings.STRING); + if (label == null) + label = graph.getRelatedValue(resource, L0.HasName, Bindings.STRING); + return label; } @@ -204,8 +228,10 @@ public class Functions { private static class DefaultMappingsDialog extends SelectionStatusDialog { private Combo vertexMappingCombo; + private Combo rightClickMappingCombo; + private Combo leftClickMappingCombo; private Combo edgeMappingCombo; - private Combo crsCombo; + //private Combo crsCombo; private Composite composite; private Resource configuration; @@ -213,14 +239,15 @@ public class Functions { private Map edgeMappings = new HashMap<>(); private Map composites = new HashMap<>(); private Map crss = new HashMap<>(); - private Map> components = new HashMap<>(); private Resource defaultVertexMapping; private Resource defaultEdgeMapping; - private Resource defaultCRS; + private Resource rightClickVertexMapping; + private Resource leftClickVertexMapping; + //private Resource defaultCRS; - private Combo compositeMappingCombo; - private Combo componentMappingCombo; + //private Combo compositeMappingCombo; + //private Combo componentMappingCombo; protected DefaultMappingsDialog(Shell parentShell, Resource configuration) { super(parentShell); @@ -232,6 +259,14 @@ public class Functions { return defaultVertexMapping; } + public Resource getRightClickVertexMapping() { + return rightClickVertexMapping; + } + + public Resource getLeftClickVertexMapping() { + return leftClickVertexMapping; + } + public Resource getDefaultEdgeMapping() { return defaultEdgeMapping; } @@ -241,40 +276,41 @@ public class Functions { composite = (Composite) super.createDialogArea(parent); createMappingsGroup(composite); - createExistingCompositeGroup(composite); - createCRSSettingsGroup(composite); + //createExistingCompositeGroup(composite); + //createCRSSettingsGroup(composite); // compute default values Simantics.getSession().asyncRequest(new ReadRequest() { @Override public void run(ReadGraph graph) throws DatabaseException { - - vertexMappings = getVertexMappings(graph, configuration); - edgeMappings = getEdgeMappings(graph, configuration); + Resource indexRoot = graph.sync(new IndexRoot(configuration)); + vertexMappings = getVertexMappings(graph, indexRoot); + edgeMappings = getEdgeMappings(graph, indexRoot); composites = getComposites(graph, configuration); - if (composites.size() > 0) { - components = getComponents(graph, composites.get(0)); - } - + crss = getCRSs(graph, configuration); composite.getDisplay().asyncExec(() -> { vertexMappingCombo.setItems(vertexMappings.keySet().toArray(new String[vertexMappings.size()])); + rightClickMappingCombo.setItems(vertexMappings.keySet().toArray(new String[vertexMappings.size()])); + leftClickMappingCombo.setItems(vertexMappings.keySet().toArray(new String[vertexMappings.size()])); edgeMappingCombo.setItems(edgeMappings.keySet().toArray(new String[edgeMappings.size()])); - crsCombo.setItems(crss.keySet().toArray(new String[crss.size()])); + //crsCombo.setItems(crss.keySet().toArray(new String[crss.size()])); - compositeMappingCombo.setItems(composites.keySet().toArray(new String[composites.size()])); + //compositeMappingCombo.setItems(composites.keySet().toArray(new String[composites.size()])); vertexMappingCombo.select(0); + rightClickMappingCombo.select(0); + leftClickMappingCombo.select(0); edgeMappingCombo.select(0); - crsCombo.select(0); + //crsCombo.select(0); - if (!composites.isEmpty()) - compositeMappingCombo.select(0); + //if (!composites.isEmpty()) + // compositeMappingCombo.select(0); }); } @@ -282,16 +318,7 @@ public class Functions { return composite; } - protected Map> getComponents(ReadGraph graph, Resource resource) { - // TODO Auto-generated method stub - return null; - } - protected Map getComposites(ReadGraph graph, Resource element) throws DatabaseException { - - Resource indexRoot = graph.sync(new IndexRoot(element)); - List diagrams = ModelingUtils.searchByType(graph, indexRoot, DiagramResource.getInstance(graph).Diagram); - List nonDistrictComposites = composites.values().stream().filter(comp -> { try { return !graph.isInstanceOf(comp, DistrictNetworkResource.getInstance(graph).Composite); @@ -329,7 +356,19 @@ public class Functions { vertexMappingCombo = new Combo(cmposite, SWT.READ_ONLY | SWT.BORDER); GridDataFactory.fillDefaults().grab(true, false).applyTo(vertexMappingCombo); - + + Label rightClickMappingLabel = new Label(cmposite, SWT.NONE); + rightClickMappingLabel.setText("Default right click mapping"); + + rightClickMappingCombo = new Combo(cmposite, SWT.READ_ONLY | SWT.BORDER); + GridDataFactory.fillDefaults().grab(true, false).applyTo(vertexMappingCombo); + + Label leftClickMappingLabel = new Label(cmposite, SWT.NONE); + leftClickMappingLabel.setText("Default left click mapping"); + + leftClickMappingCombo = new Combo(cmposite, SWT.READ_ONLY | SWT.BORDER); + GridDataFactory.fillDefaults().grab(true, false).applyTo(vertexMappingCombo); + Label edgeMappingLabel = new Label(cmposite, SWT.NONE); edgeMappingLabel.setText("Default edge mapping"); @@ -351,22 +390,22 @@ public class Functions { Label compositeMappingLabel = new Label(cmposite, SWT.NONE); compositeMappingLabel.setText("Select composite"); - compositeMappingCombo = new Combo(cmposite, SWT.READ_ONLY | SWT.BORDER); - GridDataFactory.fillDefaults().grab(true, false).applyTo(compositeMappingCombo); - compositeMappingCombo.addSelectionListener(new SelectionAdapter() { - - @Override - public void widgetSelected(SelectionEvent e) { - super.widgetSelected(e); - recalculateMappapleComponents(); - } - }); - - Label compojnentMappingLabel = new Label(cmposite, SWT.NONE); - compojnentMappingLabel.setText("Select component"); +// compositeMappingCombo = new Combo(cmposite, SWT.READ_ONLY | SWT.BORDER); +// GridDataFactory.fillDefaults().grab(true, false).applyTo(compositeMappingCombo); +// compositeMappingCombo.addSelectionListener(new SelectionAdapter() { +// +// @Override +// public void widgetSelected(SelectionEvent e) { +// super.widgetSelected(e); +// recalculateMappapleComponents(); +// } +// }); - componentMappingCombo = new Combo(cmposite, SWT.READ_ONLY | SWT.BORDER); - GridDataFactory.fillDefaults().grab(true, false).applyTo(componentMappingCombo); +// Label compojnentMappingLabel = new Label(cmposite, SWT.NONE); +// compojnentMappingLabel.setText("Select component"); +// +// componentMappingCombo = new Combo(cmposite, SWT.READ_ONLY | SWT.BORDER); +// GridDataFactory.fillDefaults().grab(true, false).applyTo(componentMappingCombo); } protected void recalculateMappapleComponents() { @@ -397,9 +436,9 @@ public class Functions { Label vertexMappingLabel = new Label(cmposite, SWT.NONE); vertexMappingLabel.setText("Default CRS"); - crsCombo = new Combo(cmposite, SWT.READ_ONLY | SWT.BORDER); - GridData textData = new GridData(SWT.FILL, SWT.CENTER, true, false); - crsCombo.setLayoutData(textData); + //crsCombo = new Combo(cmposite, SWT.READ_ONLY | SWT.BORDER); + //GridData textData = new GridData(SWT.FILL, SWT.CENTER, true, false); + //crsCombo.setLayoutData(textData); } @@ -407,11 +446,13 @@ public class Functions { protected void computeResult() { defaultVertexMapping = vertexMappings.get(vertexMappingCombo.getItem(vertexMappingCombo.getSelectionIndex())); defaultEdgeMapping = edgeMappings.get(edgeMappingCombo.getItem(edgeMappingCombo.getSelectionIndex())); - defaultCRS = crss.get(crsCombo.getItem(crsCombo.getSelectionIndex())); + rightClickVertexMapping = vertexMappings.get(rightClickMappingCombo.getItem(rightClickMappingCombo.getSelectionIndex())); + leftClickVertexMapping = vertexMappings.get(leftClickMappingCombo.getItem(leftClickMappingCombo.getSelectionIndex())); + //defaultCRS = crss.get(crsCombo.getItem(crsCombo.getSelectionIndex())); } public Resource getCRS() { - return defaultCRS; + return crss.get("EPSG_4326"); // this is only supported } } @@ -424,36 +465,34 @@ public class Functions { procedure.execute(null); return; } - Simantics.getSession().asyncRequest( - NewCompositeActionFactory.createCompositeRequest(target, defaultName, compositeType), - new Procedure() { - @Override - public void execute(Resource composite) { - Simantics.getSession().asyncRequest(new WriteRequest() { - - @Override - public void perform(WriteGraph graph) throws DatabaseException { - DistrictNetworkResource DN = DistrictNetworkResource.getInstance(graph); - Resource diagram = graph.getSingleObject(composite, ModelingResources.getInstance(graph).CompositeToDiagram); - graph.claim(diagram, DN.EdgeDefaultMapping, dialog.getDefaultEdgeMapping()); - graph.claim(diagram, DN.VertexDefaultMapping, dialog.getDefaultVertexMapping()); - graph.claim(diagram, DN.HasSpatialRefSystem, dialog.getCRS()); - - // Generated name prefix from composite name - String compositeName = graph.getRelatedValue2(composite, Layer0.getInstance(graph).HasName, Bindings.STRING); - graph.claimLiteral(diagram, Layer0X.getInstance(graph).HasGeneratedNamePrefix, "N" + compositeName.substring(compositeName.length() - 1, compositeName.length())); - } - }); - DefaultActions.asyncPerformDefaultAction(Simantics.getSession(), composite, false, false, true); - procedure.execute(composite); - } - - @Override - public void exception(Throwable t) { - LOGGER.error("Failed to create composite, see exception for details.", t); - procedure.exception(t); - } - }); + Simantics.getSession().asyncRequest(new WriteResultRequest() { + + @Override + public Resource perform(WriteGraph graph) throws DatabaseException { + return DistrictNetworkUtil.createNetworkDiagram(graph, target, compositeType, defaultName, + dialog.getDefaultEdgeMapping(), + dialog.getDefaultVertexMapping(), + dialog.getRightClickVertexMapping(), + dialog.getLeftClickVertexMapping(), + dialog.getCRS() + ); + } + + + }, new Procedure() { + + @Override + public void execute(Resource composite) { + DefaultActions.asyncPerformDefaultAction(Simantics.getSession(), composite, false, false, true); + procedure.execute(composite); + } + + @Override + public void exception(Throwable t) { + LOGGER.error("Failed to create composite, see exception for details.", t); + procedure.exception(t); + } + }); } public static Collection getDistrictDiagrams(ReadGraph graph) throws DatabaseException { @@ -462,9 +501,156 @@ public class Functions { DistrictNetworkResource DN = DistrictNetworkResource.getInstance(graph); Set results = new HashSet<>(); for (Resource indexRoot : indexRoots) { - Collection diagrams = ModelingUtils.searchByType(graph, indexRoot, DN.Diagram); + Collection diagrams = QueryIndexUtils.searchByType(graph, indexRoot, DN.Diagram); results.addAll(diagrams); } return results; } + + private static List listInstanceNames(ReadGraph graph, Variable context, Resource type) throws DatabaseException { + Resource indexRoot = Variables.getIndexRoot(graph, context); + DistrictNetworkResource DN = DistrictNetworkResource.getInstance(graph); + List properties = Collections.emptyList();//QueryIndexUtils.searchByType(graph, indexRoot, DN.Vertex_ScaleProperty); + return properties.stream() + .map(m -> createEnumeratedValue(graph, m)) + .map(EnumeratedValue::getName) + .collect(Collectors.toList()); + } + + @SCLValue(type = "ReadGraph -> Resource -> Variable -> b") + public static Function1 hasDiameterValue(ReadGraph graph, Resource resource, Variable context) throws DatabaseException { + return directPropertyValueFunction(DistrictNetworkResource.getInstance(graph).Edge_HasDiameter, 0); + } + + @SCLValue(type = "ReadGraph -> Resource -> Variable -> b") + public static Function1 hasNominalMassFlowValue(ReadGraph graph, Resource resource, Variable context) throws DatabaseException { + return directPropertyValueFunction(DistrictNetworkResource.getInstance(graph).Edge_HasNominalMassFlow, 0); + } + + @SCLValue(type = "ReadGraph -> Resource -> Variable -> b") + public static Function1 hasNominalSupplyPressure(ReadGraph graph, Resource resource, Variable context) throws DatabaseException { + return directPropertyValueFunction(DistrictNetworkResource.getInstance(graph).Vertex_HasSupplyPressure, 0); + } + + @SCLValue(type = "ReadGraph -> Resource -> Variable -> b") + public static Function1 hasElevation(ReadGraph graph, Resource resource, Variable context) throws DatabaseException { + return directPropertyValueFunction(DistrictNetworkResource.getInstance(graph).Vertex_HasElevation, 0); + } + + private static final Function1 ONE = new FunctionImpl1() { + private final Double ONE = 1.0; + @Override + public Double apply(Resource edge) { + return ONE; + } + @Override + public String toString() { + return "1"; + } + }; + + @SCLValue(type = "ReadGraph -> Resource -> Variable -> b") + public static Function1 constantOne(ReadGraph graph, Resource resource, Variable context) throws DatabaseException { + return ONE; + } + + private static Function1 directPropertyValueFunction(Resource property, double defaultValue) throws DatabaseException { + Double def = defaultValue; + return new FunctionImpl1() { + @Override + public Double apply(Resource edge) { + ReadGraph graph = (ReadGraph) SCLContext.getCurrent().get("graph"); + try { + Double d = graph.getPossibleRelatedValue(edge, property, Bindings.DOUBLE); + return d != null ? d : def; + } catch (DatabaseException e) { + LOGGER.error("Failed to evaluate property value", e); + return def; + } + } + }; + } + + private static class RangeValidator implements Function1 { + private double min; + private double max; + public RangeValidator(double min, double max) { + this.min = min; + this.max = max; + } + @Override + public String apply(String s) { + try { + double d = Double.parseDouble(s); + if (d < min) + return "Value must be greater than or equal to " + min; + if (d > max) + return "Value must be less than or equal to " + max; + return null; + } catch (NumberFormatException e) { + return "Specified value is not a number"; + } + } + } + + @SCLValue(type = "ReadGraph -> Resource -> Variable -> b") + public static Object hueValidator(ReadGraph graph, Resource r, Variable context) throws DatabaseException { + return new RangeValidator(0, 360); + } + + @SCLValue(type = "ReadGraph -> Resource -> Variable -> b") + public static Object saturationValidator(ReadGraph graph, Resource r, Variable context) throws DatabaseException { + return new RangeValidator(0, 100); + } + + @SCLValue(type = "ReadGraph -> Resource -> Variable -> b") + public static Object brightnessValidator(ReadGraph graph, Resource r, Variable context) throws DatabaseException { + String importEntry = null; + Resource root = Variables.getPossibleIndexRoot(graph, context); + if (root != null) { + Resource sclmain = Layer0Utils.getPossibleChild(graph, root, "SCLMain"); + if (sclmain != null) { + importEntry = graph.getPossibleURI(sclmain); + } + } + SCLContext ctx = SCLContext.getCurrent(); + Object oldGraph = ctx.put("graph", graph); + try { + return new BrightnessExpressionValidator( + importEntry != null + ? Arrays.asList(importEntry) + : Collections.emptyList()); + } finally { + ctx.put("graph", oldGraph); + } + } + + private static class BrightnessExpressionValidator implements Function1 { + private CommandSession session; + + public BrightnessExpressionValidator(List importEntries) { + this.session = new CommandSession(SCLOsgi.MODULE_REPOSITORY, SCLReportingHandler.DEFAULT); + this.session.setImportEntries(imports(importEntries)); + } + + private ArrayList imports(List entries) { + ArrayList result = new ArrayList<>(); + entries.stream().map(CommandSessionImportEntry::new).forEach(result::add); + if (entries.isEmpty()) + result.add(new CommandSessionImportEntry("Simantics/District/SCLMain")); + return result; + } + + @Override + public String apply(String s) { + s = s.trim(); + if (!s.startsWith("=")) + return "Expression expected, must start with '='"; + CompilationError[] errors = session.validate(s.substring(1)); + if(errors.length == 0) + return null; + return errors[0].description; + } + } + }