]> gerrit.simantics Code Review - simantics/platform.git/blobdiff - bundles/org.simantics.document.server/src/org/simantics/document/server/request/ServerSCLHandlerValueRequest.java
Merge "Added effectful Kleisli composition to Prelude."
[simantics/platform.git] / bundles / org.simantics.document.server / src / org / simantics / document / server / request / ServerSCLHandlerValueRequest.java
index 286cae2728c9f12660c71e5fe2dc38b934dd0568..8ca55eaa5ab67affe0460dcd7ff0c2ff4a81d277 100644 (file)
@@ -1,5 +1,6 @@
 package org.simantics.document.server.request;
 
+import java.util.Collections;
 import java.util.List;
 import java.util.Map;
 
@@ -8,12 +9,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.request.VariableRead;
 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.ServerSCLHandlerValueRequest.CompilationContext;
 import org.simantics.layer0.Layer0;
 import org.simantics.scl.compiler.elaboration.expressions.EApply;
@@ -22,10 +25,8 @@ 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.TCon;
 import org.simantics.scl.compiler.types.Type;
 import org.simantics.scl.compiler.types.Types;
@@ -36,14 +37,9 @@ 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 ServerSCLHandlerValueRequest extends AbstractExpressionCompilationRequest<CompilationContext, Variable> {
+public class ServerSCLHandlerValueRequest extends AbstractExpressionCompilationRequest<CompilationContext, Object> {
 
-    private static final Logger LOGGER = LoggerFactory.getLogger(ServerSCLHandlerValueRequest.class);
-
-       private final Variable context;
        private final Pair<Resource,Resource> componentTypeAndRoot;
        private final Resource literal;
        protected String possibleExpectedValueType;
@@ -58,16 +54,19 @@ public class ServerSCLHandlerValueRequest extends AbstractExpressionCompilationR
                }
        }
 
-       private ServerSCLHandlerValueRequest(Variable context, Pair<Resource,Resource> componentTypeAndRoot, Resource literal, String possibleExpectedValueType) {
+       private ServerSCLHandlerValueRequest(Pair<Resource,Resource> componentTypeAndRoot, Resource literal, String possibleExpectedValueType) {
                assert(literal != null);
-               this.context = context;
                this.literal = literal;
                this.componentTypeAndRoot = componentTypeAndRoot;
                this.possibleExpectedValueType = possibleExpectedValueType;
        }
 
        public ServerSCLHandlerValueRequest(ReadGraph graph, Variable context) throws DatabaseException {
-               this(context, getComponentTypeAndRoot(graph, context), context.getRepresents(graph), resolveExpectedValueType(graph, context));
+               this(getComponentTypeAndRoot(graph, context), context.getRepresents(graph), resolveExpectedValueType(graph, context.getPredicateResource(graph)));
+       }
+
+       public ServerSCLHandlerValueRequest(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 {
@@ -85,6 +84,28 @@ public class ServerSCLHandlerValueRequest extends AbstractExpressionCompilationR
                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));
+                               return Pair.make(type, root);
+                       } else {
+                               Resource doc = graph.syncRequest(new PossibleTypedParent(component, DocumentationResource.getInstance(graph).Document));
+                               Resource componentType = graph.getSingleType(doc); 
+                               Resource root = graph.syncRequest(new IndexRoot(doc));
+                               return Pair.make(componentType, 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");
+               }
+               //throw new IllegalStateException();
+       }
 
     public static List<TCon> getEffects(ReadGraph graph, Variable context) throws DatabaseException {
         try {
@@ -101,8 +122,8 @@ public class ServerSCLHandlerValueRequest extends AbstractExpressionCompilationR
                SCLContext sclContext = SCLContext.getCurrent();
         Object oldGraph = sclContext.get("graph");
                try {
-                       Function1<Variable,Object> exp = graph.syncRequest(new ServerSCLHandlerValueRequest(graph, context),
-                                       TransientCacheListener.<Function1<Variable,Object>>instance());
+                       Function1<Object,Object> exp = graph.syncRequest(new ServerSCLHandlerValueRequest(graph, context),
+                                       TransientCacheListener.instance());
                        sclContext.put("graph", graph);
                        return exp.apply(context);
                } catch (DatabaseException e) {
@@ -114,6 +135,14 @@ public class ServerSCLHandlerValueRequest extends AbstractExpressionCompilationR
                }
        }
 
+       public static Function1<Object, Object> compile(ReadGraph graph, Variable context) throws DatabaseException {
+               return graph.syncRequest(new ServerSCLHandlerValueRequest(graph, context), TransientCacheListener.instance());
+       }
+
+       public static Function1<Object, Object> compile(ReadGraph graph, Resource s, Resource o, Resource p) throws DatabaseException {
+               return graph.syncRequest(new ServerSCLHandlerValueRequest(graph, s, o, p), TransientCacheListener.<Function1<Object,Object>>instance());
+       }
+
        @Override
        protected String getExpressionText(ReadGraph graph)
                        throws DatabaseException {
@@ -133,30 +162,26 @@ public class ServerSCLHandlerValueRequest extends AbstractExpressionCompilationR
 
        @Override
        protected CompilationContext getCompilationContext(ReadGraph graph) throws DatabaseException {
-               
-               return graph.syncRequest(new VariableRead<CompilationContext>(context) {
-                       
+               return graph.syncRequest(new UnaryRead<Pair<Resource,Resource>,CompilationContext>(componentTypeAndRoot) {
                        @Override
                        public CompilationContext perform(ReadGraph graph) throws DatabaseException {
-                               
-                               Pair<Resource,Resource> parameter = getComponentTypeAndRoot(graph, variable);
                                RuntimeEnvironment runtimeEnvironment = graph.syncRequest(getRuntimeEnvironmentRequest(parameter.first, parameter.second));
-                               
-                               Map<String, ComponentTypeProperty> propertyMap =
-                                               graph.syncRequest(new ReadComponentTypeInterfaceRequest(parameter.first, runtimeEnvironment.getEnvironment()),
-                                                               TransientCacheListener.<Map<String, ComponentTypeProperty>>instance());
-
-//                             Map<String, ComponentTypeProperty> result = new HashMap<String,ComponentTypeProperty>(propertyMap); 
-//                             for(DataDefinition dd : Functions.dataDefinitions(graph, variable)) {
-//                                     result.put(dd.target, null);
-//                             }
-                               
+                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();
+                }
+//                             Map<String, ComponentTypeProperty> propertyMap =
+//                                             graph.syncRequest(new ReadComponentTypeInterfaceRequest(parameter.first, runtimeEnvironment.getEnvironment()),
+//                                                             TransientCacheListener.<Map<String, ComponentTypeProperty>>instance());
                                return new CompilationContext(runtimeEnvironment, propertyMap);
-                               
                        }
-                       
                });
-               
        }
 
        @Override
@@ -230,23 +255,12 @@ public class ServerSCLHandlerValueRequest extends AbstractExpressionCompilationR
 
     @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);
     }
 
-
        @Override
        public int hashCode() {
-               final int prime = 31;
-               int result = 1;
-               result = prime * result + ((context == null) ? 0 : context.hashCode());
-               return result;
+               return 31*(31*getClass().hashCode() + literal.hashCode()) + componentTypeAndRoot.hashCode();
        }
 
        @Override
@@ -258,27 +272,7 @@ public class ServerSCLHandlerValueRequest extends AbstractExpressionCompilationR
                if (getClass() != obj.getClass())
                        return false;
                ServerSCLHandlerValueRequest other = (ServerSCLHandlerValueRequest) obj;
-               if (context == null) {
-                       if (other.context != null)
-                               return false;
-               } else if (!context.equals(other.context))
-                       return false;
-               return true;
+               return literal.equals(other.literal) && componentTypeAndRoot.equals(other.componentTypeAndRoot);
        }
 
-//     @Override
-//     public int hashCode() {
-//             return 31*(31*getClass().hashCode() + literal.hashCode()) + componentTypeAndRoot.hashCode();
-//     }
-//
-//     @Override
-//     public boolean equals(Object obj) {
-//             if(this == obj)
-//                     return true;
-//             if(obj == null || obj.getClass() != getClass())
-//                     return false;
-//             ServerSCLHandlerValueRequest other = (ServerSCLHandlerValueRequest)obj;
-//             return literal.equals(other.literal) && componentTypeAndRoot.equals(other.componentTypeAndRoot);
-//     }
-       
 }