]> gerrit.simantics Code Review - simantics/platform.git/blobdiff - bundles/org.simantics.modeling/src/org/simantics/modeling/scl/CompileSCLMonitorRequest.java
Migrated source code from Simantics SVN
[simantics/platform.git] / bundles / org.simantics.modeling / src / org / simantics / modeling / scl / CompileSCLMonitorRequest.java
diff --git a/bundles/org.simantics.modeling/src/org/simantics/modeling/scl/CompileSCLMonitorRequest.java b/bundles/org.simantics.modeling/src/org/simantics/modeling/scl/CompileSCLMonitorRequest.java
new file mode 100644 (file)
index 0000000..9c371b6
--- /dev/null
@@ -0,0 +1,161 @@
+package org.simantics.modeling.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.common.request.ResourceRead;\r
+import org.simantics.db.exception.DatabaseException;\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.variable.Variable;\r
+import org.simantics.layer0.Layer0;\r
+import org.simantics.modeling.ComponentTypeSubstructure;\r
+import org.simantics.modeling.scl.CompileSCLMonitorRequest.CompilationContext;\r
+import org.simantics.scl.compiler.common.names.Name;\r
+import org.simantics.scl.compiler.constants.StringConstant;\r
+import org.simantics.scl.compiler.elaboration.expressions.EApply;\r
+import org.simantics.scl.compiler.elaboration.expressions.EConstant;\r
+import org.simantics.scl.compiler.elaboration.expressions.ELiteral;\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.Environment;\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
+import org.simantics.utils.datastructures.Pair;\r
+\r
+\r
+public class CompileSCLMonitorRequest extends AbstractExpressionCompilationRequest<CompilationContext, Variable> {\r
+    \r
+    protected static Name BROWSE = Name.create("Simantics/Variables", "browse");\r
+    protected static Name VALUE = Name.create("Simantics/Variables", "value");\r
+    \r
+    private final Resource componentType;\r
+    private final Resource literal;\r
+    private final Resource relation;\r
+    \r
+    public static class CompilationContext extends AbstractExpressionCompilationContext {\r
+        public final ComponentTypeSubstructure substructure;\r
+        \r
+        public CompilationContext(RuntimeEnvironment runtimeEnvironment,\r
+                ComponentTypeSubstructure substructure) {\r
+            super(runtimeEnvironment);\r
+            this.substructure = substructure;\r
+        }\r
+    }\r
+    \r
+    private CompileSCLMonitorRequest(Resource componentType, Resource literal, Resource relation) {\r
+        this.componentType = componentType;\r
+        this.literal = literal;\r
+        this.relation = relation;\r
+    }\r
+    \r
+    public CompileSCLMonitorRequest(ReadGraph graph, Variable context)\r
+            throws DatabaseException {\r
+        this(context.getParent(graph).getType(graph),\r
+                context.getRepresents(graph),\r
+                context.getPredicateResource(graph));\r
+    }\r
+\r
+    public static Object compileAndEvaluate(ReadGraph graph, Variable context) throws DatabaseException {\r
+        SCLContext sclContext = SCLContext.getCurrent();\r
+        Object oldGraph = sclContext.get("graph");\r
+        try {\r
+            Function1<Variable,Object> exp = graph.syncRequest(new CompileSCLMonitorRequest(graph, context),\r
+                    TransientCacheListener.<Function1<Variable,Object>>instance());\r
+            sclContext.put("graph", graph);\r
+            return exp.apply(context.getParent(graph));\r
+        } catch (DatabaseException e) {\r
+            e.printStackTrace();\r
+            throw (DatabaseException)e;\r
+        } catch (Throwable t) {\r
+            t.printStackTrace();\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
+    @Override\r
+    protected Type getExpectedType(ReadGraph graph, CompilationContext context)\r
+            throws DatabaseException {\r
+        Layer0 L0 = Layer0.getInstance(graph);\r
+        String valueType = graph.getPossibleRelatedValue(relation, L0.RequiresValueType, 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
+        return graph.syncRequest(new ResourceRead<CompilationContext>(componentType) {\r
+            @Override\r
+            public CompilationContext perform(ReadGraph graph)\r
+                    throws DatabaseException {\r
+                Resource indexRoot = graph.syncRequest(new IndexRoot(resource));\r
+                RuntimeEnvironment runtimeEnvironment = graph.syncRequest(new RuntimeEnvironmentRequest(indexRoot));\r
+                return new CompilationContext(runtimeEnvironment, ComponentTypeSubstructure.forType(graph, resource));\r
+            }\r
+        });\r
+    }\r
+\r
+    @Override\r
+    protected Type getContextVariableType() {\r
+        return VARIABLE;\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
+        Pair<String,Type> entry = context.substructure.possibleTypedRVI(name);\r
+        if(entry == null)\r
+            return null;\r
+        Environment environment = context.runtimeEnvironment.getEnvironment();\r
+        Expression propertyVariable = new EApply(\r
+                new EConstant(environment.getValue(BROWSE)),\r
+                new EVariable(contextVariable),\r
+                new ELiteral(new StringConstant(entry.first))\r
+                );\r
+        return makeTypeFlexible(environment, new EApply(\r
+                new EConstant(environment.getValue(VALUE), entry.second),\r
+                propertyVariable\r
+                ), entry.second);\r
+    }\r
+    \r
+    @Override\r
+    public int hashCode() {\r
+        return 31*(31*getClass().hashCode() + literal.hashCode()) + componentType.hashCode();\r
+    }\r
+    \r
+    @Override\r
+    public boolean equals(Object obj) {\r
+        if(this == obj)\r
+            return true;\r
+        if(obj == null || obj.getClass() != getClass())\r
+            return false;\r
+        CompileSCLMonitorRequest other = (CompileSCLMonitorRequest)obj;\r
+        return literal.equals(other.literal) && componentType.equals(other.componentType);\r
+    }\r
+}\r