]> gerrit.simantics Code Review - simantics/platform.git/blob
283e1b5ddcadd6f836fef90a811ce8392bac97a7
[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.db.layer0.variable.Variable;
18 import org.simantics.layer0.Layer0;
19 import org.simantics.scl.compiler.elaboration.expressions.EApply;
20 import org.simantics.scl.compiler.elaboration.expressions.EConstant;
21 import org.simantics.scl.compiler.elaboration.expressions.EVariable;
22 import org.simantics.scl.compiler.elaboration.expressions.Expression;
23 import org.simantics.scl.compiler.elaboration.modules.SCLValue;
24 import org.simantics.scl.compiler.environment.Environment;
25 import org.simantics.scl.compiler.environment.Environments;
26 import org.simantics.scl.compiler.runtime.RuntimeEnvironment;
27 import org.simantics.scl.compiler.top.SCLExpressionCompilationException;
28 import org.simantics.scl.compiler.types.Type;
29 import org.simantics.structural2.scl.AbstractCompileStructuralValueRequest.CompilationContext;
30
31 /**
32  * Compiles an SCL expression that is attached to a literal
33  * whose parent is a component that is a part of a component type.
34  * 
35  * @author Antti Villberg
36  */
37 abstract public class AbstractCompileStructuralValueRequest extends AbstractExpressionCompilationRequest<CompilationContext, Variable> {
38     
39     protected final Resource relation;
40     
41     public static class CompilationContext extends AbstractExpressionCompilationContext {
42         public final Map<String, ComponentTypeProperty> propertyMap;
43         
44         public CompilationContext(RuntimeEnvironment runtimeEnvironment,
45                 Map<String, ComponentTypeProperty> propertyMap) {
46             super(runtimeEnvironment);
47             this.propertyMap = propertyMap;
48         }
49     }
50     
51     public AbstractCompileStructuralValueRequest(Resource relation) {
52         this.relation = relation;
53     }
54
55     @Override
56     abstract protected String getExpressionText(ReadGraph graph) throws DatabaseException;
57     abstract protected Resource getIndexRoot(ReadGraph graph) throws DatabaseException;
58     abstract protected Resource getComponentType(ReadGraph graph) throws DatabaseException;
59     
60     @Override
61     protected Type getExpectedType(ReadGraph graph, CompilationContext context)
62             throws DatabaseException {
63         Layer0 L0 = Layer0.getInstance(graph);
64         String valueType = graph.getPossibleRelatedValue(relation, L0.RequiresValueType, Bindings.STRING);
65         if(valueType != null) {
66             Resource relationIndexRoot = graph.syncRequest(new PossibleIndexRoot(relation));
67             RuntimeEnvironment relationRuntimeEnvironment = relationIndexRoot != null ? graph.syncRequest(new RuntimeEnvironmentRequest(relationIndexRoot)) : context.runtimeEnvironment;
68             try {
69                 return Environments.getType(relationRuntimeEnvironment.getEnvironment(), valueType);
70             } catch (SCLExpressionCompilationException e) {
71                 e.printStackTrace();
72             }
73         }
74         return super.getExpectedType(graph, context);
75     }
76     
77     
78
79     @Override
80     protected CompilationContext getCompilationContext(ReadGraph graph)
81             throws DatabaseException {
82         Resource indexRoot = getIndexRoot(graph);
83         Resource componentType = getComponentType(graph);
84         if(componentType == null) {
85             RuntimeEnvironment runtimeEnvironment = graph.syncRequest(new RuntimeEnvironmentRequest(indexRoot));
86             return new CompilationContext(runtimeEnvironment, Collections.<String,ComponentTypeProperty>emptyMap());
87         } else {
88                 return graph.syncRequest(new ResourceRead2<CompilationContext>(componentType, indexRoot) {
89                     @Override
90                     public CompilationContext perform(ReadGraph graph)
91                             throws DatabaseException {
92                         RuntimeEnvironment runtimeEnvironment = graph.syncRequest(new RuntimeEnvironmentRequest2(resource, resource2));
93                         Map<String, ComponentTypeProperty> propertyMap =
94                                 graph.syncRequest(new ReadComponentTypeInterfaceRequest(resource, runtimeEnvironment.getEnvironment()),
95                                         TransientCacheListener.<Map<String, ComponentTypeProperty>>instance());
96                         return new CompilationContext(runtimeEnvironment, propertyMap);
97                     }
98                 });
99         }
100     }
101
102     @Override
103     protected Type getContextVariableType() {
104         return VARIABLE; 
105     }
106
107     protected static Expression accessInputVariable(Environment environment,
108             org.simantics.scl.compiler.elaboration.expressions.Variable contextVariable) {
109         SCLValue variableParentFunction = environment.getValue(VARIABLE_PARENT);
110         return new EApply(new EConstant(variableParentFunction),
111                 new EApply(new EConstant(variableParentFunction),
112                         new EVariable(contextVariable)));
113     }
114     
115     @Override
116     protected Expression getVariableAccessExpression(
117             ReadGraph graph,
118             CompilationContext context,
119             org.simantics.scl.compiler.elaboration.expressions.Variable contextVariable,
120             String name) throws DatabaseException {
121         ComponentTypeProperty property = context.propertyMap.get(name);
122         if(property != null) {
123             Environment environment = context.runtimeEnvironment.getEnvironment();
124             return getPropertyFlexible(environment,
125                     accessInputVariable(environment, contextVariable),
126                     name,
127                     property.type);
128         }
129         else if(name.equals("input")) {
130             Environment environment = context.runtimeEnvironment.getEnvironment();
131             return accessInputVariable(environment, contextVariable);
132         }
133         else if(name.equals("self"))
134             return new EVariable(contextVariable);
135         else
136             return null;
137     }
138     
139 }