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