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 if (!graph.isInstanceOf(mapElement, DN.Edge))
61 Resource diagram = graph.getSingleObject(mapElement, Layer0.getInstance(graph).PartOf);
62 Set<Resource> edgesToUse = graph.syncRequest(new MidBranchEdgeSetRequest(diagram), TransientCacheListener.instance());
63 if (!edgesToUse.contains(mapElement))
66 DiagramResource DIA = DiagramResource.getInstance(graph);
67 StructuralResource2 STR = StructuralResource2.getInstance(graph);
69 Resource module = DistrictNetworkUtil.getMappedComponentCached(graph, mapElement);
73 Resource moduleType = graph.getPossibleType(module, STR.Component);
74 if (moduleType == null)
77 Function1<Variable, String> function = getUCPipelineInfoFunctionCached(graph, moduleType);
83 Variable variable = Variables.getVariable(graph, module);
84 Variable moduleVariable = Variables.possibleActiveVariable(graph, variable);
85 if (moduleVariable == null)
86 moduleVariable = variable;
88 result = Simantics.applySCLRead(graph, function, moduleVariable);
89 } catch (PendingVariableException | MissingVariableValueException e) {
93 Resource v1 = graph.getSingleObject(mapElement, DN.HasStartVertex);
94 double[] coords1 = graph.getRelatedValue(v1, DIA.HasLocation);
95 Resource v2 = graph.getSingleObject(mapElement, DN.HasEndVertex);
96 double[] coords2 = graph.getRelatedValue(v2, DIA.HasLocation);
97 Point2D p1 = DistrictNetworkNodeUtils.calculatePoint2D(new Point2D.Double(coords1[0], coords1[1]), null);
98 Point2D p2 = DistrictNetworkNodeUtils.calculatePoint2D(new Point2D.Double(coords2[0], coords2[1]), null);
100 return new StyleResult(p1, p2, result);
104 public void applyStyleForNode(EvaluationContext evaluationContext, INode parent, StyleResult result) {
105 if (result == null) {
106 cleanupStyleForNode(evaluationContext, parent);
110 DistrictNetworkStaticInfoNode node = ProfileVariables.claimChild(parent, "*", DistrictNetworkStaticInfoNode.NODE_KEY, DistrictNetworkStaticInfoNode.class, evaluationContext);
114 Point2D p1 = result.p1;
115 Point2D p2 = result.p2;
117 double sign = Math.signum(p1.getX() - p2.getX());
118 Point2D.Double origin = new Point2D.Double(0.5 * (p1.getX() + p2.getX()), 0.5 * (p1.getY() + p2.getY()));
119 Point2D direction = new Point2D.Double(0.5 * sign * (p1.getX() - p2.getX()), 0.5 * sign * (p1.getY() - p2.getY()));
121 node.setLocation(origin, direction);
122 node.setInfo(result.info);
125 private static Function1<Variable, String> getUCPipelineInfoFunctionCached(ReadGraph graph, Resource componentType)
126 throws DatabaseException {
127 return graph.syncRequest(new UCPipelineInfoRequest(componentType), TransientCacheListener.instance());
130 private static final class UCPipelineInfoRequest extends ResourceRead<Function1<Variable, String>> {
131 public UCPipelineInfoRequest(Resource resource) {
135 @SuppressWarnings("unchecked")
137 public Function1<Variable, String> perform(ReadGraph graph) throws DatabaseException {
138 Resource actionsModule = Layer0Utils.getPossibleChild(graph, resource, ACTIONS_MODULE);
139 if (actionsModule == null || !graph.isInstanceOf(actionsModule, Layer0.getInstance(graph).SCLModule))
142 String uri = graph.getURI(actionsModule);
143 SCLContext sclContext = SCLContext.getCurrent();
144 Object oldGraph = sclContext.get("graph");
146 sclContext.put("graph", graph);
147 return (Function1<Variable, String>) SCLOsgi.MODULE_REPOSITORY.getValue(uri, PIPELINE_INFO);
148 } catch (ValueNotFound e1) {
151 sclContext.put("graph", oldGraph);
157 protected void cleanupStyleForNode(EvaluationContext evaluationContext, INode node) {
158 ProfileVariables.denyChild(node, DistrictNetworkStaticInfoNode.NODE_KEY);