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, Variable> {
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 while(represents == null) {
82 parent = parent.getParent(graph);
83 represents = parent.getPossibleRepresents(graph);
85 Resource root = graph.syncRequest(new IndexRoot(property.getRepresents(graph)));
86 return Pair.make(parent.getType(graph), root);
89 public static List<TCon> getEffects(ReadGraph graph, Variable context) throws DatabaseException {
91 ServerSCLHandlerValueRequest req = new ServerSCLHandlerValueRequest(graph, context);
92 return req.getExpressionEffects(graph);
93 } catch (DatabaseException e) {
94 throw (DatabaseException)e;
95 } catch (Throwable t) {
96 throw new DatabaseException(t);
100 public static Object compileAndEvaluate(ReadGraph graph, Variable context) throws DatabaseException {
101 SCLContext sclContext = SCLContext.getCurrent();
102 Object oldGraph = sclContext.get("graph");
104 Function1<Variable,Object> exp = graph.syncRequest(new ServerSCLHandlerValueRequest(graph, context),
105 TransientCacheListener.<Function1<Variable,Object>>instance());
106 sclContext.put("graph", graph);
107 return exp.apply(context);
108 } catch (DatabaseException e) {
109 throw (DatabaseException)e;
110 } catch (Throwable t) {
111 throw new DatabaseException(t);
113 sclContext.put("graph", oldGraph);
118 protected String getExpressionText(ReadGraph graph)
119 throws DatabaseException {
120 Layer0 L0 = Layer0.getInstance(graph);
121 String exp = graph.getRelatedValue(literal, L0.SCLValue_expression, Bindings.STRING);
122 return "\\context -> " + exp;
125 protected RuntimeEnvironmentRequest2 getRuntimeEnvironmentRequest(Resource componentType, Resource indexRoot) {
126 return new RuntimeEnvironmentRequest2(componentType, indexRoot) {
128 protected void fillEnvironmentSpecification(
129 EnvironmentSpecification environmentSpecification) {
135 protected CompilationContext getCompilationContext(ReadGraph graph) throws DatabaseException {
137 return graph.syncRequest(new UnaryRead<Pair<Resource,Resource>,CompilationContext>(componentTypeAndRoot) {
140 public CompilationContext perform(ReadGraph graph) throws DatabaseException {
142 RuntimeEnvironment runtimeEnvironment = graph.syncRequest(getRuntimeEnvironmentRequest(parameter.first, parameter.second));
143 Map<String, ComponentTypeProperty> propertyMap =
144 graph.syncRequest(new ReadComponentTypeInterfaceRequest(parameter.first, runtimeEnvironment.getEnvironment()),
145 TransientCacheListener.<Map<String, ComponentTypeProperty>>instance());
146 return new CompilationContext(runtimeEnvironment, propertyMap);
156 protected Type getContextVariableType() {
160 private static Expression accessInputVariable(Environment environment,
161 org.simantics.scl.compiler.elaboration.expressions.Variable contextVariable) {
162 SCLValue variableParentFunction = environment.getValue(VARIABLE_PARENT);
163 return new EApply(new EConstant(variableParentFunction),
164 new EApply(new EConstant(variableParentFunction),
165 new EVariable(contextVariable)));
168 protected static Expression standardGetProperty(
169 Environment environment,
170 org.simantics.scl.compiler.elaboration.expressions.Variable contextVariable,
173 return getPropertyFlexible(environment, accessInputVariable(environment, contextVariable), name, type);
177 protected Expression getVariableAccessExpression(
179 CompilationContext context,
180 org.simantics.scl.compiler.elaboration.expressions.Variable contextVariable,
181 String name) throws DatabaseException {
182 ComponentTypeProperty property = context.propertyMap.get(name);
184 return standardGetProperty(
185 context.runtimeEnvironment.getEnvironment(),
188 property.type == null ? Types.metaVar(Kinds.STAR) : property.type);
191 // if(context.propertyMap.containsKey(name)) {
193 // org.simantics.scl.compiler.elaboration.expressions.Variable parametersVariable = new org.simantics.scl.compiler.elaboration.expressions.Variable("context", COMMAND_CONTEXT);
195 // Environment environment = context.runtimeEnvironment.getEnvironment();
197 //// return new EApply(
198 //// new EConstant(environment.getValue(FROM_DYNAMIC), Types.STRING),
199 // return new EApply(
200 // new EConstant(environment.getValue(CONTEXT_VARIABLE), Types.DYNAMIC),
201 // new EVariable(parametersVariable),
202 // new ELiteral(new StringConstant(name)));
206 return getSpecialVariableAccessExpression(graph, context, contextVariable, name);
211 protected Expression getSpecialVariableAccessExpression(ReadGraph graph,
212 CompilationContext context,
213 org.simantics.scl.compiler.elaboration.expressions.Variable contextVariable,
214 String name) throws DatabaseException {
215 if(name.equals("input")) {
216 Environment environment = context.runtimeEnvironment.getEnvironment();
217 return accessInputVariable(environment, contextVariable);
218 } else if(name.equals("self"))
219 return new EVariable(contextVariable);
225 protected Type getExpectedType(ReadGraph graph, CompilationContext context) throws DatabaseException {
226 return super.getExpectedType(graph, context);
230 public int hashCode() {
231 return 31*(31*getClass().hashCode() + literal.hashCode()) + componentTypeAndRoot.hashCode();
235 public boolean equals(Object obj) {
240 if (getClass() != obj.getClass())
242 ServerSCLHandlerValueRequest other = (ServerSCLHandlerValueRequest) obj;
243 return literal.equals(other.literal) && componentTypeAndRoot.equals(other.componentTypeAndRoot);
246 private static Pair<Resource,Resource> getComponentTypeAndRoot(ReadGraph graph, Resource component) throws DatabaseException {
247 if(component != null) {
248 Resource type = graph.syncRequest(new FindPossibleComponentTypeRequest(component));
250 Resource root = graph.syncRequest(new IndexRoot(type));
251 return Pair.make(type, root);
253 Resource doc = graph.syncRequest(new PossibleTypedParent(component, DocumentationResource.getInstance(graph).Document));
254 Resource root = graph.syncRequest(new IndexRoot(component));
255 return Pair.make(graph.getSingleType(doc), root);
258 throw new IllegalStateException();