]> gerrit.simantics Code Review - simantics/district.git/blob
6c25d237886fc82825045d7f45e08c257511a5e6
[simantics/district.git] /
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.function.Supplier;
9 import java.util.stream.Collectors;
10 import java.util.stream.Stream;
11
12 import org.simantics.NameLabelUtil;
13 import org.simantics.Simantics;
14 import org.simantics.db.ReadGraph;
15 import org.simantics.db.Resource;
16 import org.simantics.db.common.NamedResource;
17 import org.simantics.db.common.request.ObjectsWithSupertype;
18 import org.simantics.db.exception.DatabaseException;
19 import org.simantics.db.layer0.util.Layer0Utils;
20 import org.simantics.district.network.visualisations.model.DynamicArrowContribution;
21 import org.simantics.district.network.visualisations.model.DynamicColorContribution;
22 import org.simantics.district.network.visualisations.model.DynamicColorMap;
23 import org.simantics.district.network.visualisations.model.DynamicSizeContribution;
24 import org.simantics.district.network.visualisations.model.DynamicSizeMap;
25 import org.simantics.layer0.Layer0;
26 import org.simantics.scl.compiler.top.ValueNotFound;
27 import org.simantics.scl.osgi.SCLOsgi;
28 import org.simantics.scl.runtime.SCLContext;
29 import org.simantics.scl.runtime.tuple.Tuple0;
30 import org.simantics.structural.stubs.StructuralResource2;
31 import org.slf4j.Logger;
32 import org.slf4j.LoggerFactory;
33
34 public class DynamicVisualisationsContributions {
35
36     private static final Logger LOGGER = LoggerFactory.getLogger(DynamicVisualisationsContributions.class);
37
38     private static final String COMMON_DYNAMIC_VISUALISATIONS_MODULE = "CommonDynamicVisualisations";
39     private static final String COLOR_MAP_CONTRIBUTION = "colorMapContribution";
40     private static final String SIZE_MAP_CONTRIBUTION = "sizeMapContribution";
41     
42     private static final String DYNAMIC_VISUALISATIONS_CONTRIBUTION_MODULE = "DynamicVisualisationsContribution";
43     private static final String COLOR_CONTRIBUTION = "colorContribution";
44     private static final String SIZE_CONTRIBUTION = "sizeContribution";
45     private static final String ARROW_CONTRIBUTION = "arrowContribution";
46
47     public static Map<String, DynamicColorMap> dynamicColorMaps(ReadGraph graph) throws DatabaseException {
48         List<Resource> sharedOntologies = Simantics.applySCL("Simantics/SharedOntologies", "getSharedOntologies", graph, Tuple0.INSTANCE);
49         
50         Map<String, DynamicColorMap> results = new HashMap<>();
51         Layer0 L0 = Layer0.getInstance(graph);
52         for (Resource sharedOntology : sharedOntologies) {
53             Resource sclModule = Layer0Utils.getPossibleChild(graph, sharedOntology, L0.SCLModule, COMMON_DYNAMIC_VISUALISATIONS_MODULE);
54             if (sclModule != null) {
55                 String moduleURI = graph.getURI(sclModule);
56                 Object oldGraph = SCLContext.getCurrent().get("graph");
57                 try {
58                     // let's put the graph to SCLContext for resolving the color maps
59                     SCLContext.getCurrent().put("graph", graph);
60                     @SuppressWarnings("unchecked")
61                     List<DynamicColorMap> result = (List<DynamicColorMap>) SCLOsgi.MODULE_REPOSITORY.getValue(moduleURI, COLOR_MAP_CONTRIBUTION);
62                     
63                     for (DynamicColorMap colorMap : result) {
64                         results.put(colorMap.getLabel(), colorMap);
65                     }
66                 } catch (ValueNotFound e) {
67                     e.printStackTrace();
68                 } finally {
69                     SCLContext.getCurrent().put("graph", oldGraph);
70                 }
71             }
72         }
73         return results;
74     }
75
76     public static Map<String, DynamicSizeMap> dynamicSizeMaps(ReadGraph graph) throws DatabaseException {
77         List<Resource> sharedOntologies = Simantics.applySCL("Simantics/SharedOntologies", "getSharedOntologies", graph, Tuple0.INSTANCE);
78         
79         Map<String, DynamicSizeMap> results = new HashMap<>();
80         Layer0 L0 = Layer0.getInstance(graph);
81         for (Resource sharedOntology : sharedOntologies) {
82             Resource sclModule = Layer0Utils.getPossibleChild(graph, sharedOntology, L0.SCLModule, COMMON_DYNAMIC_VISUALISATIONS_MODULE);
83             if (sclModule != null) {
84                 String moduleURI = graph.getURI(sclModule);
85                 Object oldGraph = SCLContext.getCurrent().get("graph");
86                 try {
87                     // let's put the graph to SCLContext for resolving the color maps
88                     SCLContext.getCurrent().put("graph", graph);
89                     @SuppressWarnings("unchecked")
90                     List<DynamicSizeMap> result = (List<DynamicSizeMap>) SCLOsgi.MODULE_REPOSITORY.getValue(moduleURI, SIZE_MAP_CONTRIBUTION);
91                     
92                     for (DynamicSizeMap sizeMap : result) {
93                         results.put(sizeMap.getLabel(), sizeMap);
94                     }
95                 } catch (ValueNotFound e) {
96                     e.printStackTrace();
97                 } finally {
98                     SCLContext.getCurrent().put("graph", oldGraph);
99                 }
100             }
101         }
102         return results;
103     }
104
105     public static Collection<DynamicColoringObject> dynamicColoringObjects(ReadGraph graph) throws DatabaseException {
106         
107         List<Resource> sharedOntologies = Simantics.applySCL("Simantics/SharedOntologies", "getSharedOntologies", graph, Tuple0.INSTANCE);
108         
109         List<DynamicColoringObject> results = new ArrayList<>();
110         
111         for (Resource sharedOntology : sharedOntologies) {
112             Collection<Resource> findByType = graph.syncRequest(new ObjectsWithSupertype(sharedOntology, Layer0.getInstance(graph).ConsistsOf, StructuralResource2.getInstance(graph).Component));
113             //Collection<Resource> findByType = QueryIndexUtils.searchByType(graph, sharedOntology, );
114             for (Resource find : findByType) {
115                 NamedResource moduleType = new NamedResource(NameLabelUtil.modalName(graph, find), find);
116                 DynamicColoringObject dynamicColoringObject = dynamicColoringObject(graph, moduleType);
117                 if (dynamicColoringObject != null)
118                     results.add(dynamicColoringObject);
119             }
120         }
121         return results;
122     }
123     
124     public static Collection<DynamicSizingObject> dynamicSizingObjects(ReadGraph graph) throws DatabaseException {
125         
126         List<Resource> sharedOntologies = Simantics.applySCL("Simantics/SharedOntologies", "getSharedOntologies", graph, Tuple0.INSTANCE);
127         
128         List<DynamicSizingObject> results = new ArrayList<>();
129         
130         for (Resource sharedOntology : sharedOntologies) {
131             Collection<Resource> findByType = graph.syncRequest(new ObjectsWithSupertype(sharedOntology, Layer0.getInstance(graph).ConsistsOf, StructuralResource2.getInstance(graph).Component));
132             //Collection<Resource> findByType = QueryIndexUtils.searchByType(graph, sharedOntology, );
133             for (Resource find : findByType) {
134                 NamedResource moduleType = new NamedResource(NameLabelUtil.modalName(graph, find), find);
135                 DynamicSizingObject dynamicSizingObject = dynamicSizingObject(graph, moduleType);
136                 if (dynamicSizingObject != null)
137                     results.add(dynamicSizingObject);
138             }
139         }
140         return results;
141     }
142     
143     private static DynamicColoringObject dynamicColoringObject(ReadGraph graph, NamedResource moduleType) throws DatabaseException {
144         Layer0 L0 = Layer0.getInstance(graph);
145         Resource sclModule = Layer0Utils.getPossibleChild(graph, moduleType.getResource(), L0.SCLModule, DYNAMIC_VISUALISATIONS_CONTRIBUTION_MODULE);
146         if (sclModule != null) {
147             String moduleURI = graph.getURI(sclModule);
148             return new DynamicColoringObject(moduleType, getDynamicColorContributionSupplier(moduleURI, COLOR_CONTRIBUTION));
149         }
150         return null;
151     }
152     
153 //    private static DynamicColoringMap dynamicColoringMap(ReadGraph graph, NamedResource moduleType) throws DatabaseException {
154 //        Layer0 L0 = Layer0.getInstance(graph);
155 //        Resource sclModule = Layer0Utils.getPossibleChild(graph, moduleType.getResource(), L0.SCLModule, DYNAMIC_VISUALISATIONS_CONTRIBUTION_MODULE);
156 //        if (sclModule != null) {
157 //            String moduleURI = graph.getURI(sclModule);
158 //            return new DynamicColoringMap(moduleType, getDynamicColoringMapSupplier(moduleURI, COLOR_CONTRIBUTION));
159 //        }
160 //        return null;
161 //    }
162     
163     private static DynamicSizingObject dynamicSizingObject(ReadGraph graph, NamedResource moduleType) throws DatabaseException {
164         Layer0 L0 = Layer0.getInstance(graph);
165         Resource sclModule = Layer0Utils.getPossibleChild(graph, moduleType.getResource(), L0.SCLModule, DYNAMIC_VISUALISATIONS_CONTRIBUTION_MODULE);
166         if (sclModule != null) {
167             String moduleURI = graph.getURI(sclModule);
168             return new DynamicSizingObject(moduleType, getDynamicSizeContributionSupplier(moduleURI, SIZE_CONTRIBUTION));
169         }
170         return null;
171     }
172
173     public static Collection<DynamicArrowObject> dynamicEdgeArrowObjects(ReadGraph graph) throws DatabaseException {
174         
175         List<Resource> sharedOntologies = Simantics.applySCL("Simantics/SharedOntologies", "getSharedOntologies", graph, Tuple0.INSTANCE);
176         
177         List<DynamicArrowObject> results = new ArrayList<>();
178         
179         for (Resource sharedOntology : sharedOntologies) {
180             Collection<Resource> findByType = graph.syncRequest(new ObjectsWithSupertype(sharedOntology, Layer0.getInstance(graph).ConsistsOf, StructuralResource2.getInstance(graph).Component));
181             //Collection<Resource> findByType = QueryIndexUtils.searchByType(graph, sharedOntology, );
182             for (Resource find : findByType) {
183                 NamedResource moduleType = new NamedResource(NameLabelUtil.modalName(graph, find), find);
184                 DynamicArrowObject dynamicarrowObject = dynamicEdgeArrowObject(graph, moduleType);
185                 if (dynamicarrowObject != null)
186                     results.add(dynamicarrowObject);
187             }
188         }
189         return results;
190     }
191     
192     private static DynamicArrowObject dynamicEdgeArrowObject(ReadGraph graph, NamedResource moduleType) throws DatabaseException {
193         Layer0 L0 = Layer0.getInstance(graph);
194         Resource sclModule = Layer0Utils.getPossibleChild(graph, moduleType.getResource(), L0.SCLModule, DYNAMIC_VISUALISATIONS_CONTRIBUTION_MODULE);
195         if (sclModule != null) {
196             String moduleURI = graph.getURI(sclModule);
197             return new DynamicArrowObject(moduleType, getDynamicEdgeArrowContributionSupplier(moduleURI, ARROW_CONTRIBUTION));
198         }
199         return null;
200     }
201     
202     private static Supplier<Stream<DynamicColorMap>> getDynamicColorMapSupplier(String uri, String expressionText) {
203         return () -> {
204             try {
205                 @SuppressWarnings("unchecked")
206                 List<DynamicColorMap> result = (List<DynamicColorMap>) SCLOsgi.MODULE_REPOSITORY.getValue(uri, expressionText);
207                 return result.stream();//result.stream().map(DynamicColorContribution::fromTuple9);
208             } catch (ValueNotFound e) {
209                 LOGGER.error("Could not find contributions", e);
210                 //throw new RuntimeException(e);
211                 return Stream.empty();
212             }
213         };
214     }
215
216     private static Supplier<Stream<DynamicColorContribution>> getDynamicColorContributionSupplier(String uri, String expressionText) {
217         return () -> {
218             try {
219                 @SuppressWarnings("unchecked")
220                 List<DynamicColorContribution> result = (List<DynamicColorContribution>) SCLOsgi.MODULE_REPOSITORY.getValue(uri, expressionText);
221                 return result.stream();//result.stream().map(DynamicColorContribution::fromTuple9);
222             } catch (ValueNotFound e) {
223                 LOGGER.error("Could not find contributions", e);
224                 //throw new RuntimeException(e);
225                 return Stream.empty();
226             }
227         };
228     }
229
230     private static Supplier<Stream<DynamicArrowContribution>> getDynamicEdgeArrowContributionSupplier(String uri, String expressionText) {
231         return () -> {
232             try {
233                 @SuppressWarnings("unchecked")
234                 List<DynamicArrowContribution> result = (List<DynamicArrowContribution>) SCLOsgi.MODULE_REPOSITORY.getValue(uri, expressionText);
235                 return result.stream();//result.stream().map(DynamicColorContribution::fromTuple9);
236             } catch (ValueNotFound e) {
237                 LOGGER.error("Could not find contributions", e);
238                 //throw new RuntimeException(e);
239                 return Stream.empty();
240             }
241         };
242     }
243     
244     private static Supplier<Stream<DynamicSizeContribution>> getDynamicSizeContributionSupplier(String uri, String expressionText) {
245         return () -> {
246             try {
247                 @SuppressWarnings("unchecked")
248                 List<DynamicSizeContribution> result = (List<DynamicSizeContribution>) SCLOsgi.MODULE_REPOSITORY.getValue(uri, expressionText);
249                 return result.stream();//result.stream().map(DynamicColorContribution::fromTuple9);
250             } catch (ValueNotFound e) {
251                 LOGGER.error("Could not find contributions", e);
252                 //throw new RuntimeException(e);
253                 return Stream.empty();
254             }
255         };
256     }
257
258     public static class DynamicArrowObject {
259
260         private final NamedResource arrowObject;
261         private final Supplier<Stream<DynamicArrowContribution>> arrowContributionSupplier;
262         private Map<String, DynamicArrowContribution> arrowContributions;
263
264         public DynamicArrowObject(NamedResource coloringObject, Supplier<Stream<DynamicArrowContribution>> arrowContributionSupplier) {
265             this.arrowObject = coloringObject;
266             this.arrowContributionSupplier = arrowContributionSupplier;
267         }
268
269         public NamedResource getArrowObject() {
270             return arrowObject;
271         }
272
273         public Map<String, DynamicArrowContribution> getArrowContributions() {
274             if (arrowContributions == null)
275                 arrowContributions = arrowContributionSupplier.get().collect(Collectors.toMap(c -> c.getLabel(), c -> c));
276             return arrowContributions;
277         }
278     }
279     
280     public static class DynamicColoringObject {
281
282         private final NamedResource coloringObject;
283         private final Supplier<Stream<DynamicColorContribution>> colorContributionSupplier;
284         private Map<String, DynamicColorContribution> colorContributions;
285
286         public DynamicColoringObject(NamedResource coloringObject, Supplier<Stream<DynamicColorContribution>> colorContributionSupplier) {
287             this.coloringObject = coloringObject;
288             this.colorContributionSupplier = colorContributionSupplier;
289         }
290
291         public NamedResource getColoringObject() {
292             return coloringObject;
293         }
294
295         public Map<String, DynamicColorContribution> getColorContributions() {
296             if (colorContributions == null)
297                 colorContributions = colorContributionSupplier.get().collect(Collectors.toMap(c -> c.getLabel(), c -> c));
298             return colorContributions;
299         }
300     }
301
302     public static class DynamicColoringMap {
303
304         private final NamedResource coloringObject;
305         private final Supplier<Stream<DynamicColorMap>> colorContributionSupplier;
306         private Map<String, DynamicColorMap> colorContributions;
307
308         public DynamicColoringMap(NamedResource coloringObject, Supplier<Stream<DynamicColorMap>> colorContributionSupplier) {
309             this.coloringObject = coloringObject;
310             this.colorContributionSupplier = colorContributionSupplier;
311         }
312
313         public NamedResource getColoringObject() {
314             return coloringObject;
315         }
316
317         public Map<String, DynamicColorMap> getColorContributions() {
318             if (colorContributions == null)
319                 colorContributions = colorContributionSupplier.get().collect(Collectors.toMap(c -> c.getLabel(), c -> c));
320             return colorContributions;
321         }
322     }
323     
324     public static class DynamicSizingObject {
325
326         private final NamedResource sizingObject;
327         private final Supplier<Stream<DynamicSizeContribution>> sizeContributionSupplier;
328         private Map<String, DynamicSizeContribution> sizeContributions;
329         
330         public DynamicSizingObject(NamedResource coloringObject, Supplier<Stream<DynamicSizeContribution>> sizeContributionSupplier) {
331             this.sizingObject = coloringObject;
332             this.sizeContributionSupplier = sizeContributionSupplier;
333         }
334
335         public NamedResource getSizingObject() {
336             return sizingObject;
337         }
338
339         public Map<String, DynamicSizeContribution> getSizeContributions() {
340             if (sizeContributions == null)
341                 sizeContributions = sizeContributionSupplier.get().collect(Collectors.toMap(c -> c.getLabel(), c -> c));
342             return sizeContributions;
343         }
344     }
345 }