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, Object> {
30 public static class CompilationContext extends AbstractExpressionCompilationContext {
31 public CompilationContext(RuntimeEnvironment runtimeEnvironment) {
32 super(runtimeEnvironment);
36 protected final Resource relation;
37 protected final Resource literal;
39 public CompileValueRequest(Resource literal, Resource relation) {
40 this.relation = relation;
41 this.literal = literal;
44 public CompileValueRequest(ReadGraph graph, Variable context) throws DatabaseException {
45 this(context.getRepresents(graph), context.getPredicateResource(graph));
48 public static Object compileAndEvaluate(ReadGraph graph, Variable context) throws DatabaseException {
49 SCLContext sclContext = SCLContext.getCurrent();
50 Object oldGraph = sclContext.get("graph");
52 Function1<Object,Object> exp = graph.syncRequest(new CompileValueRequest(graph, context),
53 TransientCacheListener.instance());
54 sclContext.put("graph", graph);
55 return exp.apply(context);
56 } catch (DatabaseException e) {
57 throw (DatabaseException)e;
58 } catch (Throwable t) {
59 throw new DatabaseException(t);
61 sclContext.put("graph", oldGraph);
65 public static Function1<Object,Object> compile(ReadGraph graph, Resource literal, Resource predicate) throws DatabaseException {
66 SCLContext sclContext = SCLContext.getCurrent();
67 Object oldGraph = sclContext.get("graph");
69 Function1<Object,Object> exp = graph.syncRequest(new CompileValueRequest(literal, predicate), TransientCacheListener.instance());
70 sclContext.put("graph", graph);
72 } catch (DatabaseException e) {
73 throw (DatabaseException)e;
74 } catch (Throwable t) {
75 throw new DatabaseException(t);
77 sclContext.put("graph", oldGraph);
82 protected String getExpressionText(ReadGraph graph)
83 throws DatabaseException {
84 Layer0 L0 = Layer0.getInstance(graph);
85 return graph.getRelatedValue(literal, L0.SCLValue_expression, Bindings.STRING);
88 protected Resource getIndexRoot(ReadGraph graph) throws DatabaseException {
89 return graph.syncRequest(new IndexRoot(literal));
93 protected Type getExpectedType(ReadGraph graph, CompilationContext context)
94 throws DatabaseException {
95 Layer0 L0 = Layer0.getInstance(graph);
96 String valueType = graph.getPossibleRelatedValue(relation, L0.RequiresValueType, Bindings.STRING);
97 if(valueType != null) {
99 return Environments.getType(context.runtimeEnvironment.getEnvironment(), valueType);
100 } catch (SCLExpressionCompilationException e) {
104 return super.getExpectedType(graph, context);
108 protected CompilationContext getCompilationContext(ReadGraph graph)
109 throws DatabaseException {
110 Resource indexRoot = getIndexRoot(graph);
111 RuntimeEnvironment runtimeEnvironment = graph.syncRequest(new RuntimeEnvironmentRequest(indexRoot));
112 return new CompilationContext(runtimeEnvironment);
116 protected Type getContextVariableType() {
121 protected Expression getVariableAccessExpression(
123 CompilationContext context,
124 org.simantics.scl.compiler.elaboration.expressions.Variable contextVariable,
125 String name) throws DatabaseException {
126 if(name.equals("self"))
127 return new EVariable(contextVariable);
133 public int hashCode() {
134 final int prime = 31;
136 result = prime * result + ((literal == null) ? 0 : literal.hashCode());
137 result = prime * result + ((relation == null) ? 0 : relation.hashCode());
142 public boolean equals(Object obj) {
147 if (getClass() != obj.getClass())
149 CompileValueRequest other = (CompileValueRequest) obj;
150 if (literal == null) {
151 if (other.literal != null)
153 } else if (!literal.equals(other.literal))
155 if (relation == null) {
156 if (other.relation != null)
158 } else if (!relation.equals(other.relation))