]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.structural2/src/org/simantics/structural2/scl/CompileProceduralExpressionValueRequest.java
b402520d74cbe0bb5487e2d96ed15c35c8196af6
[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     @Override
63     public int hashCode() {
64         final int prime = 31;
65         int result = 1;
66         result = prime * result + ((componentType == null) ? 0 : componentType.hashCode());
67         result = prime * result + ((expression == null) ? 0 : expression.hashCode());
68         result = prime * result + ((indexRoot == null) ? 0 : indexRoot.hashCode());
69         return result;
70     }
71
72     @Override
73     public boolean equals(Object obj) {
74         if (this == obj)
75             return true;
76         if (obj == null)
77             return false;
78         if (getClass() != obj.getClass())
79             return false;
80         CompileProceduralExpressionValueRequest other = (CompileProceduralExpressionValueRequest) obj;
81         if (componentType == null) {
82             if (other.componentType != null)
83                 return false;
84         } else if (!componentType.equals(other.componentType))
85             return false;
86         if (expression == null) {
87             if (other.expression != null)
88                 return false;
89         } else if (!expression.equals(other.expression))
90             return false;
91         if (indexRoot == null) {
92             if (other.indexRoot != null)
93                 return false;
94         } else if (!indexRoot.equals(other.indexRoot))
95             return false;
96         return true;
97     }
98
99     
100 }