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;
43 protected String possibleExpectedValueType;
45 public static class CompilationContext extends AbstractExpressionCompilationContext {
46 public final Map<String, ComponentTypeProperty> propertyMap;
48 public CompilationContext(RuntimeEnvironment runtimeEnvironment,
49 Map<String, ComponentTypeProperty> propertyMap) {
50 super(runtimeEnvironment);
51 this.propertyMap = propertyMap;
55 private ServerSCLHandlerValueRequest(Variable context, Pair<Resource,Resource> componentTypeAndRoot, Resource literal, String possibleExpectedValueType) {
56 assert(literal != null);
57 this.context = context;
58 this.literal = literal;
59 this.componentTypeAndRoot = componentTypeAndRoot;
60 this.possibleExpectedValueType = possibleExpectedValueType;
63 public ServerSCLHandlerValueRequest(ReadGraph graph, Variable context) throws DatabaseException {
64 this(context, getComponentTypeAndRoot(graph, context), context.getRepresents(graph), resolveExpectedValueType(graph, context));
67 private static Pair<Resource,Resource> getComponentTypeAndRoot(ReadGraph graph, Variable property) throws DatabaseException {
68 Variable parent = property.getParent(graph);
69 Resource represents = parent.getRepresents(graph);
70 if(represents != null) {
71 Resource type = graph.syncRequest(new FindPossibleComponentTypeRequest(represents));
73 Resource root = graph.syncRequest(new IndexRoot(type));
74 return Pair.make(type, root);
77 parent = parent.getParent(graph);
78 Resource root = graph.syncRequest(new IndexRoot(property.getRepresents(graph)));
79 return Pair.make(parent.getType(graph), root);
83 public static List<TCon> getEffects(ReadGraph graph, Variable context) throws DatabaseException {
85 ServerSCLHandlerValueRequest req = new ServerSCLHandlerValueRequest(graph, context);
86 return req.getExpressionEffects(graph);
87 } catch (DatabaseException e) {
88 throw (DatabaseException)e;
89 } catch (Throwable t) {
90 throw new DatabaseException(t);
94 public static Object compileAndEvaluate(ReadGraph graph, Variable context) throws DatabaseException {
95 SCLContext sclContext = SCLContext.getCurrent();
96 Object oldGraph = sclContext.get("graph");
98 Function1<Variable,Object> exp = graph.syncRequest(new ServerSCLHandlerValueRequest(graph, context),
99 TransientCacheListener.<Function1<Variable,Object>>instance());
100 sclContext.put("graph", graph);
101 return exp.apply(context);
102 } catch (DatabaseException e) {
103 throw (DatabaseException)e;
104 } catch (Throwable t) {
105 throw new DatabaseException(t);
107 sclContext.put("graph", oldGraph);
112 protected String getExpressionText(ReadGraph graph)
113 throws DatabaseException {
114 Layer0 L0 = Layer0.getInstance(graph);
115 String exp = graph.getRelatedValue(literal, L0.SCLValue_expression, Bindings.STRING);
116 return "\\context -> " + exp;
119 protected RuntimeEnvironmentRequest2 getRuntimeEnvironmentRequest(Resource componentType, Resource indexRoot) {
120 return new RuntimeEnvironmentRequest2(componentType, indexRoot) {
122 protected void fillEnvironmentSpecification(
123 EnvironmentSpecification environmentSpecification) {
129 protected CompilationContext getCompilationContext(ReadGraph graph) throws DatabaseException {
131 return graph.syncRequest(new VariableRead<CompilationContext>(context) {
134 public CompilationContext perform(ReadGraph graph) throws DatabaseException {
136 Pair<Resource,Resource> parameter = getComponentTypeAndRoot(graph, variable);
137 RuntimeEnvironment runtimeEnvironment = graph.syncRequest(getRuntimeEnvironmentRequest(parameter.first, parameter.second));
139 Map<String, ComponentTypeProperty> propertyMap =
140 graph.syncRequest(new ReadComponentTypeInterfaceRequest(parameter.first, runtimeEnvironment.getEnvironment()),
141 TransientCacheListener.<Map<String, ComponentTypeProperty>>instance());
143 // Map<String, ComponentTypeProperty> result = new HashMap<String,ComponentTypeProperty>(propertyMap);
144 // for(DataDefinition dd : Functions.dataDefinitions(graph, variable)) {
145 // result.put(dd.target, null);
148 return new CompilationContext(runtimeEnvironment, propertyMap);
157 protected Type getContextVariableType() {
161 private static Expression accessInputVariable(Environment environment,
162 org.simantics.scl.compiler.elaboration.expressions.Variable contextVariable) {
163 SCLValue variableParentFunction = environment.getValue(VARIABLE_PARENT);
164 return new EApply(new EConstant(variableParentFunction),
165 new EApply(new EConstant(variableParentFunction),
166 new EVariable(contextVariable)));
169 protected static Expression standardGetProperty(
170 Environment environment,
171 org.simantics.scl.compiler.elaboration.expressions.Variable contextVariable,
174 return getPropertyFlexible(environment, accessInputVariable(environment, contextVariable), name, type);
178 protected Expression getVariableAccessExpression(
180 CompilationContext context,
181 org.simantics.scl.compiler.elaboration.expressions.Variable contextVariable,
182 String name) throws DatabaseException {
183 ComponentTypeProperty property = context.propertyMap.get(name);
185 return standardGetProperty(
186 context.runtimeEnvironment.getEnvironment(),
189 property.type == null ? Types.metaVar(Kinds.STAR) : property.type);
192 // if(context.propertyMap.containsKey(name)) {
194 // org.simantics.scl.compiler.elaboration.expressions.Variable parametersVariable = new org.simantics.scl.compiler.elaboration.expressions.Variable("context", COMMAND_CONTEXT);
196 // Environment environment = context.runtimeEnvironment.getEnvironment();
198 //// return new EApply(
199 //// new EConstant(environment.getValue(FROM_DYNAMIC), Types.STRING),
200 // return new EApply(
201 // new EConstant(environment.getValue(CONTEXT_VARIABLE), Types.DYNAMIC),
202 // new EVariable(parametersVariable),
203 // new ELiteral(new StringConstant(name)));
207 return getSpecialVariableAccessExpression(graph, context, contextVariable, name);
212 protected Expression getSpecialVariableAccessExpression(ReadGraph graph,
213 CompilationContext context,
214 org.simantics.scl.compiler.elaboration.expressions.Variable contextVariable,
215 String name) throws DatabaseException {
216 if(name.equals("input")) {
217 Environment environment = context.runtimeEnvironment.getEnvironment();
218 return accessInputVariable(environment, contextVariable);
219 } else if(name.equals("self"))
220 return new EVariable(contextVariable);
226 protected Type getExpectedType(ReadGraph graph, CompilationContext context) throws DatabaseException {
227 return super.getExpectedType(graph, context);
231 public int hashCode() {
232 final int prime = 31;
234 result = prime * result + ((context == null) ? 0 : context.hashCode());
239 public boolean equals(Object obj) {
244 if (getClass() != obj.getClass())
246 ServerSCLHandlerValueRequest other = (ServerSCLHandlerValueRequest) obj;
247 if (context == null) {
248 if (other.context != null)
250 } else if (!context.equals(other.context))
256 // public int hashCode() {
257 // return 31*(31*getClass().hashCode() + literal.hashCode()) + componentTypeAndRoot.hashCode();
261 // public boolean equals(Object obj) {
264 // if(obj == null || obj.getClass() != getClass())
266 // ServerSCLHandlerValueRequest other = (ServerSCLHandlerValueRequest)obj;
267 // return literal.equals(other.literal) && componentTypeAndRoot.equals(other.componentTypeAndRoot);