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