]> gerrit.simantics Code Review - simantics/district.git/blob - org.simantics.district.network/src/org/simantics/district/network/visualisations/DynamicVisualisationsContributions.java
1a16ee55389214e4cb39f7862cb06e5a6231235a
[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.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                     // ignore
68                     LOGGER.debug("Dynamic color maps not found for {}", sclModule, e);
69                 } finally {
70                     SCLContext.getCurrent().put("graph", oldGraph);
71                 }
72             }
73         }
74         return results;
75     }
76
77     public static Map<String, DynamicSizeMap> dynamicSizeMaps(ReadGraph graph) throws DatabaseException {
78         List<Resource> sharedOntologies = Simantics.applySCL("Simantics/SharedOntologies", "getSharedOntologies", graph, Tuple0.INSTANCE);
79         
80         Map<String, DynamicSizeMap> results = new HashMap<>();
81         Layer0 L0 = Layer0.getInstance(graph);
82         for (Resource sharedOntology : sharedOntologies) {
83             Resource sclModule = Layer0Utils.getPossibleChild(graph, sharedOntology, L0.SCLModule, COMMON_DYNAMIC_VISUALISATIONS_MODULE);
84             if (sclModule != null) {
85                 String moduleURI = graph.getURI(sclModule);
86                 Object oldGraph = SCLContext.getCurrent().get("graph");
87                 try {
88                     // let's put the graph to SCLContext for resolving the color maps
89                     SCLContext.getCurrent().put("graph", graph);
90                     @SuppressWarnings("unchecked")
91                     List<DynamicSizeMap> result = (List<DynamicSizeMap>) SCLOsgi.MODULE_REPOSITORY.getValue(moduleURI, SIZE_MAP_CONTRIBUTION);
92                     
93                     for (DynamicSizeMap sizeMap : result) {
94                         results.put(sizeMap.getLabel(), sizeMap);
95                     }
96                 } catch (ValueNotFound e) {
97                     // ignore
98                     LOGGER.debug("Dynamic size maps not found for {}", sclModule, e);
99                 } finally {
100                     SCLContext.getCurrent().put("graph", oldGraph);
101                 }
102             }
103         }
104         return results;
105     }
106
107     public static Collection<DynamicColoringObject> dynamicColoringObjects(ReadGraph graph) throws DatabaseException {
108         
109         List<Resource> sharedOntologies = Simantics.applySCL("Simantics/SharedOntologies", "getSharedOntologies", graph, Tuple0.INSTANCE);
110         
111         List<DynamicColoringObject> results = new ArrayList<>();
112         
113         for (Resource sharedOntology : sharedOntologies) {
114             Collection<Resource> findByType = graph.syncRequest(new ObjectsWithSupertype(sharedOntology, Layer0.getInstance(graph).ConsistsOf, StructuralResource2.getInstance(graph).Component));
115             //Collection<Resource> findByType = QueryIndexUtils.searchByType(graph, sharedOntology, );
116             for (Resource find : findByType) {
117                 NamedResource moduleType = new NamedResource(NameLabelUtil.modalName(graph, find), find);
118                 DynamicColoringObject dynamicColoringObject = dynamicColoringObject(graph, moduleType);
119                 if (dynamicColoringObject != null)
120                     results.add(dynamicColoringObject);
121             }
122         }
123         return results;
124     }
125     
126     public static Collection<DynamicSizingObject> dynamicSizingObjects(ReadGraph graph) throws DatabaseException {
127         
128         List<Resource> sharedOntologies = Simantics.applySCL("Simantics/SharedOntologies", "getSharedOntologies", graph, Tuple0.INSTANCE);
129         
130         List<DynamicSizingObject> results = new ArrayList<>();
131         
132         for (Resource sharedOntology : sharedOntologies) {
133             Collection<Resource> findByType = graph.syncRequest(new ObjectsWithSupertype(sharedOntology, Layer0.getInstance(graph).ConsistsOf, StructuralResource2.getInstance(graph).Component));
134             //Collection<Resource> findByType = QueryIndexUtils.searchByType(graph, sharedOntology, );
135             for (Resource find : findByType) {
136                 NamedResource moduleType = new NamedResource(NameLabelUtil.modalName(graph, find), find);
137                 DynamicSizingObject dynamicSizingObject = dynamicSizingObject(graph, moduleType);
138                 if (dynamicSizingObject != null)
139                     results.add(dynamicSizingObject);
140             }
141         }
142         return results;
143     }
144     
145     private static DynamicColoringObject dynamicColoringObject(ReadGraph graph, NamedResource moduleType) throws DatabaseException {
146         Layer0 L0 = Layer0.getInstance(graph);
147         Resource sclModule = Layer0Utils.getPossibleChild(graph, moduleType.getResource(), L0.SCLModule, DYNAMIC_VISUALISATIONS_CONTRIBUTION_MODULE);
148         if (sclModule != null) {
149             String moduleURI = graph.getURI(sclModule);
150             return new DynamicColoringObject(moduleType, getContributionSupplier(moduleURI, COLOR_CONTRIBUTION));
151         }
152         return null;
153     }
154
155     private static DynamicSizingObject dynamicSizingObject(ReadGraph graph, NamedResource moduleType) throws DatabaseException {
156         Layer0 L0 = Layer0.getInstance(graph);
157         Resource sclModule = Layer0Utils.getPossibleChild(graph, moduleType.getResource(), L0.SCLModule, DYNAMIC_VISUALISATIONS_CONTRIBUTION_MODULE);
158         if (sclModule != null) {
159             String moduleURI = graph.getURI(sclModule);
160             return new DynamicSizingObject(moduleType, getContributionSupplier(moduleURI, SIZE_CONTRIBUTION));
161         }
162         return null;
163     }
164
165     public static Collection<DynamicArrowObject> dynamicEdgeArrowObjects(ReadGraph graph) throws DatabaseException {
166         
167         List<Resource> sharedOntologies = Simantics.applySCL("Simantics/SharedOntologies", "getSharedOntologies", graph, Tuple0.INSTANCE);
168         
169         List<DynamicArrowObject> results = new ArrayList<>();
170         
171         for (Resource sharedOntology : sharedOntologies) {
172             Collection<Resource> findByType = graph.syncRequest(new ObjectsWithSupertype(sharedOntology, Layer0.getInstance(graph).ConsistsOf, StructuralResource2.getInstance(graph).Component));
173             //Collection<Resource> findByType = QueryIndexUtils.searchByType(graph, sharedOntology, );
174             for (Resource find : findByType) {
175                 NamedResource moduleType = new NamedResource(NameLabelUtil.modalName(graph, find), find);
176                 DynamicArrowObject dynamicarrowObject = dynamicEdgeArrowObject(graph, moduleType);
177                 if (dynamicarrowObject != null)
178                     results.add(dynamicarrowObject);
179             }
180         }
181         return results;
182     }
183     
184     private static DynamicArrowObject dynamicEdgeArrowObject(ReadGraph graph, NamedResource moduleType) throws DatabaseException {
185         Layer0 L0 = Layer0.getInstance(graph);
186         Resource sclModule = Layer0Utils.getPossibleChild(graph, moduleType.getResource(), L0.SCLModule, DYNAMIC_VISUALISATIONS_CONTRIBUTION_MODULE);
187         if (sclModule != null) {
188             String moduleURI = graph.getURI(sclModule);
189             return new DynamicArrowObject(moduleType, getContributionSupplier(moduleURI, ARROW_CONTRIBUTION));
190         }
191         return null;
192     }
193
194     private static <T> Supplier<Stream<T>> getContributionSupplier(String uri, String expressionText) {
195         return () -> {
196             try {
197                 @SuppressWarnings("unchecked")
198                 List<T> result = (List<T>) SCLOsgi.MODULE_REPOSITORY.getValue(uri, expressionText);
199                 return result.stream();//result.stream().map(DynamicColorContribution::fromTuple9);
200             } catch (ValueNotFound e) {
201                 LOGGER.trace("Could not find contributions for module {} and expression {}", uri, expressionText, e);
202                 //throw new RuntimeException(e);
203                 return Stream.empty();
204             }
205         };
206     }
207     
208     public static class DynamicArrowObject {
209
210         private final NamedResource arrowObject;
211         private final Supplier<Stream<DynamicArrowContribution>> arrowContributionSupplier;
212         private Map<String, DynamicArrowContribution> arrowContributions;
213
214         public DynamicArrowObject(NamedResource coloringObject, Supplier<Stream<DynamicArrowContribution>> arrowContributionSupplier) {
215             this.arrowObject = coloringObject;
216             this.arrowContributionSupplier = arrowContributionSupplier;
217         }
218
219         public NamedResource getArrowObject() {
220             return arrowObject;
221         }
222
223         public Map<String, DynamicArrowContribution> getArrowContributions() {
224             if (arrowContributions == null)
225                 arrowContributions = arrowContributionSupplier.get().collect(Collectors.toMap(c -> c.getLabel(), c -> c));
226             return arrowContributions;
227         }
228     }
229     
230     public static class DynamicColoringObject {
231
232         private final NamedResource coloringObject;
233         private final Supplier<Stream<DynamicColorContribution>> colorContributionSupplier;
234         private Map<String, DynamicColorContribution> colorContributions;
235
236         public DynamicColoringObject(NamedResource coloringObject, Supplier<Stream<DynamicColorContribution>> colorContributionSupplier) {
237             this.coloringObject = coloringObject;
238             this.colorContributionSupplier = colorContributionSupplier;
239         }
240
241         public NamedResource getColoringObject() {
242             return coloringObject;
243         }
244
245         public Map<String, DynamicColorContribution> getColorContributions() {
246             if (colorContributions == null)
247                 colorContributions = colorContributionSupplier.get().collect(Collectors.toMap(c -> c.getLabel(), c -> c));
248             return colorContributions;
249         }
250         
251         @Override
252         public String toString() {
253             return getClass().getSimpleName() + " [" + coloringObject.getName() + " (" + coloringObject.getResource() + "), contributions=" + colorContributionsToString() + "]";
254         }
255         
256         private String colorContributionsToString() {
257             
258             return colorContributions.keySet().stream().map(key -> {
259                 DynamicColorContribution dynamicColorContribution = colorContributions.get(key);
260                 return key + "=" + dynamicColorContribution.getLabel();
261             }).collect(Collectors.joining(", ", "{", "}"));
262         }
263     }
264
265     public static class DynamicColoringMap {
266
267         private final NamedResource coloringObject;
268         private final Supplier<Stream<DynamicColorMap>> colorContributionSupplier;
269         private Map<String, DynamicColorMap> colorContributions;
270
271         public DynamicColoringMap(NamedResource coloringObject, Supplier<Stream<DynamicColorMap>> colorContributionSupplier) {
272             this.coloringObject = coloringObject;
273             this.colorContributionSupplier = colorContributionSupplier;
274         }
275
276         public NamedResource getColoringObject() {
277             return coloringObject;
278         }
279
280         public Map<String, DynamicColorMap> getColorContributions() {
281             if (colorContributions == null)
282                 colorContributions = colorContributionSupplier.get().collect(Collectors.toMap(c -> c.getLabel(), c -> c));
283             return colorContributions;
284         }
285     }
286     
287     public static class DynamicSizingObject {
288
289         private final NamedResource sizingObject;
290         private final Supplier<Stream<DynamicSizeContribution>> sizeContributionSupplier;
291         private Map<String, DynamicSizeContribution> sizeContributions;
292         
293         public DynamicSizingObject(NamedResource coloringObject, Supplier<Stream<DynamicSizeContribution>> sizeContributionSupplier) {
294             this.sizingObject = coloringObject;
295             this.sizeContributionSupplier = sizeContributionSupplier;
296         }
297
298         public NamedResource getSizingObject() {
299             return sizingObject;
300         }
301
302         public Map<String, DynamicSizeContribution> getSizeContributions() {
303             if (sizeContributions == null)
304                 sizeContributions = sizeContributionSupplier.get().collect(Collectors.toMap(c -> c.getLabel(), c -> c));
305             return sizeContributions;
306         }
307     }
308 }