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