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