]> gerrit.simantics Code Review - simantics/platform.git/blob
c4b4e341fb85b2aa551612e028a2dafca387f06c
[simantics/platform.git] /
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.PossibleIndexRoot;
11 import org.simantics.db.common.request.ResourceRead2;
12 import org.simantics.db.exception.DatabaseException;
13 import org.simantics.db.layer0.scl.AbstractExpressionCompilationContext;
14 import org.simantics.db.layer0.scl.AbstractExpressionCompilationRequest;
15 import org.simantics.db.layer0.util.RuntimeEnvironmentRequest;
16 import org.simantics.db.layer0.util.RuntimeEnvironmentRequest2;
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 public abstract class AbstractCompileStructuralValueRequest extends AbstractExpressionCompilationRequest<CompilationContext, Object> {
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             Resource relationIndexRoot = graph.syncRequest(new PossibleIndexRoot(relation));
66             RuntimeEnvironment relationRuntimeEnvironment = relationIndexRoot != null ? graph.syncRequest(new RuntimeEnvironmentRequest(relationIndexRoot)) : context.runtimeEnvironment;
67             try {
68                 return Environments.getType(relationRuntimeEnvironment.getEnvironment(), valueType);
69             } catch (SCLExpressionCompilationException e) {
70                 e.printStackTrace();
71             }
72         }
73         return super.getExpectedType(graph, context);
74     }
75     
76     
77
78     @Override
79     protected CompilationContext getCompilationContext(ReadGraph graph)
80             throws DatabaseException {
81         Resource indexRoot = getIndexRoot(graph);
82         Resource componentType = getComponentType(graph);
83         if(componentType == null) {
84             RuntimeEnvironment runtimeEnvironment = graph.syncRequest(new RuntimeEnvironmentRequest(indexRoot));
85             return new CompilationContext(runtimeEnvironment, Collections.<String,ComponentTypeProperty>emptyMap());
86         } else {
87                 return graph.syncRequest(new ResourceRead2<CompilationContext>(componentType, indexRoot) {
88                     @Override
89                     public CompilationContext perform(ReadGraph graph)
90                             throws DatabaseException {
91                         RuntimeEnvironment runtimeEnvironment = graph.syncRequest(new RuntimeEnvironmentRequest2(resource, resource2));
92                         Map<String, ComponentTypeProperty> propertyMap =
93                                 graph.syncRequest(new ReadComponentTypeInterfaceRequest(resource, runtimeEnvironment.getEnvironment()),
94                                         TransientCacheListener.<Map<String, ComponentTypeProperty>>instance());
95                         return new CompilationContext(runtimeEnvironment, propertyMap);
96                     }
97                 });
98         }
99     }
100
101     @Override
102     protected Type getContextVariableType() {
103         return VARIABLE; 
104     }
105
106     protected static Expression accessInputVariable(Environment environment,
107             org.simantics.scl.compiler.elaboration.expressions.Variable contextVariable) {
108         SCLValue variableParentFunction = environment.getValue(VARIABLE_PARENT);
109         return new EApply(new EConstant(variableParentFunction),
110                 new EApply(new EConstant(variableParentFunction),
111                         new EVariable(contextVariable)));
112     }
113     
114     @Override
115     protected Expression getVariableAccessExpression(
116             ReadGraph graph,
117             CompilationContext context,
118             org.simantics.scl.compiler.elaboration.expressions.Variable contextVariable,
119             String name) throws DatabaseException {
120         ComponentTypeProperty property = context.propertyMap.get(name);
121         if(property != null) {
122             Environment environment = context.runtimeEnvironment.getEnvironment();
123             return getPropertyFlexible(environment,
124                     accessInputVariable(environment, contextVariable),
125                     name,
126                     property.type);
127         }
128         else if(name.equals("input")) {
129             Environment environment = context.runtimeEnvironment.getEnvironment();
130             return accessInputVariable(environment, contextVariable);
131         }
132         else if(name.equals("self"))
133             return new EVariable(contextVariable);
134         else
135             return null;
136     }
137     
138 }