]> gerrit.simantics Code Review - simantics/platform.git/blob
736c0396f4debcccbb2003dc800f905dc9dcb1d7
[simantics/platform.git] /
1 package org.simantics.spreadsheet.graph.function;
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.runtime.RuntimeEnvironment;
15 import org.simantics.scl.runtime.SCLContext;
16 import org.simantics.scl.runtime.function.Function1;
17 import org.simantics.structural2.scl.CompileStructuralValueRequest;
18 import org.simantics.structural2.scl.ComponentTypeProperty;
19 import org.simantics.structural2.scl.ReadComponentTypeInterfaceRequest;
20
21 public class CompileSCLValueRequest extends CompileStructuralValueRequest {
22
23     public CompileSCLValueRequest(ReadGraph graph, Variable context)
24             throws DatabaseException {
25         super(graph, context);
26     }
27
28     public static Object compileAndEvaluate(ReadGraph graph, Variable context) throws DatabaseException {
29         SCLContext sclContext = SCLContext.getCurrent();
30         Object oldGraph = sclContext.get("graph");
31         try {
32             Function1<Variable,Object> exp = graph.syncRequest(new CompileSCLValueRequest(graph, context),
33                     TransientCacheListener.<Function1<Variable,Object>>instance());
34             sclContext.put("graph", graph);
35             return exp.apply(context);
36         } catch (DatabaseException e) {
37             throw (DatabaseException)e;
38         } catch (Throwable t) {
39             throw new DatabaseException(t);
40         } finally {
41             sclContext.put("graph", oldGraph);
42         }
43     }
44     
45     @Override
46     protected CompilationContext getCompilationContext(ReadGraph graph) throws DatabaseException {
47         Resource indexRoot = graph.syncRequest(new IndexRoot(literal));
48         return graph.syncRequest(new ResourceRead2<CompilationContext>(component, indexRoot) {
49             @Override
50             public CompilationContext perform(ReadGraph graph) throws DatabaseException {
51                 Layer0 L0 = Layer0.getInstance(graph);
52                 Resource parent = graph.getPossibleObject(resource, L0.PartOf);
53                 Resource type = graph.getPossibleType(parent, L0.Entity);
54                 RuntimeEnvironment runtimeEnvironment = graph.syncRequest(new RuntimeEnvironmentRequest(resource2));
55                 Map<String, ComponentTypeProperty> propertyMap =
56                         graph.syncRequest(new ReadComponentTypeInterfaceRequest(type, runtimeEnvironment.getEnvironment()),
57                                 TransientCacheListener.<Map<String, ComponentTypeProperty>>instance());
58                 return new CompilationContext(runtimeEnvironment, propertyMap);
59             }
60         });
61     }
62         
63 }