]> gerrit.simantics Code Review - simantics/platform.git/blob
7283cf26b36a5786c1212f6bc7d6405042cae882
[simantics/platform.git] /
1 package org.simantics.document.server.request;
2
3 import java.util.ArrayList;
4 import java.util.List;
5 import java.util.Map;
6
7 import org.simantics.databoard.Bindings;
8 import org.simantics.db.ReadGraph;
9 import org.simantics.db.Resource;
10 import org.simantics.db.common.procedure.adapter.TransientCacheListener;
11 import org.simantics.db.common.request.IndexRoot;
12 import org.simantics.db.exception.DatabaseException;
13 import org.simantics.db.layer0.request.VariableRead;
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.db.layer0.variable.Variable;
18 import org.simantics.document.server.request.ServerSCLHandlerValueRequest.CompilationContext;
19 import org.simantics.layer0.Layer0;
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.EVariable;
23 import org.simantics.scl.compiler.elaboration.expressions.Expression;
24 import org.simantics.scl.compiler.elaboration.modules.SCLValue;
25 import org.simantics.scl.compiler.environment.Environment;
26 import org.simantics.scl.compiler.environment.Environments;
27 import org.simantics.scl.compiler.environment.specification.EnvironmentSpecification;
28 import org.simantics.scl.compiler.runtime.RuntimeEnvironment;
29 import org.simantics.scl.compiler.top.SCLExpressionCompilationException;
30 import org.simantics.scl.compiler.types.TCon;
31 import org.simantics.scl.compiler.types.TMetaVar;
32 import org.simantics.scl.compiler.types.TVar;
33 import org.simantics.scl.compiler.types.Type;
34 import org.simantics.scl.compiler.types.Types;
35 import org.simantics.scl.compiler.types.kinds.Kinds;
36 import org.simantics.scl.runtime.SCLContext;
37 import org.simantics.scl.runtime.function.Function1;
38 import org.simantics.structural2.scl.ComponentTypeProperty;
39 import org.simantics.structural2.scl.FindPossibleComponentTypeRequest;
40 import org.simantics.structural2.scl.ReadComponentTypeInterfaceRequest;
41 import org.simantics.utils.datastructures.Pair;
42 import org.slf4j.Logger;
43 import org.slf4j.LoggerFactory;
44
45 public class ServerSCLHandlerValueRequest extends AbstractExpressionCompilationRequest<CompilationContext, Variable> {
46
47     private static final Logger LOGGER = LoggerFactory.getLogger(ServerSCLHandlerValueRequest.class);
48
49         private final Variable context;
50         private final Pair<Resource,Resource> componentTypeAndRoot;
51         private final Resource literal;
52         protected String possibleExpectedValueType;
53
54         public static class CompilationContext extends AbstractExpressionCompilationContext {
55                 public final Map<String, ComponentTypeProperty> propertyMap;
56
57                 public CompilationContext(RuntimeEnvironment runtimeEnvironment,
58                                 Map<String, ComponentTypeProperty> propertyMap) {
59                         super(runtimeEnvironment);
60                         this.propertyMap = propertyMap;
61                 }
62         }
63
64         private ServerSCLHandlerValueRequest(Variable context, Pair<Resource,Resource> componentTypeAndRoot, Resource literal, String possibleExpectedValueType) {
65                 assert(literal != null);
66                 this.context = context;
67                 this.literal = literal;
68                 this.componentTypeAndRoot = componentTypeAndRoot;
69                 this.possibleExpectedValueType = possibleExpectedValueType;
70         }
71
72         public ServerSCLHandlerValueRequest(ReadGraph graph, Variable context) throws DatabaseException {
73                 this(context, getComponentTypeAndRoot(graph, context), context.getRepresents(graph), resolveExpectedValueType(graph, context));
74         }
75
76         private static Pair<Resource,Resource> getComponentTypeAndRoot(ReadGraph graph, Variable property)  throws DatabaseException {
77                 Variable parent = property.getParent(graph);
78                 Resource represents = parent.getRepresents(graph);
79                 if(represents != null) {
80                         Resource type = graph.syncRequest(new FindPossibleComponentTypeRequest(represents));
81                         if(type != null) {
82                                 Resource root = graph.syncRequest(new IndexRoot(type));
83                                 return Pair.make(type, root);
84                         }
85                 }
86                 parent = parent.getParent(graph);
87                 Resource root = graph.syncRequest(new IndexRoot(property.getRepresents(graph)));
88                 return Pair.make(parent.getType(graph), root);
89         }
90
91
92     public static List<TCon> getEffects(ReadGraph graph, Variable context) throws DatabaseException {
93         try {
94                 ServerSCLHandlerValueRequest req = new ServerSCLHandlerValueRequest(graph, context);
95                 return req.getExpressionEffects(graph);
96         } catch (DatabaseException e) {
97             throw (DatabaseException)e;
98         } catch (Throwable t) {
99             throw new DatabaseException(t);
100         }
101     }
102
103         public static Object compileAndEvaluate(ReadGraph graph, Variable context) throws DatabaseException {
104                 SCLContext sclContext = SCLContext.getCurrent();
105         Object oldGraph = sclContext.get("graph");
106                 try {
107                         Function1<Variable,Object> exp = graph.syncRequest(new ServerSCLHandlerValueRequest(graph, context),
108                                         TransientCacheListener.<Function1<Variable,Object>>instance());
109                         sclContext.put("graph", graph);
110                         return exp.apply(context);
111                 } catch (DatabaseException e) {
112                         throw (DatabaseException)e;
113                 } catch (Throwable t) {
114                         throw new DatabaseException(t);
115                 } finally {
116             sclContext.put("graph", oldGraph);
117                 }
118         }
119
120         @Override
121         protected String getExpressionText(ReadGraph graph)
122                         throws DatabaseException {
123                 Layer0 L0 = Layer0.getInstance(graph);
124                 String exp = graph.getRelatedValue(literal, L0.SCLValue_expression, Bindings.STRING);
125                 return "\\context -> " + exp;
126         }
127
128         protected RuntimeEnvironmentRequest2 getRuntimeEnvironmentRequest(Resource componentType, Resource indexRoot) {
129                 return new RuntimeEnvironmentRequest2(componentType, indexRoot) {
130                         @Override
131                         protected void fillEnvironmentSpecification(
132                                         EnvironmentSpecification environmentSpecification) {
133                         }
134                 };
135         }
136
137         @Override
138         protected CompilationContext getCompilationContext(ReadGraph graph) throws DatabaseException {
139                 
140                 return graph.syncRequest(new VariableRead<CompilationContext>(context) {
141                         
142                         @Override
143                         public CompilationContext perform(ReadGraph graph) throws DatabaseException {
144                                 
145                                 Pair<Resource,Resource> parameter = getComponentTypeAndRoot(graph, variable);
146                                 RuntimeEnvironment runtimeEnvironment = graph.syncRequest(getRuntimeEnvironmentRequest(parameter.first, parameter.second));
147                                 
148                                 Map<String, ComponentTypeProperty> propertyMap =
149                                                 graph.syncRequest(new ReadComponentTypeInterfaceRequest(parameter.first, runtimeEnvironment.getEnvironment()),
150                                                                 TransientCacheListener.<Map<String, ComponentTypeProperty>>instance());
151
152 //                              Map<String, ComponentTypeProperty> result = new HashMap<String,ComponentTypeProperty>(propertyMap); 
153 //                              for(DataDefinition dd : Functions.dataDefinitions(graph, variable)) {
154 //                                      result.put(dd.target, null);
155 //                              }
156                                 
157                                 return new CompilationContext(runtimeEnvironment, propertyMap);
158                                 
159                         }
160                         
161                 });
162                 
163         }
164
165         @Override
166         protected Type getContextVariableType() {
167                 return VARIABLE; 
168         }
169
170         private static Expression accessInputVariable(Environment environment,
171                         org.simantics.scl.compiler.elaboration.expressions.Variable contextVariable) {
172                 SCLValue variableParentFunction = environment.getValue(VARIABLE_PARENT);
173                 return new EApply(new EConstant(variableParentFunction),
174                                 new EApply(new EConstant(variableParentFunction),
175                                                 new EVariable(contextVariable)));
176         }
177
178         protected static Expression standardGetProperty(
179                         Environment environment,
180                         org.simantics.scl.compiler.elaboration.expressions.Variable contextVariable,
181                         String name,
182                         Type type) {
183                 return getPropertyFlexible(environment, accessInputVariable(environment, contextVariable), name, type);
184         }
185
186         @Override
187         protected Expression getVariableAccessExpression(
188                         ReadGraph graph,
189                         CompilationContext context,
190                         org.simantics.scl.compiler.elaboration.expressions.Variable contextVariable,
191                         String name) throws DatabaseException {
192                 ComponentTypeProperty property = context.propertyMap.get(name);
193                 if(property != null)
194                         return standardGetProperty(
195                                         context.runtimeEnvironment.getEnvironment(),
196                                         contextVariable,
197                                         name,
198                                         property.type == null ? Types.metaVar(Kinds.STAR) : property.type);
199                 else {
200                         
201 //                      if(context.propertyMap.containsKey(name)) {
202 //                              
203 //                              org.simantics.scl.compiler.elaboration.expressions.Variable parametersVariable = new org.simantics.scl.compiler.elaboration.expressions.Variable("context", COMMAND_CONTEXT);
204 //                              
205 //                              Environment environment = context.runtimeEnvironment.getEnvironment();
206 //                              
207 ////                            return new EApply(
208 ////                            new EConstant(environment.getValue(FROM_DYNAMIC), Types.STRING),
209 //                              return new EApply(
210 //                                      new EConstant(environment.getValue(CONTEXT_VARIABLE), Types.DYNAMIC),
211 //                                      new EVariable(parametersVariable),
212 //                                      new ELiteral(new StringConstant(name)));
213 //                              
214 //                      }
215                         
216                         return getSpecialVariableAccessExpression(graph, context, contextVariable, name);
217                         
218                 }
219         }
220
221         protected Expression getSpecialVariableAccessExpression(ReadGraph graph,
222                         CompilationContext context,
223                         org.simantics.scl.compiler.elaboration.expressions.Variable contextVariable,
224                         String name) throws DatabaseException {
225                 if(name.equals("input")) {
226                         Environment environment = context.runtimeEnvironment.getEnvironment();
227                         return accessInputVariable(environment, contextVariable);
228                 } else if(name.equals("self"))
229                         return new EVariable(contextVariable);
230                 else
231                         return null;
232         }
233
234     @Override
235     protected Type getExpectedType(ReadGraph graph, CompilationContext context) throws DatabaseException {
236         if(possibleExpectedValueType != null) {
237             try {
238                 Type type = Environments.getType(context.runtimeEnvironment.getEnvironment(), possibleExpectedValueType);
239                 type = Types.instantiate(Types.forAll(Types.freeVars(type).toArray(new TVar[0]), type), new ArrayList<TMetaVar>());
240                 return type;
241             } catch (SCLExpressionCompilationException e) {
242                 LOGGER.error("Could not get type for " + String.valueOf(possibleExpectedValueType), e);
243             }
244         }
245         return super.getExpectedType(graph, context);
246     }
247
248
249         @Override
250         public int hashCode() {
251                 final int prime = 31;
252                 int result = 1;
253                 result = prime * result + ((context == null) ? 0 : context.hashCode());
254                 return result;
255         }
256
257         @Override
258         public boolean equals(Object obj) {
259                 if (this == obj)
260                         return true;
261                 if (obj == null)
262                         return false;
263                 if (getClass() != obj.getClass())
264                         return false;
265                 ServerSCLHandlerValueRequest other = (ServerSCLHandlerValueRequest) obj;
266                 if (context == null) {
267                         if (other.context != null)
268                                 return false;
269                 } else if (!context.equals(other.context))
270                         return false;
271                 return true;
272         }
273
274 //      @Override
275 //      public int hashCode() {
276 //              return 31*(31*getClass().hashCode() + literal.hashCode()) + componentTypeAndRoot.hashCode();
277 //      }
278 //
279 //      @Override
280 //      public boolean equals(Object obj) {
281 //              if(this == obj)
282 //                      return true;
283 //              if(obj == null || obj.getClass() != getClass())
284 //                      return false;
285 //              ServerSCLHandlerValueRequest other = (ServerSCLHandlerValueRequest)obj;
286 //              return literal.equals(other.literal) && componentTypeAndRoot.equals(other.componentTypeAndRoot);
287 //      }
288         
289 }