]> gerrit.simantics Code Review - simantics/platform.git/blobdiff - bundles/org.simantics.db.layer0/src/org/simantics/db/layer0/scl/CompileResourceValueRequest.java
Fixed multiple issues causing dangling references to discarded queries
[simantics/platform.git] / bundles / org.simantics.db.layer0 / src / org / simantics / db / layer0 / scl / CompileResourceValueRequest.java
index feee7961768850940abba331487a38eb4f6adc4a..cdc787d2683a135985244b31414379a0ac45d791 100644 (file)
-package org.simantics.db.layer0.scl;\r
-\r
-import org.simantics.databoard.Bindings;\r
-import org.simantics.db.ReadGraph;\r
-import org.simantics.db.Resource;\r
-import org.simantics.db.common.procedure.adapter.TransientCacheListener;\r
-import org.simantics.db.common.request.IndexRoot;\r
-import org.simantics.db.exception.DatabaseException;\r
-import org.simantics.db.layer0.scl.CompileResourceValueRequest.CompilationContext;\r
-import org.simantics.db.layer0.util.RuntimeEnvironmentRequest;\r
-import org.simantics.layer0.Layer0;\r
-import org.simantics.scl.compiler.elaboration.expressions.EVariable;\r
-import org.simantics.scl.compiler.elaboration.expressions.Expression;\r
-import org.simantics.scl.compiler.environment.Environments;\r
-import org.simantics.scl.compiler.runtime.RuntimeEnvironment;\r
-import org.simantics.scl.compiler.top.SCLExpressionCompilationException;\r
-import org.simantics.scl.compiler.types.Type;\r
-import org.simantics.scl.runtime.SCLContext;\r
-import org.simantics.scl.runtime.function.Function1;\r
-\r
-/**\r
- * Compiles an SCL expression that is attached to a literal\r
- * whose parent is a component that is a part of a component type.\r
- * \r
- * @author Antti Villberg\r
- */\r
-public class CompileResourceValueRequest extends AbstractExpressionCompilationRequest<CompilationContext, Resource> {\r
-\r
-    public static class CompilationContext extends AbstractExpressionCompilationContext {\r
-        public CompilationContext(RuntimeEnvironment runtimeEnvironment) {\r
-            super(runtimeEnvironment);\r
-        }\r
-    }\r
-\r
-    protected final Resource literal;\r
-\r
-    public CompileResourceValueRequest(Resource literal) {\r
-        this.literal = literal;\r
-    }\r
-\r
-    public static Object compileAndEvaluate(ReadGraph graph, Resource literal) throws DatabaseException {\r
-        SCLContext sclContext = SCLContext.getCurrent();\r
-        Object oldGraph = sclContext.get("graph");\r
-        try {\r
-            Function1<Resource,Object> exp = graph.syncRequest(new CompileResourceValueRequest(literal),\r
-                    TransientCacheListener.<Function1<Resource,Object>>instance());\r
-            sclContext.put("graph", graph);\r
-            return exp.apply(literal);\r
-        } catch (DatabaseException e) {\r
-            throw (DatabaseException)e;\r
-        } catch (Throwable t) {\r
-            throw new DatabaseException(t);\r
-        } finally {\r
-            sclContext.put("graph", oldGraph);\r
-        }\r
-    }\r
-\r
-    @Override\r
-    protected String getExpressionText(ReadGraph graph)\r
-            throws DatabaseException {\r
-        Layer0 L0 = Layer0.getInstance(graph);\r
-        return graph.getRelatedValue(literal, L0.SCLValue_expression, Bindings.STRING);\r
-    }\r
-\r
-    protected Resource getIndexRoot(ReadGraph graph) throws DatabaseException {\r
-        return graph.syncRequest(new IndexRoot(literal));\r
-    }\r
-\r
-    @Override\r
-    protected Type getExpectedType(ReadGraph graph, CompilationContext context)\r
-            throws DatabaseException {\r
-        Layer0 L0 = Layer0.getInstance(graph);\r
-        String valueType = graph.getPossibleRelatedValue(literal, L0.HasValueType, Bindings.STRING);\r
-        if(valueType != null) {\r
-            try {\r
-                return Environments.getType(context.runtimeEnvironment.getEnvironment(), valueType);\r
-            } catch (SCLExpressionCompilationException e) {\r
-                e.printStackTrace();\r
-            }\r
-        }\r
-        return super.getExpectedType(graph, context);\r
-    }\r
-\r
-    @Override\r
-    protected CompilationContext getCompilationContext(ReadGraph graph)\r
-            throws DatabaseException {\r
-        Resource indexRoot = getIndexRoot(graph);\r
-        RuntimeEnvironment runtimeEnvironment = graph.syncRequest(new RuntimeEnvironmentRequest(indexRoot));\r
-        return new CompilationContext(runtimeEnvironment);\r
-    }\r
-\r
-    @Override\r
-    protected Type getContextVariableType() {\r
-        return RESOURCE; \r
-    }\r
-\r
-    @Override\r
-    protected Expression getVariableAccessExpression(\r
-            ReadGraph graph,\r
-            CompilationContext context,\r
-            org.simantics.scl.compiler.elaboration.expressions.Variable contextVariable,\r
-            String name) throws DatabaseException {\r
-        if(name.equals("self"))\r
-            return new EVariable(contextVariable);\r
-        else\r
-            return null;\r
-    }\r
-\r
-}\r
+package org.simantics.db.layer0.scl;
+
+import org.simantics.databoard.Bindings;
+import org.simantics.db.ReadGraph;
+import org.simantics.db.Resource;
+import org.simantics.db.common.procedure.adapter.TransientCacheListener;
+import org.simantics.db.common.request.IndexRoot;
+import org.simantics.db.exception.DatabaseException;
+import org.simantics.db.layer0.scl.CompileResourceValueRequest.CompilationContext;
+import org.simantics.db.layer0.util.RuntimeEnvironmentRequest;
+import org.simantics.layer0.Layer0;
+import org.simantics.scl.compiler.elaboration.expressions.EVariable;
+import org.simantics.scl.compiler.elaboration.expressions.Expression;
+import org.simantics.scl.compiler.environment.Environments;
+import org.simantics.scl.compiler.runtime.RuntimeEnvironment;
+import org.simantics.scl.compiler.top.SCLExpressionCompilationException;
+import org.simantics.scl.compiler.types.Type;
+import org.simantics.scl.runtime.SCLContext;
+import org.simantics.scl.runtime.function.Function1;
+
+/**
+ * Compiles an SCL expression that is attached to a literal
+ * whose parent is a component that is a part of a component type.
+ * 
+ * @author Antti Villberg
+ */
+public class CompileResourceValueRequest extends AbstractExpressionCompilationRequest<CompilationContext, Object> {
+
+    public static class CompilationContext extends AbstractExpressionCompilationContext {
+        public CompilationContext(RuntimeEnvironment runtimeEnvironment) {
+            super(runtimeEnvironment);
+        }
+    }
+
+    protected final Resource literal;
+
+    public CompileResourceValueRequest(Resource literal) {
+        this.literal = literal;
+    }
+
+    public static Object compileAndEvaluate(ReadGraph graph, Resource literal) throws DatabaseException {
+        SCLContext sclContext = SCLContext.getCurrent();
+        Object oldGraph = sclContext.get("graph");
+        try {
+            Function1<Object,Object> exp = graph.syncRequest(new CompileResourceValueRequest(literal),
+                    TransientCacheListener.instance());
+            sclContext.put("graph", graph);
+            return exp.apply(literal);
+        } catch (DatabaseException e) {
+            throw (DatabaseException)e;
+        } catch (Throwable t) {
+            throw new DatabaseException(t);
+        } finally {
+            sclContext.put("graph", oldGraph);
+        }
+    }
+
+    public static Function1<Object,Object> compile(ReadGraph graph, Resource literal) throws DatabaseException {
+        return graph.syncRequest(new CompileResourceValueRequest(literal), TransientCacheListener.instance());
+    }
+
+    @Override
+    protected String getExpressionText(ReadGraph graph)
+            throws DatabaseException {
+        Layer0 L0 = Layer0.getInstance(graph);
+        return graph.getRelatedValue(literal, L0.SCLValue_expression, Bindings.STRING);
+    }
+
+    protected Resource getIndexRoot(ReadGraph graph) throws DatabaseException {
+        return graph.syncRequest(new IndexRoot(literal));
+    }
+
+    @Override
+    protected Type getExpectedType(ReadGraph graph, CompilationContext context)
+            throws DatabaseException {
+        Layer0 L0 = Layer0.getInstance(graph);
+        String valueType = graph.getPossibleRelatedValue(literal, L0.HasValueType, Bindings.STRING);
+        if(valueType != null) {
+            try {
+                return Environments.getType(context.runtimeEnvironment.getEnvironment(), valueType);
+            } catch (SCLExpressionCompilationException e) {
+                e.printStackTrace();
+            }
+        }
+        return super.getExpectedType(graph, context);
+    }
+
+    @Override
+    protected CompilationContext getCompilationContext(ReadGraph graph)
+            throws DatabaseException {
+        Resource indexRoot = getIndexRoot(graph);
+        RuntimeEnvironment runtimeEnvironment = graph.syncRequest(new RuntimeEnvironmentRequest(indexRoot));
+        return new CompilationContext(runtimeEnvironment);
+    }
+
+    @Override
+    protected Type getContextVariableType() {
+        return RESOURCE; 
+    }
+
+    @Override
+    protected Expression getVariableAccessExpression(
+            ReadGraph graph,
+            CompilationContext context,
+            org.simantics.scl.compiler.elaboration.expressions.Variable contextVariable,
+            String name) throws DatabaseException {
+        if(name.equals("self"))
+            return new EVariable(contextVariable);
+        else
+            return null;
+    }
+
+    @Override
+    public int hashCode() {
+        final int prime = 31;
+        int result = 1;
+        result = prime * result + ((literal == null) ? 0 : literal.hashCode());
+        return result;
+    }
+
+    @Override
+    public boolean equals(Object obj) {
+        if (this == obj)
+            return true;
+        if (obj == null)
+            return false;
+        if (getClass() != obj.getClass())
+            return false;
+        CompileResourceValueRequest other = (CompileResourceValueRequest) obj;
+        if (literal == null) {
+            if (other.literal != null)
+                return false;
+        } else if (!literal.equals(other.literal))
+            return false;
+        return true;
+    }
+
+    
+}