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