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