]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.document.server/src/org/simantics/document/server/request/ServerSCLValueRequest.java
Still working for multiple readers
[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.Collections;
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.PossibleTypedParent;
12 import org.simantics.db.common.request.UnaryRead;
13 import org.simantics.db.exception.DatabaseException;
14 import org.simantics.db.layer0.scl.AbstractExpressionCompilationContext;
15 import org.simantics.db.layer0.scl.AbstractExpressionCompilationRequest;
16 import org.simantics.db.layer0.util.RuntimeEnvironmentRequest2;
17 import org.simantics.db.layer0.variable.Variable;
18 import org.simantics.document.base.ontology.DocumentationResource;
19 import org.simantics.document.server.request.ServerSCLValueRequest.CompilationContext;
20 import org.simantics.layer0.Layer0;
21 import org.simantics.scl.compiler.common.names.Name;
22 import org.simantics.scl.compiler.constants.StringConstant;
23 import org.simantics.scl.compiler.elaboration.expressions.EApply;
24 import org.simantics.scl.compiler.elaboration.expressions.EConstant;
25 import org.simantics.scl.compiler.elaboration.expressions.ELiteral;
26 import org.simantics.scl.compiler.elaboration.expressions.EVariable;
27 import org.simantics.scl.compiler.elaboration.expressions.Expression;
28 import org.simantics.scl.compiler.elaboration.modules.SCLValue;
29 import org.simantics.scl.compiler.environment.Environment;
30 import org.simantics.scl.compiler.environment.Environments;
31 import org.simantics.scl.compiler.environment.specification.EnvironmentSpecification;
32 import org.simantics.scl.compiler.runtime.RuntimeEnvironment;
33 import org.simantics.scl.compiler.top.SCLExpressionCompilationException;
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 final Pair<Resource,Resource> componentTypeAndRoot;
49         private final Resource literal;
50     protected String possibleExpectedValueType;
51
52         public static class CompilationContext extends AbstractExpressionCompilationContext {
53                 public final Map<String, ComponentTypeProperty> propertyMap;
54
55                 public CompilationContext(RuntimeEnvironment runtimeEnvironment,
56                                 Map<String, ComponentTypeProperty> propertyMap) {
57                         super(runtimeEnvironment);
58                         this.propertyMap = propertyMap;
59                 }
60         }
61
62         private ServerSCLValueRequest(Pair<Resource,Resource> componentTypeAndRoot, Resource literal, String possibleExpectedValueType) {
63                 assert(literal != null);
64                 this.literal = literal;
65                 this.componentTypeAndRoot = componentTypeAndRoot;
66                 this.possibleExpectedValueType = possibleExpectedValueType;
67         }
68
69         public ServerSCLValueRequest(ReadGraph graph, Variable context) throws DatabaseException {
70                 this(getComponentTypeAndRoot(graph, context), context.getRepresents(graph), resolveExpectedValueType(graph, context.getPredicateResource(graph)));
71         }
72
73         public ServerSCLValueRequest(ReadGraph graph, Resource s, Resource o, Resource p) throws DatabaseException {
74                 this(getComponentTypeAndRoot(graph, s), o, resolveExpectedValueType(graph, p));
75         }
76
77         private static Pair<Resource,Resource> getComponentTypeAndRoot(ReadGraph graph, Variable property)  throws DatabaseException {
78                 Variable parent = property.getParent(graph);
79                 Resource represents = parent.getRepresents(graph);
80                 if(represents != null) {
81                         Resource type = graph.syncRequest(new FindPossibleComponentTypeRequest(represents));
82                         if(type != null) {
83                                 Resource root = graph.syncRequest(new IndexRoot(type));
84                                 //System.err.println("getComponentTypeAndRoot1 " + property.getURI(graph) + " => " + graph.getPossibleURI(type) + " " + graph.getPossibleURI(root));
85                                 return Pair.make(type, root);
86                         }
87                 }
88                 while(represents == null) {
89                         parent = parent.getParent(graph);
90                         represents = parent.getPossibleRepresents(graph);
91                 }
92 //              Resource root = graph.syncRequest(new IndexRoot(property.getRepresents(graph)));
93         Resource componentType = parent.getType(graph);
94         Resource root = graph.syncRequest(new IndexRoot(parent.getRepresents(graph)));
95         //System.err.println("getComponentTypeAndRoot2 " + property.getURI(graph) + " => " + graph.getPossibleURI(componentType) + " " + graph.getPossibleURI(root));
96                 return Pair.make(componentType, root);
97         }
98
99         private static Pair<Resource,Resource> getComponentTypeAndRoot(ReadGraph graph, Resource component)  throws DatabaseException {
100                 if(component != null) {
101                         Resource type = graph.syncRequest(new FindPossibleComponentTypeRequest(component));
102                         if(type != null) {
103                                 Resource root = graph.syncRequest(new IndexRoot(type));
104           //      System.err.println("getComponentTypeAndRoot3 " + graph.getPossibleURI(component) + " => " + graph.getPossibleURI(type) + " " + graph.getPossibleURI(root));
105                                 return Pair.make(type, root);
106                         } else {
107                                 Resource doc = graph.syncRequest(new PossibleTypedParent(component, DocumentationResource.getInstance(graph).Document));
108                                 if(doc != null) {
109                                         Resource componentType = graph.getSingleType(doc);
110                         Resource root = graph.syncRequest(new IndexRoot(doc));
111                                         return Pair.make(componentType, root);
112                                 } else {
113                                         System.err.println("component = " + component);
114                                         Resource root = graph.syncRequest(new IndexRoot(component));
115 //                                      Resource componentType = graph.getSingleType(doc);
116                                         return Pair.make(null, root);
117                                 }
118                         }
119                 }
120                 throw new IllegalStateException();
121         }
122
123         public static Object compileAndEvaluate(ReadGraph graph, Variable context) throws DatabaseException {
124                 SCLContext sclContext = SCLContext.getCurrent();
125         Object oldGraph = sclContext.get("graph");
126                 try {
127                         Function1<Variable,Object> exp = compile(graph, context);
128                         sclContext.put("graph", graph);
129                         return exp.apply(context);
130                 } catch (DatabaseException e) {
131                         throw (DatabaseException)e;
132                 } catch (Throwable t) {
133                         throw new DatabaseException(t);
134                 } finally {
135             sclContext.put("graph", oldGraph);
136                 }
137         }
138         
139         public static Function1<Variable, Object> compile(ReadGraph graph, Variable context) throws DatabaseException {
140             return graph.syncRequest(new ServerSCLValueRequest(graph, context), TransientCacheListener.<Function1<Variable,Object>>instance());
141         }
142
143         public static Function1<Variable, Object> compile(ReadGraph graph, Resource s, Resource o, Resource p) throws DatabaseException {
144             return graph.syncRequest(new ServerSCLValueRequest(graph, s, o, p), TransientCacheListener.<Function1<Variable,Object>>instance());
145         }
146
147         @Override
148         protected String getExpressionText(ReadGraph graph)
149                         throws DatabaseException {
150                 Layer0 L0 = Layer0.getInstance(graph);
151                 return graph.getRelatedValue(literal, L0.SCLValue_expression, Bindings.STRING);
152         }
153
154         protected RuntimeEnvironmentRequest2 getRuntimeEnvironmentRequest(Resource componentType, Resource indexRoot) {
155                 return new RuntimeEnvironmentRequest2(componentType, indexRoot) {
156                         @Override
157                         protected void fillEnvironmentSpecification(
158                                         EnvironmentSpecification environmentSpecification) {
159                         }
160                 };
161         }
162
163         @Override
164         protected CompilationContext getCompilationContext(ReadGraph graph)
165                         throws DatabaseException {
166                 return graph.syncRequest(new UnaryRead<Pair<Resource,Resource>,CompilationContext>(componentTypeAndRoot) {
167                         @Override
168                         public CompilationContext perform(ReadGraph graph)
169                                         throws DatabaseException {
170                                 RuntimeEnvironment runtimeEnvironment = graph.syncRequest(getRuntimeEnvironmentRequest(parameter.first, parameter.second));
171                                 if(parameter.first != null) {
172                                         Map<String, ComponentTypeProperty> propertyMap =
173                                                         graph.syncRequest(new ReadComponentTypeInterfaceRequest(parameter.first, runtimeEnvironment.getEnvironment()),
174                                                                         TransientCacheListener.<Map<String, ComponentTypeProperty>>instance());
175                                         return new CompilationContext(runtimeEnvironment, propertyMap);
176                                 } else {
177                                         return new CompilationContext(runtimeEnvironment, Collections.emptyMap());
178                                 }
179                         }
180                 });
181         }
182
183         @Override
184         protected Type getContextVariableType() {
185                 return VARIABLE; 
186         }
187
188         private static Expression accessInputVariable(Environment environment,
189                         org.simantics.scl.compiler.elaboration.expressions.Variable contextVariable) {
190                 SCLValue variableParentFunction = environment.getValue(VARIABLE_PARENT);
191                 return new EApply(new EConstant(variableParentFunction),
192                                 new EApply(new EConstant(variableParentFunction),
193                                                 new EVariable(contextVariable)));
194         }
195
196     protected static Name PROPERTY_VALUE_CACHED = Name.create("Document/All", "propertyValueCached");
197
198     protected static Expression getProperty(Environment environment, Expression variable, String propertyName, Type type) {
199         return new EApply(
200                 new EConstant(environment.getValue(FROM_DYNAMIC), type),
201                 new EApply(
202                         new EConstant(environment.getValue(PROPERTY_VALUE_CACHED), Types.DYNAMIC),
203                         variable,
204                         new ELiteral(new StringConstant(propertyName))));
205     }
206     
207     protected static Expression getPropertyFlexible(Environment environment, Expression variable, String propertyName, Type type) {
208         return makeTypeFlexible(environment, getProperty(environment, variable, propertyName, type), type);
209     }
210         
211         protected static Expression standardGetProperty(
212                         Environment environment,
213                         org.simantics.scl.compiler.elaboration.expressions.Variable contextVariable,
214                         String name,
215                         Type type) {
216                 return getPropertyFlexible(environment, accessInputVariable(environment, contextVariable), name, type);
217         }
218
219
220         @Override
221         protected Expression getVariableAccessExpression(
222                         ReadGraph graph,
223                         CompilationContext context,
224                         org.simantics.scl.compiler.elaboration.expressions.Variable contextVariable,
225                         String name) throws DatabaseException {
226                 ComponentTypeProperty property = context.propertyMap.get(name);
227                 if(property != null)
228                         return standardGetProperty(
229                                         context.runtimeEnvironment.getEnvironment(),
230                                         contextVariable,
231                                         name,
232                                         property.type == null ? Types.metaVar(Kinds.STAR) : property.type);
233                 else
234                         return getSpecialVariableAccessExpression(graph, context, contextVariable, name);
235         }
236
237         protected Expression getSpecialVariableAccessExpression(ReadGraph graph,
238                         CompilationContext context,
239                         org.simantics.scl.compiler.elaboration.expressions.Variable contextVariable,
240                         String name) throws DatabaseException {
241                 if(name.equals("input")) {
242                         Environment environment = context.runtimeEnvironment.getEnvironment();
243                         return accessInputVariable(environment, contextVariable);
244                 }
245                 else if(name.equals("self"))
246                         return new EVariable(contextVariable);
247                 else
248                         return null;
249         }
250         
251     @Override
252     protected Type getExpectedType(ReadGraph graph, CompilationContext context) throws DatabaseException {
253         return super.getExpectedType(graph, context);
254     }
255
256
257         @Override
258         public int hashCode() {
259                 return 31*(31*getClass().hashCode() + literal.hashCode()) + componentTypeAndRoot.hashCode();
260         }
261
262         @Override
263         public boolean equals(Object obj) {
264                 if(this == obj)
265                         return true;
266                 if(obj == null || obj.getClass() != getClass())
267                         return false;
268                 ServerSCLValueRequest other = (ServerSCLValueRequest)obj;
269                 return literal.equals(other.literal) && componentTypeAndRoot.equals(other.componentTypeAndRoot);
270         }
271
272     public static Function1<Variable, Object> validate(ReadGraph graph, Variable context) throws DatabaseException {
273         return graph.syncRequest(new ServerSCLValueValidationRequest(graph, context), TransientCacheListener.<Function1<Variable,Object>>instance());
274     }
275     
276     public static class ServerSCLValueValidationRequest extends ServerSCLValueRequest {
277
278         private static final Logger LOGGER = LoggerFactory.getLogger(ServerSCLHandlerValueRequest.class);
279
280         public ServerSCLValueValidationRequest(ReadGraph graph, Variable context) throws DatabaseException {
281             super(graph, context);
282         }
283
284         @Override
285         protected Type getExpectedType(ReadGraph graph, CompilationContext context) throws DatabaseException {
286             if(possibleExpectedValueType != null) {
287                 try {
288                     return Environments.getType(context.runtimeEnvironment.getEnvironment(), possibleExpectedValueType);
289                 } catch (SCLExpressionCompilationException e) {
290                     LOGGER.error("Could not get type for " + String.valueOf(possibleExpectedValueType), e);
291                 }
292             }
293             return super.getExpectedType(graph, context);
294         }
295     }
296 }