]> gerrit.simantics Code Review - simantics/district.git/commitdiff
Value type of dynamic symbol function to Maybe String 62/2662/4
authorReino Ruusu <reino.ruusu@semantum.fi>
Wed, 20 Feb 2019 17:38:20 +0000 (19:38 +0200)
committerReino Ruusu <reino.ruusu@semantum.fi>
Wed, 20 Feb 2019 18:33:38 +0000 (20:33 +0200)
Also add caching for symbol functions.

gitlab #27

Change-Id: I34ae22e6e1deae57bbf03ced6ba830c52ec74335

org.simantics.district.network.ontology/graph/DistrictNetworkProfiles.pgraph
org.simantics.district.network/src/org/simantics/district/network/profile/VertexSymbolStyle.java

index d3734e63c0103873bf17a0e7278b31c9082de093..a5c5cee59b445966d6c01034e43c20dead2ffcd8 100644 (file)
@@ -31,4 +31,4 @@ DN.VertexSymbolStyle : DIA.Style
 DN.HasSymbolFunction <R L0.HasProperty
     <-- DIA.ProfileEntry
     --> L0.Value
-    ==> "Resource -> <ReadGraph> String"
+    ==> "Resource -> <ReadGraph> Maybe String"
index 98078f82f0ada9ebc70149801f2e6051109e9d4d..96ff439235971ec19438150e36ff8f2cb019b68b 100644 (file)
@@ -1,31 +1,50 @@
 package org.simantics.district.network.profile;
 
+import org.simantics.Simantics;
 import org.simantics.db.ReadGraph;
 import org.simantics.db.Resource;
+import org.simantics.db.common.procedure.adapter.TransientCacheListener;
+import org.simantics.db.common.request.ResourceRead;
 import org.simantics.db.exception.DatabaseException;
+import org.simantics.db.layer0.exception.PendingVariableException;
 import org.simantics.district.network.ontology.DistrictNetworkResource;
+import org.simantics.layer0.Layer0;
 import org.simantics.scenegraph.INode;
 import org.simantics.scenegraph.g2d.G2DSceneGraph;
 import org.simantics.scenegraph.g2d.nodes.SingleElementNode;
 import org.simantics.scenegraph.profile.EvaluationContext;
 import org.simantics.scenegraph.profile.common.ProfileVariables;
 import org.simantics.scl.runtime.function.Function;
+import org.simantics.scl.runtime.function.FunctionImpl1;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
 
 public class VertexSymbolStyle extends ThrottledStyleBase<String> {
 
+       private static final Logger LOGGER = LoggerFactory.getLogger(VertexSymbolStyle.class);
+       
        @SuppressWarnings({ "rawtypes", "unchecked" })
        @Override
        public String calculateThrottledStyle(ReadGraph graph, Resource runtimeDiagram, Resource entry, Resource groupItem) throws DatabaseException {
-               DistrictNetworkResource DN = DistrictNetworkResource.getInstance(graph);
-               Resource symbolFunctionResource = graph.getPossibleObject(entry, DN.HasSymbolFunction);
-               if (symbolFunctionResource == null)
-                       return null;
-               
-               Function symbolFunction = (Function) graph.getPossibleValue2(symbolFunctionResource, null);
+
+               Function symbolFunction = getSymbolFunction(graph, entry);
                if (symbolFunction == null)
                        return null;
                
-               return (String) symbolFunction.apply(graph, entry);
+               try {
+                       return (String) Simantics.applySCLRead(graph, symbolFunction, groupItem);
+               } catch (PendingVariableException e) {
+                       throw e;
+               } catch (Exception e) {
+                       LOGGER.error("Getting dynamic symbol for " + groupItem + " (" + graph.getPossibleRelatedValue(groupItem, Layer0.getInstance(graph).HasName) + ") failed", e);
+                       return null;
+               }
+       }
+
+       @SuppressWarnings("rawtypes")
+       protected static Function getSymbolFunction(ReadGraph graph, Resource entry) throws DatabaseException {
+               // Cache function read for profile entry
+               return graph.syncRequest(new SymbolFunctionRequest(entry), TransientCacheListener.<Function>instance());
        }
 
        @Override
@@ -43,4 +62,29 @@ public class VertexSymbolStyle extends ThrottledStyleBase<String> {
                        ProfileVariables.claimNodeProperty(nn, "SVG", null, evaluationContext);
        }
 
+       @SuppressWarnings("rawtypes")
+       protected static final class SymbolFunctionRequest extends ResourceRead<Function> {
+               protected static final Function CONST_NULL = new FunctionImpl1<Resource, String>() {
+                       @Override
+                       public String apply(Resource p0) {
+                               return null;
+                       }
+               };
+               
+               public SymbolFunctionRequest(Resource entry) {
+                       super(entry);
+               }
+
+               @Override
+               public Function perform(ReadGraph graph) throws DatabaseException {
+                       DistrictNetworkResource DN = DistrictNetworkResource.getInstance(graph);
+                       
+                       Function symbolFunction = (Function) graph.getPossibleRelatedValue2(resource, DN.HasSymbolFunction, resource);
+                       if (symbolFunction == null)
+                               symbolFunction = CONST_NULL;
+                       
+                       return symbolFunction;
+               }
+       }
+
 }