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