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