]> gerrit.simantics Code Review - simantics/district.git/blob - 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
1 package org.simantics.district.network.visualisations;
2
3 import java.util.ArrayList;
4 import java.util.Collection;
5 import java.util.HashMap;
6 import java.util.List;
7 import java.util.Map;
8 import java.util.Optional;
9 import java.util.function.Supplier;
10 import java.util.stream.Collectors;
11 import java.util.stream.Stream;
12
13 import org.simantics.NameLabelUtil;
14 import org.simantics.Simantics;
15 import org.simantics.db.ReadGraph;
16 import org.simantics.db.Resource;
17 import org.simantics.db.common.NamedResource;
18 import org.simantics.db.common.request.ObjectsWithSupertype;
19 import org.simantics.db.exception.DatabaseException;
20 import org.simantics.db.layer0.util.Layer0Utils;
21 import org.simantics.district.network.visualisations.model.DynamicArrowContribution;
22 import org.simantics.district.network.visualisations.model.DynamicColorContribution;
23 import org.simantics.district.network.visualisations.model.DynamicColorMap;
24 import org.simantics.district.network.visualisations.model.DynamicSizeContribution;
25 import org.simantics.district.network.visualisations.model.DynamicSizeMap;
26 import org.simantics.district.network.visualisations.model.DynamicSymbolContribution;
27 import org.simantics.district.network.visualisations.model.StaticInformationContribution;
28 import org.simantics.layer0.Layer0;
29 import org.simantics.scl.compiler.top.ValueNotFound;
30 import org.simantics.scl.osgi.SCLOsgi;
31 import org.simantics.scl.runtime.SCLContext;
32 import org.simantics.scl.runtime.tuple.Tuple0;
33 import org.simantics.structural.stubs.StructuralResource2;
34 import org.slf4j.Logger;
35 import org.slf4j.LoggerFactory;
36
37 public class DynamicVisualisationsContributions {
38
39     private static final Logger LOGGER = LoggerFactory.getLogger(DynamicVisualisationsContributions.class);
40
41     private static final String COMMON_DYNAMIC_VISUALISATIONS_MODULE = "CommonDynamicVisualisations";
42     private static final String COLOR_MAP_CONTRIBUTION = "colorMapContribution";
43     private static final String SIZE_MAP_CONTRIBUTION = "sizeMapContribution";
44     
45     private static final String DYNAMIC_VISUALISATIONS_CONTRIBUTION_MODULE = "DynamicVisualisationsContribution";
46     private static final String COLOR_CONTRIBUTION = "colorContribution";
47     private static final String SIZE_CONTRIBUTION = "sizeContribution";
48     private static final String ARROW_CONTRIBUTION = "arrowContribution";
49     private static final String STATIC_INFORMATION_CONTRIBUTION = "staticInformationContribution";
50     private static final String DYNAMIC_SYMBOL_CONTRIBUTION = "symbolContribution";
51
52     public static Map<String, DynamicColorMap> dynamicColorMaps(ReadGraph graph) throws DatabaseException {
53         List<Resource> sharedOntologies = Simantics.applySCL("Simantics/SharedOntologies", "getSharedOntologies", graph, Tuple0.INSTANCE);
54         
55         Map<String, DynamicColorMap> results = new HashMap<>();
56         Layer0 L0 = Layer0.getInstance(graph);
57         for (Resource sharedOntology : sharedOntologies) {
58             Resource sclModule = Layer0Utils.getPossibleChild(graph, sharedOntology, L0.SCLModule, COMMON_DYNAMIC_VISUALISATIONS_MODULE);
59             if (sclModule != null) {
60                 String moduleURI = graph.getURI(sclModule);
61                 Object oldGraph = SCLContext.getCurrent().get("graph");
62                 try {
63                     // let's put the graph to SCLContext for resolving the color maps
64                     SCLContext.getCurrent().put("graph", graph);
65                     @SuppressWarnings("unchecked")
66                     List<DynamicColorMap> result = (List<DynamicColorMap>) SCLOsgi.MODULE_REPOSITORY.getValue(moduleURI, COLOR_MAP_CONTRIBUTION);
67                     
68                     for (DynamicColorMap colorMap : result) {
69                         results.put(colorMap.getLabel(), colorMap);
70                     }
71                 } catch (ValueNotFound e) {
72                     // ignore
73                     LOGGER.debug("Dynamic color maps not found for {}", sclModule, e);
74                 } finally {
75                     SCLContext.getCurrent().put("graph", oldGraph);
76                 }
77             }
78         }
79         return results;
80     }
81
82     public static Map<String, DynamicSizeMap> dynamicSizeMaps(ReadGraph graph) throws DatabaseException {
83         List<Resource> sharedOntologies = Simantics.applySCL("Simantics/SharedOntologies", "getSharedOntologies", graph, Tuple0.INSTANCE);
84         
85         Map<String, DynamicSizeMap> results = new HashMap<>();
86         Layer0 L0 = Layer0.getInstance(graph);
87         for (Resource sharedOntology : sharedOntologies) {
88             Resource sclModule = Layer0Utils.getPossibleChild(graph, sharedOntology, L0.SCLModule, COMMON_DYNAMIC_VISUALISATIONS_MODULE);
89             if (sclModule != null) {
90                 String moduleURI = graph.getURI(sclModule);
91                 Object oldGraph = SCLContext.getCurrent().get("graph");
92                 try {
93                     // let's put the graph to SCLContext for resolving the color maps
94                     SCLContext.getCurrent().put("graph", graph);
95                     @SuppressWarnings("unchecked")
96                     List<DynamicSizeMap> result = (List<DynamicSizeMap>) SCLOsgi.MODULE_REPOSITORY.getValue(moduleURI, SIZE_MAP_CONTRIBUTION);
97                     
98                     for (DynamicSizeMap sizeMap : result) {
99                         results.put(sizeMap.getLabel(), sizeMap);
100                     }
101                 } catch (ValueNotFound e) {
102                     // ignore
103                     LOGGER.debug("Dynamic size maps not found for {}", sclModule, e);
104                 } finally {
105                     SCLContext.getCurrent().put("graph", oldGraph);
106                 }
107             }
108         }
109         return results;
110     }
111
112     public static Collection<DynamicColoringObject> dynamicColoringObjects(ReadGraph graph) throws DatabaseException {
113         
114         List<Resource> sharedOntologies = Simantics.applySCL("Simantics/SharedOntologies", "getSharedOntologies", graph, Tuple0.INSTANCE);
115         
116         List<DynamicColoringObject> results = new ArrayList<>();
117         
118         for (Resource sharedOntology : sharedOntologies) {
119             Collection<Resource> findByType = graph.syncRequest(new ObjectsWithSupertype(sharedOntology, Layer0.getInstance(graph).ConsistsOf, StructuralResource2.getInstance(graph).Component));
120             //Collection<Resource> findByType = QueryIndexUtils.searchByType(graph, sharedOntology, );
121             for (Resource find : findByType) {
122                 NamedResource moduleType = new NamedResource(NameLabelUtil.modalName(graph, find), find);
123                 DynamicColoringObject dynamicColoringObject = dynamicColoringObject(graph, moduleType);
124                 if (dynamicColoringObject != null)
125                     results.add(dynamicColoringObject);
126             }
127         }
128         return results;
129     }
130     
131     public static Collection<DynamicSizingObject> dynamicSizingObjects(ReadGraph graph) throws DatabaseException {
132         
133         List<Resource> sharedOntologies = Simantics.applySCL("Simantics/SharedOntologies", "getSharedOntologies", graph, Tuple0.INSTANCE);
134         
135         List<DynamicSizingObject> results = new ArrayList<>();
136         
137         for (Resource sharedOntology : sharedOntologies) {
138             Collection<Resource> findByType = graph.syncRequest(new ObjectsWithSupertype(sharedOntology, Layer0.getInstance(graph).ConsistsOf, StructuralResource2.getInstance(graph).Component));
139             //Collection<Resource> findByType = QueryIndexUtils.searchByType(graph, sharedOntology, );
140             for (Resource find : findByType) {
141                 NamedResource moduleType = new NamedResource(NameLabelUtil.modalName(graph, find), find);
142                 DynamicSizingObject dynamicSizingObject = dynamicSizingObject(graph, moduleType);
143                 if (dynamicSizingObject != null)
144                     results.add(dynamicSizingObject);
145             }
146         }
147         return results;
148     }
149     
150     private static DynamicColoringObject dynamicColoringObject(ReadGraph graph, NamedResource moduleType) throws DatabaseException {
151         Layer0 L0 = Layer0.getInstance(graph);
152         Resource sclModule = Layer0Utils.getPossibleChild(graph, moduleType.getResource(), L0.SCLModule, DYNAMIC_VISUALISATIONS_CONTRIBUTION_MODULE);
153         if (sclModule != null) {
154             String moduleURI = graph.getURI(sclModule);
155             return new DynamicColoringObject(moduleType, getContributionSupplier(moduleURI, COLOR_CONTRIBUTION));
156         }
157         return null;
158     }
159
160     private static DynamicSizingObject dynamicSizingObject(ReadGraph graph, NamedResource moduleType) throws DatabaseException {
161         Layer0 L0 = Layer0.getInstance(graph);
162         Resource sclModule = Layer0Utils.getPossibleChild(graph, moduleType.getResource(), L0.SCLModule, DYNAMIC_VISUALISATIONS_CONTRIBUTION_MODULE);
163         if (sclModule != null) {
164             String moduleURI = graph.getURI(sclModule);
165             return new DynamicSizingObject(moduleType, getContributionSupplier(moduleURI, SIZE_CONTRIBUTION));
166         }
167         return null;
168     }
169
170     public static Collection<DynamicArrowObject> dynamicEdgeArrowObjects(ReadGraph graph) throws DatabaseException {
171         
172         List<Resource> sharedOntologies = Simantics.applySCL("Simantics/SharedOntologies", "getSharedOntologies", graph, Tuple0.INSTANCE);
173         
174         List<DynamicArrowObject> results = new ArrayList<>();
175         
176         for (Resource sharedOntology : sharedOntologies) {
177             Collection<Resource> findByType = graph.syncRequest(new ObjectsWithSupertype(sharedOntology, Layer0.getInstance(graph).ConsistsOf, StructuralResource2.getInstance(graph).Component));
178             //Collection<Resource> findByType = QueryIndexUtils.searchByType(graph, sharedOntology, );
179             for (Resource find : findByType) {
180                 NamedResource moduleType = new NamedResource(NameLabelUtil.modalName(graph, find), find);
181                 DynamicArrowObject dynamicarrowObject = dynamicEdgeArrowObject(graph, moduleType);
182                 if (dynamicarrowObject != null)
183                     results.add(dynamicarrowObject);
184             }
185         }
186         return results;
187     }
188     
189     private static DynamicArrowObject dynamicEdgeArrowObject(ReadGraph graph, NamedResource moduleType) throws DatabaseException {
190         Layer0 L0 = Layer0.getInstance(graph);
191         Resource sclModule = Layer0Utils.getPossibleChild(graph, moduleType.getResource(), L0.SCLModule, DYNAMIC_VISUALISATIONS_CONTRIBUTION_MODULE);
192         if (sclModule != null) {
193             String moduleURI = graph.getURI(sclModule);
194             return new DynamicArrowObject(moduleType, getContributionSupplier(moduleURI, ARROW_CONTRIBUTION));
195         }
196         return null;
197     }
198
199     private static <T> Supplier<Stream<T>> getContributionSupplier(String uri, String expressionText) {
200         return () -> {
201             try {
202                 @SuppressWarnings("unchecked")
203                 List<T> result = (List<T>) SCLOsgi.MODULE_REPOSITORY.getValue(uri, expressionText);
204                 return result.stream();//result.stream().map(DynamicColorContribution::fromTuple9);
205             } catch (ValueNotFound e) {
206                 LOGGER.trace("Could not find contributions for module {} and expression {}", uri, expressionText, e);
207                 //throw new RuntimeException(e);
208                 return Stream.empty();
209             }
210         };
211     }
212
213     public static Collection<StaticInformationContributionObject> staticInformationContributionObjects(ReadGraph graph) throws DatabaseException {
214         
215         List<Resource> sharedOntologies = Simantics.applySCL("Simantics/SharedOntologies", "getSharedOntologies", graph, Tuple0.INSTANCE);
216         
217         List<StaticInformationContributionObject> results = new ArrayList<>();
218         
219         for (Resource sharedOntology : sharedOntologies) {
220             Collection<Resource> findByType = graph.syncRequest(new ObjectsWithSupertype(sharedOntology, Layer0.getInstance(graph).ConsistsOf, StructuralResource2.getInstance(graph).Component));
221             //Collection<Resource> findByType = QueryIndexUtils.searchByType(graph, sharedOntology, );
222             for (Resource find : findByType) {
223                 NamedResource moduleType = new NamedResource(NameLabelUtil.modalName(graph, find), find);
224                 StaticInformationContributionObject staticInformationContributionObject = staticInformationContributionObject(graph, moduleType);
225                 if (staticInformationContributionObject != null)
226                     results.add(staticInformationContributionObject);
227             }
228         }
229         return results;
230     }
231     
232     private static StaticInformationContributionObject staticInformationContributionObject(ReadGraph graph, NamedResource moduleType) throws DatabaseException {
233         Layer0 L0 = Layer0.getInstance(graph);
234         Resource sclModule = Layer0Utils.getPossibleChild(graph, moduleType.getResource(), L0.SCLModule, DYNAMIC_VISUALISATIONS_CONTRIBUTION_MODULE);
235         if (sclModule != null) {
236             String moduleURI = graph.getURI(sclModule);
237             return new StaticInformationContributionObject(moduleType, getContributionSupplier(moduleURI, STATIC_INFORMATION_CONTRIBUTION));
238         }
239         return null;
240     }
241
242     public static Collection<DynamicSymbolContributionObject> dynamicSymbolContributionObjects(ReadGraph graph) throws DatabaseException {
243         
244         List<Resource> sharedOntologies = Simantics.applySCL("Simantics/SharedOntologies", "getSharedOntologies", graph, Tuple0.INSTANCE);
245         List<DynamicSymbolContributionObject> results = new ArrayList<>();
246         
247         for (Resource sharedOntology : sharedOntologies) {
248             Collection<Resource> findByType = graph.syncRequest(new ObjectsWithSupertype(sharedOntology, Layer0.getInstance(graph).ConsistsOf, StructuralResource2.getInstance(graph).Component));
249             //Collection<Resource> findByType = QueryIndexUtils.searchByType(graph, sharedOntology, );
250             for (Resource find : findByType) {
251                 NamedResource moduleType = new NamedResource(NameLabelUtil.modalName(graph, find), find);
252                 DynamicSymbolContributionObject dynamicSymbolContributionObject = dynamicSymbolContributionObject(graph, moduleType);
253                 if (dynamicSymbolContributionObject != null)
254                     results.add(dynamicSymbolContributionObject);
255             }
256         }
257         return results;
258     }
259     
260     private static DynamicSymbolContributionObject dynamicSymbolContributionObject(ReadGraph graph, NamedResource moduleType) throws DatabaseException {
261         Layer0 L0 = Layer0.getInstance(graph);
262         Resource sclModule = Layer0Utils.getPossibleChild(graph, moduleType.getResource(), L0.SCLModule, DYNAMIC_VISUALISATIONS_CONTRIBUTION_MODULE);
263         if (sclModule != null) {
264             String moduleURI = graph.getURI(sclModule);
265             return new DynamicSymbolContributionObject(moduleType, getContributionSupplier(moduleURI, DYNAMIC_SYMBOL_CONTRIBUTION));
266         }
267         return null;
268     }
269
270     public static class DynamicArrowObject {
271
272         private final NamedResource arrowObject;
273         private final Supplier<Stream<DynamicArrowContribution>> arrowContributionSupplier;
274         private Map<String, DynamicArrowContribution> arrowContributions;
275
276         public DynamicArrowObject(NamedResource coloringObject, Supplier<Stream<DynamicArrowContribution>> arrowContributionSupplier) {
277             this.arrowObject = coloringObject;
278             this.arrowContributionSupplier = arrowContributionSupplier;
279         }
280
281         public NamedResource getArrowObject() {
282             return arrowObject;
283         }
284
285         public Map<String, DynamicArrowContribution> getArrowContributions() {
286             if (arrowContributions == null)
287                 arrowContributions = arrowContributionSupplier.get().collect(Collectors.toMap(c -> c.getLabel(), c -> c));
288             return arrowContributions;
289         }
290     }
291
292     public static class StaticInformationContributionObject {
293
294         private final NamedResource staticInformationContributionObject;
295         private final Supplier<Stream<StaticInformationContribution>> staticInformationContributionSupplier;
296         private Map<String, StaticInformationContribution> staticInformationContributions;
297
298         public StaticInformationContributionObject(NamedResource staticInformationContributionObject, Supplier<Stream<StaticInformationContribution>> staticInformationContributionSupplier) {
299             this.staticInformationContributionObject = staticInformationContributionObject;
300             this.staticInformationContributionSupplier = staticInformationContributionSupplier;
301         }
302
303         public NamedResource getStaticInformationContributionObject() {
304             return staticInformationContributionObject;
305         }
306
307         public Map<String, StaticInformationContribution> getStaticInformationContributions() {
308             if (staticInformationContributions == null)
309                 staticInformationContributions = staticInformationContributionSupplier.get().collect(Collectors.toMap(c -> c.getLabel(), c -> c));
310             return staticInformationContributions;
311         }
312     }
313
314     public static class DynamicSymbolContributionObject {
315
316         private final NamedResource dynamicSymbolContributionObject;
317         private final Supplier<Stream<DynamicSymbolContribution>> dynamicSymbolContributionSupplier;
318         private DynamicSymbolContribution dynamicSymbolContribution;
319
320         public DynamicSymbolContributionObject(NamedResource dynamicSymbolContributionObject, Supplier<Stream<DynamicSymbolContribution>> dynamicSymbolContributionSupplier) {
321             this.dynamicSymbolContributionObject = dynamicSymbolContributionObject;
322             this.dynamicSymbolContributionSupplier = dynamicSymbolContributionSupplier;
323         }
324
325         public NamedResource getDynamicSymbolContributionObject() {
326             return dynamicSymbolContributionObject;
327         }
328
329         public DynamicSymbolContribution getDynamicSymbolContribution() {
330             if (dynamicSymbolContribution == null) {
331                 Optional<DynamicSymbolContribution> findFirst = dynamicSymbolContributionSupplier.get().findFirst();
332                 findFirst.ifPresent(dsc -> {
333                     dynamicSymbolContribution = dsc;
334                 });
335             }
336             return dynamicSymbolContribution;
337         }
338
339         public void resolveSymbols(ReadGraph graph) {
340             SCLContext current = SCLContext.getCurrent();
341             Object oldGraph = current.put("graph", graph);
342             try {
343                 DynamicSymbolContribution dsc = getDynamicSymbolContribution();
344                 if (dsc != null)
345                     dsc.resolveSymbolMap();
346             } finally {
347                 current.put("graph", oldGraph);
348             }
349         }
350     }
351
352     public static class DynamicColoringObject {
353
354         private final NamedResource coloringObject;
355         private final Supplier<Stream<DynamicColorContribution>> colorContributionSupplier;
356         private Map<String, DynamicColorContribution> colorContributions;
357
358         public DynamicColoringObject(NamedResource coloringObject, Supplier<Stream<DynamicColorContribution>> colorContributionSupplier) {
359             this.coloringObject = coloringObject;
360             this.colorContributionSupplier = colorContributionSupplier;
361         }
362
363         public NamedResource getColoringObject() {
364             return coloringObject;
365         }
366
367         public Map<String, DynamicColorContribution> getColorContributions() {
368             if (colorContributions == null)
369                 colorContributions = colorContributionSupplier.get().collect(Collectors.toMap(c -> c.getLabel(), c -> c));
370             return colorContributions;
371         }
372         
373         @Override
374         public String toString() {
375             return getClass().getSimpleName() + " [" + coloringObject.getName() + " (" + coloringObject.getResource() + "), contributions=" + colorContributionsToString() + "]";
376         }
377         
378         private String colorContributionsToString() {
379             
380             return colorContributions.keySet().stream().map(key -> {
381                 DynamicColorContribution dynamicColorContribution = colorContributions.get(key);
382                 return key + "=" + dynamicColorContribution.getLabel();
383             }).collect(Collectors.joining(", ", "{", "}"));
384         }
385     }
386
387     public static class DynamicColoringMap {
388
389         private final NamedResource coloringObject;
390         private final Supplier<Stream<DynamicColorMap>> colorContributionSupplier;
391         private Map<String, DynamicColorMap> colorContributions;
392
393         public DynamicColoringMap(NamedResource coloringObject, Supplier<Stream<DynamicColorMap>> colorContributionSupplier) {
394             this.coloringObject = coloringObject;
395             this.colorContributionSupplier = colorContributionSupplier;
396         }
397
398         public NamedResource getColoringObject() {
399             return coloringObject;
400         }
401
402         public Map<String, DynamicColorMap> getColorContributions() {
403             if (colorContributions == null)
404                 colorContributions = colorContributionSupplier.get().collect(Collectors.toMap(c -> c.getLabel(), c -> c));
405             return colorContributions;
406         }
407     }
408     
409     public static class DynamicSizingObject {
410
411         private final NamedResource sizingObject;
412         private final Supplier<Stream<DynamicSizeContribution>> sizeContributionSupplier;
413         private Map<String, DynamicSizeContribution> sizeContributions;
414         
415         public DynamicSizingObject(NamedResource coloringObject, Supplier<Stream<DynamicSizeContribution>> sizeContributionSupplier) {
416             this.sizingObject = coloringObject;
417             this.sizeContributionSupplier = sizeContributionSupplier;
418         }
419
420         public NamedResource getSizingObject() {
421             return sizingObject;
422         }
423
424         public Map<String, DynamicSizeContribution> getSizeContributions() {
425             if (sizeContributions == null)
426                 sizeContributions = sizeContributionSupplier.get().collect(Collectors.toMap(c -> c.getLabel(), c -> c));
427             return sizeContributions;
428         }
429     }
430 }