1 package org.simantics.district.network.profile;
3 import org.simantics.Simantics;
4 import org.simantics.db.ReadGraph;
5 import org.simantics.db.Resource;
6 import org.simantics.db.common.procedure.adapter.TransientCacheListener;
7 import org.simantics.db.common.request.ResourceRead;
8 import org.simantics.db.exception.DatabaseException;
9 import org.simantics.district.network.ontology.DistrictNetworkResource;
10 import org.simantics.layer0.Layer0;
11 import org.simantics.scenegraph.INode;
12 import org.simantics.scenegraph.g2d.G2DSceneGraph;
13 import org.simantics.scenegraph.g2d.nodes.SingleElementNode;
14 import org.simantics.scenegraph.profile.EvaluationContext;
15 import org.simantics.scenegraph.profile.common.ProfileVariables;
16 import org.simantics.scl.runtime.function.Function;
17 import org.simantics.scl.runtime.function.FunctionImpl1;
18 import org.slf4j.Logger;
19 import org.slf4j.LoggerFactory;
22 public class VertexSymbolStyle extends ThrottledStyleBase<String> {
24 private static final Logger LOGGER = LoggerFactory.getLogger(VertexSymbolStyle.class);
27 public String calculateThrottledStyle(ReadGraph graph, Resource runtimeDiagram, Resource entry, Resource groupItem) throws DatabaseException {
28 return calculateStyle(graph, entry, groupItem);
31 @SuppressWarnings({ "rawtypes", "unchecked" })
32 public static String calculateStyle(ReadGraph graph, Resource entry, Resource groupItem) throws DatabaseException {
33 // Prevent PendingVariableExceptions from coming through
34 boolean wasSynchronous = graph.setSynchronous(true);
36 Function symbolFunction = getSymbolFunction(graph, entry);
37 if (symbolFunction == null)
41 return (String) Simantics.applySCLRead(graph, symbolFunction, groupItem);
42 } catch (Exception e) {
43 LOGGER.error("Getting dynamic symbol for " + groupItem + " ("
44 + graph.getPossibleRelatedValue(groupItem, Layer0.getInstance(graph).HasName) + ") failed", e);
48 graph.setSynchronous(wasSynchronous);
52 @SuppressWarnings("rawtypes")
53 public static Function getSymbolFunction(ReadGraph graph, Resource entry) throws DatabaseException {
54 // Cache function read for profile entry
55 return graph.syncRequest(new SymbolFunctionRequest(entry), TransientCacheListener.<Function>instance());
59 public void applyThrottledStyleForNode(EvaluationContext observer, INode node, String value) {
60 SingleElementNode n = (SingleElementNode) node;
61 for (INode nn : n.getNodes())
62 ProfileVariables.claimNodeProperty(nn, "SVG", value, observer);
66 protected void cleanupStyleForNode(EvaluationContext evaluationContext, INode node) {
67 ((G2DSceneGraph)node.getRootNode()).clearPending(node);
68 SingleElementNode n = (SingleElementNode) node;
69 for (INode nn : n.getNodes())
70 ProfileVariables.claimNodeProperty(nn, "SVG", null, evaluationContext);
73 @SuppressWarnings("rawtypes")
74 protected static final class SymbolFunctionRequest extends ResourceRead<Function> {
75 protected static final Function CONST_NULL = new FunctionImpl1<Resource, String>() {
77 public String apply(Resource p0) {
82 public SymbolFunctionRequest(Resource entry) {
87 public Function perform(ReadGraph graph) throws DatabaseException {
88 DistrictNetworkResource DN = DistrictNetworkResource.getInstance(graph);
90 Function symbolFunction = (Function) graph.getPossibleRelatedValue2(resource, DN.HasSymbolFunction, resource);
91 if (symbolFunction == null)
92 symbolFunction = CONST_NULL;
94 return symbolFunction;