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.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;
33 public class DynamicVisualisationsContributions {
35 private static final Logger LOGGER = LoggerFactory.getLogger(DynamicVisualisationsContributions.class);
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";
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";
45 public static Map<String, DynamicColorMap> dynamicColorMaps(ReadGraph graph) throws DatabaseException {
46 List<Resource> sharedOntologies = Simantics.applySCL("Simantics/SharedOntologies", "getSharedOntologies", graph, Tuple0.INSTANCE);
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");
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);
61 for (DynamicColorMap colorMap : result) {
62 results.put(colorMap.getLabel(), colorMap);
64 } catch (ValueNotFound e) {
67 SCLContext.getCurrent().put("graph", oldGraph);
74 public static Map<String, DynamicSizeMap> dynamicSizeMaps(ReadGraph graph) throws DatabaseException {
75 List<Resource> sharedOntologies = Simantics.applySCL("Simantics/SharedOntologies", "getSharedOntologies", graph, Tuple0.INSTANCE);
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");
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);
90 for (DynamicSizeMap sizeMap : result) {
91 results.put(sizeMap.getLabel(), sizeMap);
93 } catch (ValueNotFound e) {
96 SCLContext.getCurrent().put("graph", oldGraph);
103 public static Collection<DynamicColoringObject> dynamicColoringObjects(ReadGraph graph) throws DatabaseException {
105 List<Resource> sharedOntologies = Simantics.applySCL("Simantics/SharedOntologies", "getSharedOntologies", graph, Tuple0.INSTANCE);
107 List<DynamicColoringObject> results = new ArrayList<>();
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);
122 public static Collection<DynamicSizingObject> dynamicSizingObjects(ReadGraph graph) throws DatabaseException {
124 List<Resource> sharedOntologies = Simantics.applySCL("Simantics/SharedOntologies", "getSharedOntologies", graph, Tuple0.INSTANCE);
126 List<DynamicSizingObject> results = new ArrayList<>();
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);
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));
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));
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));
171 private static Supplier<Stream<DynamicColorMap>> getDynamicColorMapSupplier(String uri, String expressionText) {
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();
185 private static Supplier<Stream<DynamicColorContribution>> getDynamicColorContributionSupplier(String uri, String expressionText) {
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();
199 private static Supplier<Stream<DynamicSizeContribution>> getDynamicSizeContributionSupplier(String uri, String expressionText) {
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();
213 public static class DynamicColoringObject {
215 private final NamedResource coloringObject;
216 private final Supplier<Stream<DynamicColorContribution>> colorContributionSupplier;
217 private Map<String, DynamicColorContribution> colorContributions;
219 public DynamicColoringObject(NamedResource coloringObject, Supplier<Stream<DynamicColorContribution>> colorContributionSupplier) {
220 this.coloringObject = coloringObject;
221 this.colorContributionSupplier = colorContributionSupplier;
224 public NamedResource getColoringObject() {
225 return coloringObject;
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;
235 public static class DynamicColoringMap {
237 private final NamedResource coloringObject;
238 private final Supplier<Stream<DynamicColorMap>> colorContributionSupplier;
239 private Map<String, DynamicColorMap> colorContributions;
241 public DynamicColoringMap(NamedResource coloringObject, Supplier<Stream<DynamicColorMap>> colorContributionSupplier) {
242 this.coloringObject = coloringObject;
243 this.colorContributionSupplier = colorContributionSupplier;
246 public NamedResource getColoringObject() {
247 return coloringObject;
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;
257 public static class DynamicSizingObject {
259 private final NamedResource sizingObject;
260 private final Supplier<Stream<DynamicSizeContribution>> sizeContributionSupplier;
261 private Map<String, DynamicSizeContribution> sizeContributions;
263 public DynamicSizingObject(NamedResource coloringObject, Supplier<Stream<DynamicSizeContribution>> sizeContributionSupplier) {
264 this.sizingObject = coloringObject;
265 this.sizeContributionSupplier = sizeContributionSupplier;
268 public NamedResource getSizingObject() {
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;