1 package org.simantics.district.network.ui.nodes;
3 import java.awt.geom.Point2D;
6 import org.simantics.Simantics;
7 import org.simantics.db.ReadGraph;
8 import org.simantics.db.Resource;
9 import org.simantics.db.common.procedure.adapter.TransientCacheListener;
10 import org.simantics.db.common.request.ResourceRead;
11 import org.simantics.db.exception.DatabaseException;
12 import org.simantics.db.layer0.exception.MissingVariableValueException;
13 import org.simantics.db.layer0.exception.PendingVariableException;
14 import org.simantics.db.layer0.util.Layer0Utils;
15 import org.simantics.db.layer0.variable.Variable;
16 import org.simantics.db.layer0.variable.Variables;
17 import org.simantics.diagram.profile.StyleBase;
18 import org.simantics.diagram.stubs.DiagramResource;
19 import org.simantics.district.network.DistrictNetworkUtil;
20 import org.simantics.district.network.ontology.DistrictNetworkResource;
21 import org.simantics.district.network.profile.MidBranchEdgeSetRequest;
22 import org.simantics.layer0.Layer0;
23 import org.simantics.scenegraph.INode;
24 import org.simantics.scenegraph.profile.EvaluationContext;
25 import org.simantics.scenegraph.profile.common.ProfileVariables;
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.function.Function1;
30 import org.simantics.structural.stubs.StructuralResource2;
32 public class DistrictNetworkStaticInfoStyle extends StyleBase<DistrictNetworkStaticInfoStyle.StyleResult> {
34 private static final String ACTIONS_MODULE = "Actions";
35 private static final String PIPELINE_INFO = "pipelineInfo";
37 public static class StyleResult {
38 public StyleResult(Point2D p1, Point2D p2, String info) {
49 public DistrictNetworkStaticInfoStyle(Resource style) {
54 public StyleResult calculateStyle(ReadGraph graph, Resource runtimeDiagram, Resource entry, Resource mapElement)
55 throws DatabaseException {
56 DistrictNetworkResource DN = DistrictNetworkResource.getInstance(graph);
58 boolean isEdge = graph.isInstanceOf(mapElement, DN.Edge);
59 boolean isVertex = graph.isInstanceOf(mapElement, DN.Vertex);
60 if (!isEdge && !isVertex)
64 Resource diagram = graph.getSingleObject(mapElement, Layer0.getInstance(graph).PartOf);
65 Set<Resource> edgesToUse = graph.syncRequest(new MidBranchEdgeSetRequest(diagram), TransientCacheListener.instance());
66 if (!edgesToUse.contains(mapElement))
70 DiagramResource DIA = DiagramResource.getInstance(graph);
71 StructuralResource2 STR = StructuralResource2.getInstance(graph);
73 Resource module = DistrictNetworkUtil.getMappedComponentCached(graph, mapElement);
77 Resource moduleType = graph.getPossibleType(module, STR.Component);
78 if (moduleType == null)
81 Function1<Variable, String> function = getUCPipelineInfoFunctionCached(graph, moduleType);
87 Variable variable = Variables.getVariable(graph, module);
88 Variable moduleVariable = Variables.possibleActiveVariable(graph, variable);
89 if (moduleVariable == null)
90 moduleVariable = variable;
92 result = Simantics.applySCLRead(graph, function, moduleVariable);
93 } catch (PendingVariableException | MissingVariableValueException e) {
98 double[] coords = graph.getRelatedValue(mapElement, DIA.HasLocation);
99 Point2D p = DistrictNetworkNodeUtils.calculatePoint2D(new Point2D.Double(coords[0], coords[1]), null);
100 return new StyleResult(p, p, result);
103 Resource v1 = graph.getSingleObject(mapElement, DN.HasStartVertex);
104 double[] coords1 = graph.getRelatedValue(v1, DIA.HasLocation);
105 Resource v2 = graph.getSingleObject(mapElement, DN.HasEndVertex);
106 double[] coords2 = graph.getRelatedValue(v2, DIA.HasLocation);
107 Point2D p1 = DistrictNetworkNodeUtils.calculatePoint2D(new Point2D.Double(coords1[0], coords1[1]), null);
108 Point2D p2 = DistrictNetworkNodeUtils.calculatePoint2D(new Point2D.Double(coords2[0], coords2[1]), null);
110 return new StyleResult(p1, p2, result);
117 public void applyStyleForNode(EvaluationContext evaluationContext, INode parent, StyleResult result) {
118 if (result == null) {
119 cleanupStyleForNode(evaluationContext, parent);
123 DistrictNetworkStaticInfoNode node = ProfileVariables.claimChild(parent, "*", DistrictNetworkStaticInfoNode.NODE_KEY, DistrictNetworkStaticInfoNode.class, evaluationContext);
127 Point2D p1 = result.p1;
128 Point2D p2 = result.p2;
131 node.setLocation(p1, new Point2D.Double(1.0, 0.0));
134 double sign = Math.signum(p1.getX() - p2.getX());
135 Point2D.Double origin = new Point2D.Double(0.5 * (p1.getX() + p2.getX()), 0.5 * (p1.getY() + p2.getY()));
136 Point2D direction = new Point2D.Double(0.5 * sign * (p1.getX() - p2.getX()), 0.5 * sign * (p1.getY() - p2.getY()));
138 node.setLocation(origin, direction);
141 node.setInfo(result.info);
144 private static Function1<Variable, String> getUCPipelineInfoFunctionCached(ReadGraph graph, Resource componentType)
145 throws DatabaseException {
146 return graph.syncRequest(new UCPipelineInfoRequest(componentType), TransientCacheListener.instance());
149 private static final class UCPipelineInfoRequest extends ResourceRead<Function1<Variable, String>> {
150 public UCPipelineInfoRequest(Resource resource) {
154 @SuppressWarnings("unchecked")
156 public Function1<Variable, String> perform(ReadGraph graph) throws DatabaseException {
157 Resource actionsModule = Layer0Utils.getPossibleChild(graph, resource, ACTIONS_MODULE);
158 if (actionsModule == null || !graph.isInstanceOf(actionsModule, Layer0.getInstance(graph).SCLModule))
161 String uri = graph.getURI(actionsModule);
162 SCLContext sclContext = SCLContext.getCurrent();
163 Object oldGraph = sclContext.get("graph");
165 sclContext.put("graph", graph);
166 return (Function1<Variable, String>) SCLOsgi.MODULE_REPOSITORY.getValue(uri, PIPELINE_INFO);
167 } catch (ValueNotFound e1) {
170 sclContext.put("graph", oldGraph);
176 protected void cleanupStyleForNode(EvaluationContext evaluationContext, INode node) {
177 ProfileVariables.denyChild(node, "*", DistrictNetworkStaticInfoNode.NODE_KEY);