]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.document.server/src/org/simantics/document/server/request/ServerSCLValueRequest.java
Variable optimizations for documents (Simupedia)
[simantics/platform.git] / bundles / org.simantics.document.server / src / org / simantics / document / server / request / ServerSCLValueRequest.java
1 package org.simantics.document.server.request;
2
3 import java.util.Map;
4
5 import org.simantics.databoard.Bindings;
6 import org.simantics.db.ReadGraph;
7 import org.simantics.db.Resource;
8 import org.simantics.db.common.procedure.adapter.TransientCacheListener;
9 import org.simantics.db.common.request.IndexRoot;
10 import org.simantics.db.common.request.PossibleTypedParent;
11 import org.simantics.db.common.request.UnaryRead;
12 import org.simantics.db.exception.DatabaseException;
13 import org.simantics.db.layer0.scl.AbstractExpressionCompilationContext;
14 import org.simantics.db.layer0.scl.AbstractExpressionCompilationRequest;
15 import org.simantics.db.layer0.util.RuntimeEnvironmentRequest2;
16 import org.simantics.db.layer0.variable.Variable;
17 import org.simantics.document.base.ontology.DocumentationResource;
18 import org.simantics.document.server.request.ServerSCLValueRequest.CompilationContext;
19 import org.simantics.layer0.Layer0;
20 import org.simantics.scl.compiler.common.names.Name;
21 import org.simantics.scl.compiler.constants.StringConstant;
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.ELiteral;
25 import org.simantics.scl.compiler.elaboration.expressions.EVariable;
26 import org.simantics.scl.compiler.elaboration.expressions.Expression;
27 import org.simantics.scl.compiler.elaboration.modules.SCLValue;
28 import org.simantics.scl.compiler.environment.Environment;
29 import org.simantics.scl.compiler.environment.Environments;
30 import org.simantics.scl.compiler.environment.specification.EnvironmentSpecification;
31 import org.simantics.scl.compiler.runtime.RuntimeEnvironment;
32 import org.simantics.scl.compiler.top.SCLExpressionCompilationException;
33 import org.simantics.scl.compiler.types.Type;
34 import org.simantics.scl.compiler.types.Types;
35 import org.simantics.scl.compiler.types.kinds.Kinds;
36 import org.simantics.scl.runtime.SCLContext;
37 import org.simantics.scl.runtime.function.Function1;
38 import org.simantics.structural2.scl.ComponentTypeProperty;
39 import org.simantics.structural2.scl.FindPossibleComponentTypeRequest;
40 import org.simantics.structural2.scl.ReadComponentTypeInterfaceRequest;
41 import org.simantics.utils.datastructures.Pair;
42 import org.slf4j.Logger;
43 import org.slf4j.LoggerFactory;
44
45 public class ServerSCLValueRequest extends AbstractExpressionCompilationRequest<CompilationContext, Object> {
46
47         private final Pair<Resource,Resource> componentTypeAndRoot;
48         private final Resource literal;
49     protected String possibleExpectedValueType;
50
51         public static class CompilationContext extends AbstractExpressionCompilationContext {
52                 public final Map<String, ComponentTypeProperty> propertyMap;
53
54                 public CompilationContext(RuntimeEnvironment runtimeEnvironment,
55                                 Map<String, ComponentTypeProperty> propertyMap) {
56                         super(runtimeEnvironment);
57                         this.propertyMap = propertyMap;
58                 }
59         }
60
61         private ServerSCLValueRequest(Pair<Resource,Resource> componentTypeAndRoot, Resource literal, String possibleExpectedValueType) {
62                 assert(literal != null);
63                 this.literal = literal;
64                 this.componentTypeAndRoot = componentTypeAndRoot;
65                 this.possibleExpectedValueType = possibleExpectedValueType;
66         }
67
68         public ServerSCLValueRequest(ReadGraph graph, Variable context) throws DatabaseException {
69                 this(getComponentTypeAndRoot(graph, context), context.getRepresents(graph), resolveExpectedValueType(graph, context.getPredicateResource(graph)));
70         }
71
72         public ServerSCLValueRequest(ReadGraph graph, Resource s, Resource o, Resource p) throws DatabaseException {
73             this(getComponentTypeAndRoot(graph, s), o, resolveExpectedValueType(graph, p));
74         }
75
76         private static Pair<Resource,Resource> getComponentTypeAndRoot(ReadGraph graph, Variable property)  throws DatabaseException {
77                 Variable parent = property.getParent(graph);
78                 Resource represents = parent.getRepresents(graph);
79                 if(represents != null) {
80                         Resource type = graph.syncRequest(new FindPossibleComponentTypeRequest(represents));
81                         if(type != null) {
82                                 Resource root = graph.syncRequest(new IndexRoot(type));
83                                 return Pair.make(type, root);
84                         }
85                 }
86                 parent = parent.getParent(graph);
87                 Resource root = graph.syncRequest(new IndexRoot(property.getRepresents(graph)));
88                 return Pair.make(parent.getType(graph), root);
89         }
90
91         private static Pair<Resource,Resource> getComponentTypeAndRoot(ReadGraph graph, Resource component)  throws DatabaseException {
92                 if(component != null) {
93                         Resource type = graph.syncRequest(new FindPossibleComponentTypeRequest(component));
94                         if(type != null) {
95                                 Resource root = graph.syncRequest(new IndexRoot(type));
96                                 //      System.err.println("getComponentTypeAndRoot3 " + graph.getPossibleURI(component) + " => " + graph.getPossibleURI(type) + " " + graph.getPossibleURI(root));
97                                 return Pair.make(type, root);
98                         } else {
99                                 Resource doc = graph.syncRequest(new PossibleTypedParent(component, DocumentationResource.getInstance(graph).Document));
100                                 if(doc != null) {
101                                         Resource componentType = graph.getSingleType(doc);
102                                         Resource root = graph.syncRequest(new IndexRoot(doc));
103                                         return Pair.make(componentType, root);
104                                 } else {
105                                         System.err.println("component = " + component);
106                                         Resource root = graph.syncRequest(new IndexRoot(component));
107 //                                      Resource componentType = graph.getSingleType(doc);
108                                         return Pair.make(null, root);
109                                 }
110                         }
111                 }
112                 throw new IllegalStateException();
113         }
114
115         public static Object compileAndEvaluate(ReadGraph graph, Variable context) throws DatabaseException {
116                 SCLContext sclContext = SCLContext.getCurrent();
117         Object oldGraph = sclContext.get("graph");
118                 try {
119                         Function1<Object,Object> exp = compile(graph, context);
120                         sclContext.put("graph", graph);
121                         return exp.apply(context);
122                 } catch (DatabaseException e) {
123                         throw (DatabaseException)e;
124                 } catch (Throwable t) {
125                         throw new DatabaseException(t);
126                 } finally {
127             sclContext.put("graph", oldGraph);
128                 }
129         }
130
131         public static Function1<Object, Object> compile(ReadGraph graph, Variable context) throws DatabaseException {
132                 return graph.syncRequest(new ServerSCLValueRequest(graph, context), TransientCacheListener.instance());
133         }
134
135         public static Function1<Object, Object> compile(ReadGraph graph, Resource s, Resource o, Resource p) throws DatabaseException {
136                 return graph.syncRequest(new ServerSCLValueRequest(graph, s, o, p), TransientCacheListener.instance());
137         }
138
139         @Override
140         protected String getExpressionText(ReadGraph graph)
141                         throws DatabaseException {
142                 Layer0 L0 = Layer0.getInstance(graph);
143                 return graph.getRelatedValue(literal, L0.SCLValue_expression, Bindings.STRING);
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)
157                         throws DatabaseException {
158                 return graph.syncRequest(new UnaryRead<Pair<Resource,Resource>,CompilationContext>(componentTypeAndRoot) {
159                         @Override
160                         public CompilationContext perform(ReadGraph graph)
161                                         throws DatabaseException {
162                                 RuntimeEnvironment runtimeEnvironment = graph.syncRequest(getRuntimeEnvironmentRequest(parameter.first, parameter.second));
163                                 Map<String, ComponentTypeProperty> propertyMap =
164                                                 graph.syncRequest(new ReadComponentTypeInterfaceRequest(parameter.first, runtimeEnvironment.getEnvironment()),
165                                                                 TransientCacheListener.<Map<String, ComponentTypeProperty>>instance());
166                                 return new CompilationContext(runtimeEnvironment, propertyMap);
167                         }
168                 });
169         }
170
171         @Override
172         protected Type getContextVariableType() {
173                 return VARIABLE; 
174         }
175
176         private static Expression accessInputVariable(Environment environment,
177                         org.simantics.scl.compiler.elaboration.expressions.Variable contextVariable) {
178                 SCLValue variableParentFunction = environment.getValue(VARIABLE_PARENT);
179                 return new EApply(new EConstant(variableParentFunction),
180                                 new EApply(new EConstant(variableParentFunction),
181                                                 new EVariable(contextVariable)));
182         }
183
184     protected static Name PROPERTY_VALUE_CACHED = Name.create("Document/All", "propertyValueCached");
185
186     protected static Expression getProperty(Environment environment, Expression variable, String propertyName, Type type) {
187         return new EApply(
188                 new EConstant(environment.getValue(FROM_DYNAMIC), type),
189                 new EApply(
190                         new EConstant(environment.getValue(PROPERTY_VALUE_CACHED), Types.DYNAMIC),
191                         variable,
192                         new ELiteral(new StringConstant(propertyName))));
193     }
194     
195     protected static Expression getPropertyFlexible(Environment environment, Expression variable, String propertyName, Type type) {
196         return makeTypeFlexible(environment, getProperty(environment, variable, propertyName, type), type);
197     }
198         
199         protected static Expression standardGetProperty(
200                         Environment environment,
201                         org.simantics.scl.compiler.elaboration.expressions.Variable contextVariable,
202                         String name,
203                         Type type) {
204                 return getPropertyFlexible(environment, accessInputVariable(environment, contextVariable), name, type);
205         }
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                         return getSpecialVariableAccessExpression(graph, context, contextVariable, name);
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                 }
233                 else if(name.equals("self"))
234                         return new EVariable(contextVariable);
235                 else
236                         return null;
237         }
238         
239     @Override
240     protected Type getExpectedType(ReadGraph graph, CompilationContext context) throws DatabaseException {
241         return super.getExpectedType(graph, context);
242     }
243
244
245         @Override
246         public int hashCode() {
247                 return 31*(31*getClass().hashCode() + literal.hashCode()) + componentTypeAndRoot.hashCode();
248         }
249
250         @Override
251         public boolean equals(Object obj) {
252                 if(this == obj)
253                         return true;
254                 if(obj == null || obj.getClass() != getClass())
255                         return false;
256                 ServerSCLValueRequest other = (ServerSCLValueRequest)obj;
257                 return literal.equals(other.literal) && componentTypeAndRoot.equals(other.componentTypeAndRoot);
258         }
259
260     public static Function1<Object, Object> validate(ReadGraph graph, Variable context) throws DatabaseException {
261         return graph.syncRequest(new ServerSCLValueValidationRequest(graph, context), TransientCacheListener.instance());
262     }
263     
264     public static class ServerSCLValueValidationRequest extends ServerSCLValueRequest {
265
266         private static final Logger LOGGER = LoggerFactory.getLogger(ServerSCLHandlerValueRequest.class);
267
268         public ServerSCLValueValidationRequest(ReadGraph graph, Variable context) throws DatabaseException {
269             super(graph, context);
270         }
271
272         @Override
273         protected Type getExpectedType(ReadGraph graph, CompilationContext context) throws DatabaseException {
274             if(possibleExpectedValueType != null) {
275                 try {
276                     return Environments.getType(context.runtimeEnvironment.getEnvironment(), possibleExpectedValueType);
277                 } catch (SCLExpressionCompilationException e) {
278                     LOGGER.error("Could not get type for " + String.valueOf(possibleExpectedValueType), e);
279                 }
280             }
281             return super.getExpectedType(graph, context);
282         }
283     }
284 }