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.common.request.PossibleTypedParent;
12 import org.simantics.db.common.request.UnaryRead;
13 import org.simantics.db.exception.DatabaseException;
14 import org.simantics.db.layer0.scl.AbstractExpressionCompilationContext;
15 import org.simantics.db.layer0.scl.AbstractExpressionCompilationRequest;
16 import org.simantics.db.layer0.util.RuntimeEnvironmentRequest2;
17 import org.simantics.db.layer0.variable.Variable;
18 import org.simantics.document.base.ontology.DocumentationResource;
19 import org.simantics.document.server.request.ServerSCLHandlerValueRequest.CompilationContext;
20 import org.simantics.layer0.Layer0;
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.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.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;
40 public class ServerSCLHandlerValueRequest extends AbstractExpressionCompilationRequest<CompilationContext, Object> {
42 private final Pair<Resource,Resource> componentTypeAndRoot;
43 private final Resource literal;
44 protected String possibleExpectedValueType;
46 public static class CompilationContext extends AbstractExpressionCompilationContext {
47 public final Map<String, ComponentTypeProperty> propertyMap;
49 public CompilationContext(RuntimeEnvironment runtimeEnvironment,
50 Map<String, ComponentTypeProperty> propertyMap) {
51 super(runtimeEnvironment);
52 this.propertyMap = propertyMap;
56 private ServerSCLHandlerValueRequest(Pair<Resource,Resource> componentTypeAndRoot, Resource literal, String possibleExpectedValueType) {
57 assert(literal != null);
58 this.literal = literal;
59 this.componentTypeAndRoot = componentTypeAndRoot;
60 this.possibleExpectedValueType = possibleExpectedValueType;
63 public ServerSCLHandlerValueRequest(ReadGraph graph, Variable context) throws DatabaseException {
64 this(getComponentTypeAndRoot(graph, context), context.getRepresents(graph), resolveExpectedValueType(graph, context.getPredicateResource(graph)));
67 public ServerSCLHandlerValueRequest(ReadGraph graph, Resource s, Resource o, Resource p) throws DatabaseException {
68 this(getComponentTypeAndRoot(graph, s), o, resolveExpectedValueType(graph, p));
71 private static Pair<Resource,Resource> getComponentTypeAndRoot(ReadGraph graph, Variable property) throws DatabaseException {
72 Variable parent = property.getParent(graph);
73 Resource represents = parent.getRepresents(graph);
74 if(represents != null) {
75 Resource type = graph.syncRequest(new FindPossibleComponentTypeRequest(represents));
77 Resource root = graph.syncRequest(new IndexRoot(type));
78 return Pair.make(type, root);
81 parent = parent.getParent(graph);
82 Resource root = graph.syncRequest(new IndexRoot(property.getRepresents(graph)));
83 return Pair.make(parent.getType(graph), root);
86 private static Pair<Resource,Resource> getComponentTypeAndRoot(ReadGraph graph, Resource component) throws DatabaseException {
87 if(component != null) {
88 Resource type = graph.syncRequest(new FindPossibleComponentTypeRequest(component));
90 Resource root = graph.syncRequest(new IndexRoot(type));
91 return Pair.make(type, root);
93 Resource doc = graph.syncRequest(new PossibleTypedParent(component, DocumentationResource.getInstance(graph).Document));
94 Resource componentType = graph.getSingleType(doc);
95 Resource root = graph.syncRequest(new IndexRoot(doc));
96 return Pair.make(componentType, root);
99 throw new IllegalStateException();
102 public static List<TCon> getEffects(ReadGraph graph, Variable context) throws DatabaseException {
104 ServerSCLHandlerValueRequest req = new ServerSCLHandlerValueRequest(graph, context);
105 return req.getExpressionEffects(graph);
106 } catch (DatabaseException e) {
107 throw (DatabaseException)e;
108 } catch (Throwable t) {
109 throw new DatabaseException(t);
113 public static Object compileAndEvaluate(ReadGraph graph, Variable context) throws DatabaseException {
114 SCLContext sclContext = SCLContext.getCurrent();
115 Object oldGraph = sclContext.get("graph");
117 Function1<Object,Object> exp = graph.syncRequest(new ServerSCLHandlerValueRequest(graph, context),
118 TransientCacheListener.instance());
119 sclContext.put("graph", graph);
120 return exp.apply(context);
121 } catch (DatabaseException e) {
122 throw (DatabaseException)e;
123 } catch (Throwable t) {
124 throw new DatabaseException(t);
126 sclContext.put("graph", oldGraph);
130 public static Function1<Object, Object> compile(ReadGraph graph, Variable context) throws DatabaseException {
131 return graph.syncRequest(new ServerSCLHandlerValueRequest(graph, context), TransientCacheListener.instance());
134 public static Function1<Object, Object> compile(ReadGraph graph, Resource s, Resource o, Resource p) throws DatabaseException {
135 return graph.syncRequest(new ServerSCLHandlerValueRequest(graph, s, o, p), TransientCacheListener.<Function1<Object,Object>>instance());
139 protected String getExpressionText(ReadGraph graph)
140 throws DatabaseException {
141 Layer0 L0 = Layer0.getInstance(graph);
142 String exp = graph.getRelatedValue(literal, L0.SCLValue_expression, Bindings.STRING);
143 return "\\context -> " + exp;
146 protected RuntimeEnvironmentRequest2 getRuntimeEnvironmentRequest(Resource componentType, Resource indexRoot) {
147 return new RuntimeEnvironmentRequest2(componentType, indexRoot) {
149 protected void fillEnvironmentSpecification(
150 EnvironmentSpecification environmentSpecification) {
156 protected CompilationContext getCompilationContext(ReadGraph graph) throws DatabaseException {
157 return graph.syncRequest(new UnaryRead<Pair<Resource,Resource>,CompilationContext>(componentTypeAndRoot) {
159 public CompilationContext perform(ReadGraph graph) throws DatabaseException {
160 RuntimeEnvironment runtimeEnvironment = graph.syncRequest(getRuntimeEnvironmentRequest(parameter.first, parameter.second));
161 Map<String, ComponentTypeProperty> propertyMap =
162 graph.syncRequest(new ReadComponentTypeInterfaceRequest(parameter.first, runtimeEnvironment.getEnvironment()),
163 TransientCacheListener.<Map<String, ComponentTypeProperty>>instance());
164 return new CompilationContext(runtimeEnvironment, propertyMap);
170 protected Type getContextVariableType() {
174 private static Expression accessInputVariable(Environment environment,
175 org.simantics.scl.compiler.elaboration.expressions.Variable contextVariable) {
176 SCLValue variableParentFunction = environment.getValue(VARIABLE_PARENT);
177 return new EApply(new EConstant(variableParentFunction),
178 new EApply(new EConstant(variableParentFunction),
179 new EVariable(contextVariable)));
182 protected static Expression standardGetProperty(
183 Environment environment,
184 org.simantics.scl.compiler.elaboration.expressions.Variable contextVariable,
187 return getPropertyFlexible(environment, accessInputVariable(environment, contextVariable), name, type);
191 protected Expression getVariableAccessExpression(
193 CompilationContext context,
194 org.simantics.scl.compiler.elaboration.expressions.Variable contextVariable,
195 String name) throws DatabaseException {
196 ComponentTypeProperty property = context.propertyMap.get(name);
198 return standardGetProperty(
199 context.runtimeEnvironment.getEnvironment(),
202 property.type == null ? Types.metaVar(Kinds.STAR) : property.type);
205 // if(context.propertyMap.containsKey(name)) {
207 // org.simantics.scl.compiler.elaboration.expressions.Variable parametersVariable = new org.simantics.scl.compiler.elaboration.expressions.Variable("context", COMMAND_CONTEXT);
209 // Environment environment = context.runtimeEnvironment.getEnvironment();
211 //// return new EApply(
212 //// new EConstant(environment.getValue(FROM_DYNAMIC), Types.STRING),
213 // return new EApply(
214 // new EConstant(environment.getValue(CONTEXT_VARIABLE), Types.DYNAMIC),
215 // new EVariable(parametersVariable),
216 // new ELiteral(new StringConstant(name)));
220 return getSpecialVariableAccessExpression(graph, context, contextVariable, name);
225 protected Expression getSpecialVariableAccessExpression(ReadGraph graph,
226 CompilationContext context,
227 org.simantics.scl.compiler.elaboration.expressions.Variable contextVariable,
228 String name) throws DatabaseException {
229 if(name.equals("input")) {
230 Environment environment = context.runtimeEnvironment.getEnvironment();
231 return accessInputVariable(environment, contextVariable);
232 } else if(name.equals("self"))
233 return new EVariable(contextVariable);
239 protected Type getExpectedType(ReadGraph graph, CompilationContext context) throws DatabaseException {
240 return super.getExpectedType(graph, context);
244 public int hashCode() {
245 return 31*(31*getClass().hashCode() + literal.hashCode()) + componentTypeAndRoot.hashCode();
249 public boolean equals(Object obj) {
254 if (getClass() != obj.getClass())
256 ServerSCLHandlerValueRequest other = (ServerSCLHandlerValueRequest) obj;
257 return literal.equals(other.literal) && componentTypeAndRoot.equals(other.componentTypeAndRoot);