]> gerrit.simantics Code Review - simantics/platform.git/blobdiff - bundles/org.simantics.document.server/src/org/simantics/document/server/request/ServerSCLValueRequest.java
Document performance optimizations
[simantics/platform.git] / bundles / org.simantics.document.server / src / org / simantics / document / server / request / ServerSCLValueRequest.java
index 25afd86893fd17a83475aa11f4ec9b2ea148ffb3..dce4b34ab44e46c7d28ef70b3902b0663ea4fbe6 100644 (file)
@@ -1,5 +1,6 @@
 package org.simantics.document.server.request;
 
+import java.util.Collections;
 import java.util.Map;
 
 import org.simantics.databoard.Bindings;
@@ -7,12 +8,14 @@ import org.simantics.db.ReadGraph;
 import org.simantics.db.Resource;
 import org.simantics.db.common.procedure.adapter.TransientCacheListener;
 import org.simantics.db.common.request.IndexRoot;
+import org.simantics.db.common.request.PossibleTypedParent;
 import org.simantics.db.common.request.UnaryRead;
 import org.simantics.db.exception.DatabaseException;
 import org.simantics.db.layer0.scl.AbstractExpressionCompilationContext;
 import org.simantics.db.layer0.scl.AbstractExpressionCompilationRequest;
 import org.simantics.db.layer0.util.RuntimeEnvironmentRequest2;
 import org.simantics.db.layer0.variable.Variable;
+import org.simantics.document.base.ontology.DocumentationResource;
 import org.simantics.document.server.request.ServerSCLValueRequest.CompilationContext;
 import org.simantics.layer0.Layer0;
 import org.simantics.scl.compiler.common.names.Name;
@@ -24,8 +27,10 @@ import org.simantics.scl.compiler.elaboration.expressions.EVariable;
 import org.simantics.scl.compiler.elaboration.expressions.Expression;
 import org.simantics.scl.compiler.elaboration.modules.SCLValue;
 import org.simantics.scl.compiler.environment.Environment;
+import org.simantics.scl.compiler.environment.Environments;
 import org.simantics.scl.compiler.environment.specification.EnvironmentSpecification;
 import org.simantics.scl.compiler.runtime.RuntimeEnvironment;
+import org.simantics.scl.compiler.top.SCLExpressionCompilationException;
 import org.simantics.scl.compiler.types.Type;
 import org.simantics.scl.compiler.types.Types;
 import org.simantics.scl.compiler.types.kinds.Kinds;
@@ -35,11 +40,14 @@ import org.simantics.structural2.scl.ComponentTypeProperty;
 import org.simantics.structural2.scl.FindPossibleComponentTypeRequest;
 import org.simantics.structural2.scl.ReadComponentTypeInterfaceRequest;
 import org.simantics.utils.datastructures.Pair;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
 
-public class ServerSCLValueRequest extends AbstractExpressionCompilationRequest<CompilationContext, Variable> {
+public class ServerSCLValueRequest extends AbstractExpressionCompilationRequest<CompilationContext, Object> {
 
        private final Pair<Resource,Resource> componentTypeAndRoot;
        private final Resource literal;
+    protected String possibleExpectedValueType;
 
        public static class CompilationContext extends AbstractExpressionCompilationContext {
                public final Map<String, ComponentTypeProperty> propertyMap;
@@ -51,14 +59,19 @@ public class ServerSCLValueRequest extends AbstractExpressionCompilationRequest<
                }
        }
 
-       private ServerSCLValueRequest(Pair<Resource,Resource> componentTypeAndRoot, Resource literal) {
+       private ServerSCLValueRequest(Pair<Resource,Resource> componentTypeAndRoot, Resource literal, String possibleExpectedValueType) {
                assert(literal != null);
                this.literal = literal;
                this.componentTypeAndRoot = componentTypeAndRoot;
+               this.possibleExpectedValueType = possibleExpectedValueType;
        }
 
        public ServerSCLValueRequest(ReadGraph graph, Variable context) throws DatabaseException {
-               this(getComponentTypeAndRoot(graph, context), context.getRepresents(graph));
+               this(getComponentTypeAndRoot(graph, context), context.getRepresents(graph), resolveExpectedValueType(graph, context.getPredicateResource(graph)));
+       }
+
+       public ServerSCLValueRequest(ReadGraph graph, Resource s, Resource o, Resource p) throws DatabaseException {
+           this(getComponentTypeAndRoot(graph, s, o), o, resolveExpectedValueType(graph, p));
        }
 
        private static Pair<Resource,Resource> getComponentTypeAndRoot(ReadGraph graph, Variable property)  throws DatabaseException {
@@ -76,12 +89,41 @@ public class ServerSCLValueRequest extends AbstractExpressionCompilationRequest<
                return Pair.make(parent.getType(graph), root);
        }
 
+       private static Pair<Resource,Resource> getComponentTypeAndRoot(ReadGraph graph, Resource component, Resource literal)  throws DatabaseException {
+               if(component != null) {
+                       Resource type = graph.syncRequest(new FindPossibleComponentTypeRequest(component));
+                       if(type != null) {
+                               Resource root = graph.syncRequest(new IndexRoot(type));
+                               //      System.err.println("getComponentTypeAndRoot3 " + graph.getPossibleURI(component) + " => " + graph.getPossibleURI(type) + " " + graph.getPossibleURI(root));
+                               return Pair.make(type, root);
+                       } else {
+                               Resource doc = graph.syncRequest(new PossibleTypedParent(component, DocumentationResource.getInstance(graph).Document));
+                               if(doc != null) {
+                                       Resource componentType = graph.getSingleType(doc);
+                                       Resource root = graph.syncRequest(new IndexRoot(doc));
+                                       return Pair.make(componentType, root);
+                               } else {
+                                       //System.err.println("component = " + component);
+                                       Resource root = graph.syncRequest(new IndexRoot(component));
+//                                     Resource componentType = graph.getSingleType(doc);
+                                       return Pair.make(null, root);
+                               }
+                       }
+        // TODO: For Antti to consider and fix later
+        // Introduced to handle procedural user components where component == null
+        } else if (literal != null) {
+            Resource root = graph.syncRequest(new IndexRoot(literal));
+            return Pair.make(null, root);
+        } else {
+            throw new DatabaseException("Couldn't resolve component type and root for component == null && literal == null");
+        }
+       }
+
        public static Object compileAndEvaluate(ReadGraph graph, Variable context) throws DatabaseException {
                SCLContext sclContext = SCLContext.getCurrent();
         Object oldGraph = sclContext.get("graph");
                try {
-                       Function1<Variable,Object> exp = graph.syncRequest(new ServerSCLValueRequest(graph, context),
-                                       TransientCacheListener.<Function1<Variable,Object>>instance());
+                       Function1<Object,Object> exp = compile(graph, context);
                        sclContext.put("graph", graph);
                        return exp.apply(context);
                } catch (DatabaseException e) {
@@ -93,6 +135,14 @@ public class ServerSCLValueRequest extends AbstractExpressionCompilationRequest<
                }
        }
 
+       public static Function1<Object, Object> compile(ReadGraph graph, Variable context) throws DatabaseException {
+               return graph.syncRequest(new ServerSCLValueRequest(graph, context), TransientCacheListener.instance());
+       }
+
+       public static Function1<Object, Object> compile(ReadGraph graph, Resource s, Resource o, Resource p) throws DatabaseException {
+               return graph.syncRequest(new ServerSCLValueRequest(graph, s, o, p), TransientCacheListener.instance());
+       }
+
        @Override
        protected String getExpressionText(ReadGraph graph)
                        throws DatabaseException {
@@ -117,9 +167,16 @@ public class ServerSCLValueRequest extends AbstractExpressionCompilationRequest<
                        public CompilationContext perform(ReadGraph graph)
                                        throws DatabaseException {
                                RuntimeEnvironment runtimeEnvironment = graph.syncRequest(getRuntimeEnvironmentRequest(parameter.first, parameter.second));
-                               Map<String, ComponentTypeProperty> propertyMap =
+                               Map<String, ComponentTypeProperty> propertyMap;
+                               if (parameter.first != null) {
+                                   propertyMap =
                                                graph.syncRequest(new ReadComponentTypeInterfaceRequest(parameter.first, runtimeEnvironment.getEnvironment()),
                                                                TransientCacheListener.<Map<String, ComponentTypeProperty>>instance());
+                               } else {
+                                   // TODO: Antti to consider
+                                   // To handle procedural user components
+                                   propertyMap = Collections.emptyMap();
+                               }
                                return new CompilationContext(runtimeEnvironment, propertyMap);
                        }
                });
@@ -192,6 +249,12 @@ public class ServerSCLValueRequest extends AbstractExpressionCompilationRequest<
                else
                        return null;
        }
+       
+    @Override
+    protected Type getExpectedType(ReadGraph graph, CompilationContext context) throws DatabaseException {
+        return super.getExpectedType(graph, context);
+    }
+
 
        @Override
        public int hashCode() {
@@ -207,4 +270,29 @@ public class ServerSCLValueRequest extends AbstractExpressionCompilationRequest<
                ServerSCLValueRequest other = (ServerSCLValueRequest)obj;
                return literal.equals(other.literal) && componentTypeAndRoot.equals(other.componentTypeAndRoot);
        }
+
+    public static Function1<Object, Object> validate(ReadGraph graph, Variable context) throws DatabaseException {
+        return graph.syncRequest(new ServerSCLValueValidationRequest(graph, context), TransientCacheListener.instance());
+    }
+    
+    public static class ServerSCLValueValidationRequest extends ServerSCLValueRequest {
+
+        private static final Logger LOGGER = LoggerFactory.getLogger(ServerSCLHandlerValueRequest.class);
+
+        public ServerSCLValueValidationRequest(ReadGraph graph, Variable context) throws DatabaseException {
+            super(graph, context);
+        }
+
+        @Override
+        protected Type getExpectedType(ReadGraph graph, CompilationContext context) throws DatabaseException {
+            if(possibleExpectedValueType != null) {
+                try {
+                    return Environments.getType(context.runtimeEnvironment.getEnvironment(), possibleExpectedValueType);
+                } catch (SCLExpressionCompilationException e) {
+                    LOGGER.error("Could not get type for " + String.valueOf(possibleExpectedValueType), e);
+                }
+            }
+            return super.getExpectedType(graph, context);
+        }
+    }
 }