]> gerrit.simantics Code Review - simantics/platform.git/blob
246acfdec72236a9640e7c3b3f0d27ded78b29f8
[simantics/platform.git] /
1 package org.simantics.document.server.request;\r
2 \r
3 import java.util.Map;\r
4 \r
5 import org.simantics.databoard.Bindings;\r
6 import org.simantics.db.ReadGraph;\r
7 import org.simantics.db.Resource;\r
8 import org.simantics.db.common.procedure.adapter.TransientCacheListener;\r
9 import org.simantics.db.common.request.IndexRoot;\r
10 import org.simantics.db.common.request.UnaryRead;\r
11 import org.simantics.db.exception.DatabaseException;\r
12 import org.simantics.db.layer0.scl.AbstractExpressionCompilationContext;\r
13 import org.simantics.db.layer0.scl.AbstractExpressionCompilationRequest;\r
14 import org.simantics.db.layer0.util.RuntimeEnvironmentRequest;\r
15 import org.simantics.db.layer0.variable.Variable;\r
16 import org.simantics.document.server.request.ServerSCLValueRequest.CompilationContext;\r
17 import org.simantics.layer0.Layer0;\r
18 import org.simantics.scl.compiler.common.names.Name;\r
19 import org.simantics.scl.compiler.constants.StringConstant;\r
20 import org.simantics.scl.compiler.elaboration.expressions.EApply;\r
21 import org.simantics.scl.compiler.elaboration.expressions.EConstant;\r
22 import org.simantics.scl.compiler.elaboration.expressions.ELiteral;\r
23 import org.simantics.scl.compiler.elaboration.expressions.EVariable;\r
24 import org.simantics.scl.compiler.elaboration.expressions.Expression;\r
25 import org.simantics.scl.compiler.elaboration.modules.SCLValue;\r
26 import org.simantics.scl.compiler.environment.Environment;\r
27 import org.simantics.scl.compiler.environment.specification.EnvironmentSpecification;\r
28 import org.simantics.scl.compiler.runtime.RuntimeEnvironment;\r
29 import org.simantics.scl.compiler.types.Type;\r
30 import org.simantics.scl.compiler.types.Types;\r
31 import org.simantics.scl.compiler.types.kinds.Kinds;\r
32 import org.simantics.scl.runtime.SCLContext;\r
33 import org.simantics.scl.runtime.function.Function1;\r
34 import org.simantics.structural2.scl.ComponentTypeProperty;\r
35 import org.simantics.structural2.scl.FindPossibleComponentTypeRequest;\r
36 import org.simantics.structural2.scl.ReadComponentTypeInterfaceRequest;\r
37 import org.simantics.utils.datastructures.Pair;\r
38 \r
39 public class ServerSCLValueRequest extends AbstractExpressionCompilationRequest<CompilationContext, Variable> {\r
40 \r
41         private final Pair<Resource,Resource> componentTypeAndRoot;\r
42         private final Resource literal;\r
43 \r
44         public static class CompilationContext extends AbstractExpressionCompilationContext {\r
45                 public final Map<String, ComponentTypeProperty> propertyMap;\r
46 \r
47                 public CompilationContext(RuntimeEnvironment runtimeEnvironment,\r
48                                 Map<String, ComponentTypeProperty> propertyMap) {\r
49                         super(runtimeEnvironment);\r
50                         this.propertyMap = propertyMap;\r
51                 }\r
52         }\r
53 \r
54         private ServerSCLValueRequest(Pair<Resource,Resource> componentTypeAndRoot, Resource literal) {\r
55                 assert(literal != null);\r
56                 this.literal = literal;\r
57                 this.componentTypeAndRoot = componentTypeAndRoot;\r
58         }\r
59 \r
60         public ServerSCLValueRequest(ReadGraph graph, Variable context) throws DatabaseException {\r
61                 this(getComponentTypeAndRoot(graph, context), context.getRepresents(graph));\r
62         }\r
63 \r
64         private static Pair<Resource,Resource> getComponentTypeAndRoot(ReadGraph graph, Variable property)  throws DatabaseException {\r
65                 Variable parent = property.getParent(graph);\r
66                 Resource represents = parent.getRepresents(graph);\r
67                 if(represents != null) {\r
68                         Resource type = graph.syncRequest(new FindPossibleComponentTypeRequest(represents));\r
69                         if(type != null) {\r
70                                 Resource root = graph.syncRequest(new IndexRoot(type));\r
71                                 return Pair.make(type, root);\r
72                         }\r
73                 }\r
74                 parent = parent.getParent(graph);\r
75                 Resource root = graph.syncRequest(new IndexRoot(property.getRepresents(graph)));\r
76                 return Pair.make(parent.getType(graph), root);\r
77         }\r
78 \r
79         public static Object compileAndEvaluate(ReadGraph graph, Variable context) throws DatabaseException {\r
80                 SCLContext sclContext = SCLContext.getCurrent();\r
81         Object oldGraph = sclContext.get("graph");\r
82                 try {\r
83                         Function1<Variable,Object> exp = graph.syncRequest(new ServerSCLValueRequest(graph, context),\r
84                                         TransientCacheListener.<Function1<Variable,Object>>instance());\r
85                         sclContext.put("graph", graph);\r
86                         return exp.apply(context);\r
87                 } catch (DatabaseException e) {\r
88                         throw (DatabaseException)e;\r
89                 } catch (Throwable t) {\r
90                         throw new DatabaseException(t);\r
91                 } finally {\r
92             sclContext.put("graph", oldGraph);\r
93                 }\r
94         }\r
95 \r
96         @Override\r
97         protected String getExpressionText(ReadGraph graph)\r
98                         throws DatabaseException {\r
99                 Layer0 L0 = Layer0.getInstance(graph);\r
100                 return graph.getRelatedValue(literal, L0.SCLValue_expression, Bindings.STRING);\r
101         }\r
102 \r
103         protected RuntimeEnvironmentRequest getRuntimeEnvironmentRequest(Resource indexRoot) {\r
104                 return new RuntimeEnvironmentRequest(indexRoot) {\r
105                         @Override\r
106                         protected void fillEnvironmentSpecification(\r
107                                         EnvironmentSpecification environmentSpecification) {\r
108                         }\r
109                 };\r
110         }\r
111 \r
112         @Override\r
113         protected CompilationContext getCompilationContext(ReadGraph graph)\r
114                         throws DatabaseException {\r
115                 return graph.syncRequest(new UnaryRead<Pair<Resource,Resource>,CompilationContext>(componentTypeAndRoot) {\r
116                         @Override\r
117                         public CompilationContext perform(ReadGraph graph)\r
118                                         throws DatabaseException {\r
119                                 RuntimeEnvironment runtimeEnvironment = graph.syncRequest(getRuntimeEnvironmentRequest(parameter.second));\r
120                                 Map<String, ComponentTypeProperty> propertyMap =\r
121                                                 graph.syncRequest(new ReadComponentTypeInterfaceRequest(parameter.first, runtimeEnvironment.getEnvironment()),\r
122                                                                 TransientCacheListener.<Map<String, ComponentTypeProperty>>instance());\r
123                                 return new CompilationContext(runtimeEnvironment, propertyMap);\r
124                         }\r
125                 });\r
126         }\r
127 \r
128         @Override\r
129         protected Type getContextVariableType() {\r
130                 return VARIABLE; \r
131         }\r
132 \r
133         private static Expression accessInputVariable(Environment environment,\r
134                         org.simantics.scl.compiler.elaboration.expressions.Variable contextVariable) {\r
135                 SCLValue variableParentFunction = environment.getValue(VARIABLE_PARENT);\r
136                 return new EApply(new EConstant(variableParentFunction),\r
137                                 new EApply(new EConstant(variableParentFunction),\r
138                                                 new EVariable(contextVariable)));\r
139         }\r
140 \r
141     protected static Name PROPERTY_VALUE_CACHED = Name.create("Document/All", "propertyValueCached");\r
142 \r
143     protected static Expression getProperty(Environment environment, Expression variable, String propertyName, Type type) {\r
144         return new EApply(\r
145                 new EConstant(environment.getValue(FROM_DYNAMIC), type),\r
146                 new EApply(\r
147                         new EConstant(environment.getValue(PROPERTY_VALUE_CACHED), Types.DYNAMIC),\r
148                         variable,\r
149                         new ELiteral(new StringConstant(propertyName))));\r
150     }\r
151     \r
152     protected static Expression getPropertyFlexible(Environment environment, Expression variable, String propertyName, Type type) {\r
153         return makeTypeFlexible(environment, getProperty(environment, variable, propertyName, type), type);\r
154     }\r
155         \r
156         protected static Expression standardGetProperty(\r
157                         Environment environment,\r
158                         org.simantics.scl.compiler.elaboration.expressions.Variable contextVariable,\r
159                         String name,\r
160                         Type type) {\r
161                 return getPropertyFlexible(environment, accessInputVariable(environment, contextVariable), name, type);\r
162         }\r
163 \r
164 \r
165         @Override\r
166         protected Expression getVariableAccessExpression(\r
167                         ReadGraph graph,\r
168                         CompilationContext context,\r
169                         org.simantics.scl.compiler.elaboration.expressions.Variable contextVariable,\r
170                         String name) throws DatabaseException {\r
171                 ComponentTypeProperty property = context.propertyMap.get(name);\r
172                 if(property != null)\r
173                         return standardGetProperty(\r
174                                         context.runtimeEnvironment.getEnvironment(),\r
175                                         contextVariable,\r
176                                         name,\r
177                                         property.type == null ? Types.metaVar(Kinds.STAR) : property.type);\r
178                 else\r
179                         return getSpecialVariableAccessExpression(graph, context, contextVariable, name);\r
180         }\r
181 \r
182         protected Expression getSpecialVariableAccessExpression(ReadGraph graph,\r
183                         CompilationContext context,\r
184                         org.simantics.scl.compiler.elaboration.expressions.Variable contextVariable,\r
185                         String name) throws DatabaseException {\r
186                 if(name.equals("input")) {\r
187                         Environment environment = context.runtimeEnvironment.getEnvironment();\r
188                         return accessInputVariable(environment, contextVariable);\r
189                 }\r
190                 else if(name.equals("self"))\r
191                         return new EVariable(contextVariable);\r
192                 else\r
193                         return null;\r
194         }\r
195 \r
196         @Override\r
197         public int hashCode() {\r
198                 return 31*(31*getClass().hashCode() + literal.hashCode()) + componentTypeAndRoot.hashCode();\r
199         }\r
200 \r
201         @Override\r
202         public boolean equals(Object obj) {\r
203                 if(this == obj)\r
204                         return true;\r
205                 if(obj == null || obj.getClass() != getClass())\r
206                         return false;\r
207                 ServerSCLValueRequest other = (ServerSCLValueRequest)obj;\r
208                 return literal.equals(other.literal) && componentTypeAndRoot.equals(other.componentTypeAndRoot);\r
209         }\r
210 }\r