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