]> gerrit.simantics Code Review - simantics/district.git/blobdiff - org.simantics.district.network/src/org/simantics/district/network/visualisations/DynamicVisualisationsContributions.java
First version of district visualisations
[simantics/district.git] / org.simantics.district.network / src / org / simantics / district / network / visualisations / DynamicVisualisationsContributions.java
diff --git a/org.simantics.district.network/src/org/simantics/district/network/visualisations/DynamicVisualisationsContributions.java b/org.simantics.district.network/src/org/simantics/district/network/visualisations/DynamicVisualisationsContributions.java
new file mode 100644 (file)
index 0000000..45e52fb
--- /dev/null
@@ -0,0 +1,278 @@
+package org.simantics.district.network.visualisations;
+
+import java.util.ArrayList;
+import java.util.Collection;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+import java.util.function.Supplier;
+import java.util.stream.Collectors;
+import java.util.stream.Stream;
+
+import org.simantics.NameLabelUtil;
+import org.simantics.Simantics;
+import org.simantics.db.ReadGraph;
+import org.simantics.db.Resource;
+import org.simantics.db.common.NamedResource;
+import org.simantics.db.common.request.ObjectsWithSupertype;
+import org.simantics.db.exception.DatabaseException;
+import org.simantics.db.layer0.util.Layer0Utils;
+import org.simantics.district.network.visualisations.model.DynamicColorContribution;
+import org.simantics.district.network.visualisations.model.DynamicColorMap;
+import org.simantics.district.network.visualisations.model.DynamicSizeContribution;
+import org.simantics.district.network.visualisations.model.DynamicSizeMap;
+import org.simantics.layer0.Layer0;
+import org.simantics.scl.compiler.top.ValueNotFound;
+import org.simantics.scl.osgi.SCLOsgi;
+import org.simantics.scl.runtime.SCLContext;
+import org.simantics.scl.runtime.tuple.Tuple0;
+import org.simantics.structural.stubs.StructuralResource2;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+public class DynamicVisualisationsContributions {
+
+    private static final Logger LOGGER = LoggerFactory.getLogger(DynamicVisualisationsContributions.class);
+
+    private static final String COMMON_DYNAMIC_VISUALISATIONS_MODULE = "CommonDynamicVisualisations";
+    private static final String COLOR_MAP_CONTRIBUTION = "colorMapContribution";
+    private static final String SIZE_MAP_CONTRIBUTION = "sizeMapContribution";
+    
+    private static final String DYNAMIC_VISUALISATIONS_CONTRIBUTION_MODULE = "DynamicVisualisationsContribution";
+    private static final String COLOR_CONTRIBUTION = "colorContribution";
+    private static final String SIZE_CONTRIBUTION = "sizeContribution";
+
+    public static Map<String, DynamicColorMap> dynamicColorMaps(ReadGraph graph) throws DatabaseException {
+        List<Resource> sharedOntologies = Simantics.applySCL("Simantics/SharedOntologies", "getSharedOntologies", graph, Tuple0.INSTANCE);
+        
+        Map<String, DynamicColorMap> results = new HashMap<>();
+        Layer0 L0 = Layer0.getInstance(graph);
+        for (Resource sharedOntology : sharedOntologies) {
+            Resource sclModule = Layer0Utils.getPossibleChild(graph, sharedOntology, L0.SCLModule, COMMON_DYNAMIC_VISUALISATIONS_MODULE);
+            if (sclModule != null) {
+                String moduleURI = graph.getURI(sclModule);
+                Object oldGraph = SCLContext.getCurrent().get("graph");
+                try {
+                    // let's put the graph to SCLContext for resolving the color maps
+                    SCLContext.getCurrent().put("graph", graph);
+                    @SuppressWarnings("unchecked")
+                    List<DynamicColorMap> result = (List<DynamicColorMap>) SCLOsgi.MODULE_REPOSITORY.getValue(moduleURI, COLOR_MAP_CONTRIBUTION);
+                    
+                    for (DynamicColorMap colorMap : result) {
+                        results.put(colorMap.getLabel(), colorMap);
+                    }
+                } catch (ValueNotFound e) {
+                    e.printStackTrace();
+                } finally {
+                    SCLContext.getCurrent().put("graph", oldGraph);
+                }
+            }
+        }
+        return results;
+    }
+
+    public static Map<String, DynamicSizeMap> dynamicSizeMaps(ReadGraph graph) throws DatabaseException {
+        List<Resource> sharedOntologies = Simantics.applySCL("Simantics/SharedOntologies", "getSharedOntologies", graph, Tuple0.INSTANCE);
+        
+        Map<String, DynamicSizeMap> results = new HashMap<>();
+        Layer0 L0 = Layer0.getInstance(graph);
+        for (Resource sharedOntology : sharedOntologies) {
+            Resource sclModule = Layer0Utils.getPossibleChild(graph, sharedOntology, L0.SCLModule, COMMON_DYNAMIC_VISUALISATIONS_MODULE);
+            if (sclModule != null) {
+                String moduleURI = graph.getURI(sclModule);
+                Object oldGraph = SCLContext.getCurrent().get("graph");
+                try {
+                    // let's put the graph to SCLContext for resolving the color maps
+                    SCLContext.getCurrent().put("graph", graph);
+                    @SuppressWarnings("unchecked")
+                    List<DynamicSizeMap> result = (List<DynamicSizeMap>) SCLOsgi.MODULE_REPOSITORY.getValue(moduleURI, SIZE_MAP_CONTRIBUTION);
+                    
+                    for (DynamicSizeMap sizeMap : result) {
+                        results.put(sizeMap.getLabel(), sizeMap);
+                    }
+                } catch (ValueNotFound e) {
+                    e.printStackTrace();
+                } finally {
+                    SCLContext.getCurrent().put("graph", oldGraph);
+                }
+            }
+        }
+        return results;
+    }
+
+    public static Collection<DynamicColoringObject> dynamicColoringObjects(ReadGraph graph) throws DatabaseException {
+        
+        List<Resource> sharedOntologies = Simantics.applySCL("Simantics/SharedOntologies", "getSharedOntologies", graph, Tuple0.INSTANCE);
+        
+        List<DynamicColoringObject> results = new ArrayList<>();
+        
+        for (Resource sharedOntology : sharedOntologies) {
+            Collection<Resource> findByType = graph.syncRequest(new ObjectsWithSupertype(sharedOntology, Layer0.getInstance(graph).ConsistsOf, StructuralResource2.getInstance(graph).Component));
+            //Collection<Resource> findByType = QueryIndexUtils.searchByType(graph, sharedOntology, );
+            for (Resource find : findByType) {
+                NamedResource moduleType = new NamedResource(NameLabelUtil.modalName(graph, find), find);
+                DynamicColoringObject dynamicColoringObject = dynamicColoringObject(graph, moduleType);
+                if (dynamicColoringObject != null)
+                    results.add(dynamicColoringObject);
+            }
+        }
+        return results;
+    }
+    
+    public static Collection<DynamicSizingObject> dynamicSizingObjects(ReadGraph graph) throws DatabaseException {
+        
+        List<Resource> sharedOntologies = Simantics.applySCL("Simantics/SharedOntologies", "getSharedOntologies", graph, Tuple0.INSTANCE);
+        
+        List<DynamicSizingObject> results = new ArrayList<>();
+        
+        for (Resource sharedOntology : sharedOntologies) {
+            Collection<Resource> findByType = graph.syncRequest(new ObjectsWithSupertype(sharedOntology, Layer0.getInstance(graph).ConsistsOf, StructuralResource2.getInstance(graph).Component));
+            //Collection<Resource> findByType = QueryIndexUtils.searchByType(graph, sharedOntology, );
+            for (Resource find : findByType) {
+                NamedResource moduleType = new NamedResource(NameLabelUtil.modalName(graph, find), find);
+                DynamicSizingObject dynamicSizingObject = dynamicSizingObject(graph, moduleType);
+                if (dynamicSizingObject != null)
+                    results.add(dynamicSizingObject);
+            }
+        }
+        return results;
+    }
+    
+    private static DynamicColoringObject dynamicColoringObject(ReadGraph graph, NamedResource moduleType) throws DatabaseException {
+        Layer0 L0 = Layer0.getInstance(graph);
+        Resource sclModule = Layer0Utils.getPossibleChild(graph, moduleType.getResource(), L0.SCLModule, DYNAMIC_VISUALISATIONS_CONTRIBUTION_MODULE);
+        if (sclModule != null) {
+            String moduleURI = graph.getURI(sclModule);
+            return new DynamicColoringObject(moduleType, getDynamicColorContributionSupplier(moduleURI, COLOR_CONTRIBUTION));
+        }
+        return null;
+    }
+    
+//    private static DynamicColoringMap dynamicColoringMap(ReadGraph graph, NamedResource moduleType) throws DatabaseException {
+//        Layer0 L0 = Layer0.getInstance(graph);
+//        Resource sclModule = Layer0Utils.getPossibleChild(graph, moduleType.getResource(), L0.SCLModule, DYNAMIC_VISUALISATIONS_CONTRIBUTION_MODULE);
+//        if (sclModule != null) {
+//            String moduleURI = graph.getURI(sclModule);
+//            return new DynamicColoringMap(moduleType, getDynamicColoringMapSupplier(moduleURI, COLOR_CONTRIBUTION));
+//        }
+//        return null;
+//    }
+    
+    private static DynamicSizingObject dynamicSizingObject(ReadGraph graph, NamedResource moduleType) throws DatabaseException {
+        Layer0 L0 = Layer0.getInstance(graph);
+        Resource sclModule = Layer0Utils.getPossibleChild(graph, moduleType.getResource(), L0.SCLModule, DYNAMIC_VISUALISATIONS_CONTRIBUTION_MODULE);
+        if (sclModule != null) {
+            String moduleURI = graph.getURI(sclModule);
+            return new DynamicSizingObject(moduleType, getDynamicSizeContributionSupplier(moduleURI, SIZE_CONTRIBUTION));
+        }
+        return null;
+    }
+
+    private static Supplier<Stream<DynamicColorMap>> getDynamicColorMapSupplier(String uri, String expressionText) {
+        return () -> {
+            try {
+                @SuppressWarnings("unchecked")
+                List<DynamicColorMap> result = (List<DynamicColorMap>) SCLOsgi.MODULE_REPOSITORY.getValue(uri, expressionText);
+                return result.stream();//result.stream().map(DynamicColorContribution::fromTuple9);
+            } catch (ValueNotFound e) {
+                LOGGER.error("Could not find contributions", e);
+                //throw new RuntimeException(e);
+                return Stream.empty();
+            }
+        };
+    }
+
+    private static Supplier<Stream<DynamicColorContribution>> getDynamicColorContributionSupplier(String uri, String expressionText) {
+        return () -> {
+            try {
+                @SuppressWarnings("unchecked")
+                List<DynamicColorContribution> result = (List<DynamicColorContribution>) SCLOsgi.MODULE_REPOSITORY.getValue(uri, expressionText);
+                return result.stream();//result.stream().map(DynamicColorContribution::fromTuple9);
+            } catch (ValueNotFound e) {
+                LOGGER.error("Could not find contributions", e);
+                //throw new RuntimeException(e);
+                return Stream.empty();
+            }
+        };
+    }
+
+    private static Supplier<Stream<DynamicSizeContribution>> getDynamicSizeContributionSupplier(String uri, String expressionText) {
+        return () -> {
+            try {
+                @SuppressWarnings("unchecked")
+                List<DynamicSizeContribution> result = (List<DynamicSizeContribution>) SCLOsgi.MODULE_REPOSITORY.getValue(uri, expressionText);
+                return result.stream();//result.stream().map(DynamicColorContribution::fromTuple9);
+            } catch (ValueNotFound e) {
+                LOGGER.error("Could not find contributions", e);
+                //throw new RuntimeException(e);
+                return Stream.empty();
+            }
+        };
+    }
+
+    public static class DynamicColoringObject {
+
+        private final NamedResource coloringObject;
+        private final Supplier<Stream<DynamicColorContribution>> colorContributionSupplier;
+        private Map<String, DynamicColorContribution> colorContributions;
+
+        public DynamicColoringObject(NamedResource coloringObject, Supplier<Stream<DynamicColorContribution>> colorContributionSupplier) {
+            this.coloringObject = coloringObject;
+            this.colorContributionSupplier = colorContributionSupplier;
+        }
+
+        public NamedResource getColoringObject() {
+            return coloringObject;
+        }
+
+        public Map<String, DynamicColorContribution> getColorContributions() {
+            if (colorContributions == null)
+                colorContributions = colorContributionSupplier.get().collect(Collectors.toMap(c -> c.getLabel(), c -> c));
+            return colorContributions;
+        }
+    }
+
+    public static class DynamicColoringMap {
+
+        private final NamedResource coloringObject;
+        private final Supplier<Stream<DynamicColorMap>> colorContributionSupplier;
+        private Map<String, DynamicColorMap> colorContributions;
+
+        public DynamicColoringMap(NamedResource coloringObject, Supplier<Stream<DynamicColorMap>> colorContributionSupplier) {
+            this.coloringObject = coloringObject;
+            this.colorContributionSupplier = colorContributionSupplier;
+        }
+
+        public NamedResource getColoringObject() {
+            return coloringObject;
+        }
+
+        public Map<String, DynamicColorMap> getColorContributions() {
+            if (colorContributions == null)
+                colorContributions = colorContributionSupplier.get().collect(Collectors.toMap(c -> c.getLabel(), c -> c));
+            return colorContributions;
+        }
+    }
+    
+    public static class DynamicSizingObject {
+
+        private final NamedResource sizingObject;
+        private final Supplier<Stream<DynamicSizeContribution>> sizeContributionSupplier;
+        private Map<String, DynamicSizeContribution> sizeContributions;
+        
+        public DynamicSizingObject(NamedResource coloringObject, Supplier<Stream<DynamicSizeContribution>> sizeContributionSupplier) {
+            this.sizingObject = coloringObject;
+            this.sizeContributionSupplier = sizeContributionSupplier;
+        }
+
+        public NamedResource getSizingObject() {
+            return sizingObject;
+        }
+
+        public Map<String, DynamicSizeContribution> getSizeContributions() {
+            if (sizeContributions == null)
+                sizeContributions = sizeContributionSupplier.get().collect(Collectors.toMap(c -> c.getLabel(), c -> c));
+            return sizeContributions;
+        }
+    }
+}