]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.db.layer0/src/org/simantics/db/layer0/scl/CompileResourceValueRequest.java
Documented difference of RuntimeEnvironmentRequest and Runti...quest2
[simantics/platform.git] / bundles / org.simantics.db.layer0 / src / org / simantics / db / layer0 / scl / CompileResourceValueRequest.java
1 package org.simantics.db.layer0.scl;
2
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;
20
21 /**
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.
24  * 
25  * @author Antti Villberg
26  */
27 public class CompileResourceValueRequest extends AbstractExpressionCompilationRequest<CompilationContext, Resource> {
28
29     public static class CompilationContext extends AbstractExpressionCompilationContext {
30         public CompilationContext(RuntimeEnvironment runtimeEnvironment) {
31             super(runtimeEnvironment);
32         }
33     }
34
35     protected final Resource literal;
36
37     public CompileResourceValueRequest(Resource literal) {
38         this.literal = literal;
39     }
40
41     public static Object compileAndEvaluate(ReadGraph graph, Resource literal) throws DatabaseException {
42         SCLContext sclContext = SCLContext.getCurrent();
43         Object oldGraph = sclContext.get("graph");
44         try {
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);
53         } finally {
54             sclContext.put("graph", oldGraph);
55         }
56     }
57
58     @Override
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);
63     }
64
65     protected Resource getIndexRoot(ReadGraph graph) throws DatabaseException {
66         return graph.syncRequest(new IndexRoot(literal));
67     }
68
69     @Override
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) {
75             try {
76                 return Environments.getType(context.runtimeEnvironment.getEnvironment(), valueType);
77             } catch (SCLExpressionCompilationException e) {
78                 e.printStackTrace();
79             }
80         }
81         return super.getExpectedType(graph, context);
82     }
83
84     @Override
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);
90     }
91
92     @Override
93     protected Type getContextVariableType() {
94         return RESOURCE; 
95     }
96
97     @Override
98     protected Expression getVariableAccessExpression(
99             ReadGraph graph,
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);
105         else
106             return null;
107     }
108
109 }