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