DocumentRequest performance enhancements (Simupedia) 28/628/4
authorAntti Villberg <antti.villberg@semantum.fi>
Thu, 15 Jun 2017 03:26:13 +0000 (06:26 +0300)
committerTuukka Lehtonen <tuukka.lehtonen@semantum.fi>
Fri, 16 Jun 2017 09:18:04 +0000 (12:18 +0300)
refs #7303

Change-Id: I719ac5a0c0c1dc02850d5ed140e7234ae280cdf6

18 files changed:
bundles/org.simantics.document.base.ontology/graph/Components.pgraph
bundles/org.simantics.document.base.ontology/graph/Functions.pgraph
bundles/org.simantics.document.base.ontology/graph/Properties.pgraph
bundles/org.simantics.document.server/src/org/simantics/document/server/DocumentServerUtils.java
bundles/org.simantics.document.server/src/org/simantics/document/server/DocumentValue.java [new file with mode: 0644]
bundles/org.simantics.document.server/src/org/simantics/document/server/Functions.java
bundles/org.simantics.document.server/src/org/simantics/document/server/VariableStructuralContext.java
bundles/org.simantics.document.server/src/org/simantics/document/server/bean/Command.java
bundles/org.simantics.document.server/src/org/simantics/document/server/request/DefaultFields.java [new file with mode: 0644]
bundles/org.simantics.document.server/src/org/simantics/document/server/request/DocumentContext.java [deleted file]
bundles/org.simantics.document.server/src/org/simantics/document/server/request/DocumentRequest.java
bundles/org.simantics.document.server/src/org/simantics/document/server/request/HandleEventRequest.java
bundles/org.simantics.document.server/src/org/simantics/document/server/request/NodeRequest.java
bundles/org.simantics.document.server/src/org/simantics/document/server/request/NodeRequestStatic.java [deleted file]
bundles/org.simantics.document.server/src/org/simantics/document/server/request/NodesRequest.java
bundles/org.simantics.document.server/src/org/simantics/document/server/request/NodesRequest2.java
bundles/org.simantics.document.server/src/org/simantics/document/server/request/ServerSCLHandlerValueRequest.java
bundles/org.simantics.document.server/src/org/simantics/document/server/request/ServerSCLValueRequest.java

index bd6c97123d43eab2ac009b12cde9994903b8e99e..5fa11e906f7433e02c585926695e3e1a710dd15a 100644 (file)
@@ -62,6 +62,10 @@ COMPONENTS.Component <T STR.Component
     @sclAttribute PROPERTIES.commands "commandList self"
 
 COMPONENTS.PrimitiveComponent <T COMPONENTS.Component
+    @L0.assert PROPERTIES.primitiveProperties
+      _ : L0.Value
+        L0.domainProperties FUNCTIONS.primitiveProperties
+        
 
 COMPONENTS.ParentlessComponent <T COMPONENTS.PrimitiveComponent /* Component without a parent, e.g. Root, DialogBox */
 
index 5d76767a61af5b6e5d3bb4c71939369a57f351f6..cd1877b76bdfc87acae2877e437d96f9d7b77969 100644 (file)
@@ -38,4 +38,8 @@ FUNCTIONS.componentNamingStrategy : L0.Function
   
 FUNCTIONS.pathExists : L0.Function
   L0.HasValueType "Boolean"
+
+FUNCTIONS.primitiveProperties : L0.ExternalValue
+  L0.HasValueType "VariableMap"
+
   
\ No newline at end of file
index 9093ebe9476de4d0364bec1059bc6d861ec08bf3..b5d56fc4c7617e1d3310276a6df5a82edee705d0 100644 (file)
@@ -48,7 +48,10 @@ PROPERTIES.experiment <R L0.HasProperty : L0.FunctionalRelation
 
 PROPERTIES.model <R L0.HasProperty : L0.FunctionalRelation
     ==> "Variable"
-    
+
+PROPERTIES.primitiveProperties <R L0.HasProperty : L0.FunctionalRelation
+    ==> "String"
+
 PROPERTIES.exists
     @defAttribute L0.Boolean    
     L0.RequiresValueType "Boolean" 
index 8e0a0e99138ff728e8a37fa733cb8f1df21c9276..4821bbd1ca0e4c48a1cbe71210d1e53acb2c20be 100644 (file)
@@ -3,6 +3,7 @@ package org.simantics.document.server;
 import java.util.ArrayList;
 import java.util.Collection;
 import java.util.Collections;
+import java.util.List;
 import java.util.Set;
 import java.util.SortedMap;
 import java.util.TreeMap;
@@ -18,6 +19,8 @@ import org.simantics.db.layer0.variable.Variable;
 import org.simantics.db.layer0.variable.Variables;
 import org.simantics.document.base.ontology.DocumentationResource;
 import org.simantics.document.server.Functions.RootVariable;
+import org.simantics.document.server.request.NodeRequest;
+import org.simantics.document.server.request.NodeRequestUtils;
 import org.simantics.structural2.variables.Connection;
 import org.simantics.structural2.variables.VariableConnectionPointDescriptor;
 import org.simantics.utils.datastructures.Pair;
@@ -70,32 +73,29 @@ public class DocumentServerUtils {
        
                SortedMap<String, Variable> childMap = new TreeMap<String, Variable>(AlphanumComparator.COMPARATOR);
                
-               for(Variable property : variable.getProperties(graph)) {
-                       Collection<String> classifications = property.getPossiblePropertyValue(graph, Variables.CLASSIFICATIONS);
-                       if(classifications != null) {
-                               if(classifications.contains(DocumentationResource.URIs.Document_ChildRelation)) {
-                                       Resource cp = property.getPossiblePredicateResource(graph);
-                                       String i = graph.getRelatedValue(cp, DOC.Document_ChildRelation_ordinal, Bindings.STRING);
-                                       
-                                       Connection conn = property.getValue(graph);
-                                       Variable childConnectionPoint = DocumentServerUtils.getPossibleOtherConnectionPoint(graph, property, conn);
-                                       if(childConnectionPoint != null) {
-                                               childMap.put(i, childConnectionPoint.getParent(graph));
-                                       }
-                               } else if (DOC.Relations_partN.equals(property.getPossiblePredicateResource(graph))) {
-                                       Connection conn = property.getValue(graph);
-
-                                       for (Variable childConnectionPoint : DocumentServerUtils.getOtherConnectionPoints(graph, property, conn)) {
-                                               Variable child = childConnectionPoint.getParent(graph);
-                                               String i = findManualOrdinal(graph, child);
-                                               if (i == null) {
-                                                       i = "0";
-                                               }
-                                               childMap.put(i, child);
-                                       }
+               for(Variable property : variable.getProperties(graph, DocumentationResource.URIs.Document_ChildRelation)) {
+                       Resource cp = property.getPossiblePredicateResource(graph);
+                       String i = graph.getRelatedValue(cp, DOC.Document_ChildRelation_ordinal, Bindings.STRING);
+                       Connection conn = property.getValue(graph);
+                       Variable childConnectionPoint = DocumentServerUtils.getPossibleOtherConnectionPoint(graph, property, conn);
+                       if(childConnectionPoint != null) {
+                               childMap.put(i, childConnectionPoint.getParent(graph));
+                       }
+               }
+               
+               Variable property = variable.getPossibleProperty(graph, "partN");
+               if(property != null) {
+                       Connection conn = property.getValue(graph);
+                       for (Variable childConnectionPoint : DocumentServerUtils.getOtherConnectionPoints(graph, property, conn)) {
+                               Variable child = childConnectionPoint.getParent(graph);
+                               String i = findManualOrdinal(graph, child);
+                               if (i == null) {
+                                       i = "0";
                                }
+                               childMap.put(i, child);
                        }
                }
+
                return childMap.values();
 
        }    
@@ -137,6 +137,24 @@ public class DocumentServerUtils {
                return null;
 
        }
+
+       public static Variable getPossibleChildConnectionPoint(ReadGraph graph, Variable connectionPoint, Connection conn) throws DatabaseException {
+
+               Collection<VariableConnectionPointDescriptor> descs = conn.getConnectionPointDescriptors(graph, null);
+               if(descs.size() != 2) return null;
+
+       DocumentationResource DOC = DocumentationResource.getInstance(graph);
+               
+               for(VariableConnectionPointDescriptor desc : descs) {
+                       Resource res = desc.getConnectionPointResource(graph);
+                       if(graph.isInstanceOf(res, DOC.Relations_parentRelation)) continue;
+                       //if(desc.isFlattenedFrom(graph, connectionPoint)) continue;
+                       return desc.getVariable(graph);
+               }
+               
+               return null;
+
+       }
        
        public static Collection<Variable> getOtherConnectionPoints(ReadGraph graph, Variable connectionPoint, Connection conn) throws DatabaseException {
 
@@ -196,24 +214,6 @@ public class DocumentServerUtils {
 
        }
 
-       public static class DocumentValue extends VariableRead<Object> {
-
-               public DocumentValue(Variable variable) {
-                       super(variable);
-               }
-
-               @Override
-               public Object perform(ReadGraph graph) throws DatabaseException {
-                       return variable.getValue(graph);
-               }
-               
-               @Override
-               public String toString() {
-                       return "DocumentValue[" + variable + "]";
-               }
-               
-       }
-       
        public static Object getValue(ReadGraph graph, Variable attrib) throws DatabaseException {
                return graph.syncRequest(new DocumentValue(attrib));
        }
@@ -293,42 +293,60 @@ public class DocumentServerUtils {
                return variable.getProperties(graph, DOC.Document_AttributeRelation);
        }
        
-       static class AttributesRequest extends VariableRead<Pair<Collection<Variable>, Collection<Variable>>> {
+       public static class AttributesRequest extends VariableRead<Pair<JSONObject, Collection<Variable>>> {
 
                public AttributesRequest(Variable variable) {
                        super(variable);
                }
 
                @Override
-               public Pair<Collection<Variable>,Collection<Variable>> perform(ReadGraph graph) throws DatabaseException {
+               public Pair<JSONObject,Collection<Variable>> perform(ReadGraph graph) throws DatabaseException {
                ArrayList<Variable> statics = new ArrayList<Variable>();
-               ArrayList<Variable> dynamics = new ArrayList<Variable>();
                DocumentationResource DOC = DocumentationResource.getInstance(graph);
-               for(Variable property : getAttributes(graph, DOC, variable)) {
-                       Boolean defaultProperty = property.getPossiblePropertyValue(graph, "default");
-                       if(defaultProperty != null && defaultProperty) {
-//                             System.err.println("" + property.getURI(graph) + " is default");
-                               continue;
-                       } 
-//                     else {
-//                             System.err.println("" + property.getURI(graph) + " is not default");
-//                     }
-                       Boolean dynamicProperty = property.getPossiblePropertyValue(graph, DOC.Document_AttributeRelation_dynamic);
-                       if(dynamicProperty != null && dynamicProperty) dynamics.add(property);
-                       else statics.add(property);
+               
+               Variable primitives = variable.getProperty(graph, DOC.Properties_primitiveProperties);
+               for(Variable property : primitives.getProperties(graph)) {
+                       statics.add(property);
+                       // NO SUPPORT FOR DYNAMICS AT THIS STAGE
                }
-               return new Pair<Collection<Variable>, Collection<Variable>>(statics, dynamics);
+               
+               JSONObject staticContent = computeStatic(graph, variable, statics);
+               
+               return new Pair<JSONObject, Collection<Variable>>(staticContent, Collections.emptyList());
+               
+               }
+               
+               JSONObject computeStatic(ReadGraph graph, Variable variable, ArrayList<Variable> statics) throws DatabaseException {
+                       
+                       JSONObject base = graph.syncRequest(new org.simantics.document.server.request.DefaultFields(variable));
+                       JSONObject object = base.clone();
+                       
+                       for(Variable attrib : statics) {
+                               String name = attrib.getName(graph);
+                               try {
+                                   Object value = DocumentServerUtils.getValue(graph, attrib);
+                                   object.addJSONField(name, value);
+                               } catch (Throwable t) {
+                                   List<String> errorList = object.getJSONField(NodeRequest.ERRORS);
+                                   if(errorList == null)
+                                       errorList = new ArrayList<String>();
+                                   
+                           String errorMessage = NodeRequestUtils.formatErrorMessage(name, t);
+                           
+                                   errorList.add(errorMessage);
+                                   object.addJSONField(NodeRequest.ERRORS, errorList);
+                               }
+
+                       }
+
+                       return object;
+                       
                }
                
        }
-       
-    public static Collection<Variable> getStaticAttributes(ReadGraph graph, DocumentationResource DOC, Variable variable) throws DatabaseException {
-       Pair<Collection<Variable>, Collection<Variable>> attribs = graph.syncRequest(new AttributesRequest(variable));
-       return attribs.first;
-    }
 
        public static Collection<Variable> getDynamicAttributes(ReadGraph graph, final DocumentationResource DOC, Variable variable) throws DatabaseException {
-       Pair<Collection<Variable>, Collection<Variable>> attribs = graph.syncRequest(new AttributesRequest(variable));
+       Pair<JSONObject, Collection<Variable>> attribs = graph.syncRequest(new AttributesRequest(variable));
        return attribs.second;
        }
        
diff --git a/bundles/org.simantics.document.server/src/org/simantics/document/server/DocumentValue.java b/bundles/org.simantics.document.server/src/org/simantics/document/server/DocumentValue.java
new file mode 100644 (file)
index 0000000..7a97a2b
--- /dev/null
@@ -0,0 +1,37 @@
+package org.simantics.document.server;
+
+import org.simantics.db.ReadGraph;
+import org.simantics.db.exception.DatabaseException;
+import org.simantics.db.layer0.request.VariableRead;
+import org.simantics.db.layer0.variable.Variable;
+import org.simantics.document.server.request.DocumentRequest;
+
+public class DocumentValue extends VariableRead<Object> {
+
+       public DocumentValue(Variable variable) {
+               super(variable);
+       }
+
+       @Override
+       public Object perform(ReadGraph graph) throws DatabaseException {
+               if(DocumentRequest.PROFILE) return performProfiled(graph);
+               return variable.getValue(graph);
+       }
+       
+       public Object performProfiled(ReadGraph graph) throws DatabaseException {
+               long start = System.nanoTime();
+               Object result = variable.getValue(graph);
+               //System.err.println("DocumentValue " + result + " " + variable.getURI(graph));
+       long dura = System.nanoTime()-start;
+        if(dura > DocumentRequest.PROFILE_THRESHOLD_VALUEREQUEST * 1e3) {
+               System.err.println("DocumentValue " + System.identityHashCode(this) + " in " + 1e-6*dura + "ms. "+ variable.getURI(graph));
+       }
+               return result;
+       }
+       
+       @Override
+       public String toString() {
+               return "DocumentValue[" + variable + "]";
+       }
+       
+}
\ No newline at end of file
index d0ec54557353555349b5e66f222002b8c86f15ed..8b68cc409cde83d0442ae081163c15e3dcf0071c 100644 (file)
@@ -5,6 +5,7 @@ import java.io.StringWriter;
 import java.util.ArrayList;
 import java.util.Collection;
 import java.util.Collections;
+import java.util.HashMap;
 import java.util.List;
 import java.util.Map;
 import java.util.TreeMap;
@@ -14,10 +15,13 @@ 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;
 import org.simantics.db.Resource;
 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.TransientCacheListener;
@@ -29,6 +33,8 @@ import org.simantics.db.common.utils.NameUtils;
 import org.simantics.db.exception.DatabaseException;
 import org.simantics.db.layer0.function.All;
 import org.simantics.db.layer0.request.ProjectModels;
+import org.simantics.db.layer0.request.PropertyInfo;
+import org.simantics.db.layer0.request.PropertyInfoRequest;
 import org.simantics.db.layer0.request.VariableProperty;
 import org.simantics.db.layer0.request.VariableRead;
 import org.simantics.db.layer0.request.VariableValueWithBinding;
@@ -36,11 +42,15 @@ 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.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;
 import org.simantics.document.server.bean.DataDefinition;
@@ -83,12 +93,102 @@ import org.simantics.simulation.experiment.IExperiment;
 import org.simantics.simulation.ontology.SimulationResource;
 import org.simantics.simulation.project.IExperimentManager;
 import org.simantics.structural2.variables.Connection;
+import org.simantics.structural2.variables.StandardProceduralChildVariable;
 import org.simantics.structural2.variables.VariableConnectionPointDescriptor;
 
 import gnu.trove.map.hash.THashMap;
 
 public class Functions {
+
+    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() {
+
+        @Override
+        public Variable getVariable(ReadGraph graph, Variable context, String name) throws DatabaseException {
+            return All.getStandardPropertyDomainPropertyVariableFromValue(graph, context, name);
+        }
+
+        @Override
+        public Map<String, Variable> getVariables(final ReadGraph graph, Variable context, Map<String, Variable> map) throws DatabaseException {
+
+            Variable parent = context.getParent(graph);
+
+            DocumentationResource DOC = DocumentationResource.getInstance(graph);
+
+            if(parent instanceof StandardProceduralChildVariable) {
+
+                StandardProceduralChildVariable procedural = (StandardProceduralChildVariable)parent;
+                for(Variable property : procedural.getProperties(graph/*, DocumentationResource.URIs.Document_AttributeRelation*/)) {
+                    if(property instanceof StandardAssertedGraphPropertyVariable) {
+                        StandardAssertedGraphPropertyVariable ass = (StandardAssertedGraphPropertyVariable)property;
+                        if("datadefinitions".equals(ass.property.name) || "commands".equals(ass.property.name)) {
+                            Object value = property.getPossibleValue(graph);
+                            if(value != null) map.put(ass.property.name, new ConstantPropertyVariable(parent, ass.property.name, value, null));
+                        }
+                        continue;
+                    }
+                    Resource predicate = property.getPossiblePredicateResource(graph);
+                    if(predicate != null) {
+                        PropertyInfo info = graph.syncRequest(new PropertyInfoRequest(predicate));
+                        if(info.hasClassification(DocumentationResource.URIs.Document_AttributeRelation)) {
+                            Variable prop = parent.getProperty(graph, predicate);
+                            Object value = prop.getValue(graph);
+                            if(map == null) map = new HashMap<String,Variable>();
+                            map.put(info.name, new ConstantPropertyVariable(parent, info.name, value, null));
+                        }
+                    }
+                }
+
+            } else {
+
+                DirectQuerySupport dqs = graph.getService(DirectQuerySupport.class);
+                PrimitivePropertyStatementsProcedure foo = new PrimitivePropertyStatementsProcedure();
+
+                dqs.forEachDirectPersistentStatement(graph, parent.getRepresents(graph), foo);
        
+                for(Statement stm : foo.result) {
+                    Resource predicate = stm.getPredicate();
+                    PropertyInfo info = graph.syncRequest(new PropertyInfoRequest(predicate));
+                    if(info.isHasProperty && info.hasClassification(DocumentationResource.URIs.Document_AttributeRelation)) {
+                        if(map == null) map = new HashMap<String,Variable>();
+                        Variable prop = new StandardGraphPropertyVariable(graph, parent, predicate);
+                        Object value = prop.getValue(graph);
+                        map.put(info.name, new ConstantPropertyVariable(parent, info.name, value, null));
+                    }
+                }
+
+                Variable prop = new StandardGraphPropertyVariable(graph, parent, DOC.Properties_dataDefinitions);
+                Object value = prop.getPossibleValue(graph);
+                if(value != null) map.put("dataDefinitions", new ConstantPropertyVariable(parent, "dataDefinitions", value, null));
+                prop = new StandardGraphPropertyVariable(graph, parent, DOC.Properties_commands);
+                value = prop.getPossibleValue(graph);
+                if(value != null) map.put("commands", new ConstantPropertyVariable(parent, "commands", value, null));
+
+            }
+
+            if(map == null) return Collections.emptyMap();
+
+            return map;
+
+        }
+
+    };
+
     @SCLValue(type = "VariableMap")
     public static VariableMap inputSpaceChildren = new VariableMapImpl() {
 
@@ -96,13 +196,13 @@ public class Functions {
             Variable root = Variables.getRootVariable(graph);
             return new DocumentProxyChildVariable(context, context, root, ProxyChildVariable.CONTEXT_BEGIN);
         }
-        
+
         @Override
         public Variable getVariable(ReadGraph graph, Variable context, String name) throws DatabaseException {
 
             if(ProxyChildVariable.CONTEXT_BEGIN.equals(name)) return getProxy(graph, context);
             return All.standardChildDomainChildren.getVariable(graph, context, name);
-            
+
         }
 
         @Override
@@ -112,24 +212,24 @@ public class Functions {
             if(map == null) map = new THashMap<String,Variable>();
             map.put(ProxyChildVariable.CONTEXT_BEGIN, getProxy(graph, context));
             return map;
-            
+
         }
-        
+
     };
-    
+
     static class DocumentProxyChildVariable extends ProxyChildVariable {
 
         public DocumentProxyChildVariable(Variable base, Variable parent, Variable other, String name) {
             super(base, parent, other, name);
         }
-    
+
         @Override
         public Variable create(Variable base, Variable parent, Variable other, String name) {
             return new DocumentProxyChildVariable(base, parent, other, name);
         }
-        
+
         public Variable getPossibleChild(ReadGraph graph, String name) throws DatabaseException {
-            
+
                if(CONTEXT_END.equals(name)) {
                if(other instanceof ProxyChildVariable) {
                        // The context is also a proxy - let it do the job
@@ -138,11 +238,11 @@ public class Functions {
                        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);
@@ -150,28 +250,28 @@ public class Functions {
                result.add(new RootVariable(this, base.getRepresents(graph)));
             }
             return result;
-            
-        }       
-        
+
+        }
+
     }
-    
+
     static class RootVariable extends StandardGraphChildVariable {
 
         public RootVariable(DocumentProxyChildVariable parent, Resource resource) {
             super(parent, null, resource);
         }
-        
+
         @Override
         public String getName(ReadGraph graph) throws DatabaseException {
             return ProxyChildVariable.CONTEXT_END;
         }
-        
+
         @SuppressWarnings("deprecation")
         @Override
         public Variable getNameVariable(ReadGraph graph) throws DatabaseException {
             return new ConstantPropertyVariable(this, Variables.NAME, ProxyChildVariable.CONTEXT_END, Bindings.STRING);
         }
-        
+
     }
 
     @SCLValue(type = "ReadGraph -> Resource -> Variable -> Variable")
@@ -187,14 +287,14 @@ public class Functions {
        }
        return Variables.getVariable(graph, uri);
     }
-    
+
     public static Variable stateVariable(ReadGraph graph, Variable self) throws DatabaseException {
        Variable session = graph.syncRequest(new ProxySessionRequest(self));
        if (session == null)
                throw new DatabaseException("No state for " + self.getURI(graph));
        return session.getPossibleChild(graph, "__scl__");
     }
-    
+
     @SCLValue(type = "ReadGraph -> Resource -> Variable -> Variable")
     public static Variable state(ReadGraph graph, Resource converter, Variable context) throws DatabaseException {
        Variable session = graph.syncRequest(new ProxySessionRequest(context));
@@ -213,7 +313,7 @@ public class Functions {
     public static Variable session(ReadGraph graph, Resource converter, Variable context) throws DatabaseException {
        return graph.syncRequest(new ProxySessionRequest(context));
     }
-    
+
     @SCLValue(type = "ReadGraph -> Resource -> Variable -> Variable")
     public static Variable experiment(ReadGraph graph, Resource converter, Variable context) throws DatabaseException {
         // Try if experiment (run) has been used as the input
@@ -222,16 +322,16 @@ public class Functions {
         while(var != null && !graph.isInstanceOf(var.getRepresents(graph), SR.Run)) {
             var = var.getParent(graph);
         }
-        
+
         if(var != null) {
             IExperiment exp = getExperiment(graph, var);
             if(exp == null)
                 return null;
         }
-        
+
         return var;
     }
-    
+
     @SCLValue(type = "ReadGraph -> Resource -> Variable -> Variable")
     public static Variable model(ReadGraph graph, Resource converter, Variable context) throws DatabaseException {
         Variable var = input(graph, converter, context);
@@ -239,51 +339,51 @@ public class Functions {
         while(var != null && !graph.isInstanceOf(var.getRepresents(graph), MOD.StructuralModel)) {
             var = var.getParent(graph);
         }
-        
+
         return var;
     }
-    
+
     private static Collection<Variable> getBroadcasted(ReadGraph graph, Variable target) throws DatabaseException {
-       
+
        ArrayList<Variable> result = new ArrayList<Variable>();
-       
+
         DocumentationResource DOC = DocumentationResource.getInstance(graph);
 
        Variable broadcasted = target.getPossibleProperty(graph, DOC.Relations_broadcasted);
        if(broadcasted != null) result.add(broadcasted);
-       
+
        for(Variable child : DocumentServerUtils.getChildrenInOrdinalOrder(graph, target)) {
                result.addAll(getBroadcasted(graph, child));
        }
-       
+
        return result;
-       
+
     }
 
     private static List<Command> getCommands(ReadGraph graph, Collection<Variable> commandVariables, String trigger, CommandContext constants, boolean broadcast) throws DatabaseException {
-       
+
        if(commandVariables.isEmpty()) return Collections.emptyList();
-       
+
         String t = trigger;
         TreeMap<Integer, List<Command>> sequences = new TreeMap<Integer, List<Command>>();
-        
+
         DocumentationResource DOC = DocumentationResource.getInstance(graph);
-        
+
         int count = 0;
         for (Variable c : commandVariables) {
-               
+
             if(trigger == null)
                t = c.getName(graph);
-            
+
             Connection conn = c.getValue(graph);
             Variable targetConnectionPoint = DocumentServerUtils.getPossibleCommandTriggerConnectionPoint(graph, c, conn);
             if (targetConnectionPoint != null) {
                 Variable target = targetConnectionPoint.getParent(graph);
                 if (target != null) {
-                    
+
                     Boolean enabled = target.getPossiblePropertyValue(graph, DOC.Properties_exists, Bindings.BOOLEAN);
                     if(enabled != null && !enabled) continue;
-                       
+
                     Integer ordinal;
                     if (broadcast) {
                        ordinal = ++count;
@@ -295,10 +395,10 @@ public class Functions {
                                    ordinal = Integer.parseInt(o);
                            } catch (NumberFormatException e) {}
                     }
-                    
+
                     String constantKey = target.getPossiblePropertyValue(graph, "constantKey");
                     String constantValue = target.getPossiblePropertyValue(graph, "constantValue");
-                    
+
                     CommandContextMutable newConstants = (CommandContextMutable)constants; 
                     if(constantKey != null && constantValue != null) {
                        if(!constantKey.isEmpty()) {
@@ -306,7 +406,7 @@ public class Functions {
                                newConstants.putString(constantKey, constantValue);
                        }
                     }
-                    
+
                     String requiredKey = target.getPossiblePropertyValue(graph, "requiredKey");
                     if(requiredKey != null && !requiredKey.isEmpty()) {
                        if (newConstants == constants) {
@@ -314,7 +414,7 @@ public class Functions {
                        }
                                newConstants.putRow(CommandContext.REQUIRED_KEYS, Collections.<Object>singletonList(requiredKey));
                     }
-                    
+
                     String forbiddenKey = target.getPossiblePropertyValue(graph, "forbiddenKey");
                     if(forbiddenKey != null && !forbiddenKey.isEmpty()) {
                        if (newConstants == constants) {
@@ -334,7 +434,7 @@ public class Functions {
                        Command command = new Command(DocumentServerUtils.getId(graph, target), t, 
                                        targetConnectionPoint.getName(graph), newConstants);
                        sequences.put(ordinal, Collections.singletonList(command));
-                       
+
                     }
 
                 }
@@ -347,7 +447,7 @@ public class Functions {
                        commands.add(command);
                }
         }
-        
+
         return commands;
     }
 
@@ -408,7 +508,7 @@ public class Functions {
 
                                        dataDefinitions.add(new DataDefinition(DocumentServerUtils.getId(graph, data),
                                                        sourceProperty, targetProperty));
-                                
+
                                 } else if (graph.isInheritedFrom(type, DOC.Components_DefVars)) {
 
                                        List<String> sourcesProperty = toList(dataDefinition.getPropertyValue(graph, DOC.Properties_sources), String.class);
@@ -443,33 +543,31 @@ public class Functions {
                return Collections.<T>emptyList();
        }
     }
-    
+
     public static AbstractEventHandler emptyOnClick(ReadGraph graph) throws DatabaseException {
         return new EventHandler() {
-
             @Override
             public ServerResponse handle(ReadGraph graph, CommandContext parameters) throws DatabaseException {
                 return null;
             }
-
         };
     }
-    
+
     public static AbstractEventHandler writeEventHandler(ReadGraph graph, final Variable variable, final Function fn) {
-        
+
        final Session session = graph.getSession();
-       
+
         return new AbstractEventHandler() {
-            
+
                        @Override
                        public CommandResult handle(final CommandContext parameters) {
-                               
+
                 final SCLReportingHandler printer = (SCLReportingHandler)SCLContext.getCurrent().get(SCLReportingHandler.REPORTING_HANDLER);
-                
+
                 try {
-                       
+
                                        String result = session.sync(new WriteResultRequest<String>() {
-                                           
+
                                            @Override
                                            public String perform(WriteGraph graph) throws DatabaseException {
                                                SCLContext sclContext = SCLContext.getCurrent();
@@ -493,40 +591,40 @@ public class Functions {
                                                        sclContext.put("graph", oldGraph);
                                                        sclContext.put(SCLReportingHandler.REPORTING_HANDLER, oldPrinter);
                                                }
-                                               
+
                                                return null;
-                                               
+
                                            }
-                                           
+
                                        });
-                                       
+
                        return new SuccessResponse(result);
-                                       
+
                                } catch (Throwable e) {
                                        Logger.defaultLogError(e);
                                        return new org.simantics.document.server.serverResponse.Error(e.getMessage()); 
                                }
-                
+
                        }
-            
+
         };
     }
-    
+
     public static AbstractEventHandler readEventHandler(ReadGraph graph, final Variable variable, final Function fn) {
-        
+
        final Session session = graph.getSession();
-       
+
         return new AbstractEventHandler() {
-            
+
                        @Override
                        public CommandResult handle(final CommandContext parameters) {
-                               
+
                 final SCLReportingHandler printer = (SCLReportingHandler)SCLContext.getCurrent().get(SCLReportingHandler.REPORTING_HANDLER);
-                
+
                 try {
-                       
+
                                        String result = session.sync(new UniqueRead<String>() {
-                                           
+
                                            @Override
                                            public String perform(ReadGraph graph) throws DatabaseException {
                                                SCLContext sclContext = SCLContext.getCurrent();
@@ -550,40 +648,40 @@ public class Functions {
                                                        sclContext.put("graph", oldGraph);
                                                        sclContext.put(SCLReportingHandler.REPORTING_HANDLER, oldPrinter);
                                                }
-                                               
+
                                                return null;
-                                               
+
                                            }
-                                           
+
                                        });
-                                       
+
                        return new SuccessResponse(result);
-                                       
+
                                } catch (Throwable e) {
                                        Logger.defaultLogError(e);
                                        return new org.simantics.document.server.serverResponse.Error(e.getMessage()); 
                                }
-                
+
                        }
-            
+
         };
     }
 
     public static AbstractEventHandler readEventHandler2(ReadGraph graph, final Function fn) {
-        
+
        final Session session = graph.getSession();
-       
+
         return new AbstractEventHandler() {
-            
+
                        @Override
                        public CommandResult handle(final CommandContext parameters) {
-                               
+
                 final SCLReportingHandler printer = (SCLReportingHandler)SCLContext.getCurrent().get(SCLReportingHandler.REPORTING_HANDLER);
-                
+
                 try {
-                       
+
                        CommandResult result = session.sync(new UniqueRead<CommandResult>() {
-                                           
+
                                            @Override
                                            public CommandResult perform(ReadGraph graph) throws DatabaseException {
                                                SCLContext sclContext = SCLContext.getCurrent();
@@ -601,22 +699,22 @@ public class Functions {
                                                        sclContext.put("graph", oldGraph);
                                                        sclContext.put(SCLReportingHandler.REPORTING_HANDLER, oldPrinter);
                                                }
-                                               
+
                                                return null;
-                                               
+
                                            }
-                                           
+
                                        });
-                                       
+
                        return result;
-                                       
+
                                } catch (Throwable e) {
                                        Logger.defaultLogError(e);
                                        return new org.simantics.document.server.serverResponse.Error(e.getMessage()); 
                                }
-                
+
                        }
-            
+
         };
     }
 
@@ -626,18 +724,18 @@ public class Functions {
         if(anyFunction == null) return null;
 
         final List<TCon> effects = ServerSCLHandlerValueRequest.getEffects(graph, anyFunction);
-       
+
        final Function1<CommandContext, CommandResult> fn = anyFunction.getValue(graph);
         String expression = anyFunction.getPropertyValue(graph, "expression");
-        
+
        final Session session = graph.getSession();
-       
+
         return new AbstractResponseHandler(expression) {
-            
+
                private String formatError(RequestProcessor proc, Throwable t) {
-                       
+
                        try {
-                               
+
                                        return proc.syncRequest(new UniqueRead<String>() {
 
                                                @Override
@@ -654,33 +752,33 @@ public class Functions {
                                                        message.append(" handler=" + path + "\n");
                                                        message.append(" expression=" + expr + "\n");
                                                        message.append(" message=" + t.getMessage() + "\n");
-                                                       
+
                                                        StringWriter sw = new StringWriter();
                                                        t.printStackTrace(new PrintWriter(sw));
                                                        message.append(" stack trace=" + sw);
-                                                       
+
                                                        return message.toString();
                                                }
-                                               
+
                                        });
-                                       
+
                                } catch (DatabaseException e) {
-                                       
+
                                        return e.getMessage();
-                                       
+
                                }
-                       
+
                }
-               
+
                        @Override
                        public CommandResult handle(final CommandContext parameters) {
-                               
+
                                IConsole console = parameters.getValue("__console__");
                                SCLReportingHandler printer = (console != null) ? new ConsoleSCLReportingHandler(console)
                                                : (SCLReportingHandler) SCLContext.getCurrent().get(SCLReportingHandler.REPORTING_HANDLER);
-                
+
                 try {
-                       
+
                        Object result = null;
                        if(effects.contains(Types.WRITE_GRAPH)) {
 
@@ -704,18 +802,18 @@ public class Functions {
 
                                                        }
                                });
-                               
+
                        } else if(effects.contains(Types.READ_GRAPH)) {
 
                                result = session.sync(new UniqueRead<Object>() {
 
                                        @Override
                                        public Object perform(ReadGraph graph) throws DatabaseException {
-                                               
+
                                                SCLContext sclContext = SCLContext.getCurrent();
                                                Object oldGraph = sclContext.put("graph", graph);
                                                Object oldPrinter = sclContext.put(SCLReportingHandler.REPORTING_HANDLER, printer);
-                                               
+
                                                try {
                                                        Object response = fn.apply(parameters);
                                                        return response;
@@ -725,12 +823,12 @@ public class Functions {
                                                        sclContext.put("graph", oldGraph);
                                                        sclContext.put(SCLReportingHandler.REPORTING_HANDLER, oldPrinter);
                                                }
-                                               
+
                                        }
                                });
 
                        } else {
-                               
+
                                                SCLContext sclContext = SCLContext.getCurrent();
                                                Object oldPrinter = sclContext.put(SCLReportingHandler.REPORTING_HANDLER, printer);
                                                try {
@@ -744,39 +842,39 @@ public class Functions {
                        if (result instanceof org.simantics.document.server.serverResponse.Error) {
                                return (CommandResult)result;
                        }
-                       
+
                        if (result instanceof CommandResult) {
                                return (CommandResult)result;
                        } else {
                                CommandContextMutable assignments = new CommandContextImpl();
                                assignments.putValue("result", result);
                                return new ServerResponse(200, "", assignments);
-                       }                       
-                                       
+                       }
+
                                } catch (Throwable e) {
                                        return new org.simantics.document.server.serverResponse.Error(formatError(Simantics.getSession(), e));
                                }
-                
+
                        }
-            
+
         };
     }
-    
+
     public static AbstractEventHandler writeEventHandler2(ReadGraph graph, final Function fn) {
-        
+
        final Session session = graph.getSession();
-       
+
         return new AbstractEventHandler() {
-            
+
                        @Override
                        public CommandResult handle(final CommandContext parameters) {
-                               
+
                 final SCLReportingHandler printer = (SCLReportingHandler)SCLContext.getCurrent().get(SCLReportingHandler.REPORTING_HANDLER);
-                
+
                 try {
-                       
+
                        CommandResult result = session.syncRequest(new WriteResultRequest<CommandResult>() {
-                                           
+
                                            @Override
                                            public CommandResult perform(WriteGraph graph) throws DatabaseException {
                                                SCLContext sclContext = SCLContext.getCurrent();
@@ -794,36 +892,36 @@ public class Functions {
                                                        sclContext.put("graph", oldGraph);
                                                        sclContext.put(SCLReportingHandler.REPORTING_HANDLER, oldPrinter);
                                                }
-                                               
+
                                                return null;
-                                               
+
                                            }
-                                           
+
                                        });
-                                       
+
                        return result;
-                                       
+
                                } catch (Throwable e) {
                                        Logger.defaultLogError(e);
                                        return new org.simantics.document.server.serverResponse.Error(e.getMessage()); 
                                }
-                
+
                        }
-            
+
         };
     }
 
     public static AbstractEventHandler eventHandler2(ReadGraph graph, final Function fn) {
-        
+
         return new AbstractEventHandler() {
-            
+
                        @Override
                        public CommandResult handle(final CommandContext parameters) {
-                               
+
                 final SCLReportingHandler printer = (SCLReportingHandler)SCLContext.getCurrent().get(SCLReportingHandler.REPORTING_HANDLER);
-                
+
                 try {
-                       
+
                        SCLContext sclContext = SCLContext.getCurrent();
                        Object oldPrinter = sclContext.put(SCLReportingHandler.REPORTING_HANDLER, printer);
                        try {
@@ -839,30 +937,30 @@ public class Functions {
                        }
 
                        return null;
-                                               
+
                                } catch (Throwable e) {
                                        Logger.defaultLogError(e);
                                        return new org.simantics.document.server.serverResponse.Error(e.getMessage()); 
                                }
-                
+
                        }
-            
+
         };
     }
 
     public static AbstractEventHandler eventHandler(ReadGraph graph, final Function fn) {
-       
+
         return new AbstractEventHandler() {
-            
+
                        @Override
                        public CommandResult handle(final CommandContext parameters) {
-                               
+
                 final SCLReportingHandler printer = (SCLReportingHandler)SCLContext.getCurrent().get(SCLReportingHandler.REPORTING_HANDLER);
-                
+
                 try {
 
                        String result = "";
-                       
+
                                SCLContext sclContext = SCLContext.getCurrent();
                                Object oldPrinter = sclContext.put(SCLReportingHandler.REPORTING_HANDLER, printer);
                                try {
@@ -881,16 +979,16 @@ public class Functions {
                                } finally {
                                        sclContext.put(SCLReportingHandler.REPORTING_HANDLER, oldPrinter);
                                }
-                                       
+
                        return new SuccessResponse(result);
-                                       
+
                                } catch (Throwable e) {
                                        Logger.defaultLogError(e);
                                        return new org.simantics.document.server.serverResponse.Error(e.getMessage()); 
                                }
-                
+
                        }
-            
+
         };
     }
 
@@ -950,18 +1048,18 @@ public class Functions {
 
         return null;
     }
-    
+
     public static CommandContextMutable putTuple(CommandContextMutable context, String key, Object tuple) {
        List<Object> list = new ArrayList<Object>();
        if (tuple instanceof Tuple) {
                Collections.addAll(list, ((Tuple)tuple).toArray());
        } else {
-               list.add(tuple);                
+               list.add(tuple);
        }
        context.putRow(key, list);
        return context;
     }
-    
+
     public static List<Object> getTuples(CommandContext context, String key) {
        List<List<Object>> rows = context.getRows(key);
        List<Object> tuples = new ArrayList<Object>();
@@ -979,11 +1077,11 @@ public class Functions {
        }
        return tuples;
     }
-    
+
     public static String printContext(CommandContext context) {
        return context.toString();
     }
-    
+
     @SCLValue(type = "AbstractEventHandler")
     public static AbstractEventHandler emptyEvent = new AbstractEventHandler() {
 
@@ -995,75 +1093,75 @@ public class Functions {
     };
 
     public static String sclStateKey(ReadGraph graph, Variable base, Variable self, String ref) throws DatabaseException {
-       
+
        String baseURI = base.getURI(graph);
-       
+
        String selfURI = self.getURI(graph);
-       
+
        String prefix = selfURI.substring(0, selfURI.indexOf(ProxyChildVariable.CONTEXT_BEGIN));
        String suffix = selfURI.substring(selfURI.lastIndexOf(ProxyChildVariable.CONTEXT_END) + ProxyChildVariable.CONTEXT_END.length());
        String stripped = prefix + suffix;
-       
+
        String relative = Variables.getRelativeRVI(baseURI, stripped); 
-       
+
        return Variables.getRVI(relative, ref);
-       
+
     }
-    
+
     public static Variable sclStateVariable(ReadGraph graph, Variable base, Variable self, String ref) throws DatabaseException {
-       
+
        String id = sclStateKey(graph, base, self, ref);
 
        Variable sclVar = base.getPossibleChild(graph, "__scl__");
        if(sclVar == null) return null;
-       
+
        return sclVar.getPossibleProperty(graph, id);
-       
+
     }
-    
+
     public static Object sclStateValueOrDefault(ReadGraph graph, Variable base, Variable self, String ref, Object defaultValue) throws DatabaseException {
-       
+
        Variable stateVariable = sclStateVariable(graph, base, self, ref);
        if (stateVariable != null) {
-               
+
                return stateVariable.getValue(graph);
-               
+
        } 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;
-               
+
        }
     }
-    
+
     public static void setSclStateValue(WriteGraph graph, Variable base, Variable self, String ref, Object value) throws DatabaseException {
-       
+
        String id = sclStateKey(graph, base, self, ref);
 
        SCLRealm realm = SCLSessionManager.getOrCreateSCLRealm(base.getURI(graph)+"/__scl__");
        realm.getConnection().setVariable(id, getSCLType(value), value);
        realm.refreshVariablesSync();
-               
+
     }
-    
+
     public static Object projectComponentState(ReadGraph graph, Variable self, String ref, Object defaultValue) throws DatabaseException {
        Resource project = Simantics.getProjectResource();
        Variable component = self.getParent(graph);
        Variable projectVariable = Variables.getVariable(graph, project);
        return sclStateValueOrDefault(graph, projectVariable, component, ref, defaultValue);
     }
-    
+
     public static void setProjectComponentState(WriteGraph graph, Variable self, String ref, Object value) throws DatabaseException {
        Resource project = Simantics.getProjectResource();
        Variable component = self.getParent(graph);
        Variable projectVariable = Variables.getVariable(graph, project);
        setSclStateValue(graph, projectVariable, component, ref, value);
     }
-    
+
     private static Type getSCLType(Object value) throws DatabaseException {
        Binding b = Bindings.getBindingUnchecked(value.getClass());
        Datatype t = b.type();
@@ -1084,7 +1182,7 @@ public class Functions {
     public static String documentModelContributionLabel(ReadGraph graph, Variable var) throws DatabaseException {
        return var.getName(graph);
     }
-    
+
     public static Object getPropertyValueCached(ReadGraph graph, Variable variable, String name, Binding binding) throws DatabaseException {
        Variable property = graph.syncRequest(new VariableProperty(variable, name));
        return graph.syncRequest(new VariableValueWithBinding<Object>(property, binding));
@@ -1108,7 +1206,7 @@ public class Functions {
                return graph.syncRequest(new ParentExistsRequest(variable.getParent(graph)));
 
                }
-       
+
     } 
 
     public static class PathExistsRequest extends VariableRead<Boolean> {
@@ -1119,20 +1217,20 @@ public class Functions {
 
                @Override
                public Boolean perform(ReadGraph graph) throws DatabaseException {
-                       
+
                        Variable widget = variable.getParent(graph);
-               
+
                Boolean exists = widget.getPossiblePropertyValue(graph, "exists");
                        if (exists == null || !exists) return false;
-               
+
                if (!graph.syncRequest(new ParentExistsRequest(widget.getParent(graph)))) return false;
-               
+
                DocumentationResource DOC = DocumentationResource.getInstance(graph);
-               Collection <Variable> cps = widget.getProperties(graph, DOC.Relations_parentRelation);
+               Collection <Variable> cps = widget.getProperties(graph, DocumentationResource.URIs.Relations_parentRelation);
                for (Variable cp : cps) {
-                       
+
                        Connection conn = cp.getValue(graph);
-                               Variable otherCp = DocumentServerUtils.getPossibleOtherConnectionPoint(graph, cp, conn);
+                               Variable otherCp = DocumentServerUtils.getPossibleChildConnectionPoint(graph, cp, conn);
                                if (otherCp != null) {
                                        Variable parentWidget = otherCp.getParent(graph);
                                        if (parentWidget.<Boolean>getPropertyValue(graph, "pathExists")) {
@@ -1161,17 +1259,16 @@ public class Functions {
                                        }
                                }
                        }
-               
+
                        Resource type = widget.getType(graph);
                        return graph.isInheritedFrom(type, DOC.Components_ParentlessComponent) ||
                                (graph.isInheritedFrom(type, DOC.DocumentComponent) && cps.isEmpty());
                }
-    } 
-    
+    }
+
     @SCLValue(type = "ReadGraph -> Resource -> Variable -> Boolean")
     public static boolean pathExists(ReadGraph graph, Resource converter, Variable context) throws DatabaseException {
        return graph.syncRequest(new PathExistsRequest(context));
     }
-    
-    
+
 }
\ No newline at end of file
index d1c0c89715022fc85b0d9f46df5656d53959054e..b714253e2e86a8a576575e915eefd537023740a5 100644 (file)
@@ -11,19 +11,16 @@ import org.simantics.db.exception.DatabaseException;
 import org.simantics.db.layer0.request.VariableRead;
 import org.simantics.db.layer0.variable.Variable;
 import org.simantics.db.service.CollectionSupport;
-import org.simantics.document.base.ontology.DocumentationResource;
 import org.simantics.structural2.scl.StructuralComponent;
 
 public class VariableStructuralContext extends StructuralComponent<Variable> {
 
        final private Variable selfVariable;
        final private Variable variable;
-       final private DocumentationResource DOC;
        
        public VariableStructuralContext(ReadGraph graph, Variable selfVariable) throws DatabaseException {
                this.selfVariable = selfVariable;
                this.variable = selfVariable.getParent(graph).getParent(graph);
-               this.DOC = DocumentationResource.getInstance(graph);
        }
        
        @Override
index 8b6802829a3a686e9a099b6933c98135968090b6..f47c0c5c38ff53eade8eaa9834d641aa4e0cba06 100644 (file)
@@ -1,6 +1,5 @@
 package org.simantics.document.server.bean;
 
-import org.simantics.databoard.util.Bean;
 import org.simantics.document.server.io.CommandContext;
 import org.simantics.document.server.io.ICommand;
 
diff --git a/bundles/org.simantics.document.server/src/org/simantics/document/server/request/DefaultFields.java b/bundles/org.simantics.document.server/src/org/simantics/document/server/request/DefaultFields.java
new file mode 100644 (file)
index 0000000..643e977
--- /dev/null
@@ -0,0 +1,79 @@
+package org.simantics.document.server.request;
+
+import org.simantics.databoard.Bindings;
+import org.simantics.databoard.Databoard;
+import org.simantics.databoard.binding.Binding;
+import org.simantics.db.ReadGraph;
+import org.simantics.db.Resource;
+import org.simantics.db.exception.DatabaseException;
+import org.simantics.db.layer0.request.VariableRead;
+import org.simantics.db.layer0.variable.Variable;
+import org.simantics.document.base.ontology.DocumentationResource;
+import org.simantics.document.server.DocumentServerUtils;
+import org.simantics.document.server.JSONObject;
+import org.simantics.layer0.Layer0;
+import org.simantics.structural.stubs.StructuralResource2;
+
+/**
+ * @author Antti Villberg
+ * @since 1.30.0
+ */
+public class DefaultFields extends VariableRead<JSONObject> {
+
+    public DefaultFields(Variable variable) {
+        super(variable);
+    }
+
+    @Override
+    public JSONObject perform(ReadGraph graph) throws DatabaseException {
+
+        Layer0 L0 = Layer0.getInstance(graph);
+        StructuralResource2.getInstance(graph);
+        DocumentationResource DOC = DocumentationResource.getInstance(graph);
+
+        String id = DocumentServerUtils.getId(graph, variable);
+
+        Binding jsonBinding = graph.getService(Databoard.class).getBindingUnchecked(JSONObject.class);
+        JSONObject object = new JSONObject(jsonBinding, id);
+
+        Resource type = variable.getType(graph);
+
+        Variable parentConnectionPoint = DocumentServerUtils.getParentConnectionPoint(graph, variable);
+        while (parentConnectionPoint != null) {
+            Variable par = parentConnectionPoint.getParent(graph);
+            if(!DOC.Components_DummyContainer.equals(par.getType(graph))) break;
+            parentConnectionPoint = DocumentServerUtils.getParentConnectionPoint(graph, par);
+        }
+
+        if(parentConnectionPoint != null) {
+            String parent = DocumentServerUtils.getId(graph, parentConnectionPoint.getParent(graph));
+            Resource cpPredicate = parentConnectionPoint.getPossiblePredicateResource(graph);
+            String i = null;
+            if (DOC.Relations_partN.equals(cpPredicate)) {
+                i = DocumentServerUtils.findManualOrdinal(graph, variable);
+            } else {
+                i = graph.getPossibleRelatedValue(cpPredicate, DOC.Document_ChildRelation_ordinal, Bindings.STRING);
+            }
+            if (i == null) {
+                i = "0";
+            }
+
+            object.addJSONField("parent", parent);
+            object.addJSONField("parentOrd", i);
+        } else {
+            if(DOC.Components_Root.equals(type) || graph.isInheritedFrom(type, DOC.Components_Root)) {
+                object.addJSONField("parent", "root");
+                object.addJSONField("parentOrd", "0");
+            } if(!graph.isInheritedFrom(type, DOC.Components_ParentlessComponent)) {
+                object.addJSONField("parent", "");
+                object.addJSONField("parentOrd", "0");
+            }
+        }
+
+        object.addJSONField("id", id);
+        object.addJSONField("type", graph.<String>getRelatedValue(type, L0.HasName, Bindings.STRING));
+        return object;
+
+    }
+
+}
\ No newline at end of file
diff --git a/bundles/org.simantics.document.server/src/org/simantics/document/server/request/DocumentContext.java b/bundles/org.simantics.document.server/src/org/simantics/document/server/request/DocumentContext.java
deleted file mode 100644 (file)
index df9a6db..0000000
+++ /dev/null
@@ -1,27 +0,0 @@
-package org.simantics.document.server.request;
-
-import gnu.trove.map.hash.THashMap;
-
-import java.util.Map;
-
-import org.simantics.db.ReadGraph;
-import org.simantics.document.base.ontology.DocumentationResource;
-import org.simantics.structural2.scl.ComponentTypeProperty;
-
-public class DocumentContext /*extends ExpressionContext*/ {
-/*
-    private static Map<String,ComponentTypeProperty> get(ReadGraph graph) {
-        Map<String,ComponentTypeProperty> properties = new THashMap<String,ComponentTypeProperty>();
-        properties .put("input", new ComponentTypeProperty(DocumentationResource.getInstance(graph).Properties_input, ActualCompileRequest.VARIABLE));
-        properties .put("self", new ComponentTypeProperty(DocumentationResource.getInstance(graph).Properties_self, ActualCompileRequest.VARIABLE));
-        properties .put("model", new ComponentTypeProperty(DocumentationResource.getInstance(graph).Properties_model, ActualCompileRequest.VARIABLE));
-        properties .put("experiment", new ComponentTypeProperty(DocumentationResource.getInstance(graph).Properties_experiment, ActualCompileRequest.VARIABLE));
-        properties .put("graph", new ComponentTypeProperty(DocumentationResource.getInstance(graph).Properties_graph, ActualCompileRequest.READ_GRAPH_INTERNAL));
-        return properties;
-    }
-    
-       public DocumentContext(ReadGraph graph) {
-               super(get(graph));
-       }
-*/
-}
\ No newline at end of file
index b0234ec8c49d305cee6d39f8fff9150cbbfb2916..9a53060bef2c0b246a21e465da1f0e53756d2f17 100644 (file)
@@ -17,6 +17,9 @@ import org.simantics.document.server.JSONObject;
 public class DocumentRequest extends VariableRead<List<JSONObject>> {
        
        public static boolean PROFILE = false;
+       // Thresholds in microseconds
+       public static int PROFILE_THRESHOLD_NODEREQUEST = 2000;
+       public static int PROFILE_THRESHOLD_VALUEREQUEST = 500;
 
     public DocumentRequest(Variable var) {
         super(var);
@@ -60,7 +63,7 @@ public class DocumentRequest extends VariableRead<List<JSONObject>> {
         
         if(PROFILE) {
                long dura = System.nanoTime()-s;
-               System.err.println("DocumentRequest " + System.identityHashCode(this) + " " + variable.getURI(graph) + " in " + 1e-6*dura + "ms.");
+               System.err.println("DocumentRequest " + System.identityHashCode(this) + " in " + 1e-6*dura + "ms. " + variable.getURI(graph));
         }
 
                return result;
index f481984bac8d1b130340c986a08a0d97e6ea19b3..606b361b62729ef2f5b74cdb8ad7f379a974049c 100644 (file)
@@ -6,7 +6,6 @@ import org.simantics.db.layer0.variable.Variable;
 import org.simantics.db.layer0.variable.Variables;
 import org.simantics.db.request.Read;
 import org.simantics.document.server.handler.AbstractEventHandler;
-import org.simantics.document.server.io.CommandContext;
 import org.simantics.structural.stubs.StructuralResource2;
 
 public class HandleEventRequest implements Read<AbstractEventHandler> {
index 71b66a76ba8ea32f9909e5785b0c89a279fd39d8..98bd0fb821dc0286238da7a310c85149247633a0 100644 (file)
@@ -1,6 +1,7 @@
 package org.simantics.document.server.request;
 
 import java.util.ArrayList;
+import java.util.Collection;
 import java.util.List;
 import java.util.Map;
 
@@ -8,11 +9,13 @@ import org.simantics.db.ReadGraph;
 import org.simantics.db.exception.DatabaseException;
 import org.simantics.db.layer0.request.VariableRead;
 import org.simantics.db.layer0.variable.Variable;
+import org.simantics.document.server.DocumentServerUtils.AttributesRequest;
 import org.simantics.document.server.JSONObject;
+import org.simantics.utils.datastructures.Pair;
 
 public class NodeRequest extends VariableRead<JSONObject> {
-    
-    public static String ERRORS = "Errors";
+
+    public static final String ERRORS = "Errors";
 
        public NodeRequest(Variable node) {
                super(node);
@@ -21,38 +24,53 @@ public class NodeRequest extends VariableRead<JSONObject> {
        @SuppressWarnings("unchecked")
     @Override
        public JSONObject perform(ReadGraph graph) throws DatabaseException {
-               
+
                long s = System.nanoTime();
 
-           JSONObject staticContent = graph.syncRequest(new NodeRequestStatic(variable));
+               Pair<JSONObject, Collection<Variable>> attribs = graph.syncRequest(new AttributesRequest(variable));
+
+           JSONObject staticContent = attribs.first;
+           if(attribs.second.isEmpty()) {
+
+               if(DocumentRequest.PROFILE) {
+                       long dura = System.nanoTime()-s;
+                   if(dura > DocumentRequest.PROFILE_THRESHOLD_NODEREQUEST * 1e3) {
+                       System.err.println("NodeRequest " + System.identityHashCode(this) + " in " + 1e-6*dura + "ms. " + variable.getURI(graph));
+                   }
+               }
+
+               return staticContent;
+           }
+
            Map<String,Object> dynamicContent = graph.syncRequest(new NodeRequestDynamic(variable));
-           
            if(dynamicContent.isEmpty()) {
                return staticContent;
            }
-           
+
         JSONObject result = staticContent.clone();
         result.add(dynamicContent);
 
         if(dynamicContent.containsKey(ERRORS) || staticContent.getJSONField(ERRORS) != null) {
-            ArrayList<String> errorList = new ArrayList<String>();
-            
+            ArrayList<String> errorList = new ArrayList<>();
+
             if(dynamicContent.containsKey(ERRORS))
                 errorList.addAll((List<String>)dynamicContent.get(ERRORS));
-            
+
             if(staticContent.getJSONField(ERRORS) != null)
                 errorList.addAll((List<String>)staticContent.getJSONField(ERRORS));
-            
+
             result.addJSONField(ERRORS, errorList);
         }
-        
+
         if(DocumentRequest.PROFILE) {
-               long dura = System.nanoTime()-s;
-               System.err.println("NodeRequest " + System.identityHashCode(this) + " " + variable.getURI(graph) + " in " + 1e-6*dura + "ms." + result);
+            long dura = System.nanoTime()-s;
+            if(dura > DocumentRequest.PROFILE_THRESHOLD_NODEREQUEST * 1e3) {
+                System.err.println("NodeRequest " + System.identityHashCode(this) + " in " + 1e-6*dura + "ms. " + variable.getURI(graph));
+            }
         }
         
-           return result;
+        return result;
 
-       }               
+    }
 
 }
\ No newline at end of file
diff --git a/bundles/org.simantics.document.server/src/org/simantics/document/server/request/NodeRequestStatic.java b/bundles/org.simantics.document.server/src/org/simantics/document/server/request/NodeRequestStatic.java
deleted file mode 100644 (file)
index 8a65fb1..0000000
+++ /dev/null
@@ -1,123 +0,0 @@
-package org.simantics.document.server.request;
-
-import java.util.ArrayList;
-import java.util.List;
-
-import org.simantics.databoard.Bindings;
-import org.simantics.databoard.Databoard;
-import org.simantics.databoard.binding.Binding;
-import org.simantics.db.ReadGraph;
-import org.simantics.db.Resource;
-import org.simantics.db.exception.DatabaseException;
-import org.simantics.db.layer0.request.VariableRead;
-import org.simantics.db.layer0.variable.Variable;
-import org.simantics.document.base.ontology.DocumentationResource;
-import org.simantics.document.server.DocumentServerUtils;
-import org.simantics.document.server.JSONObject;
-import org.simantics.layer0.Layer0;
-import org.simantics.structural.stubs.StructuralResource2;
-
-class NodeRequestStatic extends VariableRead<JSONObject> {
-
-       public NodeRequestStatic(Variable node) {
-               super(node);
-       }
-
-       public static class Headers extends VariableRead<JSONObject> {
-
-               public Headers(Variable variable) {
-                       super(variable);
-               }
-
-               @Override
-               public JSONObject perform(ReadGraph graph) throws DatabaseException {
-
-                       Layer0 L0 = Layer0.getInstance(graph);
-                       StructuralResource2.getInstance(graph);
-                       DocumentationResource DOC = DocumentationResource.getInstance(graph);
-
-                       String id = DocumentServerUtils.getId(graph, variable);
-                       
-                       Binding jsonBinding = graph.getService(Databoard.class).getBindingUnchecked(JSONObject.class);
-                       JSONObject object = new JSONObject(jsonBinding, id);
-
-                       Resource type = variable.getType(graph);
-
-                       Variable parentConnectionPoint = DocumentServerUtils.getParentConnectionPoint(graph, variable);
-                       while (parentConnectionPoint != null) {
-                               Variable par = parentConnectionPoint.getParent(graph);
-                               if(!DOC.Components_DummyContainer.equals(par.getType(graph))) break;
-                               parentConnectionPoint = DocumentServerUtils.getParentConnectionPoint(graph, par);
-                       }
-                       
-                       if(parentConnectionPoint != null) {
-                               String parent = DocumentServerUtils.getId(graph, parentConnectionPoint.getParent(graph));
-                               Resource cpPredicate = parentConnectionPoint.getPossiblePredicateResource(graph);
-                               String i = null;
-                               if (DOC.Relations_partN.equals(cpPredicate)) {
-                                       i = DocumentServerUtils.findManualOrdinal(graph, variable);
-                               } else {
-                                       i = graph.getPossibleRelatedValue(cpPredicate, DOC.Document_ChildRelation_ordinal, Bindings.STRING);
-                               }
-                               if (i == null) {
-                                       i = "0";
-                               }
-                               
-                               object.addJSONField("parent", parent);
-                               object.addJSONField("parentOrd", i);
-                       } else {
-                           if(DOC.Components_Root.equals(type) || graph.isInheritedFrom(type, DOC.Components_Root)) {
-                               object.addJSONField("parent", "root");
-                               object.addJSONField("parentOrd", "0");
-                           } if(!graph.isInheritedFrom(type, DOC.Components_ParentlessComponent)) {
-                               object.addJSONField("parent", "");
-                               object.addJSONField("parentOrd", "0");
-                           }
-                       }
-
-                       object.addJSONField("id", id);
-                       object.addJSONField("type", graph.<String>getRelatedValue(type, L0.HasName, Bindings.STRING));
-                       return object;
-                       
-               }
-               
-       }
-       
-       @Override
-       public JSONObject perform(ReadGraph graph) throws DatabaseException {
-
-               long s = System.nanoTime();
-
-               JSONObject base = graph.syncRequest(new Headers(variable));
-               JSONObject object = base.clone();
-               
-               DocumentationResource DOC = DocumentationResource.getInstance(graph);
-               
-               for(Variable attrib : DocumentServerUtils.getStaticAttributes(graph, DOC, variable)) {
-                       String name = attrib.getName(graph);
-                       try {
-                           Object value = DocumentServerUtils.getValue(graph, attrib);
-                           object.addJSONField(name, value);
-                       } catch (Throwable t) {
-                           List<String> errorList = object.getJSONField(NodeRequest.ERRORS);
-                           if(errorList == null)
-                               errorList = new ArrayList<String>();
-                           
-                   String errorMessage = NodeRequestUtils.formatErrorMessage(name, t);
-                   
-                           errorList.add(errorMessage);
-                           object.addJSONField(NodeRequest.ERRORS, errorList);
-                       }
-
-               }
-
-        if(DocumentRequest.PROFILE) {
-            long dura = System.nanoTime()-s;
-            System.err.println("NodeRequestStatic " + System.identityHashCode(this) + " " + variable.getURI(graph) + " in " + 1e-6*dura + "ms." + object);
-        }
-               
-               return object;
-
-       }               
-
-}
\ No newline at end of file
index 5a55285677ae132e8f638be9fd949ee1291f58fe..f3f1177fc4d1f613950bc7b4f7d56e2ec010105f 100644 (file)
@@ -21,13 +21,13 @@ public class NodesRequest extends VariableRead<Set<Variable>> {
 
        @Override
        public Set<Variable> perform(ReadGraph graph) throws DatabaseException {
-           
+
                long s = System.nanoTime();
                
                StructuralResource2.getInstance(graph);
                if(variable == null)
                        return Collections.emptySet();
-
+               
                Set<Variable> nodes = new THashSet<Variable>();
                Collection<Variable> children = graph.syncRequest(new VariableChildren(variable));
                for(Variable child : children) {
@@ -37,7 +37,7 @@ public class NodesRequest extends VariableRead<Set<Variable>> {
                
         if(DocumentRequest.PROFILE) {
                long dura = System.nanoTime()-s;
-               System.err.println("NodesRequest " + System.identityHashCode(this) + " " + variable.getURI(graph) + " in " + 1e-6*dura + "ms.");
+               System.err.println("NodesRequest " + System.identityHashCode(this) + " in " + 1e-6*dura + "ms. " + variable.getURI(graph));
         }
                
                return nodes;
index 09b5ccdd7dd03f73c67192d5366bb3982127bde4..0f57a01c03f2cfc87ff34c9f3a1339fa9dbe9d52 100644 (file)
@@ -22,8 +22,6 @@ public class NodesRequest2 extends VariableRead<Set<Variable>> {
        @Override
        public Set<Variable> perform(ReadGraph graph) throws DatabaseException {
            
-               long s = System.nanoTime();
-               
                DocumentationResource DOC = DocumentationResource.getInstance(graph);
 
                Resource type = variable.getPossibleType(graph);
@@ -42,11 +40,6 @@ public class NodesRequest2 extends VariableRead<Set<Variable>> {
                                Set<Variable> nodes = graph.syncRequest(new NodesRequest2(child));
                                result.addAll(nodes);
                        }
-
-               if(DocumentRequest.PROFILE) {
-                       long dura = System.nanoTime()-s;
-                       System.err.println("NodesRequest2 " + System.identityHashCode(this) + " " + variable.getURI(graph) + " in " + 1e-6*dura + "ms.");
-               }
                
                        return result;
                }
index eb268174040df793f08a49efc2d5476a3dbe2be1..2071b8323646596c9fa86c0d1c0fc734a9773b95 100644 (file)
@@ -1,6 +1,5 @@
 package org.simantics.document.server.request;
 
-import java.util.HashMap;
 import java.util.List;
 import java.util.Map;
 
@@ -15,15 +14,10 @@ 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.server.Functions;
-import org.simantics.document.server.bean.DataDefinition;
 import org.simantics.document.server.request.ServerSCLHandlerValueRequest.CompilationContext;
 import org.simantics.layer0.Layer0;
-import org.simantics.scl.compiler.constants.StringConstant;
 import org.simantics.scl.compiler.elaboration.expressions.EApply;
 import org.simantics.scl.compiler.elaboration.expressions.EConstant;
-import org.simantics.scl.compiler.elaboration.expressions.ELiteral;
-import org.simantics.scl.compiler.elaboration.expressions.EVar;
 import org.simantics.scl.compiler.elaboration.expressions.EVariable;
 import org.simantics.scl.compiler.elaboration.expressions.Expression;
 import org.simantics.scl.compiler.elaboration.modules.SCLValue;
index fa8afe0a021a4daae8d0680c4f3497b223548fd0..25afd86893fd17a83475aa11f4ec9b2ea148ffb3 100644 (file)
@@ -11,7 +11,6 @@ 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.RuntimeEnvironmentRequest;
 import org.simantics.db.layer0.util.RuntimeEnvironmentRequest2;
 import org.simantics.db.layer0.variable.Variable;
 import org.simantics.document.server.request.ServerSCLValueRequest.CompilationContext;