1 package org.simantics.document.server.request;
\r
3 import java.util.Map;
\r
5 import org.simantics.databoard.Bindings;
\r
6 import org.simantics.db.ReadGraph;
\r
7 import org.simantics.db.Resource;
\r
8 import org.simantics.db.common.procedure.adapter.TransientCacheListener;
\r
9 import org.simantics.db.common.request.IndexRoot;
\r
10 import org.simantics.db.common.request.UnaryRead;
\r
11 import org.simantics.db.exception.DatabaseException;
\r
12 import org.simantics.db.layer0.scl.AbstractExpressionCompilationContext;
\r
13 import org.simantics.db.layer0.scl.AbstractExpressionCompilationRequest;
\r
14 import org.simantics.db.layer0.util.RuntimeEnvironmentRequest;
\r
15 import org.simantics.db.layer0.variable.Variable;
\r
16 import org.simantics.document.server.request.ServerSCLValueRequest.CompilationContext;
\r
17 import org.simantics.layer0.Layer0;
\r
18 import org.simantics.scl.compiler.common.names.Name;
\r
19 import org.simantics.scl.compiler.constants.StringConstant;
\r
20 import org.simantics.scl.compiler.elaboration.expressions.EApply;
\r
21 import org.simantics.scl.compiler.elaboration.expressions.EConstant;
\r
22 import org.simantics.scl.compiler.elaboration.expressions.ELiteral;
\r
23 import org.simantics.scl.compiler.elaboration.expressions.EVariable;
\r
24 import org.simantics.scl.compiler.elaboration.expressions.Expression;
\r
25 import org.simantics.scl.compiler.elaboration.modules.SCLValue;
\r
26 import org.simantics.scl.compiler.environment.Environment;
\r
27 import org.simantics.scl.compiler.environment.specification.EnvironmentSpecification;
\r
28 import org.simantics.scl.compiler.runtime.RuntimeEnvironment;
\r
29 import org.simantics.scl.compiler.types.Type;
\r
30 import org.simantics.scl.compiler.types.Types;
\r
31 import org.simantics.scl.compiler.types.kinds.Kinds;
\r
32 import org.simantics.scl.runtime.SCLContext;
\r
33 import org.simantics.scl.runtime.function.Function1;
\r
34 import org.simantics.structural2.scl.ComponentTypeProperty;
\r
35 import org.simantics.structural2.scl.FindPossibleComponentTypeRequest;
\r
36 import org.simantics.structural2.scl.ReadComponentTypeInterfaceRequest;
\r
37 import org.simantics.utils.datastructures.Pair;
\r
39 public class ServerSCLValueRequest extends AbstractExpressionCompilationRequest<CompilationContext, Variable> {
\r
41 private final Pair<Resource,Resource> componentTypeAndRoot;
\r
42 private final Resource literal;
\r
44 public static class CompilationContext extends AbstractExpressionCompilationContext {
\r
45 public final Map<String, ComponentTypeProperty> propertyMap;
\r
47 public CompilationContext(RuntimeEnvironment runtimeEnvironment,
\r
48 Map<String, ComponentTypeProperty> propertyMap) {
\r
49 super(runtimeEnvironment);
\r
50 this.propertyMap = propertyMap;
\r
54 private ServerSCLValueRequest(Pair<Resource,Resource> componentTypeAndRoot, Resource literal) {
\r
55 assert(literal != null);
\r
56 this.literal = literal;
\r
57 this.componentTypeAndRoot = componentTypeAndRoot;
\r
60 public ServerSCLValueRequest(ReadGraph graph, Variable context) throws DatabaseException {
\r
61 this(getComponentTypeAndRoot(graph, context), context.getRepresents(graph));
\r
64 private static Pair<Resource,Resource> getComponentTypeAndRoot(ReadGraph graph, Variable property) throws DatabaseException {
\r
65 Variable parent = property.getParent(graph);
\r
66 Resource represents = parent.getRepresents(graph);
\r
67 if(represents != null) {
\r
68 Resource type = graph.syncRequest(new FindPossibleComponentTypeRequest(represents));
\r
70 Resource root = graph.syncRequest(new IndexRoot(type));
\r
71 return Pair.make(type, root);
\r
74 parent = parent.getParent(graph);
\r
75 Resource root = graph.syncRequest(new IndexRoot(property.getRepresents(graph)));
\r
76 return Pair.make(parent.getType(graph), root);
\r
79 public static Object compileAndEvaluate(ReadGraph graph, Variable context) throws DatabaseException {
\r
80 SCLContext sclContext = SCLContext.getCurrent();
\r
81 Object oldGraph = sclContext.get("graph");
\r
83 Function1<Variable,Object> exp = graph.syncRequest(new ServerSCLValueRequest(graph, context),
\r
84 TransientCacheListener.<Function1<Variable,Object>>instance());
\r
85 sclContext.put("graph", graph);
\r
86 return exp.apply(context);
\r
87 } catch (DatabaseException e) {
\r
88 throw (DatabaseException)e;
\r
89 } catch (Throwable t) {
\r
90 throw new DatabaseException(t);
\r
92 sclContext.put("graph", oldGraph);
\r
97 protected String getExpressionText(ReadGraph graph)
\r
98 throws DatabaseException {
\r
99 Layer0 L0 = Layer0.getInstance(graph);
\r
100 return graph.getRelatedValue(literal, L0.SCLValue_expression, Bindings.STRING);
\r
103 protected RuntimeEnvironmentRequest getRuntimeEnvironmentRequest(Resource indexRoot) {
\r
104 return new RuntimeEnvironmentRequest(indexRoot) {
\r
106 protected void fillEnvironmentSpecification(
\r
107 EnvironmentSpecification environmentSpecification) {
\r
113 protected CompilationContext getCompilationContext(ReadGraph graph)
\r
114 throws DatabaseException {
\r
115 return graph.syncRequest(new UnaryRead<Pair<Resource,Resource>,CompilationContext>(componentTypeAndRoot) {
\r
117 public CompilationContext perform(ReadGraph graph)
\r
118 throws DatabaseException {
\r
119 RuntimeEnvironment runtimeEnvironment = graph.syncRequest(getRuntimeEnvironmentRequest(parameter.second));
\r
120 Map<String, ComponentTypeProperty> propertyMap =
\r
121 graph.syncRequest(new ReadComponentTypeInterfaceRequest(parameter.first, runtimeEnvironment.getEnvironment()),
\r
122 TransientCacheListener.<Map<String, ComponentTypeProperty>>instance());
\r
123 return new CompilationContext(runtimeEnvironment, propertyMap);
\r
129 protected Type getContextVariableType() {
\r
133 private static Expression accessInputVariable(Environment environment,
\r
134 org.simantics.scl.compiler.elaboration.expressions.Variable contextVariable) {
\r
135 SCLValue variableParentFunction = environment.getValue(VARIABLE_PARENT);
\r
136 return new EApply(new EConstant(variableParentFunction),
\r
137 new EApply(new EConstant(variableParentFunction),
\r
138 new EVariable(contextVariable)));
\r
141 protected static Name PROPERTY_VALUE_CACHED = Name.create("Document/All", "propertyValueCached");
\r
143 protected static Expression getProperty(Environment environment, Expression variable, String propertyName, Type type) {
\r
145 new EConstant(environment.getValue(FROM_DYNAMIC), type),
\r
147 new EConstant(environment.getValue(PROPERTY_VALUE_CACHED), Types.DYNAMIC),
\r
149 new ELiteral(new StringConstant(propertyName))));
\r
152 protected static Expression getPropertyFlexible(Environment environment, Expression variable, String propertyName, Type type) {
\r
153 return makeTypeFlexible(environment, getProperty(environment, variable, propertyName, type), type);
\r
156 protected static Expression standardGetProperty(
\r
157 Environment environment,
\r
158 org.simantics.scl.compiler.elaboration.expressions.Variable contextVariable,
\r
161 return getPropertyFlexible(environment, accessInputVariable(environment, contextVariable), name, type);
\r
166 protected Expression getVariableAccessExpression(
\r
168 CompilationContext context,
\r
169 org.simantics.scl.compiler.elaboration.expressions.Variable contextVariable,
\r
170 String name) throws DatabaseException {
\r
171 ComponentTypeProperty property = context.propertyMap.get(name);
\r
172 if(property != null)
\r
173 return standardGetProperty(
\r
174 context.runtimeEnvironment.getEnvironment(),
\r
177 property.type == null ? Types.metaVar(Kinds.STAR) : property.type);
\r
179 return getSpecialVariableAccessExpression(graph, context, contextVariable, name);
\r
182 protected Expression getSpecialVariableAccessExpression(ReadGraph graph,
\r
183 CompilationContext context,
\r
184 org.simantics.scl.compiler.elaboration.expressions.Variable contextVariable,
\r
185 String name) throws DatabaseException {
\r
186 if(name.equals("input")) {
\r
187 Environment environment = context.runtimeEnvironment.getEnvironment();
\r
188 return accessInputVariable(environment, contextVariable);
\r
190 else if(name.equals("self"))
\r
191 return new EVariable(contextVariable);
\r
197 public int hashCode() {
\r
198 return 31*(31*getClass().hashCode() + literal.hashCode()) + componentTypeAndRoot.hashCode();
\r
202 public boolean equals(Object obj) {
\r
205 if(obj == null || obj.getClass() != getClass())
\r
207 ServerSCLValueRequest other = (ServerSCLValueRequest)obj;
\r
208 return literal.equals(other.literal) && componentTypeAndRoot.equals(other.componentTypeAndRoot);
\r