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