Merge "Remove unnecessary getComparableKey from HashMapBinding"
[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 + ((relation == null) ? 0 : relation.hashCode());
67         result = prime * result + ((componentType == null) ? 0 : componentType.hashCode());
68         result = prime * result + ((expression == null) ? 0 : expression.hashCode());
69         result = prime * result + ((indexRoot == null) ? 0 : indexRoot.hashCode());
70         return result;
71     }
72
73     @Override
74     public boolean equals(Object obj) {
75         if (this == obj)
76             return true;
77         if (obj == null)
78             return false;
79         if (getClass() != obj.getClass())
80             return false;
81         CompileProceduralExpressionValueRequest other = (CompileProceduralExpressionValueRequest) obj;
82         if (relation == null) {
83             if (other.relation != null)
84                 return false;
85         } else if (!relation.equals(other.relation))
86             return false;
87         if (componentType == null) {
88             if (other.componentType != null)
89                 return false;
90         } else if (!componentType.equals(other.componentType))
91             return false;
92         if (expression == null) {
93             if (other.expression != null)
94                 return false;
95         } else if (!expression.equals(other.expression))
96             return false;
97         if (indexRoot == null) {
98             if (other.indexRoot != null)
99                 return false;
100         } else if (!indexRoot.equals(other.indexRoot))
101             return false;
102         return true;
103     }
104
105     
106 }