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