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