Merge "Use proper environment to resolve SCL types in ontology module"
authorHannu Niemistö <hannu.niemisto@semantum.fi>
Wed, 10 Oct 2018 06:53:07 +0000 (06:53 +0000)
committerGerrit Code Review <gerrit2@simantics>
Wed, 10 Oct 2018 06:53:07 +0000 (06:53 +0000)
bundles/org.simantics.db.impl/src/org/simantics/db/impl/graph/ReadGraphImpl.java
bundles/org.simantics.db.layer0/src/org/simantics/db/layer0/util/RuntimeEnvironmentRequest.java
bundles/org.simantics.db.layer0/src/org/simantics/db/layer0/util/RuntimeEnvironmentRequest2.java
bundles/org.simantics.scl.compiler/src/org/simantics/scl/compiler/types/Types.java

index 77af46d557e740e8d73e4bc9c9252b2adb7e7a98..3feb22e00ebda688379b6ed7c8785b09fb8035ca 100644 (file)
@@ -18,7 +18,6 @@ import java.io.IOException;
 import java.io.PrintStream;
 import java.lang.reflect.Array;
 import java.lang.reflect.InvocationTargetException;
-import java.nio.BufferUnderflowException;
 import java.util.ArrayList;
 import java.util.Collection;
 import java.util.Collections;
@@ -135,7 +134,6 @@ import org.simantics.db.exception.NoSingleResultException;
 import org.simantics.db.exception.ResourceNotFoundException;
 import org.simantics.db.exception.ServiceException;
 import org.simantics.db.exception.ValidationException;
-import org.simantics.db.impl.BlockingAsyncProcedure;
 import org.simantics.db.impl.RelationContextImpl;
 import org.simantics.db.impl.ResourceImpl;
 import org.simantics.db.impl.internal.RandomAccessValueSupport;
@@ -1107,9 +1105,10 @@ public class ReadGraphImpl implements AsyncReadGraph {
 
                assert (subject != null);
 
+               byte[] bytes = null;
                try {
                        
-                       byte[] bytes = processor.getValue(this, subject);
+                       bytes = processor.getValue(this, subject);
                        if (bytes == null) throw new DoesNotContainValueException("No value for resource " + subject);
 
                        Serializer serializer = getSerializer(binding);
@@ -1119,20 +1118,9 @@ public class ReadGraphImpl implements AsyncReadGraph {
 
                        throw new DoesNotContainValueException(e);
 
-               } catch (IOException e) {
-
-                       throw new ServiceException(e);
-
-               } catch (DatabaseException e) {
-                   
-            throw new ServiceException(e);
-            
-        } catch (BufferUnderflowException e) {
-            // This is sometimes thrown when deserialize fails because wrong format.
-            // For callers of this method this is just an service exception.
-            throw new ServiceException(e);
-        }
-
+               } catch (Throwable t) {
+                       throw new ServiceException("Could not getValue for subject " + debugString(subject) + " and binding " + String.valueOf(binding) + " with bytes " + safeArrayToString(bytes), t);
+               }
        }
 
        @Override
@@ -3544,14 +3532,9 @@ public class ReadGraphImpl implements AsyncReadGraph {
 //                                     else
                                                procedure.execute(graph, (T) obj);
 
-                               } catch (IOException e) {
-                                       procedure.exception(graph, e);
-                               } catch (BufferUnderflowException e) {
-                                       procedure.exception(graph, e);
                                } catch (Throwable t) {
-                                       procedure.exception(graph, t);
+                                   procedure.exception(graph, new ServiceException("Could not forValue for subject " + debugString(resource) + " and binding " + String.valueOf(binding) + " with bytes " + safeArrayToString(result), t));
                                }
-
                        }
 
                        @Override
@@ -3571,6 +3554,24 @@ public class ReadGraphImpl implements AsyncReadGraph {
                });
 
        }
+       
+    private static String safeArrayToString(byte[] a) {
+        if (a == null)
+            return "null";
+        int iMax = a.length - 1;
+        if (iMax == -1)
+            return "[]";
+
+        StringBuilder b = new StringBuilder();
+        b.append('[');
+        for (int i = 0; i < 100; i++) { // limit to first 100 items 
+            b.append(a[i]);
+            if (i == iMax)
+                return b.append(']').toString();
+            b.append(", ");
+        }
+        return b.append(", ... (" + a.length + ")]").toString();
+    }
 
        @Override
        public <T> void forValue(Resource subject, Binding binding,
@@ -4265,14 +4266,9 @@ public class ReadGraphImpl implements AsyncReadGraph {
                                        else
                                                procedure.execute(graph, (T) obj);
 
-                               } catch (IOException e) {
-                                       procedure.exception(graph, e);
-                               } catch (BufferUnderflowException e) {
-                                       procedure.exception(graph, e);
                                } catch (Throwable t) {
-                                       procedure.exception(graph, t);
+                                       procedure.exception(graph, new ServiceException("Could not forValue for subject " + debugString(resource) + " and binding " + String.valueOf(binding) + " with bytes " + safeArrayToString(result), t));
                                }
-
                        }
 
                        @Override
index cde4e02396a48515a31ca777e1265870e8903cc8..9f6b8fe530799439bcad7f3d9fab82319fc2733a 100644 (file)
@@ -26,36 +26,36 @@ public class RuntimeEnvironmentRequest extends UnaryRead<Resource, RuntimeEnviro
     public RuntimeEnvironmentRequest(Resource parameter) {
         super(parameter);
     }
-    
+
     protected void fillEnvironmentSpecification(EnvironmentSpecification environmentSpecification) {
     }
 
     static class UpdateListenerImpl extends UpdateListener {
-               
-       final EnvironmentSpecification environmentSpecification;
-       final Listener<RuntimeEnvironment> callback;
-       
-       UpdateListenerImpl(EnvironmentSpecification environmentSpecification, Listener<RuntimeEnvironment> callback) {
-               this.environmentSpecification = environmentSpecification;
-               this.callback = callback;
-       }
+
+        final EnvironmentSpecification environmentSpecification;
+        final Listener<RuntimeEnvironment> callback;
+
+        UpdateListenerImpl(EnvironmentSpecification environmentSpecification, Listener<RuntimeEnvironment> callback) {
+            this.environmentSpecification = environmentSpecification;
+            this.callback = callback;
+        }
 
         @Override
         public void notifyAboutUpdate() {
-               if(callback.isDisposed()) {
-                   stopListening();
-                       return;
-               }
-               getRuntimeEnvironment(environmentSpecification, callback, this);
+            if(callback.isDisposed()) {
+                stopListening();
+                return;
+            }
+            getRuntimeEnvironment(environmentSpecification, callback, this);
         }
-    };     
+    };
 
     public static void getRuntimeEnvironment(EnvironmentSpecification environmentSpecification, Listener<RuntimeEnvironment> callback, UpdateListenerImpl listener) {
 
         try {
-            
+
             SCLContext context = SCLContext.getCurrent();
-            
+
             RuntimeEnvironment env;
             Object graph = context.get("graph");
             if(graph == null)
@@ -63,7 +63,7 @@ public class RuntimeEnvironmentRequest extends UnaryRead<Resource, RuntimeEnviro
                     env = SimanticsInternal.getSession().syncRequest(new Read<RuntimeEnvironment>() {
                         @Override
                         public RuntimeEnvironment perform(ReadGraph graph) throws DatabaseException {
-                            
+
                             SCLContext sclContext = SCLContext.getCurrent();
                             Object oldGraph = sclContext.get("graph");
                             try {
@@ -94,7 +94,7 @@ public class RuntimeEnvironmentRequest extends UnaryRead<Resource, RuntimeEnviro
         }
 
     }
-    
+
     @Override
     public RuntimeEnvironment perform(ReadGraph graph)
             throws DatabaseException {
@@ -103,6 +103,7 @@ public class RuntimeEnvironmentRequest extends UnaryRead<Resource, RuntimeEnviro
                 "StandardLibrary", "",
                 "Simantics/All", "");
         fillEnvironmentSpecification(environmentSpecification);
+
         Resource mainModule = Layer0Utils.getPossibleChild(graph, parameter, "SCLMain");
         String mainModuleUri;
         if(mainModule != null) {
@@ -111,40 +112,40 @@ public class RuntimeEnvironmentRequest extends UnaryRead<Resource, RuntimeEnviro
         }
         else
             mainModuleUri = graph.getURI(parameter) + "/#"; // Add something dummy to the model uri that cannot be in a real URI
-        
-            return graph.syncRequest(new ParametrizedPrimitiveRead<String, RuntimeEnvironment>(mainModuleUri) {
-                
-                UpdateListenerImpl sclListener;
-                
-               @Override
-               public void register(ReadGraph graph, Listener<RuntimeEnvironment> procedure) {
-
-                       SCLContext context = SCLContext.getCurrent();
-                       Object oldGraph = context.put("graph", graph);
-                       try {
-
-                               if(procedure.isDisposed()) {
-                                       getRuntimeEnvironment(environmentSpecification, procedure, null);
-                               } else {
-                                   sclListener = new UpdateListenerImpl(environmentSpecification, procedure);
-                                   sclListener.notifyAboutUpdate();
-                                       }
-
-                       } finally {
-                               context.put("graph", oldGraph);
-                       }
-
-               }
-                
-                @Override
-                public void unregistered() {
-                       if(sclListener != null)
-                           sclListener.stopListening();
+
+        return graph.syncRequest(new ParametrizedPrimitiveRead<String, RuntimeEnvironment>(mainModuleUri) {
+
+            UpdateListenerImpl sclListener;
+
+            @Override
+            public void register(ReadGraph graph, Listener<RuntimeEnvironment> procedure) {
+
+                SCLContext context = SCLContext.getCurrent();
+                Object oldGraph = context.put("graph", graph);
+                try {
+
+                    if(procedure.isDisposed()) {
+                        getRuntimeEnvironment(environmentSpecification, procedure, null);
+                    } else {
+                        sclListener = new UpdateListenerImpl(environmentSpecification, procedure);
+                        sclListener.notifyAboutUpdate();
+                    }
+
+                } finally {
+                    context.put("graph", oldGraph);
                 }
-                
-            });
+
+            }
+
+            @Override
+            public void unregistered() {
+                if(sclListener != null)
+                    sclListener.stopListening();
+            }
+
+        });
     }
-    
+
     @Override
     public int hashCode() {
         return 31*getClass().hashCode() + super.hashCode();
index 4bbd480020141b5e602c9e7f73fb1b8170410995..ca8bc07cd53ab9a3b73d05a02e73d7473ae7db2a 100644 (file)
@@ -1,8 +1,6 @@
 package org.simantics.db.layer0.util;
 
 import java.util.Collection;
-import java.util.HashMap;
-import java.util.Map;
 
 import org.simantics.db.ReadGraph;
 import org.simantics.db.Resource;
@@ -26,13 +24,17 @@ import org.simantics.scl.runtime.SCLContext;
  * 
  * @author Hannu Niemist&ouml;
  * @author Antti Villberg
+ * 
+ * Difference between this class and {@code RuntimeEnvironmentRequest} is an additional parameter
+ * that is typically some component type. All modules under this resource are added to the environment
+ * in addition to the SCLMain of the root resource. 
  */
 public class RuntimeEnvironmentRequest2 extends BinaryRead<Resource, Resource, RuntimeEnvironment> {
 
-    public RuntimeEnvironmentRequest2(Resource parameter, Resource parameter2) {
-        super(parameter, parameter2);
+    public RuntimeEnvironmentRequest2(Resource componentType, Resource indexRoot) {
+        super(componentType, indexRoot);
     }
-    
+
     protected void fillEnvironmentSpecification(EnvironmentSpecification environmentSpecification) {
     }
 
@@ -56,7 +58,7 @@ public class RuntimeEnvironmentRequest2 extends BinaryRead<Resource, Resource, R
         }
     };
 
-    final public static void getRuntimeEnvironment(EnvironmentSpecification environmentSpecification, Listener<RuntimeEnvironment> callback, UpdateListenerImpl listener) {
+    public static void getRuntimeEnvironment(EnvironmentSpecification environmentSpecification, Listener<RuntimeEnvironment> callback, UpdateListenerImpl listener) {
 
         try {
 
@@ -120,7 +122,9 @@ public class RuntimeEnvironmentRequest2 extends BinaryRead<Resource, Resource, R
             environmentSpecification.importModule(graph.getURI(mainModule), "");
 
         return graph.syncRequest(new ParametrizedPrimitiveRead<EnvironmentSpecification, RuntimeEnvironment>(environmentSpecification) {
+
             UpdateListenerImpl sclListener;
+
             @Override
             public void register(ReadGraph graph, Listener<RuntimeEnvironment> procedure) {
 
@@ -155,4 +159,4 @@ public class RuntimeEnvironmentRequest2 extends BinaryRead<Resource, Resource, R
         return 31*getClass().hashCode() + super.hashCode();
     }
 
-}
\ No newline at end of file
+}
index e1b89eece28ab21f14e87e3e2fd20745ec85e7c5..8817eb23c6979c25acefb81f7be30c4d87a8bc7c 100644 (file)
@@ -7,6 +7,7 @@ import java.util.Collections;
 import java.util.List;
 
 import org.simantics.scl.compiler.common.exceptions.InternalCompilerError;
+import org.simantics.scl.compiler.environment.Environments;
 import org.simantics.scl.compiler.errors.Locations;
 import org.simantics.scl.compiler.internal.parsing.exceptions.SCLSyntaxErrorException;
 import org.simantics.scl.compiler.internal.parsing.parser.SCLParserImpl;
@@ -1183,14 +1184,27 @@ public class Types {
         return effect;
     }
 
+    /**
+     * Use {@link Environments#getType(org.simantics.scl.compiler.environment.Environment, String)} instead.
+     */
+    @Deprecated
     public static Type parseType(ITypeEnvironment environment, String text) throws SCLTypeParseException {
         return parseType(new TypeElaborationContext(environment), text);
     }
 
+    /**
+     * This method uses DUMMY_TYPE_ENVIRONMENT that almost does anything useful. Use
+     * {@link Environments#getType(org.simantics.scl.compiler.environment.Environment, String)} instead.
+     */
+    @Deprecated
     public static Type parseType(String text) throws SCLTypeParseException {
         return parseType(new TypeElaborationContext(DUMMY_TYPE_ENVIRONMENT), text);
     }
-    
+
+    /**
+     * Use {@link Environments#getType(org.simantics.scl.compiler.environment.Environment, String)} instead.
+     */
+    @Deprecated
     private static Type parseType(TypeElaborationContext context, String text) throws SCLTypeParseException {
         SCLParserImpl parser = new SCLParserImpl(new StringReader(text));
         try {