1 package org.simantics.document.server.request;
3 import java.util.Collections;
7 import org.simantics.databoard.Bindings;
8 import org.simantics.db.ReadGraph;
9 import org.simantics.db.Resource;
10 import org.simantics.db.common.procedure.adapter.TransientCacheListener;
11 import org.simantics.db.common.request.IndexRoot;
12 import org.simantics.db.common.request.PossibleTypedParent;
13 import org.simantics.db.common.request.UnaryRead;
14 import org.simantics.db.exception.DatabaseException;
15 import org.simantics.db.layer0.scl.AbstractExpressionCompilationContext;
16 import org.simantics.db.layer0.scl.AbstractExpressionCompilationRequest;
17 import org.simantics.db.layer0.util.RuntimeEnvironmentRequest2;
18 import org.simantics.db.layer0.variable.Variable;
19 import org.simantics.document.base.ontology.DocumentationResource;
20 import org.simantics.document.server.request.ServerSCLHandlerValueRequest.CompilationContext;
21 import org.simantics.layer0.Layer0;
22 import org.simantics.scl.compiler.elaboration.expressions.EApply;
23 import org.simantics.scl.compiler.elaboration.expressions.EConstant;
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.TCon;
31 import org.simantics.scl.compiler.types.Type;
32 import org.simantics.scl.compiler.types.Types;
33 import org.simantics.scl.compiler.types.kinds.Kinds;
34 import org.simantics.scl.runtime.SCLContext;
35 import org.simantics.scl.runtime.function.Function1;
36 import org.simantics.structural2.scl.ComponentTypeProperty;
37 import org.simantics.structural2.scl.FindPossibleComponentTypeRequest;
38 import org.simantics.structural2.scl.ReadComponentTypeInterfaceRequest;
39 import org.simantics.utils.datastructures.Pair;
41 public class ServerSCLHandlerValueRequest extends AbstractExpressionCompilationRequest<CompilationContext, Object> {
43 private final Pair<Resource,Resource> componentTypeAndRoot;
44 private final Resource literal;
45 protected String possibleExpectedValueType;
47 public static class CompilationContext extends AbstractExpressionCompilationContext {
48 public final Map<String, ComponentTypeProperty> propertyMap;
50 public CompilationContext(RuntimeEnvironment runtimeEnvironment,
51 Map<String, ComponentTypeProperty> propertyMap) {
52 super(runtimeEnvironment);
53 this.propertyMap = propertyMap;
57 private ServerSCLHandlerValueRequest(Pair<Resource,Resource> componentTypeAndRoot, Resource literal, String possibleExpectedValueType) {
58 assert(literal != null);
59 this.literal = literal;
60 this.componentTypeAndRoot = componentTypeAndRoot;
61 this.possibleExpectedValueType = possibleExpectedValueType;
64 public ServerSCLHandlerValueRequest(ReadGraph graph, Variable context) throws DatabaseException {
65 this(getComponentTypeAndRoot(graph, context), context.getRepresents(graph), resolveExpectedValueType(graph, context.getPredicateResource(graph)));
68 public ServerSCLHandlerValueRequest(ReadGraph graph, Resource s, Resource o, Resource p) throws DatabaseException {
69 this(getComponentTypeAndRoot(graph, s, o), o, resolveExpectedValueType(graph, p));
72 private static Pair<Resource,Resource> getComponentTypeAndRoot(ReadGraph graph, Variable property) throws DatabaseException {
73 Variable parent = property.getParent(graph);
74 Resource represents = parent.getRepresents(graph);
75 if(represents != null) {
76 Resource type = graph.syncRequest(new FindPossibleComponentTypeRequest(represents));
78 Resource root = graph.syncRequest(new IndexRoot(type));
79 return Pair.make(type, root);
82 parent = parent.getParent(graph);
83 Resource root = graph.syncRequest(new IndexRoot(property.getRepresents(graph)));
84 return Pair.make(parent.getType(graph), root);
87 private static Pair<Resource,Resource> getComponentTypeAndRoot(ReadGraph graph, Resource component, Resource literal) throws DatabaseException {
88 if(component != null) {
89 Resource type = graph.syncRequest(new FindPossibleComponentTypeRequest(component));
91 Resource root = graph.syncRequest(new IndexRoot(type));
92 return Pair.make(type, root);
94 Resource doc = graph.syncRequest(new PossibleTypedParent(component, DocumentationResource.getInstance(graph).Document));
95 Resource componentType = graph.getSingleType(doc);
96 Resource root = graph.syncRequest(new IndexRoot(doc));
97 return Pair.make(componentType, root);
99 // TODO: For Antti to consider and fix later
100 // Introduced to handle procedural user components where component == null
101 } else if (literal != null) {
102 Resource root = graph.syncRequest(new IndexRoot(literal));
103 return Pair.make(null, root);
105 throw new DatabaseException("Couldn't resolve component type and root for component == null && literal == null");
107 //throw new IllegalStateException();
110 public static List<TCon> getEffects(ReadGraph graph, Variable context) throws DatabaseException {
112 ServerSCLHandlerValueRequest req = new ServerSCLHandlerValueRequest(graph, context);
113 return req.getExpressionEffects(graph);
114 } catch (DatabaseException e) {
115 throw (DatabaseException)e;
116 } catch (Throwable t) {
117 throw new DatabaseException(t);
121 public static Object compileAndEvaluate(ReadGraph graph, Variable context) throws DatabaseException {
122 SCLContext sclContext = SCLContext.getCurrent();
123 Object oldGraph = sclContext.get("graph");
125 Function1<Object,Object> exp = graph.syncRequest(new ServerSCLHandlerValueRequest(graph, context),
126 TransientCacheListener.instance());
127 sclContext.put("graph", graph);
128 return exp.apply(context);
129 } catch (DatabaseException e) {
130 throw (DatabaseException)e;
131 } catch (Throwable t) {
132 throw new DatabaseException(t);
134 sclContext.put("graph", oldGraph);
138 public static Function1<Object, Object> compile(ReadGraph graph, Variable context) throws DatabaseException {
139 return graph.syncRequest(new ServerSCLHandlerValueRequest(graph, context), TransientCacheListener.instance());
142 public static Function1<Object, Object> compile(ReadGraph graph, Resource s, Resource o, Resource p) throws DatabaseException {
143 return graph.syncRequest(new ServerSCLHandlerValueRequest(graph, s, o, p), TransientCacheListener.<Function1<Object,Object>>instance());
147 protected String getExpressionText(ReadGraph graph)
148 throws DatabaseException {
149 Layer0 L0 = Layer0.getInstance(graph);
150 String exp = graph.getRelatedValue(literal, L0.SCLValue_expression, Bindings.STRING);
151 return "\\context -> " + exp;
154 protected RuntimeEnvironmentRequest2 getRuntimeEnvironmentRequest(Resource componentType, Resource indexRoot) {
155 return new RuntimeEnvironmentRequest2(componentType, indexRoot) {
157 protected void fillEnvironmentSpecification(
158 EnvironmentSpecification environmentSpecification) {
164 protected CompilationContext getCompilationContext(ReadGraph graph) throws DatabaseException {
165 return graph.syncRequest(new UnaryRead<Pair<Resource,Resource>,CompilationContext>(componentTypeAndRoot) {
167 public CompilationContext perform(ReadGraph graph) throws DatabaseException {
168 RuntimeEnvironment runtimeEnvironment = graph.syncRequest(getRuntimeEnvironmentRequest(parameter.first, parameter.second));
169 Map<String, ComponentTypeProperty> propertyMap;
170 if (parameter.first != null) {
172 graph.syncRequest(new ReadComponentTypeInterfaceRequest(parameter.first, runtimeEnvironment.getEnvironment()),
173 TransientCacheListener.<Map<String, ComponentTypeProperty>>instance());
175 // TODO: Antti to consider
176 // To handle procedural user components
177 propertyMap = Collections.emptyMap();
179 // Map<String, ComponentTypeProperty> propertyMap =
180 // graph.syncRequest(new ReadComponentTypeInterfaceRequest(parameter.first, runtimeEnvironment.getEnvironment()),
181 // TransientCacheListener.<Map<String, ComponentTypeProperty>>instance());
182 return new CompilationContext(runtimeEnvironment, propertyMap);
188 protected Type getContextVariableType() {
192 private static Expression accessInputVariable(Environment environment,
193 org.simantics.scl.compiler.elaboration.expressions.Variable contextVariable) {
194 SCLValue variableParentFunction = environment.getValue(VARIABLE_PARENT);
195 return new EApply(new EConstant(variableParentFunction),
196 new EApply(new EConstant(variableParentFunction),
197 new EVariable(contextVariable)));
200 protected static Expression standardGetProperty(
201 Environment environment,
202 org.simantics.scl.compiler.elaboration.expressions.Variable contextVariable,
205 return getPropertyFlexible(environment, accessInputVariable(environment, contextVariable), name, type);
209 protected Expression getVariableAccessExpression(
211 CompilationContext context,
212 org.simantics.scl.compiler.elaboration.expressions.Variable contextVariable,
213 String name) throws DatabaseException {
214 ComponentTypeProperty property = context.propertyMap.get(name);
216 return standardGetProperty(
217 context.runtimeEnvironment.getEnvironment(),
220 property.type == null ? Types.metaVar(Kinds.STAR) : property.type);
223 // if(context.propertyMap.containsKey(name)) {
225 // org.simantics.scl.compiler.elaboration.expressions.Variable parametersVariable = new org.simantics.scl.compiler.elaboration.expressions.Variable("context", COMMAND_CONTEXT);
227 // Environment environment = context.runtimeEnvironment.getEnvironment();
229 //// return new EApply(
230 //// new EConstant(environment.getValue(FROM_DYNAMIC), Types.STRING),
231 // return new EApply(
232 // new EConstant(environment.getValue(CONTEXT_VARIABLE), Types.DYNAMIC),
233 // new EVariable(parametersVariable),
234 // new ELiteral(new StringConstant(name)));
238 return getSpecialVariableAccessExpression(graph, context, contextVariable, name);
243 protected Expression getSpecialVariableAccessExpression(ReadGraph graph,
244 CompilationContext context,
245 org.simantics.scl.compiler.elaboration.expressions.Variable contextVariable,
246 String name) throws DatabaseException {
247 if(name.equals("input")) {
248 Environment environment = context.runtimeEnvironment.getEnvironment();
249 return accessInputVariable(environment, contextVariable);
250 } else if(name.equals("self"))
251 return new EVariable(contextVariable);
257 protected Type getExpectedType(ReadGraph graph, CompilationContext context) throws DatabaseException {
258 return super.getExpectedType(graph, context);
262 public int hashCode() {
263 return 31*(31*getClass().hashCode() + literal.hashCode()) + componentTypeAndRoot.hashCode();
267 public boolean equals(Object obj) {
272 if (getClass() != obj.getClass())
274 ServerSCLHandlerValueRequest other = (ServerSCLHandlerValueRequest) obj;
275 return literal.equals(other.literal) && componentTypeAndRoot.equals(other.componentTypeAndRoot);