1 package org.simantics.document.server.request;
5 import org.simantics.databoard.Bindings;
6 import org.simantics.db.ReadGraph;
7 import org.simantics.db.Resource;
8 import org.simantics.db.common.procedure.adapter.TransientCacheListener;
9 import org.simantics.db.common.request.IndexRoot;
10 import org.simantics.db.common.request.UnaryRead;
11 import org.simantics.db.exception.DatabaseException;
12 import org.simantics.db.layer0.scl.AbstractExpressionCompilationContext;
13 import org.simantics.db.layer0.scl.AbstractExpressionCompilationRequest;
14 import org.simantics.db.layer0.util.RuntimeEnvironmentRequest;
15 import org.simantics.db.layer0.util.RuntimeEnvironmentRequest2;
16 import org.simantics.db.layer0.variable.Variable;
17 import org.simantics.document.server.request.ServerSCLValueRequest.CompilationContext;
18 import org.simantics.layer0.Layer0;
19 import org.simantics.scl.compiler.common.names.Name;
20 import org.simantics.scl.compiler.constants.StringConstant;
21 import org.simantics.scl.compiler.elaboration.expressions.EApply;
22 import org.simantics.scl.compiler.elaboration.expressions.EConstant;
23 import org.simantics.scl.compiler.elaboration.expressions.ELiteral;
24 import org.simantics.scl.compiler.elaboration.expressions.EVariable;
25 import org.simantics.scl.compiler.elaboration.expressions.Expression;
26 import org.simantics.scl.compiler.elaboration.modules.SCLValue;
27 import org.simantics.scl.compiler.environment.Environment;
28 import org.simantics.scl.compiler.environment.specification.EnvironmentSpecification;
29 import org.simantics.scl.compiler.runtime.RuntimeEnvironment;
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;
40 public class ServerSCLValueRequest extends AbstractExpressionCompilationRequest<CompilationContext, Variable> {
42 private final Pair<Resource,Resource> componentTypeAndRoot;
43 private final Resource literal;
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 ServerSCLValueRequest(Pair<Resource,Resource> componentTypeAndRoot, Resource literal) {
56 assert(literal != null);
57 this.literal = literal;
58 this.componentTypeAndRoot = componentTypeAndRoot;
61 public ServerSCLValueRequest(ReadGraph graph, Variable context) throws DatabaseException {
62 this(getComponentTypeAndRoot(graph, context), context.getRepresents(graph));
65 private static Pair<Resource,Resource> getComponentTypeAndRoot(ReadGraph graph, Variable property) throws DatabaseException {
66 Variable parent = property.getParent(graph);
67 Resource represents = parent.getRepresents(graph);
68 if(represents != null) {
69 Resource type = graph.syncRequest(new FindPossibleComponentTypeRequest(represents));
71 Resource root = graph.syncRequest(new IndexRoot(type));
72 return Pair.make(type, root);
75 parent = parent.getParent(graph);
76 Resource root = graph.syncRequest(new IndexRoot(property.getRepresents(graph)));
77 return Pair.make(parent.getType(graph), root);
80 public static Object compileAndEvaluate(ReadGraph graph, Variable context) throws DatabaseException {
81 SCLContext sclContext = SCLContext.getCurrent();
82 Object oldGraph = sclContext.get("graph");
84 Function1<Variable,Object> exp = graph.syncRequest(new ServerSCLValueRequest(graph, context),
85 TransientCacheListener.<Function1<Variable,Object>>instance());
86 sclContext.put("graph", graph);
87 return exp.apply(context);
88 } catch (DatabaseException e) {
89 throw (DatabaseException)e;
90 } catch (Throwable t) {
91 throw new DatabaseException(t);
93 sclContext.put("graph", oldGraph);
98 protected String getExpressionText(ReadGraph graph)
99 throws DatabaseException {
100 Layer0 L0 = Layer0.getInstance(graph);
101 return graph.getRelatedValue(literal, L0.SCLValue_expression, Bindings.STRING);
104 protected RuntimeEnvironmentRequest2 getRuntimeEnvironmentRequest(Resource componentType, Resource indexRoot) {
105 return new RuntimeEnvironmentRequest2(componentType, indexRoot) {
107 protected void fillEnvironmentSpecification(
108 EnvironmentSpecification environmentSpecification) {
114 protected CompilationContext getCompilationContext(ReadGraph graph)
115 throws DatabaseException {
116 return graph.syncRequest(new UnaryRead<Pair<Resource,Resource>,CompilationContext>(componentTypeAndRoot) {
118 public CompilationContext perform(ReadGraph graph)
119 throws DatabaseException {
120 RuntimeEnvironment runtimeEnvironment = graph.syncRequest(getRuntimeEnvironmentRequest(parameter.first, parameter.second));
121 Map<String, ComponentTypeProperty> propertyMap =
122 graph.syncRequest(new ReadComponentTypeInterfaceRequest(parameter.first, runtimeEnvironment.getEnvironment()),
123 TransientCacheListener.<Map<String, ComponentTypeProperty>>instance());
124 return new CompilationContext(runtimeEnvironment, propertyMap);
130 protected Type getContextVariableType() {
134 private static Expression accessInputVariable(Environment environment,
135 org.simantics.scl.compiler.elaboration.expressions.Variable contextVariable) {
136 SCLValue variableParentFunction = environment.getValue(VARIABLE_PARENT);
137 return new EApply(new EConstant(variableParentFunction),
138 new EApply(new EConstant(variableParentFunction),
139 new EVariable(contextVariable)));
142 protected static Name PROPERTY_VALUE_CACHED = Name.create("Document/All", "propertyValueCached");
144 protected static Expression getProperty(Environment environment, Expression variable, String propertyName, Type type) {
146 new EConstant(environment.getValue(FROM_DYNAMIC), type),
148 new EConstant(environment.getValue(PROPERTY_VALUE_CACHED), Types.DYNAMIC),
150 new ELiteral(new StringConstant(propertyName))));
153 protected static Expression getPropertyFlexible(Environment environment, Expression variable, String propertyName, Type type) {
154 return makeTypeFlexible(environment, getProperty(environment, variable, propertyName, type), type);
157 protected static Expression standardGetProperty(
158 Environment environment,
159 org.simantics.scl.compiler.elaboration.expressions.Variable contextVariable,
162 return getPropertyFlexible(environment, accessInputVariable(environment, contextVariable), name, type);
167 protected Expression getVariableAccessExpression(
169 CompilationContext context,
170 org.simantics.scl.compiler.elaboration.expressions.Variable contextVariable,
171 String name) throws DatabaseException {
172 ComponentTypeProperty property = context.propertyMap.get(name);
174 return standardGetProperty(
175 context.runtimeEnvironment.getEnvironment(),
178 property.type == null ? Types.metaVar(Kinds.STAR) : property.type);
180 return getSpecialVariableAccessExpression(graph, context, contextVariable, name);
183 protected Expression getSpecialVariableAccessExpression(ReadGraph graph,
184 CompilationContext context,
185 org.simantics.scl.compiler.elaboration.expressions.Variable contextVariable,
186 String name) throws DatabaseException {
187 if(name.equals("input")) {
188 Environment environment = context.runtimeEnvironment.getEnvironment();
189 return accessInputVariable(environment, contextVariable);
191 else if(name.equals("self"))
192 return new EVariable(contextVariable);
198 public int hashCode() {
199 return 31*(31*getClass().hashCode() + literal.hashCode()) + componentTypeAndRoot.hashCode();
203 public boolean equals(Object obj) {
206 if(obj == null || obj.getClass() != getClass())
208 ServerSCLValueRequest other = (ServerSCLValueRequest)obj;
209 return literal.equals(other.literal) && componentTypeAndRoot.equals(other.componentTypeAndRoot);