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.CompileValueRequest.CompilationContext;
10 import org.simantics.db.layer0.util.RuntimeEnvironmentRequest;
11 import org.simantics.db.layer0.variable.Variable;
12 import org.simantics.layer0.Layer0;
13 import org.simantics.scl.compiler.elaboration.expressions.EVariable;
14 import org.simantics.scl.compiler.elaboration.expressions.Expression;
15 import org.simantics.scl.compiler.environment.Environments;
16 import org.simantics.scl.compiler.runtime.RuntimeEnvironment;
17 import org.simantics.scl.compiler.top.SCLExpressionCompilationException;
18 import org.simantics.scl.compiler.types.Type;
19 import org.simantics.scl.runtime.SCLContext;
20 import org.simantics.scl.runtime.function.Function1;
23 * Compiles an SCL expression that is attached to a literal
24 * whose parent is a component that is a part of a component type.
26 * @author Tuukka Lehtonen
28 public class CompileValueRequest extends AbstractExpressionCompilationRequest<CompilationContext, Variable> {
30 public static class CompilationContext extends AbstractExpressionCompilationContext {
31 public CompilationContext(RuntimeEnvironment runtimeEnvironment) {
32 super(runtimeEnvironment);
36 protected final Resource relation;
37 protected final Resource component;
38 protected final Resource literal;
40 public CompileValueRequest(Resource component, Resource literal, Resource relation) {
41 this.relation = relation;
42 this.component = component;
43 this.literal = literal;
46 public CompileValueRequest(ReadGraph graph, Variable context) throws DatabaseException {
47 this(context.getParent(graph).getRepresents(graph),
48 context.getRepresents(graph),
49 context.getPredicateResource(graph));
52 public static Object compileAndEvaluate(ReadGraph graph, Variable context) throws DatabaseException {
53 SCLContext sclContext = SCLContext.getCurrent();
54 Object oldGraph = sclContext.get("graph");
56 Function1<Variable,Object> exp = graph.syncRequest(new CompileValueRequest(graph, context),
57 TransientCacheListener.<Function1<Variable,Object>>instance());
58 sclContext.put("graph", graph);
59 return exp.apply(context);
60 } catch (DatabaseException e) {
61 throw (DatabaseException)e;
62 } catch (Throwable t) {
63 throw new DatabaseException(t);
65 sclContext.put("graph", oldGraph);
70 protected String getExpressionText(ReadGraph graph)
71 throws DatabaseException {
72 Layer0 L0 = Layer0.getInstance(graph);
73 return graph.getRelatedValue(literal, L0.SCLValue_expression, Bindings.STRING);
76 protected Resource getIndexRoot(ReadGraph graph) throws DatabaseException {
77 return graph.syncRequest(new IndexRoot(literal));
81 protected Type getExpectedType(ReadGraph graph, CompilationContext context)
82 throws DatabaseException {
83 Layer0 L0 = Layer0.getInstance(graph);
84 String valueType = graph.getPossibleRelatedValue(relation, L0.RequiresValueType, Bindings.STRING);
85 if(valueType != null) {
87 return Environments.getType(context.runtimeEnvironment.getEnvironment(), valueType);
88 } catch (SCLExpressionCompilationException e) {
92 return super.getExpectedType(graph, context);
96 protected CompilationContext getCompilationContext(ReadGraph graph)
97 throws DatabaseException {
98 Resource indexRoot = getIndexRoot(graph);
99 RuntimeEnvironment runtimeEnvironment = graph.syncRequest(new RuntimeEnvironmentRequest(indexRoot));
100 return new CompilationContext(runtimeEnvironment);
104 protected Type getContextVariableType() {
109 protected Expression getVariableAccessExpression(
111 CompilationContext context,
112 org.simantics.scl.compiler.elaboration.expressions.Variable contextVariable,
113 String name) throws DatabaseException {
114 if(name.equals("self"))
115 return new EVariable(contextVariable);