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.function.Supplier;
9 import java.util.stream.Collectors;
10 import java.util.stream.Stream;
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;
34 public class DynamicVisualisationsContributions {
36 private static final Logger LOGGER = LoggerFactory.getLogger(DynamicVisualisationsContributions.class);
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";
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";
47 public static Map<String, DynamicColorMap> dynamicColorMaps(ReadGraph graph) throws DatabaseException {
48 List<Resource> sharedOntologies = Simantics.applySCL("Simantics/SharedOntologies", "getSharedOntologies", graph, Tuple0.INSTANCE);
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");
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);
63 for (DynamicColorMap colorMap : result) {
64 results.put(colorMap.getLabel(), colorMap);
66 } catch (ValueNotFound e) {
69 SCLContext.getCurrent().put("graph", oldGraph);
76 public static Map<String, DynamicSizeMap> dynamicSizeMaps(ReadGraph graph) throws DatabaseException {
77 List<Resource> sharedOntologies = Simantics.applySCL("Simantics/SharedOntologies", "getSharedOntologies", graph, Tuple0.INSTANCE);
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");
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);
92 for (DynamicSizeMap sizeMap : result) {
93 results.put(sizeMap.getLabel(), sizeMap);
95 } catch (ValueNotFound e) {
98 SCLContext.getCurrent().put("graph", oldGraph);
105 public static Collection<DynamicColoringObject> dynamicColoringObjects(ReadGraph graph) throws DatabaseException {
107 List<Resource> sharedOntologies = Simantics.applySCL("Simantics/SharedOntologies", "getSharedOntologies", graph, Tuple0.INSTANCE);
109 List<DynamicColoringObject> results = new ArrayList<>();
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);
124 public static Collection<DynamicSizingObject> dynamicSizingObjects(ReadGraph graph) throws DatabaseException {
126 List<Resource> sharedOntologies = Simantics.applySCL("Simantics/SharedOntologies", "getSharedOntologies", graph, Tuple0.INSTANCE);
128 List<DynamicSizingObject> results = new ArrayList<>();
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);
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));
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));
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));
173 public static Collection<DynamicArrowObject> dynamicEdgeArrowObjects(ReadGraph graph) throws DatabaseException {
175 List<Resource> sharedOntologies = Simantics.applySCL("Simantics/SharedOntologies", "getSharedOntologies", graph, Tuple0.INSTANCE);
177 List<DynamicArrowObject> results = new ArrayList<>();
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);
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));
202 private static Supplier<Stream<DynamicColorMap>> getDynamicColorMapSupplier(String uri, String expressionText) {
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();
216 private static Supplier<Stream<DynamicColorContribution>> getDynamicColorContributionSupplier(String uri, String expressionText) {
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();
230 private static Supplier<Stream<DynamicArrowContribution>> getDynamicEdgeArrowContributionSupplier(String uri, String expressionText) {
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();
244 private static Supplier<Stream<DynamicSizeContribution>> getDynamicSizeContributionSupplier(String uri, String expressionText) {
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();
258 public static class DynamicArrowObject {
260 private final NamedResource arrowObject;
261 private final Supplier<Stream<DynamicArrowContribution>> arrowContributionSupplier;
262 private Map<String, DynamicArrowContribution> arrowContributions;
264 public DynamicArrowObject(NamedResource coloringObject, Supplier<Stream<DynamicArrowContribution>> arrowContributionSupplier) {
265 this.arrowObject = coloringObject;
266 this.arrowContributionSupplier = arrowContributionSupplier;
269 public NamedResource getArrowObject() {
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;
280 public static class DynamicColoringObject {
282 private final NamedResource coloringObject;
283 private final Supplier<Stream<DynamicColorContribution>> colorContributionSupplier;
284 private Map<String, DynamicColorContribution> colorContributions;
286 public DynamicColoringObject(NamedResource coloringObject, Supplier<Stream<DynamicColorContribution>> colorContributionSupplier) {
287 this.coloringObject = coloringObject;
288 this.colorContributionSupplier = colorContributionSupplier;
291 public NamedResource getColoringObject() {
292 return coloringObject;
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;
302 public static class DynamicColoringMap {
304 private final NamedResource coloringObject;
305 private final Supplier<Stream<DynamicColorMap>> colorContributionSupplier;
306 private Map<String, DynamicColorMap> colorContributions;
308 public DynamicColoringMap(NamedResource coloringObject, Supplier<Stream<DynamicColorMap>> colorContributionSupplier) {
309 this.coloringObject = coloringObject;
310 this.colorContributionSupplier = colorContributionSupplier;
313 public NamedResource getColoringObject() {
314 return coloringObject;
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;
324 public static class DynamicSizingObject {
326 private final NamedResource sizingObject;
327 private final Supplier<Stream<DynamicSizeContribution>> sizeContributionSupplier;
328 private Map<String, DynamicSizeContribution> sizeContributions;
330 public DynamicSizingObject(NamedResource coloringObject, Supplier<Stream<DynamicSizeContribution>> sizeContributionSupplier) {
331 this.sizingObject = coloringObject;
332 this.sizeContributionSupplier = sizeContributionSupplier;
335 public NamedResource getSizingObject() {
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;