- public static Object compileAndEvaluate(ReadGraph graph, Variable context) throws DatabaseException {
- SCLContext sclContext = SCLContext.getCurrent();
- Object oldGraph = sclContext.get("graph");
- try {
- Function1<Variable,Object> exp = graph.syncRequest(new ServerSCLHandlerValueRequest(graph, context),
- TransientCacheListener.<Function1<Variable,Object>>instance());
- sclContext.put("graph", graph);
- return exp.apply(context);
- } catch (DatabaseException e) {
- throw (DatabaseException)e;
- } catch (Throwable t) {
- throw new DatabaseException(t);
- } finally {
- sclContext.put("graph", oldGraph);
- }
- }
-
- @Override
- protected String getExpressionText(ReadGraph graph)
- throws DatabaseException {
- Layer0 L0 = Layer0.getInstance(graph);
- String exp = graph.getRelatedValue(literal, L0.SCLValue_expression, Bindings.STRING);
- return "\\context -> " + exp;
- }
-
- protected RuntimeEnvironmentRequest2 getRuntimeEnvironmentRequest(Resource componentType, Resource indexRoot) {
- return new RuntimeEnvironmentRequest2(componentType, indexRoot) {
- @Override
- protected void fillEnvironmentSpecification(
- EnvironmentSpecification environmentSpecification) {
- }
- };
- }
-
- @Override
- protected CompilationContext getCompilationContext(ReadGraph graph) throws DatabaseException {
-
- return graph.syncRequest(new VariableRead<CompilationContext>(context) {
-
- @Override
- public CompilationContext perform(ReadGraph graph) throws DatabaseException {
-
- Pair<Resource,Resource> parameter = getComponentTypeAndRoot(graph, variable);
- RuntimeEnvironment runtimeEnvironment = graph.syncRequest(getRuntimeEnvironmentRequest(parameter.first, parameter.second));
-
- Map<String, ComponentTypeProperty> propertyMap =
- graph.syncRequest(new ReadComponentTypeInterfaceRequest(parameter.first, runtimeEnvironment.getEnvironment()),
- TransientCacheListener.<Map<String, ComponentTypeProperty>>instance());
-
-// Map<String, ComponentTypeProperty> result = new HashMap<String,ComponentTypeProperty>(propertyMap);
-// for(DataDefinition dd : Functions.dataDefinitions(graph, variable)) {
-// result.put(dd.target, null);
-// }
-
- return new CompilationContext(runtimeEnvironment, propertyMap);
-
- }
-
- });
-
- }
-
- @Override
- protected Type getContextVariableType() {
- return VARIABLE;
- }
-
- private static Expression accessInputVariable(Environment environment,
- org.simantics.scl.compiler.elaboration.expressions.Variable contextVariable) {
- SCLValue variableParentFunction = environment.getValue(VARIABLE_PARENT);
- return new EApply(new EConstant(variableParentFunction),
- new EApply(new EConstant(variableParentFunction),
- new EVariable(contextVariable)));
- }
-
- protected static Expression standardGetProperty(
- Environment environment,
- org.simantics.scl.compiler.elaboration.expressions.Variable contextVariable,
- String name,
- Type type) {
- return getPropertyFlexible(environment, accessInputVariable(environment, contextVariable), name, type);
- }
-
- @Override
- protected Expression getVariableAccessExpression(
- ReadGraph graph,
- CompilationContext context,
- org.simantics.scl.compiler.elaboration.expressions.Variable contextVariable,
- String name) throws DatabaseException {
- ComponentTypeProperty property = context.propertyMap.get(name);
- if(property != null)
- return standardGetProperty(
- context.runtimeEnvironment.getEnvironment(),
- contextVariable,
- name,
- property.type == null ? Types.metaVar(Kinds.STAR) : property.type);
- else {
-
-// if(context.propertyMap.containsKey(name)) {
-//
-// org.simantics.scl.compiler.elaboration.expressions.Variable parametersVariable = new org.simantics.scl.compiler.elaboration.expressions.Variable("context", COMMAND_CONTEXT);
-//
-// Environment environment = context.runtimeEnvironment.getEnvironment();
-//
-//// return new EApply(
-//// new EConstant(environment.getValue(FROM_DYNAMIC), Types.STRING),
-// return new EApply(
-// new EConstant(environment.getValue(CONTEXT_VARIABLE), Types.DYNAMIC),
-// new EVariable(parametersVariable),
-// new ELiteral(new StringConstant(name)));
-//
-// }
-
- return getSpecialVariableAccessExpression(graph, context, contextVariable, name);
-
- }
- }
-
- protected Expression getSpecialVariableAccessExpression(ReadGraph graph,
- CompilationContext context,
- org.simantics.scl.compiler.elaboration.expressions.Variable contextVariable,
- String name) throws DatabaseException {
- if(name.equals("input")) {
- Environment environment = context.runtimeEnvironment.getEnvironment();
- return accessInputVariable(environment, contextVariable);
- } else if(name.equals("self"))
- return new EVariable(contextVariable);
- else
- return null;
- }