]> gerrit.simantics Code Review - simantics/platform.git/blobdiff - bundles/org.simantics.document.server/src/org/simantics/document/server/Functions.java
Fixed multiple issues causing dangling references to discarded queries
[simantics/platform.git] / bundles / org.simantics.document.server / src / org / simantics / document / server / Functions.java
index 66cc0047c3389fe4e559486c76f8eb52bf50a8ab..8dea97ad118da8235b185d56b0e0ce539d9d7f0a 100644 (file)
@@ -8,6 +8,7 @@ import java.util.Collections;
 import java.util.HashMap;
 import java.util.List;
 import java.util.Map;
+import java.util.Set;
 import java.util.TreeMap;
 
 import org.simantics.Simantics;
@@ -24,13 +25,16 @@ import org.simantics.db.Session;
 import org.simantics.db.Statement;
 import org.simantics.db.WriteGraph;
 import org.simantics.db.common.primitiverequest.Adapter;
+import org.simantics.db.common.procedure.adapter.TransientCacheAsyncListener;
 import org.simantics.db.common.procedure.adapter.TransientCacheListener;
-import org.simantics.db.common.request.UnaryRead;
+import org.simantics.db.common.request.BinaryRead;
+import org.simantics.db.common.request.ResourceAsyncRead;
 import org.simantics.db.common.request.UniqueRead;
 import org.simantics.db.common.request.WriteResultRequest;
 import org.simantics.db.common.utils.Logger;
 import org.simantics.db.common.utils.NameUtils;
 import org.simantics.db.exception.DatabaseException;
+import org.simantics.db.exception.ServiceException;
 import org.simantics.db.layer0.function.All;
 import org.simantics.db.layer0.request.ProjectModels;
 import org.simantics.db.layer0.request.PropertyInfo;
@@ -42,9 +46,9 @@ import org.simantics.db.layer0.scl.SCLDatabaseException;
 import org.simantics.db.layer0.variable.ConstantPropertyVariable;
 import org.simantics.db.layer0.variable.ProxyChildVariable;
 import org.simantics.db.layer0.variable.ProxySessionRequest;
+import org.simantics.db.layer0.variable.ProxyVariableSupport;
 import org.simantics.db.layer0.variable.ProxyVariables;
 import org.simantics.db.layer0.variable.StandardAssertedGraphPropertyVariable;
-import org.simantics.db.layer0.variable.StandardGraphChildVariable;
 import org.simantics.db.layer0.variable.StandardGraphPropertyVariable;
 import org.simantics.db.layer0.variable.Variable;
 import org.simantics.db.layer0.variable.VariableMap;
@@ -72,8 +76,6 @@ import org.simantics.document.server.state.StateNodeManager;
 import org.simantics.document.server.state.StateRealm;
 import org.simantics.document.server.state.StateSessionManager;
 import org.simantics.modeling.ModelingResources;
-import org.simantics.modeling.scl.SCLRealm;
-import org.simantics.modeling.scl.SCLSessionManager;
 import org.simantics.modeling.services.CaseInsensitiveComponentFunctionNamingStrategy;
 import org.simantics.modeling.services.ComponentNamingStrategy;
 import org.simantics.operation.Layer0X;
@@ -109,21 +111,6 @@ public class Functions {
 
     private static final org.slf4j.Logger LOGGER = LoggerFactory.getLogger(Functions.class);
     
-    private static class PrimitivePropertyStatementsProcedure implements AsyncProcedure<DirectStatements> {
-
-        public DirectStatements result;
-
-        @Override
-        public void execute(AsyncReadGraph graph, DirectStatements result) {
-            this.result = result;
-        }
-
-        @Override
-        public void exception(AsyncReadGraph graph, Throwable throwable) {
-        }
-
-    }
-
     @SCLValue(type = "VariableMap")
     public static VariableMap primitiveProperties = new VariableMapImpl() {
        private void storePropertyValueAndExceptions(ReadGraph graph, Variable parent, String name, Variable property, Map<String, Variable> map) {
@@ -177,8 +164,8 @@ public class Functions {
                         StandardAssertedGraphPropertyVariable ass = (StandardAssertedGraphPropertyVariable)property;
                         if("dataDefinitions".equals(ass.property.name) || "commands".equals(ass.property.name)  || "pollingFunction".equals(ass.property.name)) {
                                storePropertyValueAndExceptions(graph, parent, ass.property.name, property, map);
+                            continue;
                         }
-                        continue;
                     }
                     Resource predicate = property.getPossiblePredicateResource(graph);
                     if(predicate != null) {
@@ -204,11 +191,11 @@ public class Functions {
                 }
                 
                 DirectQuerySupport dqs = graph.getService(DirectQuerySupport.class);
-                PrimitivePropertyStatementsProcedure foo = new PrimitivePropertyStatementsProcedure();
+                //PrimitivePropertyStatementsProcedure foo = new PrimitivePropertyStatementsProcedure();
 
-                dqs.forEachDirectPersistentStatement(graph, parentRes, foo);
+                DirectStatements ds = dqs.getDirectPersistentStatements(graph, parentRes);
 
-                for(Statement stm : foo.result) {
+                for(Statement stm : ds) {
                     Resource predicate = stm.getPredicate();
                     PropertyInfo info = graph.syncRequest(new PropertyInfoRequest(predicate));
 
@@ -231,6 +218,119 @@ public class Functions {
 
     };
 
+    static class DocumentPropertyKeys extends ResourceAsyncRead<List<String>> {
+
+        protected DocumentPropertyKeys(Resource resource) {
+            super(resource);
+        }
+
+        @Override
+        public void perform(AsyncReadGraph graph, final AsyncProcedure<List<String>> procedure) {
+
+            final List<String> result = new ArrayList<>();
+
+            DocumentationResource DOC = DocumentationResource.getInstance(graph);
+
+            try {
+                if(graph.hasStatement(resource, DOC.Properties_commands))
+                    result.add("commands");
+                if(graph.hasStatement(resource, DOC.Properties_dataDefinitions))
+                    result.add("dataDefinitions");
+            } catch(ServiceException e) {
+                LOGGER.info(e.getMessage(), e);
+            }
+            
+            graph.forEachDirectPredicate(resource, new AsyncProcedure<Set<Resource>>() {
+
+                @Override
+                public void execute(AsyncReadGraph graph, Set<Resource> predicates) {
+                    
+                    for(Resource predicate : predicates) {
+
+                        try {
+
+                            PropertyInfo info = graph.syncRequest(new PropertyInfoRequest(predicate));
+
+                            if(info.isHasProperty && info.hasClassification(DocumentationResource.URIs.Document_AttributeRelation)) {
+                                result.add(info.name);
+                            } else {
+                                Resource definition = graph.getPossibleObject(predicate, DOC.Document_definesAttributeRelation);
+                                if(definition != null) {
+                                    PropertyInfo info2 = graph.syncRequest(new PropertyInfoRequest(definition));
+                                    result.add(info2.name);
+                                }
+                            }
+
+                        } catch (DatabaseException e) {
+                            LOGGER.info(e.getMessage(), e);
+                        }
+
+                    }
+
+                    procedure.execute(graph, result);
+                    
+                }
+
+                @Override
+                public void exception(AsyncReadGraph graph, Throwable throwable) {
+                    LOGGER.info(throwable.getMessage(), throwable);
+                    procedure.exception(graph, throwable);
+                }
+                
+            });
+
+        }
+
+    }
+
+    static class StandardDocumentProperties implements DocumentProperties {
+
+        @Override
+        public Collection<String> getKeys(ReadGraph graph, Variable parent) throws DatabaseException {
+
+            if(parent instanceof StandardProceduralChildVariable) {
+
+                StandardProceduralChildVariable procedural = (StandardProceduralChildVariable)parent;
+                List<String> result = new ArrayList<>();
+                for(Variable property : procedural.getProperties(graph)) {
+                    if(property instanceof StandardAssertedGraphPropertyVariable) {
+                        StandardAssertedGraphPropertyVariable ass = (StandardAssertedGraphPropertyVariable)property;
+                        if("dataDefinitions".equals(ass.property.name) || "commands".equals(ass.property.name)  || "pollingFunction".equals(ass.property.name)) {
+                            result.add(ass.property.name);
+                            continue;
+                        }
+                    }
+                    Resource predicate = property.getPossiblePredicateResource(graph);
+                    if(predicate != null) {
+                        PropertyInfo info = graph.syncRequest(new PropertyInfoRequest(predicate));
+                        if(info.hasClassification(DocumentationResource.URIs.Document_AttributeRelation)) {
+                            result.add(info.name);
+                        }
+                    }
+                }
+
+                return result;
+
+            } else {
+
+                Resource parentRes = parent.getRepresents(graph);
+                return graph.syncRequest(new DocumentPropertyKeys(parentRes), TransientCacheAsyncListener.instance());
+
+            }
+
+        }
+
+        @Override
+        public Object getValue(ReadGraph graph, Variable context, String key) throws DatabaseException {
+            return context.getPropertyValue(graph, key);
+        }
+
+    }
+    
+    public static DocumentProperties primitiveProperties() throws DatabaseException {
+        return new StandardDocumentProperties();
+    }
+    
     @SCLValue(type = "VariableMap")
     public static VariableMap inputSpaceChildren = new VariableMapImpl() {
 
@@ -259,7 +359,7 @@ public class Functions {
 
     };
 
-    static class DocumentProxyChildVariable extends ProxyChildVariable {
+    static class DocumentProxyChildVariable extends ProxyChildVariable implements ProxyVariableSupport {
 
         public DocumentProxyChildVariable(Variable base, Variable parent, Variable other, String name) {
             super(base, parent, other, name);
@@ -276,42 +376,26 @@ public class Functions {
                if(other instanceof ProxyChildVariable) {
                        // The context is also a proxy - let it do the job
                     return super.getPossibleChild(graph, name);
-               } else {
-                       return new RootVariable(this, base.getRepresents(graph));
-               }
-               }
-
-            return super.getPossibleChild(graph, name);
-
-        }
-
-        public Collection<Variable> getChildren(ReadGraph graph) throws DatabaseException {
-
-            Collection<Variable> result = super.getChildren(graph);
-            if(!(base instanceof ProxyChildVariable)) {
-               result.add(new RootVariable(this, base.getRepresents(graph)));
+                } else {
+                    return ProxyVariables.tryToOwnRenamed(graph, this, base, CONTEXT_END);
+                }
             }
-            return result;
-
-        }
 
-    }
-
-    static class RootVariable extends StandardGraphChildVariable {
+            return super.getPossibleChild(graph, name);
 
-        public RootVariable(DocumentProxyChildVariable parent, Resource resource) {
-            super(parent, null, resource);
         }
 
         @Override
-        public String getName(ReadGraph graph) throws DatabaseException {
-            return ProxyChildVariable.CONTEXT_END;
+        public Variable attachTo(ReadGraph graph, Variable parent) {
+            return attachToRenamed(graph, parent, name);
         }
-
-        @SuppressWarnings("deprecation")
+        
         @Override
-        public Variable getNameVariable(ReadGraph graph) throws DatabaseException {
-            return new ConstantPropertyVariable(this, Variables.NAME, ProxyChildVariable.CONTEXT_END, Bindings.STRING);
+        public Variable attachToRenamed(ReadGraph graph, Variable parent, String name) {
+            if(this.parent.equals(base))
+                return new DocumentProxyChildVariable(parent, parent, other, name);
+            else
+                return new DocumentProxyChildVariable(base, parent, other, name);
         }
 
     }
@@ -1254,11 +1338,11 @@ public class Functions {
                                                return true;
                                        }
                                } else {
-                                       Variable parentCp = graph.sync(new UnaryRead<Connection, Variable>(conn) {
+                                       Variable parentCp = graph.sync(new BinaryRead<Variable, Connection, Variable>(widget, conn) {
                                    @Override
                                    public Variable perform(ReadGraph graph) throws DatabaseException {
                                        DocumentationResource DOC = DocumentationResource.getInstance(graph);
-                                       Collection<VariableConnectionPointDescriptor> descs = parameter.getConnectionPointDescriptors(graph, null);
+                                       Collection<VariableConnectionPointDescriptor> descs = parameter2.getConnection2().getConnectionPointDescriptors(graph, parameter, null);
 
                                                for(VariableConnectionPointDescriptor desc : descs) {
                                                        if (DOC.Relations_partN.equals(desc.getConnectionPointResource(graph))) {
@@ -1297,6 +1381,15 @@ public class Functions {
         }
     }
     
+    public static String compileDocumentSCLHandlerValueExpression(ReadGraph graph, Variable context) {
+        try {
+            ServerSCLHandlerValueRequest.compile(graph, context);
+            return "";
+        } catch (Exception e) {
+            return resolveIssueMessage(e);
+        }
+    }
+
     private static String resolveIssueMessage(Exception e) {
         if (e instanceof ImportFailureException)
             return "";