1 package org.simantics.district.network.visualisations;
3 import java.util.ArrayList;
4 import java.util.Collection;
5 import java.util.HashMap;
8 import java.util.Optional;
9 import java.util.function.Supplier;
10 import java.util.stream.Collectors;
11 import java.util.stream.Stream;
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;
37 public class DynamicVisualisationsContributions {
39 private static final Logger LOGGER = LoggerFactory.getLogger(DynamicVisualisationsContributions.class);
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";
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";
52 public static Map<String, DynamicColorMap> dynamicColorMaps(ReadGraph graph) throws DatabaseException {
53 List<Resource> sharedOntologies = Simantics.applySCL("Simantics/SharedOntologies", "getSharedOntologies", graph, Tuple0.INSTANCE);
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");
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);
68 for (DynamicColorMap colorMap : result) {
69 results.put(colorMap.getLabel(), colorMap);
71 } catch (ValueNotFound e) {
73 LOGGER.debug("Dynamic color maps not found for {}", sclModule, e);
75 SCLContext.getCurrent().put("graph", oldGraph);
82 public static Map<String, DynamicSizeMap> dynamicSizeMaps(ReadGraph graph) throws DatabaseException {
83 List<Resource> sharedOntologies = Simantics.applySCL("Simantics/SharedOntologies", "getSharedOntologies", graph, Tuple0.INSTANCE);
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");
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);
98 for (DynamicSizeMap sizeMap : result) {
99 results.put(sizeMap.getLabel(), sizeMap);
101 } catch (ValueNotFound e) {
103 LOGGER.debug("Dynamic size maps not found for {}", sclModule, e);
105 SCLContext.getCurrent().put("graph", oldGraph);
112 public static Collection<DynamicColoringObject> dynamicColoringObjects(ReadGraph graph) throws DatabaseException {
114 List<Resource> sharedOntologies = Simantics.applySCL("Simantics/SharedOntologies", "getSharedOntologies", graph, Tuple0.INSTANCE);
116 List<DynamicColoringObject> results = new ArrayList<>();
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);
131 public static Collection<DynamicSizingObject> dynamicSizingObjects(ReadGraph graph) throws DatabaseException {
133 List<Resource> sharedOntologies = Simantics.applySCL("Simantics/SharedOntologies", "getSharedOntologies", graph, Tuple0.INSTANCE);
135 List<DynamicSizingObject> results = new ArrayList<>();
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);
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));
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));
170 public static Collection<DynamicArrowObject> dynamicEdgeArrowObjects(ReadGraph graph) throws DatabaseException {
172 List<Resource> sharedOntologies = Simantics.applySCL("Simantics/SharedOntologies", "getSharedOntologies", graph, Tuple0.INSTANCE);
174 List<DynamicArrowObject> results = new ArrayList<>();
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);
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));
199 private static <T> Supplier<Stream<T>> getContributionSupplier(String uri, String expressionText) {
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();
213 public static Collection<StaticInformationContributionObject> staticInformationContributionObjects(ReadGraph graph) throws DatabaseException {
215 List<Resource> sharedOntologies = Simantics.applySCL("Simantics/SharedOntologies", "getSharedOntologies", graph, Tuple0.INSTANCE);
217 List<StaticInformationContributionObject> results = new ArrayList<>();
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);
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));
242 public static Collection<DynamicSymbolContributionObject> dynamicSymbolContributionObjects(ReadGraph graph) throws DatabaseException {
244 List<Resource> sharedOntologies = Simantics.applySCL("Simantics/SharedOntologies", "getSharedOntologies", graph, Tuple0.INSTANCE);
245 List<DynamicSymbolContributionObject> results = new ArrayList<>();
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);
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));
270 public static class DynamicArrowObject {
272 private final NamedResource arrowObject;
273 private final Supplier<Stream<DynamicArrowContribution>> arrowContributionSupplier;
274 private Map<String, DynamicArrowContribution> arrowContributions;
276 public DynamicArrowObject(NamedResource coloringObject, Supplier<Stream<DynamicArrowContribution>> arrowContributionSupplier) {
277 this.arrowObject = coloringObject;
278 this.arrowContributionSupplier = arrowContributionSupplier;
281 public NamedResource getArrowObject() {
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;
292 public static class StaticInformationContributionObject {
294 private final NamedResource staticInformationContributionObject;
295 private final Supplier<Stream<StaticInformationContribution>> staticInformationContributionSupplier;
296 private Map<String, StaticInformationContribution> staticInformationContributions;
298 public StaticInformationContributionObject(NamedResource staticInformationContributionObject, Supplier<Stream<StaticInformationContribution>> staticInformationContributionSupplier) {
299 this.staticInformationContributionObject = staticInformationContributionObject;
300 this.staticInformationContributionSupplier = staticInformationContributionSupplier;
303 public NamedResource getStaticInformationContributionObject() {
304 return staticInformationContributionObject;
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;
314 public static class DynamicSymbolContributionObject {
316 private final NamedResource dynamicSymbolContributionObject;
317 private final Supplier<Stream<DynamicSymbolContribution>> dynamicSymbolContributionSupplier;
318 private DynamicSymbolContribution dynamicSymbolContribution;
320 public DynamicSymbolContributionObject(NamedResource dynamicSymbolContributionObject, Supplier<Stream<DynamicSymbolContribution>> dynamicSymbolContributionSupplier) {
321 this.dynamicSymbolContributionObject = dynamicSymbolContributionObject;
322 this.dynamicSymbolContributionSupplier = dynamicSymbolContributionSupplier;
325 public NamedResource getDynamicSymbolContributionObject() {
326 return dynamicSymbolContributionObject;
329 public DynamicSymbolContribution getDynamicSymbolContribution() {
330 if (dynamicSymbolContribution == null) {
331 Optional<DynamicSymbolContribution> findFirst = dynamicSymbolContributionSupplier.get().findFirst();
332 findFirst.ifPresent(dsc -> {
333 dynamicSymbolContribution = dsc;
336 return dynamicSymbolContribution;
339 public void resolveSymbols(ReadGraph graph) {
340 SCLContext current = SCLContext.getCurrent();
341 Object oldGraph = current.put("graph", graph);
343 DynamicSymbolContribution dsc = getDynamicSymbolContribution();
345 dsc.resolveSymbolMap();
347 current.put("graph", oldGraph);
352 public static class DynamicColoringObject {
354 private final NamedResource coloringObject;
355 private final Supplier<Stream<DynamicColorContribution>> colorContributionSupplier;
356 private Map<String, DynamicColorContribution> colorContributions;
358 public DynamicColoringObject(NamedResource coloringObject, Supplier<Stream<DynamicColorContribution>> colorContributionSupplier) {
359 this.coloringObject = coloringObject;
360 this.colorContributionSupplier = colorContributionSupplier;
363 public NamedResource getColoringObject() {
364 return coloringObject;
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;
374 public String toString() {
375 return getClass().getSimpleName() + " [" + coloringObject.getName() + " (" + coloringObject.getResource() + "), contributions=" + colorContributionsToString() + "]";
378 private String colorContributionsToString() {
380 return colorContributions.keySet().stream().map(key -> {
381 DynamicColorContribution dynamicColorContribution = colorContributions.get(key);
382 return key + "=" + dynamicColorContribution.getLabel();
383 }).collect(Collectors.joining(", ", "{", "}"));
387 public static class DynamicColoringMap {
389 private final NamedResource coloringObject;
390 private final Supplier<Stream<DynamicColorMap>> colorContributionSupplier;
391 private Map<String, DynamicColorMap> colorContributions;
393 public DynamicColoringMap(NamedResource coloringObject, Supplier<Stream<DynamicColorMap>> colorContributionSupplier) {
394 this.coloringObject = coloringObject;
395 this.colorContributionSupplier = colorContributionSupplier;
398 public NamedResource getColoringObject() {
399 return coloringObject;
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;
409 public static class DynamicSizingObject {
411 private final NamedResource sizingObject;
412 private final Supplier<Stream<DynamicSizeContribution>> sizeContributionSupplier;
413 private Map<String, DynamicSizeContribution> sizeContributions;
415 public DynamicSizingObject(NamedResource coloringObject, Supplier<Stream<DynamicSizeContribution>> sizeContributionSupplier) {
416 this.sizingObject = coloringObject;
417 this.sizeContributionSupplier = sizeContributionSupplier;
420 public NamedResource getSizingObject() {
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;