e4f08db102d0645dbfc2ea1acf64fd62e84797d8
[simantics/platform.git] / bundles / org.simantics.structural2 / src / org / simantics / structural2 / scl / AbstractCompileStructuralValueRequest.java
1 package org.simantics.structural2.scl;
2
3 import java.util.Collections;
4 import java.util.Map;
5
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;
29
30 /**
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.
33  * 
34  * @author Antti Villberg
35  */
36 abstract public class AbstractCompileStructuralValueRequest extends AbstractExpressionCompilationRequest<CompilationContext, Variable> {
37     
38     protected final Resource relation;
39     
40     public static class CompilationContext extends AbstractExpressionCompilationContext {
41         public final Map<String, ComponentTypeProperty> propertyMap;
42         
43         public CompilationContext(RuntimeEnvironment runtimeEnvironment,
44                 Map<String, ComponentTypeProperty> propertyMap) {
45             super(runtimeEnvironment);
46             this.propertyMap = propertyMap;
47         }
48     }
49     
50     public AbstractCompileStructuralValueRequest(Resource relation) {
51         this.relation = relation;
52     }
53
54     @Override
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;
58     
59     @Override
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) {
65             try {
66                 return Environments.getType(context.runtimeEnvironment.getEnvironment(), valueType);
67             } catch (SCLExpressionCompilationException e) {
68                 e.printStackTrace();
69             }
70         }
71         return super.getExpectedType(graph, context);
72     }
73     
74     
75
76     @Override
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());
84         } else {
85                 return graph.syncRequest(new ResourceRead2<CompilationContext>(componentType, indexRoot) {
86                     @Override
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);
94                     }
95                 });
96         }
97     }
98
99     @Override
100     protected Type getContextVariableType() {
101         return VARIABLE; 
102     }
103
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)));
110     }
111     
112     @Override
113     protected Expression getVariableAccessExpression(
114             ReadGraph graph,
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),
123                     name,
124                     property.type);
125         }
126         else if(name.equals("input")) {
127             Environment environment = context.runtimeEnvironment.getEnvironment();
128             return accessInputVariable(environment, contextVariable);
129         }
130         else if(name.equals("self"))
131             return new EVariable(contextVariable);
132         else
133             return null;
134     }
135     
136 }