]> gerrit.simantics Code Review - simantics/platform.git/blobdiff - bundles/org.simantics.document.server/src/org/simantics/document/server/request/ServerSCLValueRequest.java
Merge remote-tracking branch 'origin/master' into private/db-threads
[simantics/platform.git] / bundles / org.simantics.document.server / src / org / simantics / document / server / request / ServerSCLValueRequest.java
index b4d4d2c1fc2970b567541240a9c706a63b502262..77cd9b4f4a3a8cfe175b27c40508819dc3cfa880 100644 (file)
@@ -7,12 +7,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;
@@ -42,8 +44,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;
@@ -66,7 +66,11 @@ public class ServerSCLValueRequest extends AbstractExpressionCompilationRequest<
        }
 
        public ServerSCLValueRequest(ReadGraph graph, Variable context) throws DatabaseException {
-               this(getComponentTypeAndRoot(graph, context), context.getRepresents(graph), resolveExpectedValueType(graph, context));
+               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, resolveExpectedValueType(graph, p));
        }
 
        private static Pair<Resource,Resource> getComponentTypeAndRoot(ReadGraph graph, Variable property)  throws DatabaseException {
@@ -79,11 +83,29 @@ public class ServerSCLValueRequest extends AbstractExpressionCompilationRequest<
                                return Pair.make(type, root);
                        }
                }
-               parent = parent.getParent(graph);
+               while(represents == null) {
+                       parent = parent.getParent(graph);
+                       represents = parent.getPossibleRepresents(graph);
+               }
                Resource root = graph.syncRequest(new IndexRoot(property.getRepresents(graph)));
                return Pair.make(parent.getType(graph), root);
        }
 
+       private static Pair<Resource,Resource> getComponentTypeAndRoot(ReadGraph graph, Resource component)  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 root = graph.syncRequest(new IndexRoot(component));
+                               return Pair.make(graph.getSingleType(doc), root);
+                       }
+               }
+               throw new IllegalStateException();
+       }
+
        public static Object compileAndEvaluate(ReadGraph graph, Variable context) throws DatabaseException {
                SCLContext sclContext = SCLContext.getCurrent();
         Object oldGraph = sclContext.get("graph");
@@ -104,6 +126,10 @@ public class ServerSCLValueRequest extends AbstractExpressionCompilationRequest<
            return graph.syncRequest(new ServerSCLValueRequest(graph, context), TransientCacheListener.<Function1<Variable,Object>>instance());
        }
 
+       public static Function1<Variable, Object> compile(ReadGraph graph, Resource s, Resource o, Resource p) throws DatabaseException {
+           return graph.syncRequest(new ServerSCLValueRequest(graph, s, o, p), TransientCacheListener.<Function1<Variable,Object>>instance());
+       }
+
        @Override
        protected String getExpressionText(ReadGraph graph)
                        throws DatabaseException {
@@ -206,13 +232,6 @@ public class ServerSCLValueRequest extends AbstractExpressionCompilationRequest<
        
     @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);
     }
 
@@ -231,4 +250,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);
+        }
+    }
 }