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