]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.modeling/src/org/simantics/modeling/scl/CompileSCLQueryRequest.java
8fed128d68f4683c7b172d0b5779c3276d214fec
[simantics/platform.git] / bundles / org.simantics.modeling / src / org / simantics / modeling / scl / CompileSCLQueryRequest.java
1 package org.simantics.modeling.scl;
2
3 import java.util.Map;
4
5 import org.simantics.db.ReadGraph;
6 import org.simantics.db.Resource;
7 import org.simantics.db.common.procedure.adapter.TransientCacheListener;
8 import org.simantics.db.common.request.IndexRoot;
9 import org.simantics.db.common.request.ResourceRead2;
10 import org.simantics.db.exception.DatabaseException;
11 import org.simantics.db.layer0.util.RuntimeEnvironmentRequest;
12 import org.simantics.db.layer0.variable.Variable;
13 import org.simantics.layer0.Layer0;
14 import org.simantics.scl.compiler.common.names.Name;
15 import org.simantics.scl.compiler.elaboration.expressions.EApply;
16 import org.simantics.scl.compiler.elaboration.expressions.EConstant;
17 import org.simantics.scl.compiler.elaboration.expressions.EVariable;
18 import org.simantics.scl.compiler.elaboration.expressions.Expression;
19 import org.simantics.scl.compiler.elaboration.modules.SCLValue;
20 import org.simantics.scl.compiler.environment.Environment;
21 import org.simantics.scl.compiler.runtime.RuntimeEnvironment;
22 import org.simantics.scl.compiler.types.Type;
23 import org.simantics.scl.compiler.types.Types;
24 import org.simantics.scl.runtime.SCLContext;
25 import org.simantics.scl.runtime.function.Function1;
26 import org.simantics.structural2.scl.CompileStructuralValueRequest;
27 import org.simantics.structural2.scl.ComponentTypeProperty;
28 import org.simantics.structural2.scl.ReadComponentTypeInterfaceRequest;
29
30 public class CompileSCLQueryRequest extends CompileStructuralValueRequest {
31
32     public CompileSCLQueryRequest(ReadGraph graph, Variable context)
33             throws DatabaseException {
34         super(graph, context);
35     }
36
37     public static Object compileAndEvaluate(ReadGraph graph, Variable context) throws DatabaseException {
38         SCLContext sclContext = SCLContext.getCurrent();
39         Object oldGraph = sclContext.get("graph");
40         try {
41             Function1<Variable,Object> exp = graph.syncRequest(new CompileSCLQueryRequest(graph, context),
42                     TransientCacheListener.<Function1<Variable,Object>>instance());
43             sclContext.put("graph", graph);
44             return exp.apply(context);
45         } catch (DatabaseException e) {
46             throw (DatabaseException)e;
47         } catch (Throwable t) {
48             throw new DatabaseException(t);
49         } finally {
50             sclContext.put("graph", oldGraph);
51         }
52     }
53
54     protected static Name INPUT_VARIABLE = Name.create("Simantics/Query", "inputVariable");
55     protected static Name SESSION_VARIABLE = Name.create("Simantics/Query", "sessionVariable");
56     public static final Type VARIABLE = Types.con("Simantics/Variables", "Variable"); 
57
58     protected static Expression accessQueryVariable(Environment environment,
59             org.simantics.scl.compiler.elaboration.expressions.Variable contextVariable) {
60         SCLValue variableParentFunction = environment.getValue(VARIABLE_PARENT);
61         return new EApply(new EConstant(variableParentFunction), new EVariable(contextVariable));
62     }
63
64     @Override
65     protected Expression getVariableAccessExpression(
66             ReadGraph graph,
67             CompilationContext context,
68             org.simantics.scl.compiler.elaboration.expressions.Variable contextVariable,
69             String name) throws DatabaseException {
70         ComponentTypeProperty property = context.propertyMap.get(name);
71         if(property != null) {
72             Environment environment = context.runtimeEnvironment.getEnvironment();
73             return getPropertyFlexible(environment,
74                         accessQueryVariable(environment, contextVariable),
75                     name,
76                     property.type);
77         }
78         else if(name.equals("input")) {
79             Environment environment = context.runtimeEnvironment.getEnvironment();
80             return new EApply(
81                             new EConstant(environment.getValue(INPUT_VARIABLE)),
82                             new EVariable(contextVariable));
83         }
84         else if(name.equals("session")) {
85             Environment environment = context.runtimeEnvironment.getEnvironment();
86             return new EApply(
87                             new EConstant(environment.getValue(SESSION_VARIABLE)),
88                             new EVariable(contextVariable));
89         }
90         else if(name.equals("self"))
91             return new EVariable(contextVariable);
92         else
93             return null;
94     }
95     
96     @Override
97     protected CompilationContext getCompilationContext(ReadGraph graph) throws DatabaseException {
98         Resource indexRoot = graph.syncRequest(new IndexRoot(literal));
99         return graph.syncRequest(new ResourceRead2<CompilationContext>(component, indexRoot) {
100             @Override
101             public CompilationContext perform(ReadGraph graph) throws DatabaseException {
102                 Layer0 L0 = Layer0.getInstance(graph);
103                 Resource type = graph.getPossibleType(component, L0.Entity);
104                 RuntimeEnvironment runtimeEnvironment = graph.syncRequest(new RuntimeEnvironmentRequest(resource2));
105                 Map<String, ComponentTypeProperty> propertyMap =
106                         graph.syncRequest(new ReadComponentTypeInterfaceRequest(type, runtimeEnvironment.getEnvironment()),
107                                 TransientCacheListener.<Map<String, ComponentTypeProperty>>instance());
108                 return new CompilationContext(runtimeEnvironment, propertyMap);
109             }
110         });
111     }
112     
113 }