]> gerrit.simantics Code Review - simantics/platform.git/blob
ab5e7a8e40e985a8a52e4b3b81b5013eaf621def
[simantics/platform.git] /
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                 return graph.syncRequest(new UnaryRead<Pair<Resource,Resource>,CompilationContext>(componentTypeAndRoot) {
158                         @Override
159                         public CompilationContext perform(ReadGraph graph) throws DatabaseException {
160                                 RuntimeEnvironment runtimeEnvironment = graph.syncRequest(getRuntimeEnvironmentRequest(parameter.first, parameter.second));
161                                 Map<String, ComponentTypeProperty> propertyMap =
162                                                 graph.syncRequest(new ReadComponentTypeInterfaceRequest(parameter.first, runtimeEnvironment.getEnvironment()),
163                                                                 TransientCacheListener.<Map<String, ComponentTypeProperty>>instance());
164                                 return new CompilationContext(runtimeEnvironment, propertyMap);
165                         }
166                 });
167         }
168
169         @Override
170         protected Type getContextVariableType() {
171                 return VARIABLE; 
172         }
173
174         private static Expression accessInputVariable(Environment environment,
175                         org.simantics.scl.compiler.elaboration.expressions.Variable contextVariable) {
176                 SCLValue variableParentFunction = environment.getValue(VARIABLE_PARENT);
177                 return new EApply(new EConstant(variableParentFunction),
178                                 new EApply(new EConstant(variableParentFunction),
179                                                 new EVariable(contextVariable)));
180         }
181
182         protected static Expression standardGetProperty(
183                         Environment environment,
184                         org.simantics.scl.compiler.elaboration.expressions.Variable contextVariable,
185                         String name,
186                         Type type) {
187                 return getPropertyFlexible(environment, accessInputVariable(environment, contextVariable), name, type);
188         }
189
190         @Override
191         protected Expression getVariableAccessExpression(
192                         ReadGraph graph,
193                         CompilationContext context,
194                         org.simantics.scl.compiler.elaboration.expressions.Variable contextVariable,
195                         String name) throws DatabaseException {
196                 ComponentTypeProperty property = context.propertyMap.get(name);
197                 if(property != null)
198                         return standardGetProperty(
199                                         context.runtimeEnvironment.getEnvironment(),
200                                         contextVariable,
201                                         name,
202                                         property.type == null ? Types.metaVar(Kinds.STAR) : property.type);
203                 else {
204                         
205 //                      if(context.propertyMap.containsKey(name)) {
206 //                              
207 //                              org.simantics.scl.compiler.elaboration.expressions.Variable parametersVariable = new org.simantics.scl.compiler.elaboration.expressions.Variable("context", COMMAND_CONTEXT);
208 //                              
209 //                              Environment environment = context.runtimeEnvironment.getEnvironment();
210 //                              
211 ////                            return new EApply(
212 ////                            new EConstant(environment.getValue(FROM_DYNAMIC), Types.STRING),
213 //                              return new EApply(
214 //                                      new EConstant(environment.getValue(CONTEXT_VARIABLE), Types.DYNAMIC),
215 //                                      new EVariable(parametersVariable),
216 //                                      new ELiteral(new StringConstant(name)));
217 //                              
218 //                      }
219                         
220                         return getSpecialVariableAccessExpression(graph, context, contextVariable, name);
221                         
222                 }
223         }
224
225         protected Expression getSpecialVariableAccessExpression(ReadGraph graph,
226                         CompilationContext context,
227                         org.simantics.scl.compiler.elaboration.expressions.Variable contextVariable,
228                         String name) throws DatabaseException {
229                 if(name.equals("input")) {
230                         Environment environment = context.runtimeEnvironment.getEnvironment();
231                         return accessInputVariable(environment, contextVariable);
232                 } else if(name.equals("self"))
233                         return new EVariable(contextVariable);
234                 else
235                         return null;
236         }
237
238     @Override
239     protected Type getExpectedType(ReadGraph graph, CompilationContext context) throws DatabaseException {
240         return super.getExpectedType(graph, context);
241     }
242
243         @Override
244         public int hashCode() {
245                 return 31*(31*getClass().hashCode() + literal.hashCode()) + componentTypeAndRoot.hashCode();
246         }
247
248         @Override
249         public boolean equals(Object obj) {
250                 if (this == obj)
251                         return true;
252                 if (obj == null)
253                         return false;
254                 if (getClass() != obj.getClass())
255                         return false;
256                 ServerSCLHandlerValueRequest other = (ServerSCLHandlerValueRequest) obj;
257                 return literal.equals(other.literal) && componentTypeAndRoot.equals(other.componentTypeAndRoot);
258         }
259
260 }