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, Object> {
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<Object,Object> exp = graph.syncRequest(new CompileResourceValueRequest(literal),
46 TransientCacheListener.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);
58 public static Function1<Object,Object> compile(ReadGraph graph, Resource literal) throws DatabaseException {
59 return graph.syncRequest(new CompileResourceValueRequest(literal), TransientCacheListener.instance());
63 protected String getExpressionText(ReadGraph graph)
64 throws DatabaseException {
65 Layer0 L0 = Layer0.getInstance(graph);
66 return graph.getRelatedValue(literal, L0.SCLValue_expression, Bindings.STRING);
69 protected Resource getIndexRoot(ReadGraph graph) throws DatabaseException {
70 return graph.syncRequest(new IndexRoot(literal));
74 protected Type getExpectedType(ReadGraph graph, CompilationContext context)
75 throws DatabaseException {
76 Layer0 L0 = Layer0.getInstance(graph);
77 String valueType = graph.getPossibleRelatedValue(literal, L0.HasValueType, Bindings.STRING);
78 if(valueType != null) {
80 return Environments.getType(context.runtimeEnvironment.getEnvironment(), valueType);
81 } catch (SCLExpressionCompilationException e) {
85 return super.getExpectedType(graph, context);
89 protected CompilationContext getCompilationContext(ReadGraph graph)
90 throws DatabaseException {
91 Resource indexRoot = getIndexRoot(graph);
92 RuntimeEnvironment runtimeEnvironment = graph.syncRequest(new RuntimeEnvironmentRequest(indexRoot));
93 return new CompilationContext(runtimeEnvironment);
97 protected Type getContextVariableType() {
102 protected Expression getVariableAccessExpression(
104 CompilationContext context,
105 org.simantics.scl.compiler.elaboration.expressions.Variable contextVariable,
106 String name) throws DatabaseException {
107 if(name.equals("self"))
108 return new EVariable(contextVariable);
114 public int hashCode() {
115 final int prime = 31;
117 result = prime * result + ((literal == null) ? 0 : literal.hashCode());
122 public boolean equals(Object obj) {
127 if (getClass() != obj.getClass())
129 CompileResourceValueRequest other = (CompileResourceValueRequest) obj;
130 if (literal == null) {
131 if (other.literal != null)
133 } else if (!literal.equals(other.literal))