]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.db.layer0/src/org/simantics/db/layer0/scl/CompileValueRequest.java
Support tg discovery in export/import
[simantics/platform.git] / bundles / org.simantics.db.layer0 / src / org / simantics / db / layer0 / scl / CompileValueRequest.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.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;
21
22 /**
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.
25  * 
26  * @author Tuukka Lehtonen
27  */
28 public class CompileValueRequest extends AbstractExpressionCompilationRequest<CompilationContext, Variable> {
29
30     public static class CompilationContext extends AbstractExpressionCompilationContext {
31         public CompilationContext(RuntimeEnvironment runtimeEnvironment) {
32             super(runtimeEnvironment);
33         }
34     }
35
36     protected final Resource relation;
37     protected final Resource component;
38     protected final Resource literal;
39
40     public CompileValueRequest(Resource component, Resource literal, Resource relation) {
41         this.relation = relation;
42         this.component = component;
43         this.literal = literal;
44     }
45
46     public CompileValueRequest(ReadGraph graph, Variable context) throws DatabaseException {
47         this(context.getParent(graph).getRepresents(graph),
48                 context.getRepresents(graph),
49                 context.getPredicateResource(graph));
50     }
51
52     public static Object compileAndEvaluate(ReadGraph graph, Variable context) throws DatabaseException {
53         SCLContext sclContext = SCLContext.getCurrent();
54         Object oldGraph = sclContext.get("graph");
55         try {
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);
64         } finally {
65             sclContext.put("graph", oldGraph);
66         }
67     }
68
69     @Override
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);
74     }
75
76     protected Resource getIndexRoot(ReadGraph graph) throws DatabaseException {
77         return graph.syncRequest(new IndexRoot(literal));
78     }
79
80     @Override
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) {
86             try {
87                 return Environments.getType(context.runtimeEnvironment.getEnvironment(), valueType);
88             } catch (SCLExpressionCompilationException e) {
89                 e.printStackTrace();
90             }
91         }
92         return super.getExpectedType(graph, context);
93     }
94
95     @Override
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);
101     }
102
103     @Override
104     protected Type getContextVariableType() {
105         return VARIABLE; 
106     }
107
108     @Override
109     protected Expression getVariableAccessExpression(
110             ReadGraph graph,
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);
116         else
117             return null;
118     }
119
120 }