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.db.layer0.exception.PendingVariableException;
10 import org.simantics.district.network.ontology.DistrictNetworkResource;
11 import org.simantics.layer0.Layer0;
12 import org.simantics.scenegraph.INode;
13 import org.simantics.scenegraph.g2d.G2DSceneGraph;
14 import org.simantics.scenegraph.g2d.nodes.SingleElementNode;
15 import org.simantics.scenegraph.profile.EvaluationContext;
16 import org.simantics.scenegraph.profile.common.ProfileVariables;
17 import org.simantics.scl.runtime.function.Function;
18 import org.simantics.scl.runtime.function.FunctionImpl1;
19 import org.slf4j.Logger;
20 import org.slf4j.LoggerFactory;
22 public class VertexSymbolStyle extends ThrottledStyleBase<String> {
24 private static final Logger LOGGER = LoggerFactory.getLogger(VertexSymbolStyle.class);
26 @SuppressWarnings({ "rawtypes", "unchecked" })
28 public String calculateThrottledStyle(ReadGraph graph, Resource runtimeDiagram, Resource entry, Resource groupItem) throws DatabaseException {
30 Function symbolFunction = getSymbolFunction(graph, entry);
31 if (symbolFunction == null)
35 return (String) Simantics.applySCLRead(graph, symbolFunction, groupItem);
36 } catch (PendingVariableException e) {
38 } catch (Exception e) {
39 LOGGER.error("Getting dynamic symbol for " + groupItem + " (" + graph.getPossibleRelatedValue(groupItem, Layer0.getInstance(graph).HasName) + ") failed", e);
44 @SuppressWarnings("rawtypes")
45 protected static Function getSymbolFunction(ReadGraph graph, Resource entry) throws DatabaseException {
46 // Cache function read for profile entry
47 return graph.syncRequest(new SymbolFunctionRequest(entry), TransientCacheListener.<Function>instance());
51 public void applyThrottledStyleForNode(EvaluationContext observer, INode node, String value) {
52 SingleElementNode n = (SingleElementNode) node;
53 for (INode nn : n.getNodes())
54 ProfileVariables.claimNodeProperty(nn, "SVG", value, observer);
58 protected void cleanupStyleForNode(EvaluationContext evaluationContext, INode node) {
59 ((G2DSceneGraph)node.getRootNode()).clearPending(node);
60 SingleElementNode n = (SingleElementNode) node;
61 for (INode nn : n.getNodes())
62 ProfileVariables.claimNodeProperty(nn, "SVG", null, evaluationContext);
65 @SuppressWarnings("rawtypes")
66 protected static final class SymbolFunctionRequest extends ResourceRead<Function> {
67 protected static final Function CONST_NULL = new FunctionImpl1<Resource, String>() {
69 public String apply(Resource p0) {
74 public SymbolFunctionRequest(Resource entry) {
79 public Function perform(ReadGraph graph) throws DatabaseException {
80 DistrictNetworkResource DN = DistrictNetworkResource.getInstance(graph);
82 Function symbolFunction = (Function) graph.getPossibleRelatedValue2(resource, DN.HasSymbolFunction, resource);
83 if (symbolFunction == null)
84 symbolFunction = CONST_NULL;
86 return symbolFunction;