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