]> gerrit.simantics Code Review - simantics/platform.git/commitdiff
UCs can have their own SCLModule definitions 78/178/3
authorMiro Richard Eklund <miro.eklund@semantum.fi>
Mon, 28 Nov 2016 09:15:15 +0000 (11:15 +0200)
committerTuukka Lehtonen <tuukka.lehtonen@semantum.fi>
Mon, 28 Nov 2016 10:27:01 +0000 (12:27 +0200)
User Components now see their own SCLModule definitions without
including them to the index roots SCLMain. Makes UCs more individual and
eases the usage of them.

refs #6514

Change-Id: I55e5bc78a1dc93382d9ca8d3ea78837baa59fe28

bundles/org.simantics.db.layer0/src/org/simantics/db/layer0/util/RuntimeEnvironmentRequest2.java [new file with mode: 0644]
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
bundles/org.simantics.structural2/src/org/simantics/structural2/scl/AbstractCompileStructuralValueRequest.java

diff --git a/bundles/org.simantics.db.layer0/src/org/simantics/db/layer0/util/RuntimeEnvironmentRequest2.java b/bundles/org.simantics.db.layer0/src/org/simantics/db/layer0/util/RuntimeEnvironmentRequest2.java
new file mode 100644 (file)
index 0000000..a019461
--- /dev/null
@@ -0,0 +1,166 @@
+package org.simantics.db.layer0.util;\r
+\r
+import java.util.Collection;\r
+import java.util.HashMap;\r
+import java.util.Map;\r
+\r
+import org.simantics.db.ReadGraph;\r
+import org.simantics.db.Resource;\r
+import org.simantics.db.common.request.BinaryRead;\r
+import org.simantics.db.common.request.ObjectsWithType;\r
+import org.simantics.db.common.request.ParametrizedPrimitiveRead;\r
+import org.simantics.db.exception.DatabaseException;\r
+import org.simantics.db.layer0.internal.SimanticsInternal;\r
+import org.simantics.db.procedure.Listener;\r
+import org.simantics.db.request.Read;\r
+import org.simantics.layer0.Layer0;\r
+import org.simantics.scl.compiler.environment.specification.EnvironmentSpecification;\r
+import org.simantics.scl.compiler.module.repository.ImportFailureException;\r
+import org.simantics.scl.compiler.module.repository.UpdateListener;\r
+import org.simantics.scl.compiler.runtime.RuntimeEnvironment;\r
+import org.simantics.scl.osgi.SCLOsgi;\r
+import org.simantics.scl.runtime.SCLContext;\r
+\r
+/**\r
+ * Finds the runtime environment of a model or other index root.\r
+ * \r
+ * @author Hannu Niemist&ouml;\r
+ * @author Antti Villberg\r
+ */\r
+public class RuntimeEnvironmentRequest2 extends BinaryRead<Resource, Resource, RuntimeEnvironment> {\r
+\r
+    public RuntimeEnvironmentRequest2(Resource parameter, Resource parameter2) {\r
+        super(parameter, parameter2);\r
+    }\r
+    \r
+    protected void fillEnvironmentSpecification(EnvironmentSpecification environmentSpecification) {\r
+    }\r
+\r
+    static class UpdateListenerImpl implements UpdateListener {\r
+          \r
+     final EnvironmentSpecification environmentSpecification;\r
+     final Listener<RuntimeEnvironment> callback;\r
+     \r
+     UpdateListenerImpl(EnvironmentSpecification environmentSpecification, Listener<RuntimeEnvironment> callback) {\r
+      this.environmentSpecification = environmentSpecification;\r
+      this.callback = callback;\r
+     }\r
+\r
+        @Override\r
+        public void notifyAboutUpdate() {\r
+         if(callback.isDisposed()) {\r
+          return;\r
+         }\r
+         getRuntimeEnvironment(environmentSpecification, callback, this);\r
+        }\r
+\r
+     final public static void getRuntimeEnvironment(EnvironmentSpecification environmentSpecification, Listener<RuntimeEnvironment> callback, UpdateListenerImpl listener) {\r
+\r
+   try {\r
+    \r
+          SCLContext context = SCLContext.getCurrent();\r
+          \r
+          RuntimeEnvironment env;\r
+          Object graph = context.get("graph");\r
+          if(graph == null)\r
+                    try {\r
+                        env = SimanticsInternal.getSession().syncRequest(new Read<RuntimeEnvironment>() {\r
+                            @Override\r
+                            public RuntimeEnvironment perform(ReadGraph graph) throws DatabaseException {\r
+                             \r
+                                SCLContext sclContext = SCLContext.getCurrent();\r
+                             Object oldGraph = sclContext.get("graph");\r
+                                try {\r
+                                 sclContext.put("graph", graph);\r
+                                    return SCLOsgi.MODULE_REPOSITORY.createRuntimeEnvironment(\r
+                                            environmentSpecification,\r
+                                            callback.getClass().getClassLoader(), listener);\r
+                                } catch (ImportFailureException e) {\r
+                                    throw new DatabaseException(e);\r
+                                } catch (Throwable t) {\r
+                                    throw new DatabaseException(t);\r
+                                } finally {\r
+                                    sclContext.put("graph", oldGraph);\r
+                                }\r
+                            }\r
+                        });\r
+                    } catch (DatabaseException e) {\r
+                        callback.exception(e);\r
+                        return;\r
+                    }\r
+                else \r
+              env = SCLOsgi.MODULE_REPOSITORY.createRuntimeEnvironment(\r
+                      environmentSpecification,\r
+                      callback.getClass().getClassLoader(), listener);\r
+          callback.execute(env);\r
+   } catch (ImportFailureException e) {\r
+    callback.exception(new DatabaseException(e));\r
+   }\r
+\r
+     }\r
+        \r
+    };     \r
+\r
+    // This is needed to prevent garbage collection from collecting UpdateListenerImpls\r
+    // -ModuleRepository only makes a weak reference to the listener\r
+    final static Map<EnvironmentSpecification, UpdateListenerImpl> map = new HashMap<>(); \r
+    \r
+    @Override\r
+    public RuntimeEnvironment perform(ReadGraph graph)\r
+            throws DatabaseException {\r
+        final EnvironmentSpecification environmentSpecification = EnvironmentSpecification.of(\r
+                "Builtin", "",\r
+                "Prelude", "",\r
+                "Simantics/All", "");\r
+        fillEnvironmentSpecification(environmentSpecification);\r
+        \r
+        Layer0 L0 = Layer0.getInstance(graph);\r
+        Collection<Resource> sclModules = graph.syncRequest(new ObjectsWithType(parameter, L0.ConsistsOf, L0.SCLModule));\r
+        for (Resource sclModule : sclModules)\r
+            environmentSpecification.importModule(graph.getURI(sclModule), "");\r
+        \r
+        Resource mainModule = Layer0Utils.getPossibleChild(graph, parameter2, "SCLMain");\r
+        if(mainModule != null)\r
+            environmentSpecification.importModule(graph.getURI(mainModule), "");\r
+        \r
+        return graph.syncRequest(new ParametrizedPrimitiveRead<EnvironmentSpecification, RuntimeEnvironment>(environmentSpecification) {\r
+         \r
+         @Override\r
+         public void register(ReadGraph graph, Listener<RuntimeEnvironment> procedure) {\r
+\r
+          SCLContext context = SCLContext.getCurrent();\r
+          Object oldGraph = context.put("graph", graph);\r
+          try {\r
+\r
+           if(procedure.isDisposed()) {\r
+            UpdateListenerImpl.getRuntimeEnvironment(parameter, procedure, null);\r
+           } else {\r
+            UpdateListenerImpl impl = new UpdateListenerImpl(parameter, procedure);\r
+            impl.notifyAboutUpdate();\r
+            map.put(parameter, impl);\r
+           }\r
+\r
+          } finally {\r
+           context.put("graph", oldGraph);\r
+          }\r
+\r
+         }\r
+            \r
+            @Override\r
+            public void unregistered() {\r
+             map.remove(parameter);\r
+            }\r
+            \r
+        });\r
+    }\r
+    \r
+    @Override\r
+    public int hashCode() {\r
+        return 31*getClass().hashCode() + super.hashCode();\r
+    }\r
+\r
+    public static void flush() {\r
+        map.clear();\r
+    }\r
+\r
+}
\ No newline at end of file
index ca82be6c94ee53d624fdcb539573186602cb962e..12a4e017a3bffc3ff60253befba71b98ddd7fc8c 100644 (file)
@@ -13,7 +13,7 @@ import org.simantics.db.exception.DatabaseException;
 import org.simantics.db.layer0.request.VariableRead;\r
 import org.simantics.db.layer0.scl.AbstractExpressionCompilationContext;\r
 import org.simantics.db.layer0.scl.AbstractExpressionCompilationRequest;\r
-import org.simantics.db.layer0.util.RuntimeEnvironmentRequest;\r
+import org.simantics.db.layer0.util.RuntimeEnvironmentRequest2;\r
 import org.simantics.db.layer0.variable.Variable;\r
 import org.simantics.document.server.Functions;\r
 import org.simantics.document.server.bean.DataDefinition;\r
@@ -120,8 +120,8 @@ public class ServerSCLHandlerValueRequest extends AbstractExpressionCompilationR
                return "\\context -> " + exp;\r
        }\r
 \r
-       protected RuntimeEnvironmentRequest getRuntimeEnvironmentRequest(Resource indexRoot) {\r
-               return new RuntimeEnvironmentRequest(indexRoot) {\r
+       protected RuntimeEnvironmentRequest2 getRuntimeEnvironmentRequest(Resource componentType, Resource indexRoot) {\r
+               return new RuntimeEnvironmentRequest2(componentType, indexRoot) {\r
                        @Override\r
                        protected void fillEnvironmentSpecification(\r
                                        EnvironmentSpecification environmentSpecification) {\r
@@ -138,7 +138,7 @@ public class ServerSCLHandlerValueRequest extends AbstractExpressionCompilationR
                        public CompilationContext perform(ReadGraph graph) throws DatabaseException {\r
                                \r
                                Pair<Resource,Resource> parameter = getComponentTypeAndRoot(graph, variable);\r
-                               RuntimeEnvironment runtimeEnvironment = graph.syncRequest(getRuntimeEnvironmentRequest(parameter.second));\r
+                               RuntimeEnvironment runtimeEnvironment = graph.syncRequest(getRuntimeEnvironmentRequest(parameter.first, parameter.second));\r
                                \r
                                Map<String, ComponentTypeProperty> propertyMap =\r
                                                graph.syncRequest(new ReadComponentTypeInterfaceRequest(parameter.first, runtimeEnvironment.getEnvironment()),\r
index 246acfdec72236a9640e7c3b3f0d27ded78b29f8..d9c5a0103c1ab20f121a917e5f7cfe9fd090a5bf 100644 (file)
@@ -12,6 +12,7 @@ import org.simantics.db.exception.DatabaseException;
 import org.simantics.db.layer0.scl.AbstractExpressionCompilationContext;\r
 import org.simantics.db.layer0.scl.AbstractExpressionCompilationRequest;\r
 import org.simantics.db.layer0.util.RuntimeEnvironmentRequest;\r
+import org.simantics.db.layer0.util.RuntimeEnvironmentRequest2;\r
 import org.simantics.db.layer0.variable.Variable;\r
 import org.simantics.document.server.request.ServerSCLValueRequest.CompilationContext;\r
 import org.simantics.layer0.Layer0;\r
@@ -100,8 +101,8 @@ public class ServerSCLValueRequest extends AbstractExpressionCompilationRequest<
                return graph.getRelatedValue(literal, L0.SCLValue_expression, Bindings.STRING);\r
        }\r
 \r
-       protected RuntimeEnvironmentRequest getRuntimeEnvironmentRequest(Resource indexRoot) {\r
-               return new RuntimeEnvironmentRequest(indexRoot) {\r
+       protected RuntimeEnvironmentRequest2 getRuntimeEnvironmentRequest(Resource componentType, Resource indexRoot) {\r
+               return new RuntimeEnvironmentRequest2(componentType, indexRoot) {\r
                        @Override\r
                        protected void fillEnvironmentSpecification(\r
                                        EnvironmentSpecification environmentSpecification) {\r
@@ -116,7 +117,7 @@ public class ServerSCLValueRequest extends AbstractExpressionCompilationRequest<
                        @Override\r
                        public CompilationContext perform(ReadGraph graph)\r
                                        throws DatabaseException {\r
-                               RuntimeEnvironment runtimeEnvironment = graph.syncRequest(getRuntimeEnvironmentRequest(parameter.second));\r
+                               RuntimeEnvironment runtimeEnvironment = graph.syncRequest(getRuntimeEnvironmentRequest(parameter.first, parameter.second));\r
                                Map<String, ComponentTypeProperty> propertyMap =\r
                                                graph.syncRequest(new ReadComponentTypeInterfaceRequest(parameter.first, runtimeEnvironment.getEnvironment()),\r
                                                                TransientCacheListener.<Map<String, ComponentTypeProperty>>instance());\r
index e369050e906e0f5dd422e150d47c6cffdc90c2ad..7a6b45e3ccf2c533866ae17563117d599cab16fb 100644 (file)
@@ -12,6 +12,7 @@ import org.simantics.db.exception.DatabaseException;
 import org.simantics.db.layer0.scl.AbstractExpressionCompilationContext;\r
 import org.simantics.db.layer0.scl.AbstractExpressionCompilationRequest;\r
 import org.simantics.db.layer0.util.RuntimeEnvironmentRequest;\r
+import org.simantics.db.layer0.util.RuntimeEnvironmentRequest2;\r
 import org.simantics.db.layer0.variable.Variable;\r
 import org.simantics.layer0.Layer0;\r
 import org.simantics.scl.compiler.elaboration.expressions.EApply;\r
@@ -85,7 +86,7 @@ abstract public class AbstractCompileStructuralValueRequest extends AbstractExpr
                    @Override\r
                    public CompilationContext perform(ReadGraph graph)\r
                            throws DatabaseException {\r
-                       RuntimeEnvironment runtimeEnvironment = graph.syncRequest(new RuntimeEnvironmentRequest(resource2));\r
+                       RuntimeEnvironment runtimeEnvironment = graph.syncRequest(new RuntimeEnvironmentRequest2(resource, resource2));\r
                        Map<String, ComponentTypeProperty> propertyMap =\r
                                graph.syncRequest(new ReadComponentTypeInterfaceRequest(resource, runtimeEnvironment.getEnvironment()),\r
                                        TransientCacheListener.<Map<String, ComponentTypeProperty>>instance());\r