1 package org.simantics.structural2.scl;
3 import java.util.Collections;
6 import org.simantics.databoard.Bindings;
7 import org.simantics.db.ReadGraph;
8 import org.simantics.db.Resource;
9 import org.simantics.db.common.procedure.adapter.TransientCacheListener;
10 import org.simantics.db.common.request.ResourceRead2;
11 import org.simantics.db.exception.DatabaseException;
12 import org.simantics.db.layer0.scl.AbstractExpressionCompilationContext;
13 import org.simantics.db.layer0.scl.AbstractExpressionCompilationRequest;
14 import org.simantics.db.layer0.util.RuntimeEnvironmentRequest;
15 import org.simantics.db.layer0.util.RuntimeEnvironmentRequest2;
16 import org.simantics.db.layer0.variable.Variable;
17 import org.simantics.layer0.Layer0;
18 import org.simantics.scl.compiler.elaboration.expressions.EApply;
19 import org.simantics.scl.compiler.elaboration.expressions.EConstant;
20 import org.simantics.scl.compiler.elaboration.expressions.EVariable;
21 import org.simantics.scl.compiler.elaboration.expressions.Expression;
22 import org.simantics.scl.compiler.elaboration.modules.SCLValue;
23 import org.simantics.scl.compiler.environment.Environment;
24 import org.simantics.scl.compiler.environment.Environments;
25 import org.simantics.scl.compiler.runtime.RuntimeEnvironment;
26 import org.simantics.scl.compiler.top.SCLExpressionCompilationException;
27 import org.simantics.scl.compiler.types.Type;
28 import org.simantics.structural2.scl.AbstractCompileStructuralValueRequest.CompilationContext;
31 * Compiles an SCL expression that is attached to a literal
32 * whose parent is a component that is a part of a component type.
34 * @author Antti Villberg
36 abstract public class AbstractCompileStructuralValueRequest extends AbstractExpressionCompilationRequest<CompilationContext, Variable> {
38 protected final Resource relation;
40 public static class CompilationContext extends AbstractExpressionCompilationContext {
41 public final Map<String, ComponentTypeProperty> propertyMap;
43 public CompilationContext(RuntimeEnvironment runtimeEnvironment,
44 Map<String, ComponentTypeProperty> propertyMap) {
45 super(runtimeEnvironment);
46 this.propertyMap = propertyMap;
50 public AbstractCompileStructuralValueRequest(Resource relation) {
51 this.relation = relation;
55 abstract protected String getExpressionText(ReadGraph graph) throws DatabaseException;
56 abstract protected Resource getIndexRoot(ReadGraph graph) throws DatabaseException;
57 abstract protected Resource getComponentType(ReadGraph graph) throws DatabaseException;
60 protected Type getExpectedType(ReadGraph graph, CompilationContext context)
61 throws DatabaseException {
62 Layer0 L0 = Layer0.getInstance(graph);
63 String valueType = graph.getPossibleRelatedValue(relation, L0.RequiresValueType, Bindings.STRING);
64 if(valueType != null) {
66 return Environments.getType(context.runtimeEnvironment.getEnvironment(), valueType);
67 } catch (SCLExpressionCompilationException e) {
71 return super.getExpectedType(graph, context);
77 protected CompilationContext getCompilationContext(ReadGraph graph)
78 throws DatabaseException {
79 Resource indexRoot = getIndexRoot(graph);
80 Resource componentType = getComponentType(graph);
81 if(componentType == null) {
82 RuntimeEnvironment runtimeEnvironment = graph.syncRequest(new RuntimeEnvironmentRequest(indexRoot));
83 return new CompilationContext(runtimeEnvironment, Collections.<String,ComponentTypeProperty>emptyMap());
85 return graph.syncRequest(new ResourceRead2<CompilationContext>(componentType, indexRoot) {
87 public CompilationContext perform(ReadGraph graph)
88 throws DatabaseException {
89 RuntimeEnvironment runtimeEnvironment = graph.syncRequest(new RuntimeEnvironmentRequest2(resource, resource2));
90 Map<String, ComponentTypeProperty> propertyMap =
91 graph.syncRequest(new ReadComponentTypeInterfaceRequest(resource, runtimeEnvironment.getEnvironment()),
92 TransientCacheListener.<Map<String, ComponentTypeProperty>>instance());
93 return new CompilationContext(runtimeEnvironment, propertyMap);
100 protected Type getContextVariableType() {
104 protected static Expression accessInputVariable(Environment environment,
105 org.simantics.scl.compiler.elaboration.expressions.Variable contextVariable) {
106 SCLValue variableParentFunction = environment.getValue(VARIABLE_PARENT);
107 return new EApply(new EConstant(variableParentFunction),
108 new EApply(new EConstant(variableParentFunction),
109 new EVariable(contextVariable)));
113 protected Expression getVariableAccessExpression(
115 CompilationContext context,
116 org.simantics.scl.compiler.elaboration.expressions.Variable contextVariable,
117 String name) throws DatabaseException {
118 ComponentTypeProperty property = context.propertyMap.get(name);
119 if(property != null) {
120 Environment environment = context.runtimeEnvironment.getEnvironment();
121 return getPropertyFlexible(environment,
122 accessInputVariable(environment, contextVariable),
126 else if(name.equals("input")) {
127 Environment environment = context.runtimeEnvironment.getEnvironment();
128 return accessInputVariable(environment, contextVariable);
130 else if(name.equals("self"))
131 return new EVariable(contextVariable);