1 package org.simantics.document.server.request;
3 import java.util.Collections;
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.ServerSCLValueRequest.CompilationContext;
20 import org.simantics.layer0.Layer0;
21 import org.simantics.scl.compiler.common.names.Name;
22 import org.simantics.scl.compiler.constants.StringConstant;
23 import org.simantics.scl.compiler.elaboration.expressions.EApply;
24 import org.simantics.scl.compiler.elaboration.expressions.EConstant;
25 import org.simantics.scl.compiler.elaboration.expressions.ELiteral;
26 import org.simantics.scl.compiler.elaboration.expressions.EVariable;
27 import org.simantics.scl.compiler.elaboration.expressions.Expression;
28 import org.simantics.scl.compiler.elaboration.modules.SCLValue;
29 import org.simantics.scl.compiler.environment.Environment;
30 import org.simantics.scl.compiler.environment.Environments;
31 import org.simantics.scl.compiler.environment.specification.EnvironmentSpecification;
32 import org.simantics.scl.compiler.runtime.RuntimeEnvironment;
33 import org.simantics.scl.compiler.top.SCLExpressionCompilationException;
34 import org.simantics.scl.compiler.types.Type;
35 import org.simantics.scl.compiler.types.Types;
36 import org.simantics.scl.compiler.types.kinds.Kinds;
37 import org.simantics.scl.runtime.SCLContext;
38 import org.simantics.scl.runtime.function.Function1;
39 import org.simantics.structural2.scl.ComponentTypeProperty;
40 import org.simantics.structural2.scl.FindPossibleComponentTypeRequest;
41 import org.simantics.structural2.scl.ReadComponentTypeInterfaceRequest;
42 import org.simantics.utils.datastructures.Pair;
43 import org.slf4j.Logger;
44 import org.slf4j.LoggerFactory;
46 public class ServerSCLValueRequest extends AbstractExpressionCompilationRequest<CompilationContext, Variable> {
48 private final Pair<Resource,Resource> componentTypeAndRoot;
49 private final Resource literal;
50 protected String possibleExpectedValueType;
52 public static class CompilationContext extends AbstractExpressionCompilationContext {
53 public final Map<String, ComponentTypeProperty> propertyMap;
55 public CompilationContext(RuntimeEnvironment runtimeEnvironment,
56 Map<String, ComponentTypeProperty> propertyMap) {
57 super(runtimeEnvironment);
58 this.propertyMap = propertyMap;
62 private ServerSCLValueRequest(Pair<Resource,Resource> componentTypeAndRoot, Resource literal, String possibleExpectedValueType) {
63 assert(literal != null);
64 this.literal = literal;
65 this.componentTypeAndRoot = componentTypeAndRoot;
66 this.possibleExpectedValueType = possibleExpectedValueType;
69 public ServerSCLValueRequest(ReadGraph graph, Variable context) throws DatabaseException {
70 this(getComponentTypeAndRoot(graph, context), context.getRepresents(graph), resolveExpectedValueType(graph, context.getPredicateResource(graph)));
73 public ServerSCLValueRequest(ReadGraph graph, Resource s, Resource o, Resource p) throws DatabaseException {
74 this(getComponentTypeAndRoot(graph, s), o, resolveExpectedValueType(graph, p));
77 private static Pair<Resource,Resource> getComponentTypeAndRoot(ReadGraph graph, Variable property) throws DatabaseException {
78 Variable parent = property.getParent(graph);
79 Resource represents = parent.getRepresents(graph);
80 if(represents != null) {
81 Resource type = graph.syncRequest(new FindPossibleComponentTypeRequest(represents));
83 Resource root = graph.syncRequest(new IndexRoot(type));
84 //System.err.println("getComponentTypeAndRoot1 " + property.getURI(graph) + " => " + graph.getPossibleURI(type) + " " + graph.getPossibleURI(root));
85 return Pair.make(type, root);
88 while(represents == null) {
89 parent = parent.getParent(graph);
90 represents = parent.getPossibleRepresents(graph);
92 // Resource root = graph.syncRequest(new IndexRoot(property.getRepresents(graph)));
93 Resource componentType = parent.getType(graph);
94 Resource root = graph.syncRequest(new IndexRoot(parent.getRepresents(graph)));
95 //System.err.println("getComponentTypeAndRoot2 " + property.getURI(graph) + " => " + graph.getPossibleURI(componentType) + " " + graph.getPossibleURI(root));
96 return Pair.make(componentType, root);
99 private static Pair<Resource,Resource> getComponentTypeAndRoot(ReadGraph graph, Resource component) throws DatabaseException {
100 if(component != null) {
101 Resource type = graph.syncRequest(new FindPossibleComponentTypeRequest(component));
103 Resource root = graph.syncRequest(new IndexRoot(type));
104 // System.err.println("getComponentTypeAndRoot3 " + graph.getPossibleURI(component) + " => " + graph.getPossibleURI(type) + " " + graph.getPossibleURI(root));
105 return Pair.make(type, root);
107 Resource doc = graph.syncRequest(new PossibleTypedParent(component, DocumentationResource.getInstance(graph).Document));
109 Resource componentType = graph.getSingleType(doc);
110 Resource root = graph.syncRequest(new IndexRoot(doc));
111 return Pair.make(componentType, root);
113 System.err.println("component = " + component);
114 Resource root = graph.syncRequest(new IndexRoot(component));
115 // Resource componentType = graph.getSingleType(doc);
116 return Pair.make(null, root);
120 throw new IllegalStateException();
123 public static Object compileAndEvaluate(ReadGraph graph, Variable context) throws DatabaseException {
124 SCLContext sclContext = SCLContext.getCurrent();
125 Object oldGraph = sclContext.get("graph");
127 Function1<Variable,Object> exp = compile(graph, context);
128 sclContext.put("graph", graph);
129 return exp.apply(context);
130 } catch (DatabaseException e) {
131 throw (DatabaseException)e;
132 } catch (Throwable t) {
133 throw new DatabaseException(t);
135 sclContext.put("graph", oldGraph);
139 public static Function1<Variable, Object> compile(ReadGraph graph, Variable context) throws DatabaseException {
140 return graph.syncRequest(new ServerSCLValueRequest(graph, context), TransientCacheListener.<Function1<Variable,Object>>instance());
143 public static Function1<Variable, Object> compile(ReadGraph graph, Resource s, Resource o, Resource p) throws DatabaseException {
144 return graph.syncRequest(new ServerSCLValueRequest(graph, s, o, p), TransientCacheListener.<Function1<Variable,Object>>instance());
148 protected String getExpressionText(ReadGraph graph)
149 throws DatabaseException {
150 Layer0 L0 = Layer0.getInstance(graph);
151 return graph.getRelatedValue(literal, L0.SCLValue_expression, Bindings.STRING);
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)
165 throws DatabaseException {
166 return graph.syncRequest(new UnaryRead<Pair<Resource,Resource>,CompilationContext>(componentTypeAndRoot) {
168 public CompilationContext perform(ReadGraph graph)
169 throws DatabaseException {
170 RuntimeEnvironment runtimeEnvironment = graph.syncRequest(getRuntimeEnvironmentRequest(parameter.first, parameter.second));
171 if(parameter.first != null) {
172 Map<String, ComponentTypeProperty> propertyMap =
173 graph.syncRequest(new ReadComponentTypeInterfaceRequest(parameter.first, runtimeEnvironment.getEnvironment()),
174 TransientCacheListener.<Map<String, ComponentTypeProperty>>instance());
175 return new CompilationContext(runtimeEnvironment, propertyMap);
177 return new CompilationContext(runtimeEnvironment, Collections.emptyMap());
184 protected Type getContextVariableType() {
188 private static Expression accessInputVariable(Environment environment,
189 org.simantics.scl.compiler.elaboration.expressions.Variable contextVariable) {
190 SCLValue variableParentFunction = environment.getValue(VARIABLE_PARENT);
191 return new EApply(new EConstant(variableParentFunction),
192 new EApply(new EConstant(variableParentFunction),
193 new EVariable(contextVariable)));
196 protected static Name PROPERTY_VALUE_CACHED = Name.create("Document/All", "propertyValueCached");
198 protected static Expression getProperty(Environment environment, Expression variable, String propertyName, Type type) {
200 new EConstant(environment.getValue(FROM_DYNAMIC), type),
202 new EConstant(environment.getValue(PROPERTY_VALUE_CACHED), Types.DYNAMIC),
204 new ELiteral(new StringConstant(propertyName))));
207 protected static Expression getPropertyFlexible(Environment environment, Expression variable, String propertyName, Type type) {
208 return makeTypeFlexible(environment, getProperty(environment, variable, propertyName, type), type);
211 protected static Expression standardGetProperty(
212 Environment environment,
213 org.simantics.scl.compiler.elaboration.expressions.Variable contextVariable,
216 return getPropertyFlexible(environment, accessInputVariable(environment, contextVariable), name, type);
221 protected Expression getVariableAccessExpression(
223 CompilationContext context,
224 org.simantics.scl.compiler.elaboration.expressions.Variable contextVariable,
225 String name) throws DatabaseException {
226 ComponentTypeProperty property = context.propertyMap.get(name);
228 return standardGetProperty(
229 context.runtimeEnvironment.getEnvironment(),
232 property.type == null ? Types.metaVar(Kinds.STAR) : property.type);
234 return getSpecialVariableAccessExpression(graph, context, contextVariable, name);
237 protected Expression getSpecialVariableAccessExpression(ReadGraph graph,
238 CompilationContext context,
239 org.simantics.scl.compiler.elaboration.expressions.Variable contextVariable,
240 String name) throws DatabaseException {
241 if(name.equals("input")) {
242 Environment environment = context.runtimeEnvironment.getEnvironment();
243 return accessInputVariable(environment, contextVariable);
245 else if(name.equals("self"))
246 return new EVariable(contextVariable);
252 protected Type getExpectedType(ReadGraph graph, CompilationContext context) throws DatabaseException {
253 return super.getExpectedType(graph, context);
258 public int hashCode() {
259 return 31*(31*getClass().hashCode() + literal.hashCode()) + componentTypeAndRoot.hashCode();
263 public boolean equals(Object obj) {
266 if(obj == null || obj.getClass() != getClass())
268 ServerSCLValueRequest other = (ServerSCLValueRequest)obj;
269 return literal.equals(other.literal) && componentTypeAndRoot.equals(other.componentTypeAndRoot);
272 public static Function1<Variable, Object> validate(ReadGraph graph, Variable context) throws DatabaseException {
273 return graph.syncRequest(new ServerSCLValueValidationRequest(graph, context), TransientCacheListener.<Function1<Variable,Object>>instance());
276 public static class ServerSCLValueValidationRequest extends ServerSCLValueRequest {
278 private static final Logger LOGGER = LoggerFactory.getLogger(ServerSCLHandlerValueRequest.class);
280 public ServerSCLValueValidationRequest(ReadGraph graph, Variable context) throws DatabaseException {
281 super(graph, context);
285 protected Type getExpectedType(ReadGraph graph, CompilationContext context) throws DatabaseException {
286 if(possibleExpectedValueType != null) {
288 return Environments.getType(context.runtimeEnvironment.getEnvironment(), possibleExpectedValueType);
289 } catch (SCLExpressionCompilationException e) {
290 LOGGER.error("Could not get type for " + String.valueOf(possibleExpectedValueType), e);
293 return super.getExpectedType(graph, context);