]> gerrit.simantics Code Review - simantics/platform.git/blobdiff - bundles/org.simantics.document.server/src/org/simantics/document/server/request/ServerSCLValueRequest.java
Still working for multiple readers
[simantics/platform.git] / bundles / org.simantics.document.server / src / org / simantics / document / server / request / ServerSCLValueRequest.java
index f9181d271073964b6082fef5119a679e8c371075..b2dc03b73f3fee14c847a686f9b7fe6c6229d886 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;
@@ -64,7 +67,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 {
@@ -74,12 +81,43 @@ public class ServerSCLValueRequest extends AbstractExpressionCompilationRequest<
                        Resource type = graph.syncRequest(new FindPossibleComponentTypeRequest(represents));
                        if(type != null) {
                                Resource root = graph.syncRequest(new IndexRoot(type));
+                               //System.err.println("getComponentTypeAndRoot1 " + property.getURI(graph) + " => " + graph.getPossibleURI(type) + " " + graph.getPossibleURI(root));
+                               return Pair.make(type, root);
+                       }
+               }
+               while(represents == null) {
+                       parent = parent.getParent(graph);
+                       represents = parent.getPossibleRepresents(graph);
+               }
+//             Resource root = graph.syncRequest(new IndexRoot(property.getRepresents(graph)));
+        Resource componentType = parent.getType(graph);
+        Resource root = graph.syncRequest(new IndexRoot(parent.getRepresents(graph)));
+        //System.err.println("getComponentTypeAndRoot2 " + property.getURI(graph) + " => " + graph.getPossibleURI(componentType) + " " + graph.getPossibleURI(root));
+               return Pair.make(componentType, 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));
+          //      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);
+                               }
                        }
                }
-               parent = parent.getParent(graph);
-               Resource root = graph.syncRequest(new IndexRoot(property.getRepresents(graph)));
-               return Pair.make(parent.getType(graph), root);
+               throw new IllegalStateException();
        }
 
        public static Object compileAndEvaluate(ReadGraph graph, Variable context) throws DatabaseException {
@@ -102,6 +140,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 {
@@ -126,10 +168,14 @@ 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 =
-                                               graph.syncRequest(new ReadComponentTypeInterfaceRequest(parameter.first, runtimeEnvironment.getEnvironment()),
-                                                               TransientCacheListener.<Map<String, ComponentTypeProperty>>instance());
-                               return new CompilationContext(runtimeEnvironment, propertyMap);
+                               if(parameter.first != null) {
+                                       Map<String, ComponentTypeProperty> propertyMap =
+                                                       graph.syncRequest(new ReadComponentTypeInterfaceRequest(parameter.first, runtimeEnvironment.getEnvironment()),
+                                                                       TransientCacheListener.<Map<String, ComponentTypeProperty>>instance());
+                                       return new CompilationContext(runtimeEnvironment, propertyMap);
+                               } else {
+                                       return new CompilationContext(runtimeEnvironment, Collections.emptyMap());
+                               }
                        }
                });
        }