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