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;
46 public class DistrictNetworkStaticInfoStyle extends StyleBase<DistrictNetworkStaticInfoStyle.StyleResult> {
48 private static final Logger LOGGER = LoggerFactory.getLogger(DistrictNetworkStaticInfoStyle.class);
50 private static final String ACTIONS_MODULE = "Actions";
51 private static final String PIPELINE_INFO = "pipelineInfo";
53 public static class StyleResult {
54 public StyleResult(Resource r, Point2D p1, String info) {
60 public final Resource r;
61 public final Point2D p1;
62 public final String info;
65 public int hashCode() {
68 result = prime * result + r.hashCode();
69 result = prime * result + ((info == null) ? 0 : info.hashCode());
70 result = prime * result + ((p1 == null) ? 0 : p1.hashCode());
74 public boolean equals(Object obj) {
79 if (getClass() != obj.getClass())
81 StyleResult other = (StyleResult) obj;
82 return r.equals(other.r) && Objects.equals(info, other.info) && Objects.equals(p1, other.p1);
86 public DistrictNetworkStaticInfoStyle(Resource style) {
90 private static final Point2D EDGE = new Point2D.Double();
93 public StyleResult calculateStyle(ReadGraph graph, Resource runtimeDiagram, Resource entry, Resource mapElement)
94 throws DatabaseException {
95 return calculateStyle(graph, entry, mapElement);
98 public static StyleResult calculateStyle(ReadGraph graph, Resource entry, Resource mapElement) throws DatabaseException {
99 DistrictNetworkResource DN = DistrictNetworkResource.getInstance(graph);
100 Set<Resource> types = graph.getTypes(mapElement);
101 boolean isEdge = types.contains(DN.Edge);
102 boolean isVertex = types.contains(DN.Vertex);
103 if (!isEdge && !isVertex)
107 Resource diagram = graph.getSingleObject(mapElement, Layer0.getInstance(graph).PartOf);
108 Set<Resource> edgesToUse = graph.syncRequest(new MidBranchEdgeSetRequest(diagram), TransientCacheListener.instance());
109 if (!edgesToUse.contains(mapElement))
113 Resource module = DistrictNetworkUtil.getMappedComponentCached(graph, mapElement);
117 StructuralResource2 STR = StructuralResource2.getInstance(graph);
118 Resource moduleType = graph.getPossibleType(module, STR.Component);
119 if (moduleType == null)
122 Function1<Variable, String> function = getUCPipelineInfoFunctionCached(graph, moduleType);
123 if (function == null)
128 Variable variable = Variables.getVariable(graph, module);
129 Variable moduleVariable = Variables.possibleActiveVariable(graph, variable);
130 if (moduleVariable == null)
131 moduleVariable = variable;
133 result = Simantics.applySCLRead(graph, function, moduleVariable);
134 } catch (PendingVariableException | MissingVariableValueException e) {
136 } catch (MissingVariableException e) {
137 // the requested variable is missing from the UC
138 String message = e.getMessage();
139 LOGGER.warn("Missing variable for calculating style with function {} {}", function, message);
140 result = "<" + message + ">";
144 DiagramResource DIA = DiagramResource.getInstance(graph);
145 double[] coords = graph.getRelatedValue(mapElement, DIA.HasLocation);
146 Point2D p = DistrictNetworkNodeUtils.calculatePoint2D(new Point2D.Double(coords[0], coords[1]), null);
147 return new StyleResult(mapElement, p, result);
149 return new StyleResult(mapElement, EDGE, result);
155 public void applyStyleForNode(EvaluationContext evaluationContext, INode parent, StyleResult result) {
156 if (result == null) {
157 cleanupStyleForNode(evaluationContext, parent);
161 ParentNode<?> root = (ParentNode<?>) NodeUtil.getNearestParentOfType(parent, RTreeNode.class);
163 DeferredRenderingNode deferred = ProfileVariables.claimChild(root, "", DistrictNetworkStaticInfoNode.STATIC_INFO_DEFERRED, DeferredRenderingNode.class, evaluationContext);
164 deferred.setZIndex(Integer.MAX_VALUE-1);
167 DistrictNetworkStaticInfoNode node = ProfileVariables.claimChild(parent, "*", DistrictNetworkStaticInfoNode.NODE_KEY, DistrictNetworkStaticInfoNode.class, evaluationContext);
171 Point2D p1 = result.p1;
173 node.setLocation(p1, new Point2D.Double(1.0, 0.0));
175 // StaticInfoNode takes care of positioning the text during render
176 // based on the found DistrictNetworkEdgeNode's information.
177 for (IG2DNode n : ((ConnectionNode)parent).getNodes()) {
178 if (n instanceof DistrictNetworkEdgeNode) {
179 node.setEdgeNode((DistrictNetworkEdgeNode) n);
184 node.setInfo(result.info);
187 private static Function1<Variable, String> getUCPipelineInfoFunctionCached(ReadGraph graph, Resource componentType)
188 throws DatabaseException {
189 return graph.syncRequest(new UCPipelineInfoRequest(componentType), TransientCacheListener.instance());
192 private static final class UCPipelineInfoRequest extends ResourceRead<Function1<Variable, String>> {
193 public UCPipelineInfoRequest(Resource resource) {
197 @SuppressWarnings("unchecked")
199 public Function1<Variable, String> perform(ReadGraph graph) throws DatabaseException {
200 Resource actionsModule = Layer0Utils.getPossibleChild(graph, resource, ACTIONS_MODULE);
201 if (actionsModule == null || !graph.isInstanceOf(actionsModule, Layer0.getInstance(graph).SCLModule))
204 String uri = graph.getURI(actionsModule);
205 SCLContext sclContext = SCLContext.getCurrent();
206 Object oldGraph = sclContext.get("graph");
208 sclContext.put("graph", graph);
209 return (Function1<Variable, String>) SCLOsgi.MODULE_REPOSITORY.getValue(uri, PIPELINE_INFO);
210 } catch (ValueNotFound e1) {
213 sclContext.put("graph", oldGraph);
219 protected void cleanupStyleForNode(EvaluationContext evaluationContext, INode node) {
220 ProfileVariables.denyChild(node, "*", DistrictNetworkStaticInfoNode.NODE_KEY);