]> gerrit.simantics Code Review - simantics/district.git/blobdiff - org.simantics.district.network.ui/src/org/simantics/district/network/ui/function/Functions.java
First draft of vertex size adjusting district network diagram profiles
[simantics/district.git] / org.simantics.district.network.ui / src / org / simantics / district / network / ui / function / Functions.java
index 9748f87c8fc4980f77a767e84dde9d157b6885d4..04cd94cf46658f2d3c0bbb8431275d06f8640ad0 100644 (file)
@@ -41,18 +41,21 @@ import org.simantics.db.common.request.WriteRequest;
 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.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.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.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.ui.workbench.action.DefaultActions;
 import org.simantics.utils.ui.SWTUtils;
 import org.slf4j.Logger;
@@ -116,7 +119,7 @@ public class Functions {
 
     public static Map<String, Resource> getNetworkMappingsByType(ReadGraph graph, Resource element, Resource mappingType) throws DatabaseException {
         Resource indexRoot = graph.sync(new IndexRoot(element));
-        List<Resource> mappings = ModelingUtils.searchByType(graph, indexRoot, mappingType);
+        List<Resource> mappings = QueryIndexUtils.searchByType(graph, indexRoot, mappingType);
         Map<String, Resource> result = new HashMap<>(mappings.size());
         Layer0 L0 = Layer0.getInstance(graph);
         mappings.forEach(mapping -> {
@@ -135,7 +138,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<Resource> mappings = ModelingUtils.searchByType(graph, indexRoot, mappingType);
+        List<Resource> mappings = QueryIndexUtils.searchByType(graph, indexRoot, mappingType);
         Enumeration<Resource> enums = Enumeration
                 .make(mappings.stream().map(m -> createEnumeratedValue(graph, m)).collect(Collectors.toList()));
         
@@ -180,8 +183,11 @@ public class Functions {
     
     @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;
     }
     
     
@@ -213,7 +219,6 @@ public class Functions {
         private Map<String, Resource> edgeMappings = new HashMap<>();
         private Map<String, Resource> composites = new HashMap<>();
         private Map<String, Resource> crss = new HashMap<>();
-        private Map<String, Map<String, Resource>> components = new HashMap<>();
         
         private Resource defaultVertexMapping;
         private Resource defaultEdgeMapping;
@@ -254,10 +259,7 @@ public class Functions {
                     edgeMappings = getEdgeMappings(graph, configuration);
                     
                     composites = getComposites(graph, configuration);
-                    if (composites.size() > 0) {
-                        components = getComponents(graph, composites.get(0));
-                    }
-                    
+                  
                     crss = getCRSs(graph, configuration);
                     
                     composite.getDisplay().asyncExec(() -> {
@@ -282,16 +284,7 @@ public class Functions {
             return composite;
         }
         
-        protected Map<String, Map<String, Resource>> getComponents(ReadGraph graph, Resource resource) {
-            // TODO Auto-generated method stub
-            return null;
-        }
-
         protected Map<String, Resource> getComposites(ReadGraph graph, Resource element) throws DatabaseException {
-            
-            Resource indexRoot = graph.sync(new IndexRoot(element));
-            List<Resource> diagrams = ModelingUtils.searchByType(graph, indexRoot, DiagramResource.getInstance(graph).Diagram);
-            
             List<Resource> nonDistrictComposites = composites.values().stream().filter(comp -> {
                 try {
                     return !graph.isInstanceOf(comp, DistrictNetworkResource.getInstance(graph).Composite);
@@ -462,15 +455,32 @@ public class Functions {
         DistrictNetworkResource DN = DistrictNetworkResource.getInstance(graph);
         Set<Resource> results = new HashSet<>();
         for (Resource indexRoot : indexRoots) {
-            Collection<Resource> diagrams = ModelingUtils.searchByType(graph, indexRoot, DN.Diagram);
+            Collection<Resource> diagrams = QueryIndexUtils.searchByType(graph, indexRoot, DN.Diagram);
             results.addAll(diagrams);
         }
         return results;
     }
 
+    private static List<String> listInstanceNames(ReadGraph graph, Variable context, Resource type) throws DatabaseException {
+        Resource indexRoot = Variables.getIndexRoot(graph, context);
+        DistrictNetworkResource DN = DistrictNetworkResource.getInstance(graph);
+        List<Resource> properties = 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 Object edgeThicknessPropertyEnumerationValues(ReadGraph graph, Resource resource, Variable context) throws DatabaseException {
-        return Collections.emptyList();
+        DistrictNetworkResource DN = DistrictNetworkResource.getInstance(graph);
+        return listInstanceNames(graph, context, DN.Edge_ThicknessProperty);
+    }
+
+    @SCLValue(type = "ReadGraph -> Resource -> Variable -> b")
+    public static Object nodeScalePropertyEnumerationValues(ReadGraph graph, Resource resource, Variable context) throws DatabaseException {
+        DistrictNetworkResource DN = DistrictNetworkResource.getInstance(graph);
+        return listInstanceNames(graph, context, DN.Vertex_ScaleProperty);
     }
 
     @SCLValue(type = "ReadGraph -> Resource -> Variable -> b")
@@ -480,4 +490,61 @@ public class Functions {
         return baseMappingModifier(graph, diagram, DN.Diagram_edgeThicknessProperty, DN.Edge_ThicknessProperty, context);
     }
 
+    @SCLValue(type = "ReadGraph -> Resource -> Variable -> b")
+    public static Object nodeScalePropertyModifier(ReadGraph graph, Resource resource, Variable context) throws DatabaseException {
+        Resource diagram = resolveElement(graph, context);
+        DistrictNetworkResource DN = DistrictNetworkResource.getInstance(graph);
+        return baseMappingModifier(graph, diagram, DN.Diagram_nodeScaleProperty, DN.Vertex_ScaleProperty, context);
+    }
+
+    @SCLValue(type = "ReadGraph -> Resource -> Variable -> b")
+    public static Function1<Resource, Double> 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<Resource, Double> 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<Resource, Double> 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<Resource, Double> hasElevation(ReadGraph graph, Resource resource, Variable context) throws DatabaseException {
+        return directPropertyValueFunction(DistrictNetworkResource.getInstance(graph).Vertex_HasElevation, 0);
+    }
+
+    private static final Function1<Resource, Double> ONE = new FunctionImpl1<Resource, Double>() {
+        private final Double ONE = 1.0;
+        @Override
+        public Double apply(Resource edge) {
+            return ONE;
+        }
+    };
+
+    @SCLValue(type = "ReadGraph -> Resource -> Variable -> b")
+    public static Function1<Resource, Double> constantOne(ReadGraph graph, Resource resource, Variable context) throws DatabaseException {
+        return ONE;
+    }
+
+    private static Function1<Resource, Double> directPropertyValueFunction(Resource property, double defaultValue) throws DatabaseException {
+        Double def = defaultValue;
+        return new FunctionImpl1<Resource, Double>() {
+            @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;
+                }
+            }
+        };
+    }
+
 }