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) {
68 LOGGER.debug("Dynamic color maps not found for {}", sclModule, e);
70 SCLContext.getCurrent().put("graph", oldGraph);
77 public static Map<String, DynamicSizeMap> dynamicSizeMaps(ReadGraph graph) throws DatabaseException {
78 List<Resource> sharedOntologies = Simantics.applySCL("Simantics/SharedOntologies", "getSharedOntologies", graph, Tuple0.INSTANCE);
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");
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);
93 for (DynamicSizeMap sizeMap : result) {
94 results.put(sizeMap.getLabel(), sizeMap);
96 } catch (ValueNotFound e) {
98 LOGGER.debug("Dynamic size maps not found for {}", sclModule, e);
100 SCLContext.getCurrent().put("graph", oldGraph);
107 public static Collection<DynamicColoringObject> dynamicColoringObjects(ReadGraph graph) throws DatabaseException {
109 List<Resource> sharedOntologies = Simantics.applySCL("Simantics/SharedOntologies", "getSharedOntologies", graph, Tuple0.INSTANCE);
111 List<DynamicColoringObject> results = new ArrayList<>();
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);
126 public static Collection<DynamicSizingObject> dynamicSizingObjects(ReadGraph graph) throws DatabaseException {
128 List<Resource> sharedOntologies = Simantics.applySCL("Simantics/SharedOntologies", "getSharedOntologies", graph, Tuple0.INSTANCE);
130 List<DynamicSizingObject> results = new ArrayList<>();
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);
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));
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));
165 public static Collection<DynamicArrowObject> dynamicEdgeArrowObjects(ReadGraph graph) throws DatabaseException {
167 List<Resource> sharedOntologies = Simantics.applySCL("Simantics/SharedOntologies", "getSharedOntologies", graph, Tuple0.INSTANCE);
169 List<DynamicArrowObject> results = new ArrayList<>();
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);
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));
194 private static <T> Supplier<Stream<T>> getContributionSupplier(String uri, String expressionText) {
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();
208 public static class DynamicArrowObject {
210 private final NamedResource arrowObject;
211 private final Supplier<Stream<DynamicArrowContribution>> arrowContributionSupplier;
212 private Map<String, DynamicArrowContribution> arrowContributions;
214 public DynamicArrowObject(NamedResource coloringObject, Supplier<Stream<DynamicArrowContribution>> arrowContributionSupplier) {
215 this.arrowObject = coloringObject;
216 this.arrowContributionSupplier = arrowContributionSupplier;
219 public NamedResource getArrowObject() {
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;
230 public static class DynamicColoringObject {
232 private final NamedResource coloringObject;
233 private final Supplier<Stream<DynamicColorContribution>> colorContributionSupplier;
234 private Map<String, DynamicColorContribution> colorContributions;
236 public DynamicColoringObject(NamedResource coloringObject, Supplier<Stream<DynamicColorContribution>> colorContributionSupplier) {
237 this.coloringObject = coloringObject;
238 this.colorContributionSupplier = colorContributionSupplier;
241 public NamedResource getColoringObject() {
242 return coloringObject;
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;
252 public static class DynamicColoringMap {
254 private final NamedResource coloringObject;
255 private final Supplier<Stream<DynamicColorMap>> colorContributionSupplier;
256 private Map<String, DynamicColorMap> colorContributions;
258 public DynamicColoringMap(NamedResource coloringObject, Supplier<Stream<DynamicColorMap>> colorContributionSupplier) {
259 this.coloringObject = coloringObject;
260 this.colorContributionSupplier = colorContributionSupplier;
263 public NamedResource getColoringObject() {
264 return coloringObject;
267 public Map<String, DynamicColorMap> getColorContributions() {
268 if (colorContributions == null)
269 colorContributions = colorContributionSupplier.get().collect(Collectors.toMap(c -> c.getLabel(), c -> c));
270 return colorContributions;
274 public static class DynamicSizingObject {
276 private final NamedResource sizingObject;
277 private final Supplier<Stream<DynamicSizeContribution>> sizeContributionSupplier;
278 private Map<String, DynamicSizeContribution> sizeContributions;
280 public DynamicSizingObject(NamedResource coloringObject, Supplier<Stream<DynamicSizeContribution>> sizeContributionSupplier) {
281 this.sizingObject = coloringObject;
282 this.sizeContributionSupplier = sizeContributionSupplier;
285 public NamedResource getSizingObject() {
289 public Map<String, DynamicSizeContribution> getSizeContributions() {
290 if (sizeContributions == null)
291 sizeContributions = sizeContributionSupplier.get().collect(Collectors.toMap(c -> c.getLabel(), c -> c));
292 return sizeContributions;