1 package org.simantics.document.server.request;
6 import org.simantics.databoard.Bindings;
7 import org.simantics.db.ReadGraph;
8 import org.simantics.db.Resource;
9 import org.simantics.db.common.procedure.adapter.TransientCacheListener;
10 import org.simantics.db.common.request.IndexRoot;
11 import org.simantics.db.exception.DatabaseException;
12 import org.simantics.db.layer0.request.VariableRead;
13 import org.simantics.db.layer0.scl.AbstractExpressionCompilationContext;
14 import org.simantics.db.layer0.scl.AbstractExpressionCompilationRequest;
15 import org.simantics.db.layer0.util.RuntimeEnvironmentRequest2;
16 import org.simantics.db.layer0.variable.Variable;
17 import org.simantics.document.server.request.ServerSCLHandlerValueRequest.CompilationContext;
18 import org.simantics.layer0.Layer0;
19 import org.simantics.scl.compiler.elaboration.expressions.EApply;
20 import org.simantics.scl.compiler.elaboration.expressions.EConstant;
21 import org.simantics.scl.compiler.elaboration.expressions.EVariable;
22 import org.simantics.scl.compiler.elaboration.expressions.Expression;
23 import org.simantics.scl.compiler.elaboration.modules.SCLValue;
24 import org.simantics.scl.compiler.environment.Environment;
25 import org.simantics.scl.compiler.environment.specification.EnvironmentSpecification;
26 import org.simantics.scl.compiler.runtime.RuntimeEnvironment;
27 import org.simantics.scl.compiler.types.TCon;
28 import org.simantics.scl.compiler.types.Type;
29 import org.simantics.scl.compiler.types.Types;
30 import org.simantics.scl.compiler.types.kinds.Kinds;
31 import org.simantics.scl.runtime.SCLContext;
32 import org.simantics.scl.runtime.function.Function1;
33 import org.simantics.structural2.scl.ComponentTypeProperty;
34 import org.simantics.structural2.scl.FindPossibleComponentTypeRequest;
35 import org.simantics.structural2.scl.ReadComponentTypeInterfaceRequest;
36 import org.simantics.utils.datastructures.Pair;
38 public class ServerSCLHandlerValueRequest extends AbstractExpressionCompilationRequest<CompilationContext, Variable> {
40 private final Variable context;
41 private final Pair<Resource,Resource> componentTypeAndRoot;
42 private final Resource literal;
44 public static class CompilationContext extends AbstractExpressionCompilationContext {
45 public final Map<String, ComponentTypeProperty> propertyMap;
47 public CompilationContext(RuntimeEnvironment runtimeEnvironment,
48 Map<String, ComponentTypeProperty> propertyMap) {
49 super(runtimeEnvironment);
50 this.propertyMap = propertyMap;
54 private ServerSCLHandlerValueRequest(Variable context, Pair<Resource,Resource> componentTypeAndRoot, Resource literal) {
55 assert(literal != null);
56 this.context = context;
57 this.literal = literal;
58 this.componentTypeAndRoot = componentTypeAndRoot;
61 public ServerSCLHandlerValueRequest(ReadGraph graph, Variable context) throws DatabaseException {
62 this(context, getComponentTypeAndRoot(graph, context), context.getRepresents(graph));
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));
71 Resource root = graph.syncRequest(new IndexRoot(type));
72 return Pair.make(type, root);
75 parent = parent.getParent(graph);
76 Resource root = graph.syncRequest(new IndexRoot(property.getRepresents(graph)));
77 return Pair.make(parent.getType(graph), root);
81 public static List<TCon> getEffects(ReadGraph graph, Variable context) throws DatabaseException {
83 ServerSCLHandlerValueRequest req = new ServerSCLHandlerValueRequest(graph, context);
84 return req.getExpressionEffects(graph);
85 } catch (DatabaseException e) {
86 throw (DatabaseException)e;
87 } catch (Throwable t) {
88 throw new DatabaseException(t);
92 public static Object compileAndEvaluate(ReadGraph graph, Variable context) throws DatabaseException {
93 SCLContext sclContext = SCLContext.getCurrent();
94 Object oldGraph = sclContext.get("graph");
96 Function1<Variable,Object> exp = graph.syncRequest(new ServerSCLHandlerValueRequest(graph, context),
97 TransientCacheListener.<Function1<Variable,Object>>instance());
98 sclContext.put("graph", graph);
99 return exp.apply(context);
100 } catch (DatabaseException e) {
101 throw (DatabaseException)e;
102 } catch (Throwable t) {
103 throw new DatabaseException(t);
105 sclContext.put("graph", oldGraph);
110 protected String getExpressionText(ReadGraph graph)
111 throws DatabaseException {
112 Layer0 L0 = Layer0.getInstance(graph);
113 String exp = graph.getRelatedValue(literal, L0.SCLValue_expression, Bindings.STRING);
114 return "\\context -> " + exp;
117 protected RuntimeEnvironmentRequest2 getRuntimeEnvironmentRequest(Resource componentType, Resource indexRoot) {
118 return new RuntimeEnvironmentRequest2(componentType, indexRoot) {
120 protected void fillEnvironmentSpecification(
121 EnvironmentSpecification environmentSpecification) {
127 protected CompilationContext getCompilationContext(ReadGraph graph) throws DatabaseException {
129 return graph.syncRequest(new VariableRead<CompilationContext>(context) {
132 public CompilationContext perform(ReadGraph graph) throws DatabaseException {
134 Pair<Resource,Resource> parameter = getComponentTypeAndRoot(graph, variable);
135 RuntimeEnvironment runtimeEnvironment = graph.syncRequest(getRuntimeEnvironmentRequest(parameter.first, parameter.second));
137 Map<String, ComponentTypeProperty> propertyMap =
138 graph.syncRequest(new ReadComponentTypeInterfaceRequest(parameter.first, runtimeEnvironment.getEnvironment()),
139 TransientCacheListener.<Map<String, ComponentTypeProperty>>instance());
141 // Map<String, ComponentTypeProperty> result = new HashMap<String,ComponentTypeProperty>(propertyMap);
142 // for(DataDefinition dd : Functions.dataDefinitions(graph, variable)) {
143 // result.put(dd.target, null);
146 return new CompilationContext(runtimeEnvironment, propertyMap);
155 protected Type getContextVariableType() {
159 private static Expression accessInputVariable(Environment environment,
160 org.simantics.scl.compiler.elaboration.expressions.Variable contextVariable) {
161 SCLValue variableParentFunction = environment.getValue(VARIABLE_PARENT);
162 return new EApply(new EConstant(variableParentFunction),
163 new EApply(new EConstant(variableParentFunction),
164 new EVariable(contextVariable)));
167 protected static Expression standardGetProperty(
168 Environment environment,
169 org.simantics.scl.compiler.elaboration.expressions.Variable contextVariable,
172 return getPropertyFlexible(environment, accessInputVariable(environment, contextVariable), name, type);
176 protected Expression getVariableAccessExpression(
178 CompilationContext context,
179 org.simantics.scl.compiler.elaboration.expressions.Variable contextVariable,
180 String name) throws DatabaseException {
181 ComponentTypeProperty property = context.propertyMap.get(name);
183 return standardGetProperty(
184 context.runtimeEnvironment.getEnvironment(),
187 property.type == null ? Types.metaVar(Kinds.STAR) : property.type);
190 // if(context.propertyMap.containsKey(name)) {
192 // org.simantics.scl.compiler.elaboration.expressions.Variable parametersVariable = new org.simantics.scl.compiler.elaboration.expressions.Variable("context", COMMAND_CONTEXT);
194 // Environment environment = context.runtimeEnvironment.getEnvironment();
196 //// return new EApply(
197 //// new EConstant(environment.getValue(FROM_DYNAMIC), Types.STRING),
198 // return new EApply(
199 // new EConstant(environment.getValue(CONTEXT_VARIABLE), Types.DYNAMIC),
200 // new EVariable(parametersVariable),
201 // new ELiteral(new StringConstant(name)));
205 return getSpecialVariableAccessExpression(graph, context, contextVariable, name);
210 protected Expression getSpecialVariableAccessExpression(ReadGraph graph,
211 CompilationContext context,
212 org.simantics.scl.compiler.elaboration.expressions.Variable contextVariable,
213 String name) throws DatabaseException {
214 if(name.equals("input")) {
215 Environment environment = context.runtimeEnvironment.getEnvironment();
216 return accessInputVariable(environment, contextVariable);
217 } else if(name.equals("self"))
218 return new EVariable(contextVariable);
224 public int hashCode() {
225 final int prime = 31;
227 result = prime * result + ((context == null) ? 0 : context.hashCode());
232 public boolean equals(Object obj) {
237 if (getClass() != obj.getClass())
239 ServerSCLHandlerValueRequest other = (ServerSCLHandlerValueRequest) obj;
240 if (context == null) {
241 if (other.context != null)
243 } else if (!context.equals(other.context))
249 // public int hashCode() {
250 // return 31*(31*getClass().hashCode() + literal.hashCode()) + componentTypeAndRoot.hashCode();
254 // public boolean equals(Object obj) {
257 // if(obj == null || obj.getClass() != getClass())
259 // ServerSCLHandlerValueRequest other = (ServerSCLHandlerValueRequest)obj;
260 // return literal.equals(other.literal) && componentTypeAndRoot.equals(other.componentTypeAndRoot);