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;
21 public class VertexSymbolStyle extends ThrottledStyleBase<String> {
23 private static final Logger LOGGER = LoggerFactory.getLogger(VertexSymbolStyle.class);
25 @SuppressWarnings({ "rawtypes", "unchecked" })
27 public String calculateThrottledStyle(ReadGraph graph, Resource runtimeDiagram, Resource entry, Resource groupItem) throws DatabaseException {
28 // Prevent PendingVariableExceptions from coming through
29 boolean wasSynchronous = graph.setSynchronous(true);
31 Function symbolFunction = getSymbolFunction(graph, entry);
32 if (symbolFunction == null)
36 return (String) Simantics.applySCLRead(graph, symbolFunction, groupItem);
37 } catch (Exception e) {
38 LOGGER.error("Getting dynamic symbol for " + groupItem + " (" + graph.getPossibleRelatedValue(groupItem, Layer0.getInstance(graph).HasName) + ") failed", e);
43 graph.setSynchronous(wasSynchronous);
47 @SuppressWarnings("rawtypes")
48 protected static Function getSymbolFunction(ReadGraph graph, Resource entry) throws DatabaseException {
49 // Cache function read for profile entry
50 return graph.syncRequest(new SymbolFunctionRequest(entry), TransientCacheListener.<Function>instance());
54 public void applyThrottledStyleForNode(EvaluationContext observer, INode node, String value) {
55 SingleElementNode n = (SingleElementNode) node;
56 for (INode nn : n.getNodes())
57 ProfileVariables.claimNodeProperty(nn, "SVG", value, observer);
61 protected void cleanupStyleForNode(EvaluationContext evaluationContext, INode node) {
62 ((G2DSceneGraph)node.getRootNode()).clearPending(node);
63 SingleElementNode n = (SingleElementNode) node;
64 for (INode nn : n.getNodes())
65 ProfileVariables.claimNodeProperty(nn, "SVG", null, evaluationContext);
68 @SuppressWarnings("rawtypes")
69 protected static final class SymbolFunctionRequest extends ResourceRead<Function> {
70 protected static final Function CONST_NULL = new FunctionImpl1<Resource, String>() {
72 public String apply(Resource p0) {
77 public SymbolFunctionRequest(Resource entry) {
82 public Function perform(ReadGraph graph) throws DatabaseException {
83 DistrictNetworkResource DN = DistrictNetworkResource.getInstance(graph);
85 Function symbolFunction = (Function) graph.getPossibleRelatedValue2(resource, DN.HasSymbolFunction, resource);
86 if (symbolFunction == null)
87 symbolFunction = CONST_NULL;
89 return symbolFunction;