1 package org.simantics.district.network.visualisations.triggers;
3 import java.util.Collection;
4 import java.util.HashMap;
8 import org.simantics.NameLabelUtil;
9 import org.simantics.Simantics;
10 import org.simantics.db.ReadGraph;
11 import org.simantics.db.Resource;
12 import org.simantics.db.common.NamedResource;
13 import org.simantics.db.common.request.ObjectsWithSupertype;
14 import org.simantics.db.exception.DatabaseException;
15 import org.simantics.db.layer0.util.Layer0Utils;
16 import org.simantics.layer0.Layer0;
17 import org.simantics.scl.compiler.top.ValueNotFound;
18 import org.simantics.scl.osgi.SCLOsgi;
19 import org.simantics.scl.runtime.SCLContext;
20 import org.simantics.scl.runtime.tuple.Tuple0;
21 import org.simantics.structural.stubs.StructuralResource2;
22 import org.slf4j.Logger;
23 import org.slf4j.LoggerFactory;
25 public class VisualisationTriggersContributions {
27 private static final Logger LOGGER = LoggerFactory.getLogger(VisualisationTriggersContributions.class);
28 private static final String VISUALISATIONS_TRIGGERS_MODULE = "VisualisationTriggers";
29 private static final String VISUALISATION_TRIGGER_CONTRIBUTION = "visualisationTriggers";
31 public static Map<String, List<VisualisationTrigger>> visualisationTriggers(ReadGraph graph) throws DatabaseException {
32 List<Resource> sharedOntologies = Simantics.applySCL("Simantics/SharedOntologies", "getSharedOntologies", graph, Tuple0.INSTANCE);
34 Map<String, List<VisualisationTrigger>> results = new HashMap<>();
35 Layer0 L0 = Layer0.getInstance(graph);
37 Object oldGraph = SCLContext.getCurrent().get("graph");
39 SCLContext.getCurrent().put("graph", graph);
40 for (Resource sharedOntology : sharedOntologies) {
42 Collection<Resource> userComponents = graph.syncRequest(new ObjectsWithSupertype(sharedOntology, Layer0.getInstance(graph).ConsistsOf, StructuralResource2.getInstance(graph).Component));
44 for (Resource userComponent : userComponents) {
45 NamedResource moduleType = new NamedResource(NameLabelUtil.modalName(graph, userComponent), userComponent);
46 List<VisualisationTrigger> visualisationTriggerContributions = visualisationTriggerContribution(graph, moduleType);
47 if (visualisationTriggerContributions != null)
48 results.put(moduleType.getName(), visualisationTriggerContributions);
52 SCLContext.getCurrent().put("graph", oldGraph);
57 private static List<VisualisationTrigger> visualisationTriggerContribution(ReadGraph graph, NamedResource moduleType) throws DatabaseException {
58 Layer0 L0 = Layer0.getInstance(graph);
59 Resource sclModule = Layer0Utils.getPossibleChild(graph, moduleType.getResource(), L0.SCLModule, VISUALISATIONS_TRIGGERS_MODULE);
60 if (sclModule != null) {
61 String moduleURI = graph.getURI(sclModule);
63 List<VisualisationTrigger> result = (List<VisualisationTrigger>) SCLOsgi.MODULE_REPOSITORY.getValue(moduleURI, VISUALISATION_TRIGGER_CONTRIBUTION);
65 } catch (ValueNotFound e) {
66 LOGGER.error("Could not find contributions for module {} and expression {}", moduleURI, VISUALISATION_TRIGGER_CONTRIBUTION, e);