]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.document.server/src/org/simantics/document/server/request/ServerSCLValueRequest.java
Add checking of expected type in SCL expressions to SCL issues view
[simantics/platform.git] / bundles / org.simantics.document.server / src / org / simantics / document / server / request / ServerSCLValueRequest.java
1 package org.simantics.document.server.request;
2
3 import java.util.Map;
4
5 import org.simantics.databoard.Bindings;
6 import org.simantics.db.ReadGraph;
7 import org.simantics.db.Resource;
8 import org.simantics.db.common.procedure.adapter.TransientCacheListener;
9 import org.simantics.db.common.request.IndexRoot;
10 import org.simantics.db.common.request.UnaryRead;
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.RuntimeEnvironmentRequest2;
15 import org.simantics.db.layer0.variable.Variable;
16 import org.simantics.document.server.request.ServerSCLValueRequest.CompilationContext;
17 import org.simantics.layer0.Layer0;
18 import org.simantics.scl.compiler.common.names.Name;
19 import org.simantics.scl.compiler.constants.StringConstant;
20 import org.simantics.scl.compiler.elaboration.expressions.EApply;
21 import org.simantics.scl.compiler.elaboration.expressions.EConstant;
22 import org.simantics.scl.compiler.elaboration.expressions.ELiteral;
23 import org.simantics.scl.compiler.elaboration.expressions.EVariable;
24 import org.simantics.scl.compiler.elaboration.expressions.Expression;
25 import org.simantics.scl.compiler.elaboration.modules.SCLValue;
26 import org.simantics.scl.compiler.environment.Environment;
27 import org.simantics.scl.compiler.environment.Environments;
28 import org.simantics.scl.compiler.environment.specification.EnvironmentSpecification;
29 import org.simantics.scl.compiler.runtime.RuntimeEnvironment;
30 import org.simantics.scl.compiler.top.SCLExpressionCompilationException;
31 import org.simantics.scl.compiler.types.Type;
32 import org.simantics.scl.compiler.types.Types;
33 import org.simantics.scl.compiler.types.kinds.Kinds;
34 import org.simantics.scl.runtime.SCLContext;
35 import org.simantics.scl.runtime.function.Function1;
36 import org.simantics.structural2.scl.ComponentTypeProperty;
37 import org.simantics.structural2.scl.FindPossibleComponentTypeRequest;
38 import org.simantics.structural2.scl.ReadComponentTypeInterfaceRequest;
39 import org.simantics.utils.datastructures.Pair;
40 import org.slf4j.Logger;
41 import org.slf4j.LoggerFactory;
42
43 public class ServerSCLValueRequest extends AbstractExpressionCompilationRequest<CompilationContext, Variable> {
44
45     private static final Logger LOGGER = LoggerFactory.getLogger(ServerSCLValueRequest.class);
46     
47         private final Pair<Resource,Resource> componentTypeAndRoot;
48         private final Resource literal;
49     protected String possibleExpectedValueType;
50
51         public static class CompilationContext extends AbstractExpressionCompilationContext {
52                 public final Map<String, ComponentTypeProperty> propertyMap;
53
54                 public CompilationContext(RuntimeEnvironment runtimeEnvironment,
55                                 Map<String, ComponentTypeProperty> propertyMap) {
56                         super(runtimeEnvironment);
57                         this.propertyMap = propertyMap;
58                 }
59         }
60
61         private ServerSCLValueRequest(Pair<Resource,Resource> componentTypeAndRoot, Resource literal, String possibleExpectedValueType) {
62                 assert(literal != null);
63                 this.literal = literal;
64                 this.componentTypeAndRoot = componentTypeAndRoot;
65                 this.possibleExpectedValueType = possibleExpectedValueType;
66         }
67
68         public ServerSCLValueRequest(ReadGraph graph, Variable context) throws DatabaseException {
69                 this(getComponentTypeAndRoot(graph, context), context.getRepresents(graph), resolveExpectedValueType(graph, context));
70         }
71
72         private static Pair<Resource,Resource> getComponentTypeAndRoot(ReadGraph graph, Variable property)  throws DatabaseException {
73                 Variable parent = property.getParent(graph);
74                 Resource represents = parent.getRepresents(graph);
75                 if(represents != null) {
76                         Resource type = graph.syncRequest(new FindPossibleComponentTypeRequest(represents));
77                         if(type != null) {
78                                 Resource root = graph.syncRequest(new IndexRoot(type));
79                                 return Pair.make(type, root);
80                         }
81                 }
82                 parent = parent.getParent(graph);
83                 Resource root = graph.syncRequest(new IndexRoot(property.getRepresents(graph)));
84                 return Pair.make(parent.getType(graph), root);
85         }
86
87         public static Object compileAndEvaluate(ReadGraph graph, Variable context) throws DatabaseException {
88                 SCLContext sclContext = SCLContext.getCurrent();
89         Object oldGraph = sclContext.get("graph");
90                 try {
91                         Function1<Variable,Object> exp = graph.syncRequest(new ServerSCLValueRequest(graph, context),
92                                         TransientCacheListener.<Function1<Variable,Object>>instance());
93                         sclContext.put("graph", graph);
94                         return exp.apply(context);
95                 } catch (DatabaseException e) {
96                         throw (DatabaseException)e;
97                 } catch (Throwable t) {
98                         throw new DatabaseException(t);
99                 } finally {
100             sclContext.put("graph", oldGraph);
101                 }
102         }
103
104         @Override
105         protected String getExpressionText(ReadGraph graph)
106                         throws DatabaseException {
107                 Layer0 L0 = Layer0.getInstance(graph);
108                 return graph.getRelatedValue(literal, L0.SCLValue_expression, Bindings.STRING);
109         }
110
111         protected RuntimeEnvironmentRequest2 getRuntimeEnvironmentRequest(Resource componentType, Resource indexRoot) {
112                 return new RuntimeEnvironmentRequest2(componentType, indexRoot) {
113                         @Override
114                         protected void fillEnvironmentSpecification(
115                                         EnvironmentSpecification environmentSpecification) {
116                         }
117                 };
118         }
119
120         @Override
121         protected CompilationContext getCompilationContext(ReadGraph graph)
122                         throws DatabaseException {
123                 return graph.syncRequest(new UnaryRead<Pair<Resource,Resource>,CompilationContext>(componentTypeAndRoot) {
124                         @Override
125                         public CompilationContext perform(ReadGraph graph)
126                                         throws DatabaseException {
127                                 RuntimeEnvironment runtimeEnvironment = graph.syncRequest(getRuntimeEnvironmentRequest(parameter.first, parameter.second));
128                                 Map<String, ComponentTypeProperty> propertyMap =
129                                                 graph.syncRequest(new ReadComponentTypeInterfaceRequest(parameter.first, runtimeEnvironment.getEnvironment()),
130                                                                 TransientCacheListener.<Map<String, ComponentTypeProperty>>instance());
131                                 return new CompilationContext(runtimeEnvironment, propertyMap);
132                         }
133                 });
134         }
135
136         @Override
137         protected Type getContextVariableType() {
138                 return VARIABLE; 
139         }
140
141         private static Expression accessInputVariable(Environment environment,
142                         org.simantics.scl.compiler.elaboration.expressions.Variable contextVariable) {
143                 SCLValue variableParentFunction = environment.getValue(VARIABLE_PARENT);
144                 return new EApply(new EConstant(variableParentFunction),
145                                 new EApply(new EConstant(variableParentFunction),
146                                                 new EVariable(contextVariable)));
147         }
148
149     protected static Name PROPERTY_VALUE_CACHED = Name.create("Document/All", "propertyValueCached");
150
151     protected static Expression getProperty(Environment environment, Expression variable, String propertyName, Type type) {
152         return new EApply(
153                 new EConstant(environment.getValue(FROM_DYNAMIC), type),
154                 new EApply(
155                         new EConstant(environment.getValue(PROPERTY_VALUE_CACHED), Types.DYNAMIC),
156                         variable,
157                         new ELiteral(new StringConstant(propertyName))));
158     }
159     
160     protected static Expression getPropertyFlexible(Environment environment, Expression variable, String propertyName, Type type) {
161         return makeTypeFlexible(environment, getProperty(environment, variable, propertyName, type), type);
162     }
163         
164         protected static Expression standardGetProperty(
165                         Environment environment,
166                         org.simantics.scl.compiler.elaboration.expressions.Variable contextVariable,
167                         String name,
168                         Type type) {
169                 return getPropertyFlexible(environment, accessInputVariable(environment, contextVariable), name, type);
170         }
171
172
173         @Override
174         protected Expression getVariableAccessExpression(
175                         ReadGraph graph,
176                         CompilationContext context,
177                         org.simantics.scl.compiler.elaboration.expressions.Variable contextVariable,
178                         String name) throws DatabaseException {
179                 ComponentTypeProperty property = context.propertyMap.get(name);
180                 if(property != null)
181                         return standardGetProperty(
182                                         context.runtimeEnvironment.getEnvironment(),
183                                         contextVariable,
184                                         name,
185                                         property.type == null ? Types.metaVar(Kinds.STAR) : property.type);
186                 else
187                         return getSpecialVariableAccessExpression(graph, context, contextVariable, name);
188         }
189
190         protected Expression getSpecialVariableAccessExpression(ReadGraph graph,
191                         CompilationContext context,
192                         org.simantics.scl.compiler.elaboration.expressions.Variable contextVariable,
193                         String name) throws DatabaseException {
194                 if(name.equals("input")) {
195                         Environment environment = context.runtimeEnvironment.getEnvironment();
196                         return accessInputVariable(environment, contextVariable);
197                 }
198                 else if(name.equals("self"))
199                         return new EVariable(contextVariable);
200                 else
201                         return null;
202         }
203         
204     @Override
205     protected Type getExpectedType(ReadGraph graph, CompilationContext context) throws DatabaseException {
206         if(possibleExpectedValueType != null) {
207             try {
208                 return Environments.getType(context.runtimeEnvironment.getEnvironment(), possibleExpectedValueType);
209             } catch (SCLExpressionCompilationException e) {
210                 LOGGER.error("Could not get type for " + String.valueOf(possibleExpectedValueType), e);
211             }
212         }
213         return super.getExpectedType(graph, context);
214     }
215
216
217         @Override
218         public int hashCode() {
219                 return 31*(31*getClass().hashCode() + literal.hashCode()) + componentTypeAndRoot.hashCode();
220         }
221
222         @Override
223         public boolean equals(Object obj) {
224                 if(this == obj)
225                         return true;
226                 if(obj == null || obj.getClass() != getClass())
227                         return false;
228                 ServerSCLValueRequest other = (ServerSCLValueRequest)obj;
229                 return literal.equals(other.literal) && componentTypeAndRoot.equals(other.componentTypeAndRoot);
230         }
231 }