]> gerrit.simantics Code Review - simantics/platform.git/blobdiff - bundles/org.simantics.document.server/src/org/simantics/document/server/request/ServerSCLValueRequest.java
Create separate requests for validating SCL values
[simantics/platform.git] / bundles / org.simantics.document.server / src / org / simantics / document / server / request / ServerSCLValueRequest.java
index e6cdf6ff5b43712b098594d172a91b7595de63cb..f9181d271073964b6082fef5119a679e8c371075 100644 (file)
@@ -1,6 +1,5 @@
 package org.simantics.document.server.request;
 
-import java.util.ArrayList;
 import java.util.Map;
 
 import org.simantics.databoard.Bindings;
@@ -29,8 +28,6 @@ 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.TMetaVar;
-import org.simantics.scl.compiler.types.TVar;
 import org.simantics.scl.compiler.types.Type;
 import org.simantics.scl.compiler.types.Types;
 import org.simantics.scl.compiler.types.kinds.Kinds;
@@ -45,8 +42,6 @@ import org.slf4j.LoggerFactory;
 
 public class ServerSCLValueRequest extends AbstractExpressionCompilationRequest<CompilationContext, Variable> {
 
-    private static final Logger LOGGER = LoggerFactory.getLogger(ServerSCLValueRequest.class);
-    
        private final Pair<Resource,Resource> componentTypeAndRoot;
        private final Resource literal;
     protected String possibleExpectedValueType;
@@ -91,8 +86,7 @@ public class ServerSCLValueRequest extends AbstractExpressionCompilationRequest<
                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<Variable,Object> exp = compile(graph, context);
                        sclContext.put("graph", graph);
                        return exp.apply(context);
                } catch (DatabaseException e) {
@@ -103,6 +97,10 @@ public class ServerSCLValueRequest extends AbstractExpressionCompilationRequest<
             sclContext.put("graph", oldGraph);
                }
        }
+       
+       public static Function1<Variable, Object> compile(ReadGraph graph, Variable context) throws DatabaseException {
+           return graph.syncRequest(new ServerSCLValueRequest(graph, context), TransientCacheListener.<Function1<Variable,Object>>instance());
+       }
 
        @Override
        protected String getExpressionText(ReadGraph graph)
@@ -206,15 +204,6 @@ public class ServerSCLValueRequest extends AbstractExpressionCompilationRequest<
        
     @Override
     protected Type getExpectedType(ReadGraph graph, CompilationContext context) throws DatabaseException {
-        if(possibleExpectedValueType != null) {
-            try {
-                Type type = Environments.getType(context.runtimeEnvironment.getEnvironment(), possibleExpectedValueType);
-                type = Types.instantiate(Types.forAll(Types.freeVars(type).toArray(new TVar[0]), type), new ArrayList<TMetaVar>());
-                return type;
-            } catch (SCLExpressionCompilationException e) {
-                LOGGER.error("Could not get type for " + String.valueOf(possibleExpectedValueType), e);
-            }
-        }
         return super.getExpectedType(graph, context);
     }
 
@@ -233,4 +222,29 @@ public class ServerSCLValueRequest extends AbstractExpressionCompilationRequest<
                ServerSCLValueRequest other = (ServerSCLValueRequest)obj;
                return literal.equals(other.literal) && componentTypeAndRoot.equals(other.componentTypeAndRoot);
        }
+
+    public static Function1<Variable, Object> validate(ReadGraph graph, Variable context) throws DatabaseException {
+        return graph.syncRequest(new ServerSCLValueValidationRequest(graph, context), TransientCacheListener.<Function1<Variable,Object>>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);
+        }
+    }
 }