1 package org.simantics.district.network.ui.styles;
3 import java.awt.geom.Point2D;
4 import java.util.Objects;
7 import org.simantics.Simantics;
8 import org.simantics.db.ReadGraph;
9 import org.simantics.db.Resource;
10 import org.simantics.db.common.procedure.adapter.TransientCacheListener;
11 import org.simantics.db.common.request.ResourceRead;
12 import org.simantics.db.exception.DatabaseException;
13 import org.simantics.db.layer0.exception.MissingVariableException;
14 import org.simantics.db.layer0.exception.MissingVariableValueException;
15 import org.simantics.db.layer0.exception.PendingVariableException;
16 import org.simantics.db.layer0.util.Layer0Utils;
17 import org.simantics.db.layer0.variable.Variable;
18 import org.simantics.db.layer0.variable.Variables;
19 import org.simantics.diagram.profile.StyleBase;
20 import org.simantics.diagram.stubs.DiagramResource;
21 import org.simantics.district.network.DistrictNetworkUtil;
22 import org.simantics.district.network.ontology.DistrictNetworkResource;
23 import org.simantics.district.network.profile.MidBranchEdgeSetRequest;
24 import org.simantics.district.network.ui.nodes.DeferredRenderingNode;
25 import org.simantics.district.network.ui.nodes.DistrictNetworkEdgeNode;
26 import org.simantics.district.network.ui.nodes.DistrictNetworkNodeUtils;
27 import org.simantics.district.network.ui.nodes.DistrictNetworkStaticInfoNode;
28 import org.simantics.layer0.Layer0;
29 import org.simantics.scenegraph.INode;
30 import org.simantics.scenegraph.ParentNode;
31 import org.simantics.scenegraph.g2d.IG2DNode;
32 import org.simantics.scenegraph.g2d.nodes.ConnectionNode;
33 import org.simantics.scenegraph.g2d.nodes.spatial.RTreeNode;
34 import org.simantics.scenegraph.profile.EvaluationContext;
35 import org.simantics.scenegraph.profile.common.ProfileVariables;
36 import org.simantics.scenegraph.utils.NodeUtil;
37 import org.simantics.scl.compiler.top.ValueNotFound;
38 import org.simantics.scl.osgi.SCLOsgi;
39 import org.simantics.scl.runtime.SCLContext;
40 import org.simantics.scl.runtime.function.Function1;
41 import org.simantics.structural.stubs.StructuralResource2;
42 import org.slf4j.Logger;
43 import org.slf4j.LoggerFactory;
45 public class DistrictNetworkStaticInfoStyle extends StyleBase<DistrictNetworkStaticInfoStyle.StyleResult> {
47 private static final Logger LOGGER = LoggerFactory.getLogger(DistrictNetworkStaticInfoStyle.class);
49 private static final String ACTIONS_MODULE = "Actions";
50 private static final String PIPELINE_INFO = "pipelineInfo";
52 public static final String STATIC_INFO_DEFERRED = "staticInfo";
54 public static class StyleResult {
55 public StyleResult(Resource r, Point2D p1, String info) {
61 public final Resource r;
62 public final Point2D p1;
63 public final String info;
66 public int hashCode() {
69 result = prime * result + r.hashCode();
70 result = prime * result + ((info == null) ? 0 : info.hashCode());
71 result = prime * result + ((p1 == null) ? 0 : p1.hashCode());
75 public boolean equals(Object obj) {
80 if (getClass() != obj.getClass())
82 StyleResult other = (StyleResult) obj;
83 return r.equals(other.r) && Objects.equals(info, other.info) && Objects.equals(p1, other.p1);
87 public DistrictNetworkStaticInfoStyle(Resource style) {
91 private static final Point2D EDGE = new Point2D.Double();
94 public StyleResult calculateStyle(ReadGraph graph, Resource runtimeDiagram, Resource entry, Resource mapElement)
95 throws DatabaseException {
96 DistrictNetworkResource DN = DistrictNetworkResource.getInstance(graph);
97 Set<Resource> types = graph.getTypes(mapElement);
98 boolean isEdge = types.contains(DN.Edge);
99 boolean isVertex = types.contains(DN.Vertex);
100 if (!isEdge && !isVertex)
104 Resource diagram = graph.getSingleObject(mapElement, Layer0.getInstance(graph).PartOf);
105 Set<Resource> edgesToUse = graph.syncRequest(new MidBranchEdgeSetRequest(diagram), TransientCacheListener.instance());
106 if (!edgesToUse.contains(mapElement))
110 Resource module = DistrictNetworkUtil.getMappedComponentCached(graph, mapElement);
114 StructuralResource2 STR = StructuralResource2.getInstance(graph);
115 Resource moduleType = graph.getPossibleType(module, STR.Component);
116 if (moduleType == null)
119 Function1<Variable, String> function = getUCPipelineInfoFunctionCached(graph, moduleType);
120 if (function == null)
125 Variable variable = Variables.getVariable(graph, module);
126 Variable moduleVariable = Variables.possibleActiveVariable(graph, variable);
127 if (moduleVariable == null)
128 moduleVariable = variable;
130 result = Simantics.applySCLRead(graph, function, moduleVariable);
131 } catch (PendingVariableException | MissingVariableValueException e) {
133 } catch (MissingVariableException e) {
134 // the requested variable is missing from the UC
135 String message = e.getMessage();
136 LOGGER.warn("Missing variable for calculating style with function {} {}", function, message);
137 result = "<" + message + ">";
141 DiagramResource DIA = DiagramResource.getInstance(graph);
142 double[] coords = graph.getRelatedValue(mapElement, DIA.HasLocation);
143 Point2D p = DistrictNetworkNodeUtils.calculatePoint2D(new Point2D.Double(coords[0], coords[1]), null);
144 return new StyleResult(mapElement, p, result);
146 return new StyleResult(mapElement, EDGE, result);
152 public void applyStyleForNode(EvaluationContext evaluationContext, INode parent, StyleResult result) {
153 if (result == null) {
154 cleanupStyleForNode(evaluationContext, parent);
158 ParentNode<?> root = (ParentNode<?>) NodeUtil.getNearestParentOfType(parent, RTreeNode.class);
160 DeferredRenderingNode deferred = ProfileVariables.claimChild(root, "", STATIC_INFO_DEFERRED, DeferredRenderingNode.class, evaluationContext);
161 deferred.setZIndex(Integer.MAX_VALUE-1);
164 DistrictNetworkStaticInfoNode node = ProfileVariables.claimChild(parent, "*", DistrictNetworkStaticInfoNode.NODE_KEY, DistrictNetworkStaticInfoNode.class, evaluationContext);
168 Point2D p1 = result.p1;
170 node.setLocation(p1, new Point2D.Double(1.0, 0.0));
172 // StaticInfoNode takes care of positioning the text during render
173 // based on the found DistrictNetworkEdgeNode's information.
174 for (IG2DNode n : ((ConnectionNode)parent).getNodes()) {
175 if (n instanceof DistrictNetworkEdgeNode) {
176 node.setEdgeNode((DistrictNetworkEdgeNode) n);
181 node.setInfo(result.info);
184 private static Function1<Variable, String> getUCPipelineInfoFunctionCached(ReadGraph graph, Resource componentType)
185 throws DatabaseException {
186 return graph.syncRequest(new UCPipelineInfoRequest(componentType), TransientCacheListener.instance());
189 private static final class UCPipelineInfoRequest extends ResourceRead<Function1<Variable, String>> {
190 public UCPipelineInfoRequest(Resource resource) {
194 @SuppressWarnings("unchecked")
196 public Function1<Variable, String> perform(ReadGraph graph) throws DatabaseException {
197 Resource actionsModule = Layer0Utils.getPossibleChild(graph, resource, ACTIONS_MODULE);
198 if (actionsModule == null || !graph.isInstanceOf(actionsModule, Layer0.getInstance(graph).SCLModule))
201 String uri = graph.getURI(actionsModule);
202 SCLContext sclContext = SCLContext.getCurrent();
203 Object oldGraph = sclContext.get("graph");
205 sclContext.put("graph", graph);
206 return (Function1<Variable, String>) SCLOsgi.MODULE_REPOSITORY.getValue(uri, PIPELINE_INFO);
207 } catch (ValueNotFound e1) {
210 sclContext.put("graph", oldGraph);
216 protected void cleanupStyleForNode(EvaluationContext evaluationContext, INode node) {
217 ProfileVariables.denyChild(node, "*", DistrictNetworkStaticInfoNode.NODE_KEY);