1 package org.simantics.document.server.request;
\r
3 import java.util.HashMap;
\r
4 import java.util.List;
\r
5 import java.util.Map;
\r
7 import org.simantics.databoard.Bindings;
\r
8 import org.simantics.db.ReadGraph;
\r
9 import org.simantics.db.Resource;
\r
10 import org.simantics.db.common.procedure.adapter.TransientCacheListener;
\r
11 import org.simantics.db.common.request.IndexRoot;
\r
12 import org.simantics.db.exception.DatabaseException;
\r
13 import org.simantics.db.layer0.request.VariableRead;
\r
14 import org.simantics.db.layer0.scl.AbstractExpressionCompilationContext;
\r
15 import org.simantics.db.layer0.scl.AbstractExpressionCompilationRequest;
\r
16 import org.simantics.db.layer0.util.RuntimeEnvironmentRequest;
\r
17 import org.simantics.db.layer0.variable.Variable;
\r
18 import org.simantics.document.server.Functions;
\r
19 import org.simantics.document.server.bean.DataDefinition;
\r
20 import org.simantics.document.server.request.ServerSCLHandlerValueRequest.CompilationContext;
\r
21 import org.simantics.layer0.Layer0;
\r
22 import org.simantics.scl.compiler.constants.StringConstant;
\r
23 import org.simantics.scl.compiler.elaboration.expressions.EApply;
\r
24 import org.simantics.scl.compiler.elaboration.expressions.EConstant;
\r
25 import org.simantics.scl.compiler.elaboration.expressions.ELiteral;
\r
26 import org.simantics.scl.compiler.elaboration.expressions.EVar;
\r
27 import org.simantics.scl.compiler.elaboration.expressions.EVariable;
\r
28 import org.simantics.scl.compiler.elaboration.expressions.Expression;
\r
29 import org.simantics.scl.compiler.elaboration.modules.SCLValue;
\r
30 import org.simantics.scl.compiler.environment.Environment;
\r
31 import org.simantics.scl.compiler.environment.specification.EnvironmentSpecification;
\r
32 import org.simantics.scl.compiler.runtime.RuntimeEnvironment;
\r
33 import org.simantics.scl.compiler.types.TCon;
\r
34 import org.simantics.scl.compiler.types.Type;
\r
35 import org.simantics.scl.compiler.types.Types;
\r
36 import org.simantics.scl.compiler.types.kinds.Kinds;
\r
37 import org.simantics.scl.runtime.SCLContext;
\r
38 import org.simantics.scl.runtime.function.Function1;
\r
39 import org.simantics.structural2.scl.ComponentTypeProperty;
\r
40 import org.simantics.structural2.scl.FindPossibleComponentTypeRequest;
\r
41 import org.simantics.structural2.scl.ReadComponentTypeInterfaceRequest;
\r
42 import org.simantics.utils.datastructures.Pair;
\r
44 public class ServerSCLHandlerValueRequest extends AbstractExpressionCompilationRequest<CompilationContext, Variable> {
\r
46 private final Variable context;
\r
47 private final Pair<Resource,Resource> componentTypeAndRoot;
\r
48 private final Resource literal;
\r
50 public static class CompilationContext extends AbstractExpressionCompilationContext {
\r
51 public final Map<String, ComponentTypeProperty> propertyMap;
\r
53 public CompilationContext(RuntimeEnvironment runtimeEnvironment,
\r
54 Map<String, ComponentTypeProperty> propertyMap) {
\r
55 super(runtimeEnvironment);
\r
56 this.propertyMap = propertyMap;
\r
60 private ServerSCLHandlerValueRequest(Variable context, Pair<Resource,Resource> componentTypeAndRoot, Resource literal) {
\r
61 assert(literal != null);
\r
62 this.context = context;
\r
63 this.literal = literal;
\r
64 this.componentTypeAndRoot = componentTypeAndRoot;
\r
67 public ServerSCLHandlerValueRequest(ReadGraph graph, Variable context) throws DatabaseException {
\r
68 this(context, getComponentTypeAndRoot(graph, context), context.getRepresents(graph));
\r
71 private static Pair<Resource,Resource> getComponentTypeAndRoot(ReadGraph graph, Variable property) throws DatabaseException {
\r
72 Variable parent = property.getParent(graph);
\r
73 Resource represents = parent.getRepresents(graph);
\r
74 if(represents != null) {
\r
75 Resource type = graph.syncRequest(new FindPossibleComponentTypeRequest(represents));
\r
77 Resource root = graph.syncRequest(new IndexRoot(type));
\r
78 return Pair.make(type, root);
\r
81 parent = parent.getParent(graph);
\r
82 Resource root = graph.syncRequest(new IndexRoot(property.getRepresents(graph)));
\r
83 return Pair.make(parent.getType(graph), root);
\r
87 public static List<TCon> getEffects(ReadGraph graph, Variable context) throws DatabaseException {
\r
89 ServerSCLHandlerValueRequest req = new ServerSCLHandlerValueRequest(graph, context);
\r
90 return req.getExpressionEffects(graph);
\r
91 } catch (DatabaseException e) {
\r
92 throw (DatabaseException)e;
\r
93 } catch (Throwable t) {
\r
94 throw new DatabaseException(t);
\r
98 public static Object compileAndEvaluate(ReadGraph graph, Variable context) throws DatabaseException {
\r
99 SCLContext sclContext = SCLContext.getCurrent();
\r
100 Object oldGraph = sclContext.get("graph");
\r
102 Function1<Variable,Object> exp = graph.syncRequest(new ServerSCLHandlerValueRequest(graph, context),
\r
103 TransientCacheListener.<Function1<Variable,Object>>instance());
\r
104 sclContext.put("graph", graph);
\r
105 return exp.apply(context);
\r
106 } catch (DatabaseException e) {
\r
107 throw (DatabaseException)e;
\r
108 } catch (Throwable t) {
\r
109 throw new DatabaseException(t);
\r
111 sclContext.put("graph", oldGraph);
\r
116 protected String getExpressionText(ReadGraph graph)
\r
117 throws DatabaseException {
\r
118 Layer0 L0 = Layer0.getInstance(graph);
\r
119 String exp = graph.getRelatedValue(literal, L0.SCLValue_expression, Bindings.STRING);
\r
120 return "\\context -> " + exp;
\r
123 protected RuntimeEnvironmentRequest getRuntimeEnvironmentRequest(Resource indexRoot) {
\r
124 return new RuntimeEnvironmentRequest(indexRoot) {
\r
126 protected void fillEnvironmentSpecification(
\r
127 EnvironmentSpecification environmentSpecification) {
\r
133 protected CompilationContext getCompilationContext(ReadGraph graph) throws DatabaseException {
\r
135 return graph.syncRequest(new VariableRead<CompilationContext>(context) {
\r
138 public CompilationContext perform(ReadGraph graph) throws DatabaseException {
\r
140 Pair<Resource,Resource> parameter = getComponentTypeAndRoot(graph, variable);
\r
141 RuntimeEnvironment runtimeEnvironment = graph.syncRequest(getRuntimeEnvironmentRequest(parameter.second));
\r
143 Map<String, ComponentTypeProperty> propertyMap =
\r
144 graph.syncRequest(new ReadComponentTypeInterfaceRequest(parameter.first, runtimeEnvironment.getEnvironment()),
\r
145 TransientCacheListener.<Map<String, ComponentTypeProperty>>instance());
\r
147 // Map<String, ComponentTypeProperty> result = new HashMap<String,ComponentTypeProperty>(propertyMap);
\r
148 // for(DataDefinition dd : Functions.dataDefinitions(graph, variable)) {
\r
149 // result.put(dd.target, null);
\r
152 return new CompilationContext(runtimeEnvironment, propertyMap);
\r
161 protected Type getContextVariableType() {
\r
165 private static Expression accessInputVariable(Environment environment,
\r
166 org.simantics.scl.compiler.elaboration.expressions.Variable contextVariable) {
\r
167 SCLValue variableParentFunction = environment.getValue(VARIABLE_PARENT);
\r
168 return new EApply(new EConstant(variableParentFunction),
\r
169 new EApply(new EConstant(variableParentFunction),
\r
170 new EVariable(contextVariable)));
\r
173 protected static Expression standardGetProperty(
\r
174 Environment environment,
\r
175 org.simantics.scl.compiler.elaboration.expressions.Variable contextVariable,
\r
178 return getPropertyFlexible(environment, accessInputVariable(environment, contextVariable), name, type);
\r
182 protected Expression getVariableAccessExpression(
\r
184 CompilationContext context,
\r
185 org.simantics.scl.compiler.elaboration.expressions.Variable contextVariable,
\r
186 String name) throws DatabaseException {
\r
187 ComponentTypeProperty property = context.propertyMap.get(name);
\r
188 if(property != null)
\r
189 return standardGetProperty(
\r
190 context.runtimeEnvironment.getEnvironment(),
\r
193 property.type == null ? Types.metaVar(Kinds.STAR) : property.type);
\r
196 // if(context.propertyMap.containsKey(name)) {
\r
198 // org.simantics.scl.compiler.elaboration.expressions.Variable parametersVariable = new org.simantics.scl.compiler.elaboration.expressions.Variable("context", COMMAND_CONTEXT);
\r
200 // Environment environment = context.runtimeEnvironment.getEnvironment();
\r
202 //// return new EApply(
\r
203 //// new EConstant(environment.getValue(FROM_DYNAMIC), Types.STRING),
\r
204 // return new EApply(
\r
205 // new EConstant(environment.getValue(CONTEXT_VARIABLE), Types.DYNAMIC),
\r
206 // new EVariable(parametersVariable),
\r
207 // new ELiteral(new StringConstant(name)));
\r
211 return getSpecialVariableAccessExpression(graph, context, contextVariable, name);
\r
216 protected Expression getSpecialVariableAccessExpression(ReadGraph graph,
\r
217 CompilationContext context,
\r
218 org.simantics.scl.compiler.elaboration.expressions.Variable contextVariable,
\r
219 String name) throws DatabaseException {
\r
220 if(name.equals("input")) {
\r
221 Environment environment = context.runtimeEnvironment.getEnvironment();
\r
222 return accessInputVariable(environment, contextVariable);
\r
223 } else if(name.equals("self"))
\r
224 return new EVariable(contextVariable);
\r
230 public int hashCode() {
\r
231 final int prime = 31;
\r
233 result = prime * result + ((context == null) ? 0 : context.hashCode());
\r
238 public boolean equals(Object obj) {
\r
243 if (getClass() != obj.getClass())
\r
245 ServerSCLHandlerValueRequest other = (ServerSCLHandlerValueRequest) obj;
\r
246 if (context == null) {
\r
247 if (other.context != null)
\r
249 } else if (!context.equals(other.context))
\r
255 // public int hashCode() {
\r
256 // return 31*(31*getClass().hashCode() + literal.hashCode()) + componentTypeAndRoot.hashCode();
\r
260 // public boolean equals(Object obj) {
\r
263 // if(obj == null || obj.getClass() != getClass())
\r
265 // ServerSCLHandlerValueRequest other = (ServerSCLHandlerValueRequest)obj;
\r
266 // return literal.equals(other.literal) && componentTypeAndRoot.equals(other.componentTypeAndRoot);
\r