--- /dev/null
+package org.simantics.modeling.scl;\r
+\r
+import java.util.Map;\r
+\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.ResourceRead2;\r
+import org.simantics.db.exception.DatabaseException;\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.scl.compiler.common.names.Name;\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.EVariable;\r
+import org.simantics.scl.compiler.elaboration.expressions.Expression;\r
+import org.simantics.scl.compiler.elaboration.modules.SCLValue;\r
+import org.simantics.scl.compiler.environment.Environment;\r
+import org.simantics.scl.compiler.runtime.RuntimeEnvironment;\r
+import org.simantics.scl.compiler.types.Type;\r
+import org.simantics.scl.compiler.types.Types;\r
+import org.simantics.scl.runtime.SCLContext;\r
+import org.simantics.scl.runtime.function.Function1;\r
+import org.simantics.structural2.scl.CompileStructuralValueRequest;\r
+import org.simantics.structural2.scl.ComponentTypeProperty;\r
+import org.simantics.structural2.scl.ReadComponentTypeInterfaceRequest;\r
+\r
+public class CompileSCLQueryRequest extends CompileStructuralValueRequest {\r
+\r
+ public CompileSCLQueryRequest(ReadGraph graph, Variable context)\r
+ throws DatabaseException {\r
+ super(graph, context);\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 CompileSCLQueryRequest(graph, context),\r
+ TransientCacheListener.<Function1<Variable,Object>>instance());\r
+ sclContext.put("graph", graph);\r
+ return exp.apply(context);\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
+ protected static Name INPUT_VARIABLE = Name.create("Simantics/Query", "inputVariable");\r
+ protected static Name SESSION_VARIABLE = Name.create("Simantics/Query", "sessionVariable");\r
+ public static final Type VARIABLE = Types.con("Simantics/Variables", "Variable"); \r
+\r
+ protected static Expression accessQueryVariable(Environment environment,\r
+ org.simantics.scl.compiler.elaboration.expressions.Variable contextVariable) {\r
+ SCLValue variableParentFunction = environment.getValue(VARIABLE_PARENT);\r
+ return new EApply(new EConstant(variableParentFunction), new EVariable(contextVariable));\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
+ ComponentTypeProperty property = context.propertyMap.get(name);\r
+ if(property != null) {\r
+ Environment environment = context.runtimeEnvironment.getEnvironment();\r
+ return getPropertyFlexible(environment,\r
+ accessQueryVariable(environment, contextVariable),\r
+ name,\r
+ property.type);\r
+ }\r
+ else if(name.equals("input")) {\r
+ Environment environment = context.runtimeEnvironment.getEnvironment();\r
+ return new EApply(\r
+ new EConstant(environment.getValue(INPUT_VARIABLE)),\r
+ new EVariable(contextVariable));\r
+ }\r
+ else if(name.equals("session")) {\r
+ Environment environment = context.runtimeEnvironment.getEnvironment();\r
+ return new EApply(\r
+ new EConstant(environment.getValue(SESSION_VARIABLE)),\r
+ new EVariable(contextVariable));\r
+ }\r
+ else if(name.equals("self"))\r
+ return new EVariable(contextVariable);\r
+ else\r
+ return null;\r
+ }\r
+ \r
+ @Override\r
+ protected CompilationContext getCompilationContext(ReadGraph graph) throws DatabaseException {\r
+ Resource indexRoot = graph.syncRequest(new IndexRoot(literal));\r
+ return graph.syncRequest(new ResourceRead2<CompilationContext>(component, indexRoot) {\r
+ @Override\r
+ public CompilationContext perform(ReadGraph graph) throws DatabaseException {\r
+ Layer0 L0 = Layer0.getInstance(graph);\r
+ Resource type = graph.getPossibleType(component, L0.Entity);\r
+ RuntimeEnvironment runtimeEnvironment = graph.syncRequest(new RuntimeEnvironmentRequest(resource2));\r
+ Map<String, ComponentTypeProperty> propertyMap =\r
+ graph.syncRequest(new ReadComponentTypeInterfaceRequest(type, runtimeEnvironment.getEnvironment()),\r
+ TransientCacheListener.<Map<String, ComponentTypeProperty>>instance());\r
+ return new CompilationContext(runtimeEnvironment, propertyMap);\r
+ }\r
+ });\r
+ }\r
+ \r
+}\r