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.Environments;
26 import org.simantics.scl.compiler.environment.specification.EnvironmentSpecification;
27 import org.simantics.scl.compiler.runtime.RuntimeEnvironment;
28 import org.simantics.scl.compiler.top.SCLExpressionCompilationException;
29 import org.simantics.scl.compiler.types.TCon;
30 import org.simantics.scl.compiler.types.Type;
31 import org.simantics.scl.compiler.types.Types;
32 import org.simantics.scl.compiler.types.kinds.Kinds;
33 import org.simantics.scl.runtime.SCLContext;
34 import org.simantics.scl.runtime.function.Function1;
35 import org.simantics.structural2.scl.ComponentTypeProperty;
36 import org.simantics.structural2.scl.FindPossibleComponentTypeRequest;
37 import org.simantics.structural2.scl.ReadComponentTypeInterfaceRequest;
38 import org.simantics.utils.datastructures.Pair;
39 import org.slf4j.Logger;
40 import org.slf4j.LoggerFactory;
42 public class ServerSCLHandlerValueRequest extends AbstractExpressionCompilationRequest<CompilationContext, Variable> {
44 private static final Logger LOGGER = LoggerFactory.getLogger(ServerSCLHandlerValueRequest.class);
46 private final Variable context;
47 private final Pair<Resource,Resource> componentTypeAndRoot;
48 private final Resource literal;
49 protected String possibleExpectedValueType;
51 public static class CompilationContext extends AbstractExpressionCompilationContext {
52 public final Map<String, ComponentTypeProperty> propertyMap;
54 public CompilationContext(RuntimeEnvironment runtimeEnvironment,
55 Map<String, ComponentTypeProperty> propertyMap) {
56 super(runtimeEnvironment);
57 this.propertyMap = propertyMap;
61 private ServerSCLHandlerValueRequest(Variable context, Pair<Resource,Resource> componentTypeAndRoot, Resource literal, String possibleExpectedValueType) {
62 assert(literal != null);
63 this.context = context;
64 this.literal = literal;
65 this.componentTypeAndRoot = componentTypeAndRoot;
66 this.possibleExpectedValueType = possibleExpectedValueType;
69 public ServerSCLHandlerValueRequest(ReadGraph graph, Variable context) throws DatabaseException {
70 this(context, getComponentTypeAndRoot(graph, context), context.getRepresents(graph), resolveExpectedValueType(graph, context));
73 private static Pair<Resource,Resource> getComponentTypeAndRoot(ReadGraph graph, Variable property) throws DatabaseException {
74 Variable parent = property.getParent(graph);
75 Resource represents = parent.getRepresents(graph);
76 if(represents != null) {
77 Resource type = graph.syncRequest(new FindPossibleComponentTypeRequest(represents));
79 Resource root = graph.syncRequest(new IndexRoot(type));
80 return Pair.make(type, root);
83 parent = parent.getParent(graph);
84 Resource root = graph.syncRequest(new IndexRoot(property.getRepresents(graph)));
85 return Pair.make(parent.getType(graph), root);
89 public static List<TCon> getEffects(ReadGraph graph, Variable context) throws DatabaseException {
91 ServerSCLHandlerValueRequest req = new ServerSCLHandlerValueRequest(graph, context);
92 return req.getExpressionEffects(graph);
93 } catch (DatabaseException e) {
94 throw (DatabaseException)e;
95 } catch (Throwable t) {
96 throw new DatabaseException(t);
100 public static Object compileAndEvaluate(ReadGraph graph, Variable context) throws DatabaseException {
101 SCLContext sclContext = SCLContext.getCurrent();
102 Object oldGraph = sclContext.get("graph");
104 Function1<Variable,Object> exp = graph.syncRequest(new ServerSCLHandlerValueRequest(graph, context),
105 TransientCacheListener.<Function1<Variable,Object>>instance());
106 sclContext.put("graph", graph);
107 return exp.apply(context);
108 } catch (DatabaseException e) {
109 throw (DatabaseException)e;
110 } catch (Throwable t) {
111 throw new DatabaseException(t);
113 sclContext.put("graph", oldGraph);
118 protected String getExpressionText(ReadGraph graph)
119 throws DatabaseException {
120 Layer0 L0 = Layer0.getInstance(graph);
121 String exp = graph.getRelatedValue(literal, L0.SCLValue_expression, Bindings.STRING);
122 return "\\context -> " + exp;
125 protected RuntimeEnvironmentRequest2 getRuntimeEnvironmentRequest(Resource componentType, Resource indexRoot) {
126 return new RuntimeEnvironmentRequest2(componentType, indexRoot) {
128 protected void fillEnvironmentSpecification(
129 EnvironmentSpecification environmentSpecification) {
135 protected CompilationContext getCompilationContext(ReadGraph graph) throws DatabaseException {
137 return graph.syncRequest(new VariableRead<CompilationContext>(context) {
140 public CompilationContext perform(ReadGraph graph) throws DatabaseException {
142 Pair<Resource,Resource> parameter = getComponentTypeAndRoot(graph, variable);
143 RuntimeEnvironment runtimeEnvironment = graph.syncRequest(getRuntimeEnvironmentRequest(parameter.first, parameter.second));
145 Map<String, ComponentTypeProperty> propertyMap =
146 graph.syncRequest(new ReadComponentTypeInterfaceRequest(parameter.first, runtimeEnvironment.getEnvironment()),
147 TransientCacheListener.<Map<String, ComponentTypeProperty>>instance());
149 // Map<String, ComponentTypeProperty> result = new HashMap<String,ComponentTypeProperty>(propertyMap);
150 // for(DataDefinition dd : Functions.dataDefinitions(graph, variable)) {
151 // result.put(dd.target, null);
154 return new CompilationContext(runtimeEnvironment, propertyMap);
163 protected Type getContextVariableType() {
167 private static Expression accessInputVariable(Environment environment,
168 org.simantics.scl.compiler.elaboration.expressions.Variable contextVariable) {
169 SCLValue variableParentFunction = environment.getValue(VARIABLE_PARENT);
170 return new EApply(new EConstant(variableParentFunction),
171 new EApply(new EConstant(variableParentFunction),
172 new EVariable(contextVariable)));
175 protected static Expression standardGetProperty(
176 Environment environment,
177 org.simantics.scl.compiler.elaboration.expressions.Variable contextVariable,
180 return getPropertyFlexible(environment, accessInputVariable(environment, contextVariable), name, type);
184 protected Expression getVariableAccessExpression(
186 CompilationContext context,
187 org.simantics.scl.compiler.elaboration.expressions.Variable contextVariable,
188 String name) throws DatabaseException {
189 ComponentTypeProperty property = context.propertyMap.get(name);
191 return standardGetProperty(
192 context.runtimeEnvironment.getEnvironment(),
195 property.type == null ? Types.metaVar(Kinds.STAR) : property.type);
198 // if(context.propertyMap.containsKey(name)) {
200 // org.simantics.scl.compiler.elaboration.expressions.Variable parametersVariable = new org.simantics.scl.compiler.elaboration.expressions.Variable("context", COMMAND_CONTEXT);
202 // Environment environment = context.runtimeEnvironment.getEnvironment();
204 //// return new EApply(
205 //// new EConstant(environment.getValue(FROM_DYNAMIC), Types.STRING),
206 // return new EApply(
207 // new EConstant(environment.getValue(CONTEXT_VARIABLE), Types.DYNAMIC),
208 // new EVariable(parametersVariable),
209 // new ELiteral(new StringConstant(name)));
213 return getSpecialVariableAccessExpression(graph, context, contextVariable, name);
218 protected Expression getSpecialVariableAccessExpression(ReadGraph graph,
219 CompilationContext context,
220 org.simantics.scl.compiler.elaboration.expressions.Variable contextVariable,
221 String name) throws DatabaseException {
222 if(name.equals("input")) {
223 Environment environment = context.runtimeEnvironment.getEnvironment();
224 return accessInputVariable(environment, contextVariable);
225 } else if(name.equals("self"))
226 return new EVariable(contextVariable);
232 protected Type getExpectedType(ReadGraph graph, CompilationContext context) throws DatabaseException {
233 if(possibleExpectedValueType != null) {
235 return Environments.getType(context.runtimeEnvironment.getEnvironment(), possibleExpectedValueType);
236 } catch (SCLExpressionCompilationException e) {
237 LOGGER.error("Could not get type for " + String.valueOf(possibleExpectedValueType), e);
240 return super.getExpectedType(graph, context);
245 public int hashCode() {
246 final int prime = 31;
248 result = prime * result + ((context == null) ? 0 : context.hashCode());
253 public boolean equals(Object obj) {
258 if (getClass() != obj.getClass())
260 ServerSCLHandlerValueRequest other = (ServerSCLHandlerValueRequest) obj;
261 if (context == null) {
262 if (other.context != null)
264 } else if (!context.equals(other.context))
270 // public int hashCode() {
271 // return 31*(31*getClass().hashCode() + literal.hashCode()) + componentTypeAndRoot.hashCode();
275 // public boolean equals(Object obj) {
278 // if(obj == null || obj.getClass() != getClass())
280 // ServerSCLHandlerValueRequest other = (ServerSCLHandlerValueRequest)obj;
281 // return literal.equals(other.literal) && componentTypeAndRoot.equals(other.componentTypeAndRoot);