]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.structural2/src/org/simantics/structural2/scl/CompileProceduralExpressionValueRequest.java
Merge "Variable optimizations for documents (Simupedia)"
[simantics/platform.git] / bundles / org.simantics.structural2 / src / org / simantics / structural2 / scl / CompileProceduralExpressionValueRequest.java
1 package org.simantics.structural2.scl;
2
3 import org.simantics.db.ReadGraph;
4 import org.simantics.db.Resource;
5 import org.simantics.db.common.procedure.adapter.TransientCacheListener;
6 import org.simantics.db.exception.DatabaseException;
7 import org.simantics.db.layer0.variable.Variable;
8 import org.simantics.scl.runtime.SCLContext;
9 import org.simantics.scl.runtime.function.Function1;
10
11 public class CompileProceduralExpressionValueRequest extends AbstractCompileStructuralValueRequest {
12     
13     protected final String expression;
14     protected final Resource componentType;
15     protected final Resource indexRoot;
16     
17     public CompileProceduralExpressionValueRequest(String expression, Resource componentType, Resource relation, Resource indexRoot) {
18         super(relation);
19         this.expression = expression;
20         this.componentType = componentType;
21         this.indexRoot = indexRoot;
22     }
23     
24     public CompileProceduralExpressionValueRequest(ReadGraph graph, String expression, Variable context) throws DatabaseException {
25         this(expression, context.getParent(graph).getType(graph),
26                 context.getPredicateResource(graph),
27                 context.getIndexRoot(graph));
28     }
29     
30     public static Object compileAndEvaluate(ReadGraph graph, String expression, Variable context) throws DatabaseException {
31         SCLContext sclContext = SCLContext.getCurrent();
32         Object oldGraph = sclContext.get("graph");
33         try {
34             Function1<Object,Object> exp = graph.syncRequest(new CompileProceduralExpressionValueRequest(graph, expression, context),
35                     TransientCacheListener.instance());
36             sclContext.put("graph", graph);
37             return exp.apply(context);
38         } catch (DatabaseException e) {
39             throw (DatabaseException)e;
40         } catch (Throwable t) {
41             throw new DatabaseException(t);
42         } finally {
43             sclContext.put("graph", oldGraph);
44         }
45     }
46     
47     @Override
48     protected String getExpressionText(ReadGraph graph) throws DatabaseException {
49         return expression;
50     }
51
52     @Override
53     protected Resource getIndexRoot(ReadGraph graph) throws DatabaseException {
54         return indexRoot;
55     }
56
57     @Override
58     protected Resource getComponentType(ReadGraph graph) throws DatabaseException {
59         return componentType;
60     }
61
62 }