]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.structural2/src/org/simantics/structural2/scl/AbstractCompileStructuralValueRequest.java
Use RuntimeEnvironmentRequest2 where applicable
[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.PossibleIndexRoot;
11 import org.simantics.db.common.request.ResourceRead2;
12 import org.simantics.db.common.request.RuntimeEnvironmentRequest;
13 import org.simantics.db.exception.DatabaseException;
14 import org.simantics.db.layer0.scl.AbstractExpressionCompilationContext;
15 import org.simantics.db.layer0.scl.AbstractExpressionCompilationRequest;
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 import org.slf4j.Logger;
30 import org.slf4j.LoggerFactory;
31
32 /**
33  * Compiles an SCL expression that is attached to a literal
34  * whose parent is a component that is a part of a component type.
35  * 
36  * @author Antti Villberg
37  */
38 public abstract class AbstractCompileStructuralValueRequest extends AbstractExpressionCompilationRequest<CompilationContext, Object> {
39     
40     private static final Logger LOGGER = LoggerFactory.getLogger(AbstractCompileStructuralValueRequest.class);
41
42     protected final Resource relation;
43     
44     public static class CompilationContext extends AbstractExpressionCompilationContext {
45         public final Map<String, ComponentTypeProperty> propertyMap;
46         
47         public CompilationContext(RuntimeEnvironment runtimeEnvironment,
48                 Map<String, ComponentTypeProperty> propertyMap) {
49             super(runtimeEnvironment);
50             this.propertyMap = propertyMap;
51         }
52     }
53     
54     public AbstractCompileStructuralValueRequest(Resource relation) {
55         this.relation = relation;
56     }
57
58     @Override
59     abstract protected String getExpressionText(ReadGraph graph) throws DatabaseException;
60     
61     /*
62      * @throws DatabaseException is an index root could not be found
63      */
64     abstract protected Resource getIndexRoot(ReadGraph graph) throws DatabaseException;
65     /*
66      * Does not throw if the graph is valid.
67      * 
68      * @return null if component type was not availble
69      * 
70      */
71     abstract protected Resource getComponentType(ReadGraph graph) throws DatabaseException;
72
73     protected Resource getPossibleIndexRoot(ReadGraph graph) throws DatabaseException {
74         try {
75             return getIndexRoot(graph);
76         } catch (DatabaseException e) {
77             LOGGER.error("Error while resolving index root during structural value compilation", e);
78             return null;
79         }
80     }
81     
82     @Override
83     protected Type getExpectedType(ReadGraph graph, CompilationContext context)
84             throws DatabaseException {
85         Layer0 L0 = Layer0.getInstance(graph);
86         String valueType = graph.getPossibleRelatedValue(relation, L0.RequiresValueType, Bindings.STRING);
87         if(valueType != null) {
88             Resource relationIndexRoot = graph.syncRequest(new PossibleIndexRoot(relation));
89             RuntimeEnvironment relationRuntimeEnvironment = relationIndexRoot != null ? graph.syncRequest(new RuntimeEnvironmentRequest(relationIndexRoot)) : context.runtimeEnvironment;
90             try {
91                 return Environments.getType(relationRuntimeEnvironment.getEnvironment(), valueType);
92             } catch (SCLExpressionCompilationException e) {
93                 e.printStackTrace();
94             }
95         }
96         return super.getExpectedType(graph, context);
97     }
98     
99     
100
101     @Override
102     protected CompilationContext getCompilationContext(ReadGraph graph)
103             throws DatabaseException {
104         Resource indexRoot = getIndexRoot(graph);
105         Resource componentType = getComponentType(graph);
106         if(componentType == null) {
107             RuntimeEnvironment runtimeEnvironment = graph.syncRequest(new RuntimeEnvironmentRequest(indexRoot));
108             return new CompilationContext(runtimeEnvironment, Collections.<String,ComponentTypeProperty>emptyMap());
109         } else {
110                 return graph.syncRequest(new ResourceRead2<CompilationContext>(componentType, indexRoot) {
111                     @Override
112                     public CompilationContext perform(ReadGraph graph)
113                             throws DatabaseException {
114                         RuntimeEnvironment runtimeEnvironment = graph.syncRequest(new RuntimeEnvironmentRequest2(resource, resource2));
115                         Map<String, ComponentTypeProperty> propertyMap =
116                                 graph.syncRequest(new ReadComponentTypeInterfaceRequest(resource, runtimeEnvironment.getEnvironment()),
117                                         TransientCacheListener.<Map<String, ComponentTypeProperty>>instance());
118                         return new CompilationContext(runtimeEnvironment, propertyMap);
119                     }
120                 });
121         }
122     }
123
124     @Override
125     protected Type getContextVariableType() {
126         return VARIABLE; 
127     }
128
129     protected static Expression accessInputVariable(Environment environment,
130             org.simantics.scl.compiler.elaboration.expressions.Variable contextVariable) {
131         SCLValue variableParentFunction = environment.getValue(VARIABLE_PARENT);
132         return new EApply(new EConstant(variableParentFunction),
133                 new EApply(new EConstant(variableParentFunction),
134                         new EVariable(contextVariable)));
135     }
136     
137     @Override
138     protected Expression getVariableAccessExpression(
139             ReadGraph graph,
140             CompilationContext context,
141             org.simantics.scl.compiler.elaboration.expressions.Variable contextVariable,
142             String name) throws DatabaseException {
143         ComponentTypeProperty property = context.propertyMap.get(name);
144         if(property != null) {
145             Environment environment = context.runtimeEnvironment.getEnvironment();
146             return getPropertyFlexible(environment,
147                     accessInputVariable(environment, contextVariable),
148                     name,
149                     property.type);
150         }
151         else if(name.equals("input")) {
152             Environment environment = context.runtimeEnvironment.getEnvironment();
153             return accessInputVariable(environment, contextVariable);
154         }
155         else if(name.equals("self"))
156             return new EVariable(contextVariable);
157         else
158             return null;
159     }
160     
161     @Override
162     protected String getContextDescription(ReadGraph graph) throws DatabaseException {
163         Resource componentType = getComponentType(graph);
164         if(componentType != null) {
165             String uri = graph.getPossibleURI(componentType);
166             if(uri != null)
167                 return uri;
168         }
169         // OK, no component type - report index root then
170         Resource indexRoot = getPossibleIndexRoot(graph);
171         if(indexRoot != null) {
172             String uri = graph.getPossibleURI(componentType);
173             if(uri != null)
174                 return uri;
175         }
176         // OK, nothing - report default
177         return super.getContextDescription(graph);
178     }
179     
180 }