1 package org.simantics.document.server.request;
3 import java.util.ArrayList;
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.request.ServerSCLHandlerValueRequest.CompilationContext;
19 import org.simantics.layer0.Layer0;
20 import org.simantics.scl.compiler.elaboration.expressions.EApply;
21 import org.simantics.scl.compiler.elaboration.expressions.EConstant;
22 import org.simantics.scl.compiler.elaboration.expressions.EVariable;
23 import org.simantics.scl.compiler.elaboration.expressions.Expression;
24 import org.simantics.scl.compiler.elaboration.modules.SCLValue;
25 import org.simantics.scl.compiler.environment.Environment;
26 import org.simantics.scl.compiler.environment.Environments;
27 import org.simantics.scl.compiler.environment.specification.EnvironmentSpecification;
28 import org.simantics.scl.compiler.runtime.RuntimeEnvironment;
29 import org.simantics.scl.compiler.top.SCLExpressionCompilationException;
30 import org.simantics.scl.compiler.types.TCon;
31 import org.simantics.scl.compiler.types.TMetaVar;
32 import org.simantics.scl.compiler.types.TVar;
33 import org.simantics.scl.compiler.types.Type;
34 import org.simantics.scl.compiler.types.Types;
35 import org.simantics.scl.compiler.types.kinds.Kinds;
36 import org.simantics.scl.runtime.SCLContext;
37 import org.simantics.scl.runtime.function.Function1;
38 import org.simantics.structural2.scl.ComponentTypeProperty;
39 import org.simantics.structural2.scl.FindPossibleComponentTypeRequest;
40 import org.simantics.structural2.scl.ReadComponentTypeInterfaceRequest;
41 import org.simantics.utils.datastructures.Pair;
42 import org.slf4j.Logger;
43 import org.slf4j.LoggerFactory;
45 public class ServerSCLHandlerValueRequest extends AbstractExpressionCompilationRequest<CompilationContext, Variable> {
47 private static final Logger LOGGER = LoggerFactory.getLogger(ServerSCLHandlerValueRequest.class);
49 private final Variable context;
50 private final Pair<Resource,Resource> componentTypeAndRoot;
51 private final Resource literal;
52 protected String possibleExpectedValueType;
54 public static class CompilationContext extends AbstractExpressionCompilationContext {
55 public final Map<String, ComponentTypeProperty> propertyMap;
57 public CompilationContext(RuntimeEnvironment runtimeEnvironment,
58 Map<String, ComponentTypeProperty> propertyMap) {
59 super(runtimeEnvironment);
60 this.propertyMap = propertyMap;
64 private ServerSCLHandlerValueRequest(Variable context, Pair<Resource,Resource> componentTypeAndRoot, Resource literal, String possibleExpectedValueType) {
65 assert(literal != null);
66 this.context = context;
67 this.literal = literal;
68 this.componentTypeAndRoot = componentTypeAndRoot;
69 this.possibleExpectedValueType = possibleExpectedValueType;
72 public ServerSCLHandlerValueRequest(ReadGraph graph, Variable context) throws DatabaseException {
73 this(context, getComponentTypeAndRoot(graph, context), context.getRepresents(graph), resolveExpectedValueType(graph, context));
76 private static Pair<Resource,Resource> getComponentTypeAndRoot(ReadGraph graph, Variable property) throws DatabaseException {
77 Variable parent = property.getParent(graph);
78 Resource represents = parent.getRepresents(graph);
79 if(represents != null) {
80 Resource type = graph.syncRequest(new FindPossibleComponentTypeRequest(represents));
82 Resource root = graph.syncRequest(new IndexRoot(type));
83 return Pair.make(type, root);
86 parent = parent.getParent(graph);
87 Resource root = graph.syncRequest(new IndexRoot(property.getRepresents(graph)));
88 return Pair.make(parent.getType(graph), root);
92 public static List<TCon> getEffects(ReadGraph graph, Variable context) throws DatabaseException {
94 ServerSCLHandlerValueRequest req = new ServerSCLHandlerValueRequest(graph, context);
95 return req.getExpressionEffects(graph);
96 } catch (DatabaseException e) {
97 throw (DatabaseException)e;
98 } catch (Throwable t) {
99 throw new DatabaseException(t);
103 public static Object compileAndEvaluate(ReadGraph graph, Variable context) throws DatabaseException {
104 SCLContext sclContext = SCLContext.getCurrent();
105 Object oldGraph = sclContext.get("graph");
107 Function1<Variable,Object> exp = graph.syncRequest(new ServerSCLHandlerValueRequest(graph, context),
108 TransientCacheListener.<Function1<Variable,Object>>instance());
109 sclContext.put("graph", graph);
110 return exp.apply(context);
111 } catch (DatabaseException e) {
112 throw (DatabaseException)e;
113 } catch (Throwable t) {
114 throw new DatabaseException(t);
116 sclContext.put("graph", oldGraph);
121 protected String getExpressionText(ReadGraph graph)
122 throws DatabaseException {
123 Layer0 L0 = Layer0.getInstance(graph);
124 String exp = graph.getRelatedValue(literal, L0.SCLValue_expression, Bindings.STRING);
125 return "\\context -> " + exp;
128 protected RuntimeEnvironmentRequest2 getRuntimeEnvironmentRequest(Resource componentType, Resource indexRoot) {
129 return new RuntimeEnvironmentRequest2(componentType, indexRoot) {
131 protected void fillEnvironmentSpecification(
132 EnvironmentSpecification environmentSpecification) {
138 protected CompilationContext getCompilationContext(ReadGraph graph) throws DatabaseException {
140 return graph.syncRequest(new VariableRead<CompilationContext>(context) {
143 public CompilationContext perform(ReadGraph graph) throws DatabaseException {
145 Pair<Resource,Resource> parameter = getComponentTypeAndRoot(graph, variable);
146 RuntimeEnvironment runtimeEnvironment = graph.syncRequest(getRuntimeEnvironmentRequest(parameter.first, parameter.second));
148 Map<String, ComponentTypeProperty> propertyMap =
149 graph.syncRequest(new ReadComponentTypeInterfaceRequest(parameter.first, runtimeEnvironment.getEnvironment()),
150 TransientCacheListener.<Map<String, ComponentTypeProperty>>instance());
152 // Map<String, ComponentTypeProperty> result = new HashMap<String,ComponentTypeProperty>(propertyMap);
153 // for(DataDefinition dd : Functions.dataDefinitions(graph, variable)) {
154 // result.put(dd.target, null);
157 return new CompilationContext(runtimeEnvironment, propertyMap);
166 protected Type getContextVariableType() {
170 private static Expression accessInputVariable(Environment environment,
171 org.simantics.scl.compiler.elaboration.expressions.Variable contextVariable) {
172 SCLValue variableParentFunction = environment.getValue(VARIABLE_PARENT);
173 return new EApply(new EConstant(variableParentFunction),
174 new EApply(new EConstant(variableParentFunction),
175 new EVariable(contextVariable)));
178 protected static Expression standardGetProperty(
179 Environment environment,
180 org.simantics.scl.compiler.elaboration.expressions.Variable contextVariable,
183 return getPropertyFlexible(environment, accessInputVariable(environment, contextVariable), name, type);
187 protected Expression getVariableAccessExpression(
189 CompilationContext context,
190 org.simantics.scl.compiler.elaboration.expressions.Variable contextVariable,
191 String name) throws DatabaseException {
192 ComponentTypeProperty property = context.propertyMap.get(name);
194 return standardGetProperty(
195 context.runtimeEnvironment.getEnvironment(),
198 property.type == null ? Types.metaVar(Kinds.STAR) : property.type);
201 // if(context.propertyMap.containsKey(name)) {
203 // org.simantics.scl.compiler.elaboration.expressions.Variable parametersVariable = new org.simantics.scl.compiler.elaboration.expressions.Variable("context", COMMAND_CONTEXT);
205 // Environment environment = context.runtimeEnvironment.getEnvironment();
207 //// return new EApply(
208 //// new EConstant(environment.getValue(FROM_DYNAMIC), Types.STRING),
209 // return new EApply(
210 // new EConstant(environment.getValue(CONTEXT_VARIABLE), Types.DYNAMIC),
211 // new EVariable(parametersVariable),
212 // new ELiteral(new StringConstant(name)));
216 return getSpecialVariableAccessExpression(graph, context, contextVariable, name);
221 protected Expression getSpecialVariableAccessExpression(ReadGraph graph,
222 CompilationContext context,
223 org.simantics.scl.compiler.elaboration.expressions.Variable contextVariable,
224 String name) throws DatabaseException {
225 if(name.equals("input")) {
226 Environment environment = context.runtimeEnvironment.getEnvironment();
227 return accessInputVariable(environment, contextVariable);
228 } else if(name.equals("self"))
229 return new EVariable(contextVariable);
235 protected Type getExpectedType(ReadGraph graph, CompilationContext context) throws DatabaseException {
236 if(possibleExpectedValueType != null) {
238 Type type = Environments.getType(context.runtimeEnvironment.getEnvironment(), possibleExpectedValueType);
239 type = Types.instantiate(Types.forAll(Types.freeVars(type).toArray(new TVar[0]), type), new ArrayList<TMetaVar>());
241 } catch (SCLExpressionCompilationException e) {
242 LOGGER.error("Could not get type for " + String.valueOf(possibleExpectedValueType), e);
245 return super.getExpectedType(graph, context);
250 public int hashCode() {
251 final int prime = 31;
253 result = prime * result + ((context == null) ? 0 : context.hashCode());
258 public boolean equals(Object obj) {
263 if (getClass() != obj.getClass())
265 ServerSCLHandlerValueRequest other = (ServerSCLHandlerValueRequest) obj;
266 if (context == null) {
267 if (other.context != null)
269 } else if (!context.equals(other.context))
275 // public int hashCode() {
276 // return 31*(31*getClass().hashCode() + literal.hashCode()) + componentTypeAndRoot.hashCode();
280 // public boolean equals(Object obj) {
283 // if(obj == null || obj.getClass() != getClass())
285 // ServerSCLHandlerValueRequest other = (ServerSCLHandlerValueRequest)obj;
286 // return literal.equals(other.literal) && componentTypeAndRoot.equals(other.componentTypeAndRoot);