]> gerrit.simantics Code Review - simantics/platform.git/commitdiff
Fix errors with procedural user components for computational values 35/2535/1
authorjsimomaa <jani.simomaa@gmail.com>
Wed, 21 Nov 2018 08:31:37 +0000 (10:31 +0200)
committerJani Simomaa <jani.simomaa@semantum.fi>
Mon, 10 Dec 2018 07:15:58 +0000 (07:15 +0000)
gitlab #169

Change-Id: I1d95429ea1c49ca2f8bd20d447500513a3e6f87a
(cherry picked from commit e807368d0e75809e0797d9cea1063b046fabf352)

bundles/org.simantics.db.layer0/src/org/simantics/db/layer0/adapter/SCLComputationalValue.java
bundles/org.simantics.db.layer0/src/org/simantics/db/layer0/scl/CompileValueRequest.java
bundles/org.simantics.db.layer0/src/org/simantics/db/layer0/util/RuntimeEnvironmentRequest2.java
bundles/org.simantics.document.server/src/org/simantics/document/server/request/ServerSCLValueRequest.java
bundles/org.simantics.structural2/src/org/simantics/structural2/scl/ReadComponentTypeInterfaceRequest.java
bundles/org.simantics.structural2/src/org/simantics/structural2/variables/FixedConnection.java

index 83382adb3325eaa128087ff3161c44f18755b54c..743f8e4454600edd28e8e33847616e8ddbfa5a82 100644 (file)
@@ -24,11 +24,14 @@ import org.simantics.scl.runtime.function.Function1;
  */
 public class SCLComputationalValue extends ContextualRelatedValue {
 
+    // TODO: unify and merge CompileValueRequest and CompileResourceValueRequest as one
     @Override
     public Function1<Object,Object> getFunction(ReadGraph graph, Resource s, Resource o, Resource p) throws DatabaseException {
-        if (s != null && p != null && o != null) {
-            return CompileValueRequest.compile(graph, s, o, p);
+        if (p != null && o != null) {
+            // we are now at Variable context
+            return CompileValueRequest.compile(graph, o, p);
         } else if (o != null) {
+            // we are now at Resource context
             return CompileResourceValueRequest.compile(graph, o);
         } else {
             throw new DatabaseException("Could not compile SCL expression: s=" + s + " p=" + p + " o=" + o);
index 1eb01aa393b688d0809f95dbef56d3f2d848fda2..11f3ad1854b29e28dbc9724a7149d489b508e310 100644 (file)
@@ -34,19 +34,15 @@ public class CompileValueRequest extends AbstractExpressionCompilationRequest<Co
     }
 
     protected final Resource relation;
-    protected final Resource component;
     protected final Resource literal;
 
-    public CompileValueRequest(Resource component, Resource literal, Resource relation) {
+    public CompileValueRequest(Resource literal, Resource relation) {
         this.relation = relation;
-        this.component = component;
         this.literal = literal;
     }
 
     public CompileValueRequest(ReadGraph graph, Variable context) throws DatabaseException {
-        this(context.getParent(graph).getRepresents(graph),
-                context.getRepresents(graph),
-                context.getPredicateResource(graph));
+        this(context.getRepresents(graph), context.getPredicateResource(graph));
     }
 
     public static Object compileAndEvaluate(ReadGraph graph, Variable context) throws DatabaseException {
@@ -66,12 +62,11 @@ public class CompileValueRequest extends AbstractExpressionCompilationRequest<Co
         }
     }
 
-    public static Function1<Object,Object> compile(ReadGraph graph, Resource component, Resource literal, Resource predicate) throws DatabaseException {
+    public static Function1<Object,Object> compile(ReadGraph graph, Resource literal, Resource predicate) throws DatabaseException {
         SCLContext sclContext = SCLContext.getCurrent();
         Object oldGraph = sclContext.get("graph");
         try {
-            Function1<Object,Object> exp = graph.syncRequest(new CompileValueRequest(component, literal, predicate),
-                    TransientCacheListener.instance());
+            Function1<Object,Object> exp = graph.syncRequest(new CompileValueRequest(literal, predicate), TransientCacheListener.instance());
             sclContext.put("graph", graph);
             return exp;
         } catch (DatabaseException e) {
index ca8bc07cd53ab9a3b73d05a02e73d7473ae7db2a..c51428f2f8087758621d9396e6d6bad5f3cbd138 100644 (file)
@@ -113,9 +113,14 @@ public class RuntimeEnvironmentRequest2 extends BinaryRead<Resource, Resource, R
         fillEnvironmentSpecification(environmentSpecification);
 
         Layer0 L0 = Layer0.getInstance(graph);
-        Collection<Resource> sclModules = graph.syncRequest(new ObjectsWithType(parameter, L0.ConsistsOf, L0.SCLModule));
-        for (Resource sclModule : sclModules)
-            environmentSpecification.importModule(graph.getURI(sclModule), "");
+        if (parameter != null) {
+            Collection<Resource> sclModules = graph.syncRequest(new ObjectsWithType(parameter, L0.ConsistsOf, L0.SCLModule));
+            for (Resource sclModule : sclModules) {
+                environmentSpecification.importModule(graph.getURI(sclModule), "");
+            }
+        } else {
+            // `parameter` is optional and can be null for e.g. procedural user components
+        }
 
         Resource mainModule = Layer0Utils.getPossibleChild(graph, parameter2, "SCLMain");
         if(mainModule != null)
index 09f1523a2ede4ba26ff3c4d9a52df435647d302f..7d1af5f4c1ea6d765811c036986a96a08ba2736a 100644 (file)
@@ -1,5 +1,6 @@
 package org.simantics.document.server.request;
 
+import java.util.Collections;
 import java.util.Map;
 
 import org.simantics.databoard.Bindings;
@@ -70,7 +71,7 @@ public class ServerSCLValueRequest extends AbstractExpressionCompilationRequest<
        }
 
        public ServerSCLValueRequest(ReadGraph graph, Resource s, Resource o, Resource p) throws DatabaseException {
-           this(getComponentTypeAndRoot(graph, s), o, resolveExpectedValueType(graph, p));
+           this(getComponentTypeAndRoot(graph, s, o), o, resolveExpectedValueType(graph, p));
        }
 
        private static Pair<Resource,Resource> getComponentTypeAndRoot(ReadGraph graph, Variable property)  throws DatabaseException {
@@ -88,7 +89,7 @@ public class ServerSCLValueRequest extends AbstractExpressionCompilationRequest<
                return Pair.make(parent.getType(graph), root);
        }
 
-       private static Pair<Resource,Resource> getComponentTypeAndRoot(ReadGraph graph, Resource component)  throws DatabaseException {
+       private static Pair<Resource,Resource> getComponentTypeAndRoot(ReadGraph graph, Resource component, Resource literal)  throws DatabaseException {
                if(component != null) {
                        Resource type = graph.syncRequest(new FindPossibleComponentTypeRequest(component));
                        if(type != null) {
@@ -108,8 +109,14 @@ public class ServerSCLValueRequest extends AbstractExpressionCompilationRequest<
                                        return Pair.make(null, root);
                                }
                        }
-               }
-               throw new IllegalStateException();
+        // TODO: For Antti to consider and fix later
+        // Introduced to handle procedural user components where component == null
+        } else if (literal != null) {
+            Resource root = graph.syncRequest(new IndexRoot(literal));
+            return Pair.make(null, root);
+        } else {
+            throw new DatabaseException("Couldn't resolve component type and root for component == null && literal == null");
+        }
        }
 
        public static Object compileAndEvaluate(ReadGraph graph, Variable context) throws DatabaseException {
@@ -160,9 +167,16 @@ public class ServerSCLValueRequest extends AbstractExpressionCompilationRequest<
                        public CompilationContext perform(ReadGraph graph)
                                        throws DatabaseException {
                                RuntimeEnvironment runtimeEnvironment = graph.syncRequest(getRuntimeEnvironmentRequest(parameter.first, parameter.second));
-                               Map<String, ComponentTypeProperty> propertyMap =
+                               Map<String, ComponentTypeProperty> propertyMap;
+                               if (parameter.first != null) {
+                                   propertyMap =
                                                graph.syncRequest(new ReadComponentTypeInterfaceRequest(parameter.first, runtimeEnvironment.getEnvironment()),
                                                                TransientCacheListener.<Map<String, ComponentTypeProperty>>instance());
+                               } else {
+                                   // TODO: Antti to consider
+                                   // To handle procedural user components
+                                   propertyMap = Collections.emptyMap();
+                               }
                                return new CompilationContext(runtimeEnvironment, propertyMap);
                        }
                });
index a93cbe8d4237abc857aa5bbbaa742d6c9b2b7929..6ac607e4f3730b671d2a37289eea211efe1985a5 100644 (file)
@@ -2,6 +2,7 @@ package org.simantics.structural2.scl;
 
 import gnu.trove.map.hash.THashMap;
 
+import java.util.Collections;
 import java.util.Map;
 
 import org.simantics.databoard.Bindings;
@@ -50,11 +51,16 @@ public class ReadComponentTypeInterfaceRequest extends ResourceRead<Map<String,
        @Override
        public Map<String, ComponentTypeProperty> perform(ReadGraph graph)
                        throws DatabaseException {
-               
+               if (resource == null)
+                  return Collections.emptyMap();
+
                THashMap<String, ComponentTypeProperty> result = 
                                new THashMap<String, ComponentTypeProperty>();
                
-               collect(graph, resource, result);
+               // TODO: For Antti to consider
+               // resource == null with procedural user components
+               if (resource != null)
+                   collect(graph, resource, result);
                //for(Resource t : graph.getSupertypes(resource)) collect(graph, t, result);
                
                return result;
index 7257ca4c5c137a5f8dc88fbc4bf7ca7d0e78c0cc..ecafac42f6eba7e7c4b78b18419753f93ea717ba 100644 (file)
@@ -93,13 +93,14 @@ public class FixedConnection implements Connection, Connection2 {
         for (Pair<String, Resource> cp : cps) {
             Variable component = cp.first == null ? procedural : procedural.getChild(graph, cp.first);
             Variable cp2 = component.getPossibleProperty(graph, cp.second);
-            if (cp2 != null)
+            if (cp2 != null) {
                 for (VariableConnectionPointDescriptor desc : ConnectionBrowser.flatten(graph, component, cp.second,
                         relationType)) {
                     result.add(desc.getURI(graph));
                 }
-            else
-                LOGGER.warn("no cp " + cp.first + " for " + component.getURI(graph));
+            } else {
+                logWarn(graph, cp, component, procedural);
+            }
         }
         return result;
     }
@@ -107,14 +108,15 @@ public class FixedConnection implements Connection, Connection2 {
     @Override
     public Collection<VariableConnectionPointDescriptor> getConnectionPointDescriptors(ReadGraph graph,
             Resource relationType) throws DatabaseException {
-        Set<VariableConnectionPointDescriptor> result = new THashSet<VariableConnectionPointDescriptor>();
+        Set<VariableConnectionPointDescriptor> result = new THashSet<>();
         for (Pair<String, Resource> cp : cps) {
             Variable component = cp.first == null ? procedural : procedural.getChild(graph, cp.first);
             Variable cp2 = component.getPossibleProperty(graph, cp.second);
-            if (cp2 != null)
+            if (cp2 != null) {
                 result.addAll(ConnectionBrowser.flatten(graph, component, cp.second, relationType));
-            else
-                LOGGER.warn("no cp " + cp.first + " for " + component.getURI(graph));
+            } else {
+                logWarn(graph, cp, component, procedural);
+            }
         }
         return result;
     }
@@ -153,14 +155,16 @@ public class FixedConnection implements Connection, Connection2 {
     @Override
     public Collection<VariableConnectionPointDescriptor> getConnectionPointDescriptors(ReadGraph graph,
             Variable component, Resource relationType) throws DatabaseException {
-        Set<VariableConnectionPointDescriptor> result = new THashSet<VariableConnectionPointDescriptor>();
+        Set<VariableConnectionPointDescriptor> result = new THashSet<>();
+        Variable procedural = component.getParent(graph);
         for (Pair<String, Resource> cp : cps) {
-            Variable base = cp.first == null ? component.getParent(graph) : component;
+            Variable base = cp.first == null ? procedural : procedural.getChild(graph, cp.first);
             Variable cp2 = base.getPossibleProperty(graph, cp.second);
-            if (cp2 != null)
+            if (cp2 != null) {
                 result.addAll(ConnectionBrowser.flatten(graph, base, cp.second, relationType));
-            else
-                LOGGER.warn("no cp " + cp.first + " for " + base.getURI(graph));
+            } else {
+                logWarn(graph, cp, base, procedural);
+            }
         }
         return result;
     }
@@ -168,17 +172,19 @@ public class FixedConnection implements Connection, Connection2 {
     @Override
     public Collection<Variable> getConnectionPoints(ReadGraph graph, Variable component, Resource relationType)
             throws DatabaseException {
-        Set<Variable> result = new THashSet<Variable>();
+        Set<Variable> result = new THashSet<>();
+        Variable procedural = component.getParent(graph);
         for (Pair<String, Resource> cp : cps) {
-            Variable base = cp.first == null ? component.getParent(graph) : component;
+            Variable base = cp.first == null ? procedural : procedural.getChild(graph, cp.first);
             Variable cp2 = base.getPossibleProperty(graph, cp.second);
-            if (cp2 != null)
+            if (cp2 != null) {
                 for (VariableConnectionPointDescriptor desc : ConnectionBrowser.flatten(graph, base, cp.second,
                         relationType)) {
                     result.add(desc.getVariable(graph));
                 }
-            else
-                LOGGER.warn("no cp " + cp.first + " for " + base.getURI(graph));
+            } else {
+                logWarn(graph, cp, base, procedural);
+            }
         }
         return result;
     }
@@ -186,17 +192,19 @@ public class FixedConnection implements Connection, Connection2 {
     @Override
     public Collection<String> getConnectionPointURIs(ReadGraph graph, Variable component, Resource relationType)
             throws DatabaseException {
-        Set<String> result = new THashSet<String>();
+        Set<String> result = new THashSet<>();
+        Variable procedural = component.getParent(graph);
         for (Pair<String, Resource> cp : cps) {
-            Variable base = cp.first == null ? component.getParent(graph) : component;
+            Variable base = cp.first == null ? procedural : procedural.getChild(graph, cp.first);
             Variable cp2 = base.getPossibleProperty(graph, cp.second);
-            if (cp2 != null)
+            if (cp2 != null) {
                 for (VariableConnectionPointDescriptor desc : ConnectionBrowser.flatten(graph, base, cp.second,
                         relationType)) {
                     result.add(desc.getURI(graph));
                 }
-            else
-                LOGGER.warn("no cp " + cp.first + " for " + base.getURI(graph));
+            } else {
+                logWarn(graph, cp, base, procedural);
+            }
         }
         return result;
     }
@@ -206,4 +214,11 @@ public class FixedConnection implements Connection, Connection2 {
         return this;
     }
 
+    private static void logWarn(ReadGraph graph, Pair<String, Resource> cp, Variable base, Variable procedural) throws DatabaseException {
+        LOGGER.warn("no cp " + cp.first + " for " + base.getURI(graph));
+        LOGGER.warn("    proc: " + procedural.getURI(graph));
+        LOGGER.warn("    rel: " + graph.getURI(cp.second));
+        LOGGER.warn("    base: " + base.getURI(graph));
+    }
+
 }
\ No newline at end of file