1 package org.simantics.document.server.request;
3 import java.util.ArrayList;
4 import java.util.Collections;
8 import org.simantics.databoard.Bindings;
9 import org.simantics.db.ReadGraph;
10 import org.simantics.db.Resource;
11 import org.simantics.db.common.procedure.adapter.TransientCacheListener;
12 import org.simantics.db.common.request.IndexRoot;
13 import org.simantics.db.common.request.UnaryRead;
14 import org.simantics.db.exception.DatabaseException;
15 import org.simantics.db.layer0.scl.AbstractExpressionCompilationContext;
16 import org.simantics.db.layer0.util.RuntimeEnvironmentRequest2;
17 import org.simantics.db.layer0.variable.Variable;
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.exceptions.MatchException;
31 import org.simantics.scl.compiler.types.kinds.Kinds;
32 import org.simantics.scl.compiler.types.util.MultiFunction;
33 import org.simantics.scl.runtime.SCLContext;
34 import org.simantics.scl.runtime.function.Function1;
35 import org.simantics.scl.runtime.function.FunctionImpl1;
36 import org.simantics.structural2.scl.ComponentTypeProperty;
37 import org.simantics.structural2.scl.FindPossibleComponentTypeRequest;
38 import org.simantics.structural2.scl.ReadComponentTypeInterfaceRequest;
39 import org.simantics.utils.datastructures.Pair;
41 public class ServerSCLHandlerValueRequest extends ServerSCLValueRequestBase<org.simantics.document.server.request.ServerSCLHandlerValueRequest.CompilationContext> {
43 private final Pair<Resource,Resource> componentTypeAndRoot;
44 private final Resource literal;
45 protected String possibleExpectedValueType;
47 public static class CompilationContext extends AbstractExpressionCompilationContext {
48 public final Map<String, ComponentTypeProperty> propertyMap;
50 public CompilationContext(RuntimeEnvironment runtimeEnvironment,
51 Map<String, ComponentTypeProperty> propertyMap) {
52 super(runtimeEnvironment);
53 this.propertyMap = propertyMap;
57 private ServerSCLHandlerValueRequest(Pair<Resource,Resource> componentTypeAndRoot, Resource literal, String possibleExpectedValueType) {
58 assert(literal != null);
59 this.literal = literal;
60 this.componentTypeAndRoot = componentTypeAndRoot;
61 this.possibleExpectedValueType = possibleExpectedValueType;
64 public ServerSCLHandlerValueRequest(ReadGraph graph, Variable context) throws DatabaseException {
65 this(getComponentTypeAndRoot(graph, context), context.getRepresents(graph), resolveExpectedValueType(graph, context.getPredicateResource(graph)));
68 public ServerSCLHandlerValueRequest(ReadGraph graph, Resource s, Resource o, Resource p) throws DatabaseException {
69 this(getComponentTypeAndRoot(graph, s, o), o, resolveExpectedValueType(graph, p));
72 private static Pair<Resource,Resource> getComponentTypeAndRoot(ReadGraph graph, Variable property) throws DatabaseException {
73 Variable parent = property.getParent(graph);
74 Resource represents = parent.getRepresents(graph);
75 if(represents != null) {
76 Resource type = graph.syncRequest(new FindPossibleComponentTypeRequest(represents));
78 Resource root = graph.syncRequest(new IndexRoot(type));
79 return Pair.make(type, root);
82 parent = parent.getParent(graph);
83 Resource root = graph.syncRequest(new IndexRoot(property.getRepresents(graph)));
84 return Pair.make(parent.getType(graph), root);
88 public static List<TCon> getEffects(ReadGraph graph, Variable context) throws DatabaseException {
89 HandlerFn fn = (HandlerFn)compile(graph, context);
93 public static Object compileAndEvaluate(ReadGraph graph, Variable context) throws DatabaseException {
94 SCLContext sclContext = SCLContext.getCurrent();
95 Object oldGraph = sclContext.get("graph");
97 Function1<Object,Object> exp = graph.syncRequest(new ServerSCLHandlerValueRequest(graph, context),
98 TransientCacheListener.instance());
99 sclContext.put("graph", graph);
100 return exp.apply(context);
101 } catch (DatabaseException e) {
102 throw (DatabaseException)e;
103 } catch (Throwable t) {
104 throw new DatabaseException(t);
106 sclContext.put("graph", oldGraph);
110 public static Function1<Object, Object> compile(ReadGraph graph, Variable context) throws DatabaseException {
111 return graph.syncRequest(new ServerSCLHandlerValueRequest(graph, context), TransientCacheListener.instance());
114 public static Function1<Object, Object> compile(ReadGraph graph, Resource s, Resource o, Resource p) throws DatabaseException {
115 return graph.syncRequest(new ServerSCLHandlerValueRequest(graph, s, o, p), TransientCacheListener.instance());
118 public static class HandlerFn extends FunctionImpl1<Object,Object> {
120 Function1<Object,Object> handler;
121 ArrayList<TCon> effects;
123 HandlerFn(Function1<Object,Object> handler, Type type) {
125 this.handler = handler;
126 this.effects = new ArrayList<TCon>();
127 MultiFunction mfun = Types.matchFunction(type, 1);
128 mfun.effect.collectConcreteEffects(this.effects);
129 } catch(MatchException e) {
130 // Should not happen!
131 throw new RuntimeException(e);
136 public Object apply(Object p0) {
137 return handler.apply(p0);
143 public Function1<Object, Object> perform(ReadGraph graph) throws DatabaseException {
145 CompilationContext context = getCompilationContext(graph);
146 Type expectedType = getExpectedType(graph, context);
147 Function1<Object,Object> handler = eval(prepareEvaluator(graph, context, expectedType), graph);
148 return new HandlerFn(handler, expectedType);
153 protected String getExpressionText(ReadGraph graph)
154 throws DatabaseException {
155 Layer0 L0 = Layer0.getInstance(graph);
156 String exp = graph.getRelatedValue(literal, L0.SCLValue_expression, Bindings.STRING);
157 return "\\context -> " + exp;
160 protected RuntimeEnvironmentRequest2 getRuntimeEnvironmentRequest(Resource componentType, Resource indexRoot) {
161 return new RuntimeEnvironmentRequest2(componentType, indexRoot) {
163 protected void fillEnvironmentSpecification(
164 EnvironmentSpecification environmentSpecification) {
170 protected CompilationContext getCompilationContext(ReadGraph graph) throws DatabaseException {
171 return graph.syncRequest(new UnaryRead<Pair<Resource,Resource>,CompilationContext>(componentTypeAndRoot) {
173 public CompilationContext perform(ReadGraph graph) throws DatabaseException {
174 RuntimeEnvironment runtimeEnvironment = graph.syncRequest(getRuntimeEnvironmentRequest(parameter.first, parameter.second));
175 Map<String, ComponentTypeProperty> propertyMap;
176 if (parameter.first != null) {
178 graph.syncRequest(new ReadComponentTypeInterfaceRequest(parameter.first, runtimeEnvironment.getEnvironment()),
179 TransientCacheListener.<Map<String, ComponentTypeProperty>>instance());
181 // TODO: Antti to consider
182 // To handle procedural user components
183 propertyMap = Collections.emptyMap();
185 // Map<String, ComponentTypeProperty> propertyMap =
186 // graph.syncRequest(new ReadComponentTypeInterfaceRequest(parameter.first, runtimeEnvironment.getEnvironment()),
187 // TransientCacheListener.<Map<String, ComponentTypeProperty>>instance());
188 return new CompilationContext(runtimeEnvironment, propertyMap);
194 protected Type getContextVariableType() {
198 private static Expression accessInputVariable(Environment environment,
199 org.simantics.scl.compiler.elaboration.expressions.Variable contextVariable) {
200 SCLValue variableParentFunction = environment.getValue(VARIABLE_PARENT);
201 return new EApply(new EConstant(variableParentFunction),
202 new EApply(new EConstant(variableParentFunction),
203 new EVariable(contextVariable)));
206 protected static Expression standardGetProperty(
207 Environment environment,
208 org.simantics.scl.compiler.elaboration.expressions.Variable contextVariable,
211 return getPropertyFlexible(environment, accessInputVariable(environment, contextVariable), name, type);
215 protected Expression getVariableAccessExpression(
217 CompilationContext context,
218 org.simantics.scl.compiler.elaboration.expressions.Variable contextVariable,
219 String name) throws DatabaseException {
220 ComponentTypeProperty property = context.propertyMap.get(name);
222 return standardGetProperty(
223 context.runtimeEnvironment.getEnvironment(),
226 property.type == null ? Types.metaVar(Kinds.STAR) : property.type);
229 // if(context.propertyMap.containsKey(name)) {
231 // org.simantics.scl.compiler.elaboration.expressions.Variable parametersVariable = new org.simantics.scl.compiler.elaboration.expressions.Variable("context", COMMAND_CONTEXT);
233 // Environment environment = context.runtimeEnvironment.getEnvironment();
235 //// return new EApply(
236 //// new EConstant(environment.getValue(FROM_DYNAMIC), Types.STRING),
237 // return new EApply(
238 // new EConstant(environment.getValue(CONTEXT_VARIABLE), Types.DYNAMIC),
239 // new EVariable(parametersVariable),
240 // new ELiteral(new StringConstant(name)));
244 return getSpecialVariableAccessExpression(graph, context, contextVariable, name);
249 protected Expression getSpecialVariableAccessExpression(ReadGraph graph,
250 CompilationContext context,
251 org.simantics.scl.compiler.elaboration.expressions.Variable contextVariable,
252 String name) throws DatabaseException {
253 if(name.equals("input")) {
254 Environment environment = context.runtimeEnvironment.getEnvironment();
255 return accessInputVariable(environment, contextVariable);
256 } else if(name.equals("self"))
257 return new EVariable(contextVariable);
263 protected Type getExpectedType(ReadGraph graph, CompilationContext context) throws DatabaseException {
264 return super.getExpectedType(graph, context);
268 public int hashCode() {
269 return 31*(31*getClass().hashCode() + literal.hashCode()) + componentTypeAndRoot.hashCode();
273 public boolean equals(Object obj) {
278 if (getClass() != obj.getClass())
280 ServerSCLHandlerValueRequest other = (ServerSCLHandlerValueRequest) obj;
281 return literal.equals(other.literal) && componentTypeAndRoot.equals(other.componentTypeAndRoot);