1 package org.simantics.db.layer0.scl;
3 import org.simantics.databoard.Bindings;
4 import org.simantics.db.ReadGraph;
5 import org.simantics.db.Resource;
6 import org.simantics.db.common.procedure.adapter.TransientCacheListener;
7 import org.simantics.db.common.request.IndexRoot;
8 import org.simantics.db.exception.DatabaseException;
9 import org.simantics.db.layer0.scl.CompileResourceValueRequest.CompilationContext;
10 import org.simantics.db.layer0.util.RuntimeEnvironmentRequest;
11 import org.simantics.layer0.Layer0;
12 import org.simantics.scl.compiler.elaboration.expressions.EVariable;
13 import org.simantics.scl.compiler.elaboration.expressions.Expression;
14 import org.simantics.scl.compiler.environment.Environments;
15 import org.simantics.scl.compiler.runtime.RuntimeEnvironment;
16 import org.simantics.scl.compiler.top.SCLExpressionCompilationException;
17 import org.simantics.scl.compiler.types.Type;
18 import org.simantics.scl.runtime.SCLContext;
19 import org.simantics.scl.runtime.function.Function1;
22 * Compiles an SCL expression that is attached to a literal
23 * whose parent is a component that is a part of a component type.
25 * @author Antti Villberg
27 public class CompileResourceValueRequest extends AbstractExpressionCompilationRequest<CompilationContext, Resource> {
29 public static class CompilationContext extends AbstractExpressionCompilationContext {
30 public CompilationContext(RuntimeEnvironment runtimeEnvironment) {
31 super(runtimeEnvironment);
35 protected final Resource literal;
37 public CompileResourceValueRequest(Resource literal) {
38 this.literal = literal;
41 public static Object compileAndEvaluate(ReadGraph graph, Resource literal) throws DatabaseException {
42 SCLContext sclContext = SCLContext.getCurrent();
43 Object oldGraph = sclContext.get("graph");
45 Function1<Resource,Object> exp = graph.syncRequest(new CompileResourceValueRequest(literal),
46 TransientCacheListener.<Function1<Resource,Object>>instance());
47 sclContext.put("graph", graph);
48 return exp.apply(literal);
49 } catch (DatabaseException e) {
50 throw (DatabaseException)e;
51 } catch (Throwable t) {
52 throw new DatabaseException(t);
54 sclContext.put("graph", oldGraph);
59 protected String getExpressionText(ReadGraph graph)
60 throws DatabaseException {
61 Layer0 L0 = Layer0.getInstance(graph);
62 return graph.getRelatedValue(literal, L0.SCLValue_expression, Bindings.STRING);
65 protected Resource getIndexRoot(ReadGraph graph) throws DatabaseException {
66 return graph.syncRequest(new IndexRoot(literal));
70 protected Type getExpectedType(ReadGraph graph, CompilationContext context)
71 throws DatabaseException {
72 Layer0 L0 = Layer0.getInstance(graph);
73 String valueType = graph.getPossibleRelatedValue(literal, L0.HasValueType, Bindings.STRING);
74 if(valueType != null) {
76 return Environments.getType(context.runtimeEnvironment.getEnvironment(), valueType);
77 } catch (SCLExpressionCompilationException e) {
81 return super.getExpectedType(graph, context);
85 protected CompilationContext getCompilationContext(ReadGraph graph)
86 throws DatabaseException {
87 Resource indexRoot = getIndexRoot(graph);
88 RuntimeEnvironment runtimeEnvironment = graph.syncRequest(new RuntimeEnvironmentRequest(indexRoot));
89 return new CompilationContext(runtimeEnvironment);
93 protected Type getContextVariableType() {
98 protected Expression getVariableAccessExpression(
100 CompilationContext context,
101 org.simantics.scl.compiler.elaboration.expressions.Variable contextVariable,
102 String name) throws DatabaseException {
103 if(name.equals("self"))
104 return new EVariable(contextVariable);