]> gerrit.simantics Code Review - simantics/platform.git/blobdiff - bundles/org.simantics.document.server/src/org/simantics/document/server/Functions.java
Variable optimizations for documents (Simupedia)
[simantics/platform.git] / bundles / org.simantics.document.server / src / org / simantics / document / server / Functions.java
index 9c62924153681a788159cb40c0420468bc415387..6ec2e6734a5a70808dcb02d6ef9e05c43a3a97ef 100644 (file)
@@ -15,7 +15,6 @@ import org.simantics.databoard.Bindings;
 import org.simantics.databoard.Datatypes;
 import org.simantics.databoard.binding.Binding;
 import org.simantics.databoard.type.Datatype;
-import org.simantics.db.AsyncReadGraph;
 import org.simantics.db.DirectStatements;
 import org.simantics.db.ReadGraph;
 import org.simantics.db.RequestProcessor;
@@ -25,7 +24,7 @@ import org.simantics.db.Statement;
 import org.simantics.db.WriteGraph;
 import org.simantics.db.common.primitiverequest.Adapter;
 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.UniqueRead;
 import org.simantics.db.common.request.WriteResultRequest;
 import org.simantics.db.common.utils.Logger;
@@ -42,15 +41,14 @@ 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;
 import org.simantics.db.layer0.variable.VariableMapImpl;
 import org.simantics.db.layer0.variable.Variables;
-import org.simantics.db.procedure.AsyncProcedure;
 import org.simantics.db.service.DirectQuerySupport;
 import org.simantics.document.base.ontology.DocumentationResource;
 import org.simantics.document.server.bean.Command;
@@ -68,9 +66,10 @@ import org.simantics.document.server.request.ServerSCLHandlerValueRequest;
 import org.simantics.document.server.request.ServerSCLValueRequest;
 import org.simantics.document.server.serverResponse.ServerResponse;
 import org.simantics.document.server.serverResponse.SuccessResponse;
+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;
@@ -106,21 +105,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) {
@@ -201,11 +185,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));
 
@@ -228,6 +212,34 @@ public class Functions {
 
     };
 
+    static class StandardDocumentProperties implements DocumentProperties {
+
+        @Override
+        public Collection<String> getKeys(ReadGraph graph, Variable context) throws DatabaseException {
+            
+            DocumentationResource DOC = DocumentationResource.getInstance(graph);
+            StandardGraphPropertyVariable asd = new StandardGraphPropertyVariable(graph, context, DOC.Properties_primitiveProperties);
+            Map<String, Variable> ps = primitiveProperties.getVariables(graph, asd, null);
+            return ps.keySet();
+            
+        }
+
+        @Override
+        public Object getValue(ReadGraph graph, Variable context, String key) throws DatabaseException {
+            
+            DocumentationResource DOC = DocumentationResource.getInstance(graph);
+            StandardGraphPropertyVariable asd = new StandardGraphPropertyVariable(graph, context, DOC.Properties_primitiveProperties);
+            Map<String, Variable> ps = primitiveProperties.getVariables(graph, asd, null);
+            return ps.get(key).getValue(graph);
+            
+        }
+        
+    }
+    
+    public static DocumentProperties primitiveProperties() throws DatabaseException {
+        return new StandardDocumentProperties();
+    }
+    
     @SCLValue(type = "VariableMap")
     public static VariableMap inputSpaceChildren = new VariableMapImpl() {
 
@@ -256,7 +268,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);
@@ -273,42 +285,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);
         }
 
     }
@@ -583,6 +579,7 @@ public class Functions {
        }
     }
 
+    @Deprecated
     public static AbstractEventHandler emptyOnClick(ReadGraph graph) throws DatabaseException {
         return new EventHandler() {
             @Override
@@ -592,6 +589,7 @@ public class Functions {
         };
     }
 
+    @Deprecated
     public static AbstractEventHandler writeEventHandler(ReadGraph graph, final Variable variable, final Function fn) {
 
        final Session session = graph.getSession();
@@ -624,15 +622,11 @@ public class Functions {
                                                        return (String)response;
                                                    }
                                                    return null;
-                                               } catch (Throwable t) {
-                                                   t.printStackTrace();
                                                } finally {
                                                        sclContext.put("graph", oldGraph);
                                                        sclContext.put(SCLReportingHandler.REPORTING_HANDLER, oldPrinter);
                                                }
 
-                                               return null;
-
                                            }
 
                                        });
@@ -649,6 +643,7 @@ public class Functions {
         };
     }
 
+    @Deprecated
     public static AbstractEventHandler readEventHandler(ReadGraph graph, final Variable variable, final Function fn) {
 
        final Session session = graph.getSession();
@@ -681,15 +676,11 @@ public class Functions {
                                                        return (String)response;
                                                    }
                                                    return null;
-                                               } catch (Throwable t) {
-                                                   t.printStackTrace();
                                                } finally {
                                                        sclContext.put("graph", oldGraph);
                                                        sclContext.put(SCLReportingHandler.REPORTING_HANDLER, oldPrinter);
                                                }
 
-                                               return null;
-
                                            }
 
                                        });
@@ -706,6 +697,7 @@ public class Functions {
         };
     }
 
+    @Deprecated
     public static AbstractEventHandler readEventHandler2(ReadGraph graph, final Function fn) {
 
        final Session session = graph.getSession();
@@ -732,15 +724,11 @@ public class Functions {
                                                        return (CommandResult)response;
                                                    }
                                                    return null;
-                                               } catch (Throwable t) {
-                                                   t.printStackTrace();
                                                } finally {
                                                        sclContext.put("graph", oldGraph);
                                                        sclContext.put(SCLReportingHandler.REPORTING_HANDLER, oldPrinter);
                                                }
 
-                                               return null;
-
                                            }
 
                                        });
@@ -764,12 +752,12 @@ public class Functions {
 
         final List<TCon> effects = ServerSCLHandlerValueRequest.getEffects(graph, anyFunction);
 
-       final Function1<CommandContext, CommandResult> fn = anyFunction.getValue(graph);
-        String expression = anyFunction.getPropertyValue(graph, "expression");
+       Function1<CommandContext, CommandResult> fn = anyFunction.getValue(graph);
+        //String expression = anyFunction.getPropertyValue(graph, "expression");
 
        final Session session = graph.getSession();
 
-        return new AbstractResponseHandler(expression) {
+        return new AbstractResponseHandler(fn) {
 
                private String formatError(RequestProcessor proc, Throwable t) {
 
@@ -830,10 +818,7 @@ public class Functions {
                                                Object oldGraph = sclContext.put("graph", graph);
                                                Object oldPrinter = sclContext.put(SCLReportingHandler.REPORTING_HANDLER, printer);
                                                try {
-                                                       Object response = fn.apply(parameters);
-                                                       return response;
-                                               } catch (Throwable t) {
-                                                       return new org.simantics.document.server.serverResponse.Error(formatError(graph, t));
+                                                       return fn.apply(parameters);
                                                } finally {
                                                        sclContext.put("graph", oldGraph);
                                                        sclContext.put(SCLReportingHandler.REPORTING_HANDLER, oldPrinter);
@@ -854,10 +839,7 @@ public class Functions {
                                                Object oldPrinter = sclContext.put(SCLReportingHandler.REPORTING_HANDLER, printer);
 
                                                try {
-                                                       Object response = fn.apply(parameters);
-                                                       return response;
-                                               } catch (Throwable t) {
-                                                       return new org.simantics.document.server.serverResponse.Error(formatError(graph, t));
+                                                       return fn.apply(parameters);
                                                } finally {
                                                        sclContext.put("graph", oldGraph);
                                                        sclContext.put(SCLReportingHandler.REPORTING_HANDLER, oldPrinter);
@@ -899,6 +881,7 @@ public class Functions {
         };
     }
 
+    @Deprecated
     public static AbstractEventHandler writeEventHandler2(ReadGraph graph, final Function fn) {
 
        final Session session = graph.getSession();
@@ -925,15 +908,11 @@ public class Functions {
                                                        return (CommandResult)response;
                                                    }
                                                    return null;
-                                               } catch (Throwable t) {
-                                                   t.printStackTrace();
                                                } finally {
                                                        sclContext.put("graph", oldGraph);
                                                        sclContext.put(SCLReportingHandler.REPORTING_HANDLER, oldPrinter);
                                                }
 
-                                               return null;
-
                                            }
 
                                        });
@@ -950,6 +929,8 @@ public class Functions {
         };
     }
 
+
+    @Deprecated
     public static AbstractEventHandler eventHandler2(ReadGraph graph, final Function fn) {
 
         return new AbstractEventHandler() {
@@ -969,14 +950,10 @@ public class Functions {
                                        return (CommandResult)response;
                                }
                                return null;
-                       } catch (Throwable t) {
-                               t.printStackTrace();
                        } finally {
                                sclContext.put(SCLReportingHandler.REPORTING_HANDLER, oldPrinter);
                        }
 
-                       return null;
-
                                } catch (Throwable e) {
                                        Logger.defaultLogError(e);
                                        return new org.simantics.document.server.serverResponse.Error(e.getMessage()); 
@@ -987,6 +964,7 @@ public class Functions {
         };
     }
 
+    @Deprecated
     public static AbstractEventHandler eventHandler(ReadGraph graph, final Function fn) {
 
         return new AbstractEventHandler() {
@@ -1013,8 +991,6 @@ public class Functions {
                                    if(response instanceof String) {
                                        result = (String)response;
                                    }
-                               } catch (Throwable t) {
-                                   t.printStackTrace();
                                } finally {
                                        sclContext.put(SCLReportingHandler.REPORTING_HANDLER, oldPrinter);
                                }
@@ -1167,11 +1143,6 @@ public class Functions {
 
        } else {
 
-               String id = sclStateKey(graph, base, self, ref);
-
-               SCLRealm realm = SCLSessionManager.getOrCreateSCLRealm(base.getURI(graph) + "/__scl__");
-               realm.getConnection().setVariable(id, getSCLType(defaultValue), defaultValue);
-
                return defaultValue;
 
        }
@@ -1181,9 +1152,9 @@ public class Functions {
 
        String id = sclStateKey(graph, base, self, ref);
 
-       SCLRealm realm = SCLSessionManager.getOrCreateSCLRealm(base.getURI(graph)+"/__scl__");
-       realm.getConnection().setVariable(id, getSCLType(value), value);
-       realm.refreshVariablesSync();
+               StateRealm realm = (StateRealm) StateSessionManager.getInstance().getOrCreateRealm(graph, base.getURI(graph)+"/__scl__");
+               StateNodeManager nodeManager = (StateNodeManager) realm.getNodeManager();
+               nodeManager.setState(id, value);
 
     }
 
@@ -1276,11 +1247,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))) {