]> gerrit.simantics Code Review - simantics/district.git/blobdiff - org.simantics.district.network/src/org/simantics/district/network/visualisations/DynamicVisualisationsContributions.java
Move remaining profiles to visualisations for perf
[simantics/district.git] / org.simantics.district.network / src / org / simantics / district / network / visualisations / DynamicVisualisationsContributions.java
index 45e52fb55c48c99519f4a17ca7525efec3b4d9d9..3fd5a25f341a444a7e188f9eea60ab5e941adaee 100644 (file)
@@ -5,6 +5,7 @@ import java.util.Collection;
 import java.util.HashMap;
 import java.util.List;
 import java.util.Map;
+import java.util.Optional;
 import java.util.function.Supplier;
 import java.util.stream.Collectors;
 import java.util.stream.Stream;
@@ -17,10 +18,13 @@ 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.DynamicArrowContribution;
 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.district.network.visualisations.model.DynamicSymbolContribution;
+import org.simantics.district.network.visualisations.model.StaticInformationContribution;
 import org.simantics.layer0.Layer0;
 import org.simantics.scl.compiler.top.ValueNotFound;
 import org.simantics.scl.osgi.SCLOsgi;
@@ -41,6 +45,9 @@ public class DynamicVisualisationsContributions {
     private static final String DYNAMIC_VISUALISATIONS_CONTRIBUTION_MODULE = "DynamicVisualisationsContribution";
     private static final String COLOR_CONTRIBUTION = "colorContribution";
     private static final String SIZE_CONTRIBUTION = "sizeContribution";
+    private static final String ARROW_CONTRIBUTION = "arrowContribution";
+    private static final String STATIC_INFORMATION_CONTRIBUTION = "staticInformationContribution";
+    private static final String DYNAMIC_SYMBOL_CONTRIBUTION = "symbolContribution";
 
     public static Map<String, DynamicColorMap> dynamicColorMaps(ReadGraph graph) throws DatabaseException {
         List<Resource> sharedOntologies = Simantics.applySCL("Simantics/SharedOntologies", "getSharedOntologies", graph, Tuple0.INSTANCE);
@@ -62,7 +69,8 @@ public class DynamicVisualisationsContributions {
                         results.put(colorMap.getLabel(), colorMap);
                     }
                 } catch (ValueNotFound e) {
-                    e.printStackTrace();
+                    // ignore
+                    LOGGER.debug("Dynamic color maps not found for {}", sclModule, e);
                 } finally {
                     SCLContext.getCurrent().put("graph", oldGraph);
                 }
@@ -91,7 +99,8 @@ public class DynamicVisualisationsContributions {
                         results.put(sizeMap.getLabel(), sizeMap);
                     }
                 } catch (ValueNotFound e) {
-                    e.printStackTrace();
+                    // ignore
+                    LOGGER.debug("Dynamic size maps not found for {}", sclModule, e);
                 } finally {
                     SCLContext.getCurrent().put("graph", oldGraph);
                 }
@@ -143,71 +152,201 @@ public class DynamicVisualisationsContributions {
         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 new DynamicColoringObject(moduleType, getContributionSupplier(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 new DynamicSizingObject(moduleType, getContributionSupplier(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();
+    public static Collection<DynamicArrowObject> dynamicEdgeArrowObjects(ReadGraph graph) throws DatabaseException {
+        
+        List<Resource> sharedOntologies = Simantics.applySCL("Simantics/SharedOntologies", "getSharedOntologies", graph, Tuple0.INSTANCE);
+        
+        List<DynamicArrowObject> 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);
+                DynamicArrowObject dynamicarrowObject = dynamicEdgeArrowObject(graph, moduleType);
+                if (dynamicarrowObject != null)
+                    results.add(dynamicarrowObject);
             }
-        };
+        }
+        return results;
+    }
+    
+    private static DynamicArrowObject dynamicEdgeArrowObject(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 DynamicArrowObject(moduleType, getContributionSupplier(moduleURI, ARROW_CONTRIBUTION));
+        }
+        return null;
     }
 
-    private static Supplier<Stream<DynamicColorContribution>> getDynamicColorContributionSupplier(String uri, String expressionText) {
+    private static <T> Supplier<Stream<T>> getContributionSupplier(String uri, String expressionText) {
         return () -> {
             try {
                 @SuppressWarnings("unchecked")
-                List<DynamicColorContribution> result = (List<DynamicColorContribution>) SCLOsgi.MODULE_REPOSITORY.getValue(uri, expressionText);
+                List<T> result = (List<T>) SCLOsgi.MODULE_REPOSITORY.getValue(uri, expressionText);
                 return result.stream();//result.stream().map(DynamicColorContribution::fromTuple9);
             } catch (ValueNotFound e) {
-                LOGGER.error("Could not find contributions", e);
+                LOGGER.trace("Could not find contributions for module {} and expression {}", uri, expressionText, e);
                 //throw new RuntimeException(e);
                 return Stream.empty();
             }
         };
     }
 
-    private static Supplier<Stream<DynamicSizeContribution>> getDynamicSizeContributionSupplier(String uri, String expressionText) {
-        return () -> {
+    public static Collection<StaticInformationContributionObject> staticInformationContributionObjects(ReadGraph graph) throws DatabaseException {
+        
+        List<Resource> sharedOntologies = Simantics.applySCL("Simantics/SharedOntologies", "getSharedOntologies", graph, Tuple0.INSTANCE);
+        
+        List<StaticInformationContributionObject> 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);
+                StaticInformationContributionObject staticInformationContributionObject = staticInformationContributionObject(graph, moduleType);
+                if (staticInformationContributionObject != null)
+                    results.add(staticInformationContributionObject);
+            }
+        }
+        return results;
+    }
+    
+    private static StaticInformationContributionObject staticInformationContributionObject(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 StaticInformationContributionObject(moduleType, getContributionSupplier(moduleURI, STATIC_INFORMATION_CONTRIBUTION));
+        }
+        return null;
+    }
+
+    public static Collection<DynamicSymbolContributionObject> dynamicSymbolContributionObjects(ReadGraph graph) throws DatabaseException {
+        
+        List<Resource> sharedOntologies = Simantics.applySCL("Simantics/SharedOntologies", "getSharedOntologies", graph, Tuple0.INSTANCE);
+        List<DynamicSymbolContributionObject> 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);
+                DynamicSymbolContributionObject dynamicSymbolContributionObject = dynamicSymbolContributionObject(graph, moduleType);
+                if (dynamicSymbolContributionObject != null)
+                    results.add(dynamicSymbolContributionObject);
+            }
+        }
+        return results;
+    }
+    
+    private static DynamicSymbolContributionObject dynamicSymbolContributionObject(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 DynamicSymbolContributionObject(moduleType, getContributionSupplier(moduleURI, DYNAMIC_SYMBOL_CONTRIBUTION));
+        }
+        return null;
+    }
+
+    public static class DynamicArrowObject {
+
+        private final NamedResource arrowObject;
+        private final Supplier<Stream<DynamicArrowContribution>> arrowContributionSupplier;
+        private Map<String, DynamicArrowContribution> arrowContributions;
+
+        public DynamicArrowObject(NamedResource coloringObject, Supplier<Stream<DynamicArrowContribution>> arrowContributionSupplier) {
+            this.arrowObject = coloringObject;
+            this.arrowContributionSupplier = arrowContributionSupplier;
+        }
+
+        public NamedResource getArrowObject() {
+            return arrowObject;
+        }
+
+        public Map<String, DynamicArrowContribution> getArrowContributions() {
+            if (arrowContributions == null)
+                arrowContributions = arrowContributionSupplier.get().collect(Collectors.toMap(c -> c.getLabel(), c -> c));
+            return arrowContributions;
+        }
+    }
+
+    public static class StaticInformationContributionObject {
+
+        private final NamedResource staticInformationContributionObject;
+        private final Supplier<Stream<StaticInformationContribution>> staticInformationContributionSupplier;
+        private Map<String, StaticInformationContribution> staticInformationContributions;
+
+        public StaticInformationContributionObject(NamedResource staticInformationContributionObject, Supplier<Stream<StaticInformationContribution>> staticInformationContributionSupplier) {
+            this.staticInformationContributionObject = staticInformationContributionObject;
+            this.staticInformationContributionSupplier = staticInformationContributionSupplier;
+        }
+
+        public NamedResource getStaticInformationContributionObject() {
+            return staticInformationContributionObject;
+        }
+
+        public Map<String, StaticInformationContribution> getStaticInformationContributions() {
+            if (staticInformationContributions == null)
+                staticInformationContributions = staticInformationContributionSupplier.get().collect(Collectors.toMap(c -> c.getLabel(), c -> c));
+            return staticInformationContributions;
+        }
+    }
+
+    public static class DynamicSymbolContributionObject {
+
+        private final NamedResource dynamicSymbolContributionObject;
+        private final Supplier<Stream<DynamicSymbolContribution>> dynamicSymbolContributionSupplier;
+        private DynamicSymbolContribution dynamicSymbolContribution;
+
+        public DynamicSymbolContributionObject(NamedResource dynamicSymbolContributionObject, Supplier<Stream<DynamicSymbolContribution>> dynamicSymbolContributionSupplier) {
+            this.dynamicSymbolContributionObject = dynamicSymbolContributionObject;
+            this.dynamicSymbolContributionSupplier = dynamicSymbolContributionSupplier;
+        }
+
+        public NamedResource getDynamicSymbolContributionObject() {
+            return dynamicSymbolContributionObject;
+        }
+
+        public DynamicSymbolContribution getDynamicSymbolContribution() {
+            if (dynamicSymbolContribution == null) {
+                Optional<DynamicSymbolContribution> findFirst = dynamicSymbolContributionSupplier.get().findFirst();
+                findFirst.ifPresent(dsc -> {
+                    dynamicSymbolContribution = dsc;
+                });
+            }
+            return dynamicSymbolContribution;
+        }
+
+        public void resolveSymbols(ReadGraph graph) {
+            SCLContext current = SCLContext.getCurrent();
+            Object oldGraph = current.put("graph", graph);
             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();
+                DynamicSymbolContribution dsc = getDynamicSymbolContribution();
+                if (dsc != null)
+                    dsc.resolveSymbolMap();
+            } finally {
+                current.put("graph", oldGraph);
             }
-        };
+        }
     }
 
     public static class DynamicColoringObject {
@@ -230,6 +369,19 @@ public class DynamicVisualisationsContributions {
                 colorContributions = colorContributionSupplier.get().collect(Collectors.toMap(c -> c.getLabel(), c -> c));
             return colorContributions;
         }
+        
+        @Override
+        public String toString() {
+            return getClass().getSimpleName() + " [" + coloringObject.getName() + " (" + coloringObject.getResource() + "), contributions=" + colorContributionsToString() + "]";
+        }
+        
+        private String colorContributionsToString() {
+            
+            return colorContributions.keySet().stream().map(key -> {
+                DynamicColorContribution dynamicColorContribution = colorContributions.get(key);
+                return key + "=" + dynamicColorContribution.getLabel();
+            }).collect(Collectors.joining(", ", "{", "}"));
+        }
     }
 
     public static class DynamicColoringMap {