1 package org.simantics.district.network.ui.styles;
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.district.network.ui.nodes.DistrictNetworkNodeUtils;
23 import org.simantics.district.network.ui.nodes.DistrictNetworkStaticInfoNode;
24 import org.simantics.layer0.Layer0;
25 import org.simantics.scenegraph.INode;
26 import org.simantics.scenegraph.profile.EvaluationContext;
27 import org.simantics.scenegraph.profile.common.ProfileVariables;
28 import org.simantics.scl.compiler.top.ValueNotFound;
29 import org.simantics.scl.osgi.SCLOsgi;
30 import org.simantics.scl.runtime.SCLContext;
31 import org.simantics.scl.runtime.function.Function1;
32 import org.simantics.structural.stubs.StructuralResource2;
34 public class DistrictNetworkStaticInfoStyle extends StyleBase<DistrictNetworkStaticInfoStyle.StyleResult> {
36 private static final String ACTIONS_MODULE = "Actions";
37 private static final String PIPELINE_INFO = "pipelineInfo";
39 public static class StyleResult {
40 public StyleResult(Point2D p1, Point2D p2, String info) {
51 public DistrictNetworkStaticInfoStyle(Resource style) {
56 public StyleResult calculateStyle(ReadGraph graph, Resource runtimeDiagram, Resource entry, Resource mapElement)
57 throws DatabaseException {
58 DistrictNetworkResource DN = DistrictNetworkResource.getInstance(graph);
60 boolean isEdge = graph.isInstanceOf(mapElement, DN.Edge);
61 boolean isVertex = graph.isInstanceOf(mapElement, DN.Vertex);
62 if (!isEdge && !isVertex)
66 Resource diagram = graph.getSingleObject(mapElement, Layer0.getInstance(graph).PartOf);
67 Set<Resource> edgesToUse = graph.syncRequest(new MidBranchEdgeSetRequest(diagram), TransientCacheListener.instance());
68 if (!edgesToUse.contains(mapElement))
72 DiagramResource DIA = DiagramResource.getInstance(graph);
73 StructuralResource2 STR = StructuralResource2.getInstance(graph);
75 Resource module = DistrictNetworkUtil.getMappedComponentCached(graph, mapElement);
79 Resource moduleType = graph.getPossibleType(module, STR.Component);
80 if (moduleType == null)
83 Function1<Variable, String> function = getUCPipelineInfoFunctionCached(graph, moduleType);
89 Variable variable = Variables.getVariable(graph, module);
90 Variable moduleVariable = Variables.possibleActiveVariable(graph, variable);
91 if (moduleVariable == null)
92 moduleVariable = variable;
94 result = Simantics.applySCLRead(graph, function, moduleVariable);
95 } catch (PendingVariableException | MissingVariableValueException e) {
100 double[] coords = graph.getRelatedValue(mapElement, DIA.HasLocation);
101 Point2D p = DistrictNetworkNodeUtils.calculatePoint2D(new Point2D.Double(coords[0], coords[1]), null);
102 return new StyleResult(p, p, result);
105 Resource v1 = graph.getSingleObject(mapElement, DN.HasStartVertex);
106 double[] coords1 = graph.getRelatedValue(v1, DIA.HasLocation);
107 Resource v2 = graph.getSingleObject(mapElement, DN.HasEndVertex);
108 double[] coords2 = graph.getRelatedValue(v2, DIA.HasLocation);
109 Point2D p1 = DistrictNetworkNodeUtils.calculatePoint2D(new Point2D.Double(coords1[0], coords1[1]), null);
110 Point2D p2 = DistrictNetworkNodeUtils.calculatePoint2D(new Point2D.Double(coords2[0], coords2[1]), null);
112 return new StyleResult(p1, p2, result);
119 public void applyStyleForNode(EvaluationContext evaluationContext, INode parent, StyleResult result) {
120 if (result == null) {
121 cleanupStyleForNode(evaluationContext, parent);
125 DistrictNetworkStaticInfoNode node = ProfileVariables.claimChild(parent, "*", DistrictNetworkStaticInfoNode.NODE_KEY, DistrictNetworkStaticInfoNode.class, evaluationContext);
129 Point2D p1 = result.p1;
130 Point2D p2 = result.p2;
133 node.setLocation(p1, new Point2D.Double(1.0, 0.0));
136 double sign = Math.signum(p1.getX() - p2.getX());
137 Point2D.Double origin = new Point2D.Double(0.5 * (p1.getX() + p2.getX()), 0.5 * (p1.getY() + p2.getY()));
138 Point2D direction = new Point2D.Double(0.5 * sign * (p1.getX() - p2.getX()), 0.5 * sign * (p1.getY() - p2.getY()));
140 node.setLocation(origin, direction);
143 node.setInfo(result.info);
146 private static Function1<Variable, String> getUCPipelineInfoFunctionCached(ReadGraph graph, Resource componentType)
147 throws DatabaseException {
148 return graph.syncRequest(new UCPipelineInfoRequest(componentType), TransientCacheListener.instance());
151 private static final class UCPipelineInfoRequest extends ResourceRead<Function1<Variable, String>> {
152 public UCPipelineInfoRequest(Resource resource) {
156 @SuppressWarnings("unchecked")
158 public Function1<Variable, String> perform(ReadGraph graph) throws DatabaseException {
159 Resource actionsModule = Layer0Utils.getPossibleChild(graph, resource, ACTIONS_MODULE);
160 if (actionsModule == null || !graph.isInstanceOf(actionsModule, Layer0.getInstance(graph).SCLModule))
163 String uri = graph.getURI(actionsModule);
164 SCLContext sclContext = SCLContext.getCurrent();
165 Object oldGraph = sclContext.get("graph");
167 sclContext.put("graph", graph);
168 return (Function1<Variable, String>) SCLOsgi.MODULE_REPOSITORY.getValue(uri, PIPELINE_INFO);
169 } catch (ValueNotFound e1) {
172 sclContext.put("graph", oldGraph);
178 protected void cleanupStyleForNode(EvaluationContext evaluationContext, INode node) {
179 ProfileVariables.denyChild(node, "*", DistrictNetworkStaticInfoNode.NODE_KEY);