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.PossibleTypedParent;
11 import org.simantics.db.common.request.UnaryRead;
12 import org.simantics.db.exception.DatabaseException;
13 import org.simantics.db.layer0.scl.AbstractExpressionCompilationContext;
14 import org.simantics.db.layer0.scl.AbstractExpressionCompilationRequest;
15 import org.simantics.db.layer0.util.RuntimeEnvironmentRequest2;
16 import org.simantics.db.layer0.variable.Variable;
17 import org.simantics.document.base.ontology.DocumentationResource;
18 import org.simantics.document.server.request.ServerSCLValueRequest.CompilationContext;
19 import org.simantics.layer0.Layer0;
20 import org.simantics.scl.compiler.common.names.Name;
21 import org.simantics.scl.compiler.constants.StringConstant;
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.ELiteral;
25 import org.simantics.scl.compiler.elaboration.expressions.EVariable;
26 import org.simantics.scl.compiler.elaboration.expressions.Expression;
27 import org.simantics.scl.compiler.elaboration.modules.SCLValue;
28 import org.simantics.scl.compiler.environment.Environment;
29 import org.simantics.scl.compiler.environment.Environments;
30 import org.simantics.scl.compiler.environment.specification.EnvironmentSpecification;
31 import org.simantics.scl.compiler.runtime.RuntimeEnvironment;
32 import org.simantics.scl.compiler.top.SCLExpressionCompilationException;
33 import org.simantics.scl.compiler.types.Type;
34 import org.simantics.scl.compiler.types.Types;
35 import org.simantics.scl.compiler.types.kinds.Kinds;
36 import org.simantics.scl.runtime.SCLContext;
37 import org.simantics.scl.runtime.function.Function1;
38 import org.simantics.structural2.scl.ComponentTypeProperty;
39 import org.simantics.structural2.scl.FindPossibleComponentTypeRequest;
40 import org.simantics.structural2.scl.ReadComponentTypeInterfaceRequest;
41 import org.simantics.utils.datastructures.Pair;
42 import org.slf4j.Logger;
43 import org.slf4j.LoggerFactory;
45 public class ServerSCLValueRequest extends AbstractExpressionCompilationRequest<CompilationContext, Variable> {
47 private final Pair<Resource,Resource> componentTypeAndRoot;
48 private final Resource literal;
49 protected String possibleExpectedValueType;
51 public static class CompilationContext extends AbstractExpressionCompilationContext {
52 public final Map<String, ComponentTypeProperty> propertyMap;
54 public CompilationContext(RuntimeEnvironment runtimeEnvironment,
55 Map<String, ComponentTypeProperty> propertyMap) {
56 super(runtimeEnvironment);
57 this.propertyMap = propertyMap;
61 private ServerSCLValueRequest(Pair<Resource,Resource> componentTypeAndRoot, Resource literal, String possibleExpectedValueType) {
62 assert(literal != null);
63 this.literal = literal;
64 this.componentTypeAndRoot = componentTypeAndRoot;
65 this.possibleExpectedValueType = possibleExpectedValueType;
68 public ServerSCLValueRequest(ReadGraph graph, Variable context) throws DatabaseException {
69 this(getComponentTypeAndRoot(graph, context), context.getRepresents(graph), resolveExpectedValueType(graph, context.getPredicateResource(graph)));
72 public ServerSCLValueRequest(ReadGraph graph, Resource s, Resource o, Resource p) throws DatabaseException {
73 this(getComponentTypeAndRoot(graph, s), o, resolveExpectedValueType(graph, p));
76 private static Pair<Resource,Resource> getComponentTypeAndRoot(ReadGraph graph, Variable property) throws DatabaseException {
77 Variable parent = property.getParent(graph);
78 Resource represents = parent.getRepresents(graph);
79 if(represents != null) {
80 Resource type = graph.syncRequest(new FindPossibleComponentTypeRequest(represents));
82 Resource root = graph.syncRequest(new IndexRoot(type));
83 //System.err.println("getComponentTypeAndRoot1 " + property.getURI(graph) + " => " + graph.getPossibleURI(type) + " " + graph.getPossibleURI(root));
84 return Pair.make(type, root);
87 while(represents == null) {
88 parent = parent.getParent(graph);
89 represents = parent.getPossibleRepresents(graph);
91 // Resource root = graph.syncRequest(new IndexRoot(property.getRepresents(graph)));
92 Resource componentType = parent.getType(graph);
93 Resource root = graph.syncRequest(new IndexRoot(parent.getRepresents(graph)));
94 //System.err.println("getComponentTypeAndRoot2 " + property.getURI(graph) + " => " + graph.getPossibleURI(componentType) + " " + graph.getPossibleURI(root));
95 return Pair.make(componentType, root);
98 private static Pair<Resource,Resource> getComponentTypeAndRoot(ReadGraph graph, Resource component) throws DatabaseException {
99 if(component != null) {
100 Resource type = graph.syncRequest(new FindPossibleComponentTypeRequest(component));
102 Resource root = graph.syncRequest(new IndexRoot(type));
103 // System.err.println("getComponentTypeAndRoot3 " + graph.getPossibleURI(component) + " => " + graph.getPossibleURI(type) + " " + graph.getPossibleURI(root));
104 return Pair.make(type, root);
106 Resource doc = graph.syncRequest(new PossibleTypedParent(component, DocumentationResource.getInstance(graph).Document));
107 Resource componentType = graph.getSingleType(doc);
108 Resource root = graph.syncRequest(new IndexRoot(doc));
109 // System.err.println("getComponentTypeAndRoot4 " + graph.getPossibleURI(component) + " => " + graph.getPossibleURI(componentType) + " " + graph.getPossibleURI(root));
110 return Pair.make(componentType, root);
113 throw new IllegalStateException();
116 public static Object compileAndEvaluate(ReadGraph graph, Variable context) throws DatabaseException {
117 SCLContext sclContext = SCLContext.getCurrent();
118 Object oldGraph = sclContext.get("graph");
120 Function1<Variable,Object> exp = compile(graph, context);
121 sclContext.put("graph", graph);
122 return exp.apply(context);
123 } catch (DatabaseException e) {
124 throw (DatabaseException)e;
125 } catch (Throwable t) {
126 throw new DatabaseException(t);
128 sclContext.put("graph", oldGraph);
132 public static Function1<Variable, Object> compile(ReadGraph graph, Variable context) throws DatabaseException {
133 return graph.syncRequest(new ServerSCLValueRequest(graph, context), TransientCacheListener.<Function1<Variable,Object>>instance());
136 public static Function1<Variable, Object> compile(ReadGraph graph, Resource s, Resource o, Resource p) throws DatabaseException {
137 return graph.syncRequest(new ServerSCLValueRequest(graph, s, o, p), TransientCacheListener.<Function1<Variable,Object>>instance());
141 protected String getExpressionText(ReadGraph graph)
142 throws DatabaseException {
143 Layer0 L0 = Layer0.getInstance(graph);
144 return graph.getRelatedValue(literal, L0.SCLValue_expression, Bindings.STRING);
147 protected RuntimeEnvironmentRequest2 getRuntimeEnvironmentRequest(Resource componentType, Resource indexRoot) {
148 return new RuntimeEnvironmentRequest2(componentType, indexRoot) {
150 protected void fillEnvironmentSpecification(
151 EnvironmentSpecification environmentSpecification) {
157 protected CompilationContext getCompilationContext(ReadGraph graph)
158 throws DatabaseException {
159 return graph.syncRequest(new UnaryRead<Pair<Resource,Resource>,CompilationContext>(componentTypeAndRoot) {
161 public CompilationContext perform(ReadGraph graph)
162 throws DatabaseException {
163 RuntimeEnvironment runtimeEnvironment = graph.syncRequest(getRuntimeEnvironmentRequest(parameter.first, parameter.second));
164 Map<String, ComponentTypeProperty> propertyMap =
165 graph.syncRequest(new ReadComponentTypeInterfaceRequest(parameter.first, runtimeEnvironment.getEnvironment()),
166 TransientCacheListener.<Map<String, ComponentTypeProperty>>instance());
167 return new CompilationContext(runtimeEnvironment, propertyMap);
173 protected Type getContextVariableType() {
177 private static Expression accessInputVariable(Environment environment,
178 org.simantics.scl.compiler.elaboration.expressions.Variable contextVariable) {
179 SCLValue variableParentFunction = environment.getValue(VARIABLE_PARENT);
180 return new EApply(new EConstant(variableParentFunction),
181 new EApply(new EConstant(variableParentFunction),
182 new EVariable(contextVariable)));
185 protected static Name PROPERTY_VALUE_CACHED = Name.create("Document/All", "propertyValueCached");
187 protected static Expression getProperty(Environment environment, Expression variable, String propertyName, Type type) {
189 new EConstant(environment.getValue(FROM_DYNAMIC), type),
191 new EConstant(environment.getValue(PROPERTY_VALUE_CACHED), Types.DYNAMIC),
193 new ELiteral(new StringConstant(propertyName))));
196 protected static Expression getPropertyFlexible(Environment environment, Expression variable, String propertyName, Type type) {
197 return makeTypeFlexible(environment, getProperty(environment, variable, propertyName, type), type);
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);
210 protected Expression getVariableAccessExpression(
212 CompilationContext context,
213 org.simantics.scl.compiler.elaboration.expressions.Variable contextVariable,
214 String name) throws DatabaseException {
215 ComponentTypeProperty property = context.propertyMap.get(name);
217 return standardGetProperty(
218 context.runtimeEnvironment.getEnvironment(),
221 property.type == null ? Types.metaVar(Kinds.STAR) : property.type);
223 return getSpecialVariableAccessExpression(graph, context, contextVariable, name);
226 protected Expression getSpecialVariableAccessExpression(ReadGraph graph,
227 CompilationContext context,
228 org.simantics.scl.compiler.elaboration.expressions.Variable contextVariable,
229 String name) throws DatabaseException {
230 if(name.equals("input")) {
231 Environment environment = context.runtimeEnvironment.getEnvironment();
232 return accessInputVariable(environment, contextVariable);
234 else if(name.equals("self"))
235 return new EVariable(contextVariable);
241 protected Type getExpectedType(ReadGraph graph, CompilationContext context) throws DatabaseException {
242 return super.getExpectedType(graph, context);
247 public int hashCode() {
248 return 31*(31*getClass().hashCode() + literal.hashCode()) + componentTypeAndRoot.hashCode();
252 public boolean equals(Object obj) {
255 if(obj == null || obj.getClass() != getClass())
257 ServerSCLValueRequest other = (ServerSCLValueRequest)obj;
258 return literal.equals(other.literal) && componentTypeAndRoot.equals(other.componentTypeAndRoot);
261 public static Function1<Variable, Object> validate(ReadGraph graph, Variable context) throws DatabaseException {
262 return graph.syncRequest(new ServerSCLValueValidationRequest(graph, context), TransientCacheListener.<Function1<Variable,Object>>instance());
265 public static class ServerSCLValueValidationRequest extends ServerSCLValueRequest {
267 private static final Logger LOGGER = LoggerFactory.getLogger(ServerSCLHandlerValueRequest.class);
269 public ServerSCLValueValidationRequest(ReadGraph graph, Variable context) throws DatabaseException {
270 super(graph, context);
274 protected Type getExpectedType(ReadGraph graph, CompilationContext context) throws DatabaseException {
275 if(possibleExpectedValueType != null) {
277 return Environments.getType(context.runtimeEnvironment.getEnvironment(), possibleExpectedValueType);
278 } catch (SCLExpressionCompilationException e) {
279 LOGGER.error("Could not get type for " + String.valueOf(possibleExpectedValueType), e);
282 return super.getExpectedType(graph, context);