1 package org.simantics.structural2.scl;
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;
11 public class CompileProceduralExpressionValueRequest extends AbstractCompileStructuralValueRequest {
13 protected final String expression;
14 protected final Resource componentType;
15 protected final Resource indexRoot;
17 public CompileProceduralExpressionValueRequest(String expression, Resource componentType, Resource relation, Resource indexRoot) {
19 this.expression = expression;
20 this.componentType = componentType;
21 this.indexRoot = indexRoot;
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));
30 public static Object compileAndEvaluate(ReadGraph graph, String expression, Variable context) throws DatabaseException {
31 SCLContext sclContext = SCLContext.getCurrent();
32 Object oldGraph = sclContext.get("graph");
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);
43 sclContext.put("graph", oldGraph);
48 protected String getExpressionText(ReadGraph graph) throws DatabaseException {
53 protected Resource getIndexRoot(ReadGraph graph) throws DatabaseException {
58 protected Resource getComponentType(ReadGraph graph) throws DatabaseException {
63 public int hashCode() {
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());
73 public boolean equals(Object obj) {
78 if (getClass() != obj.getClass())
80 CompileProceduralExpressionValueRequest other = (CompileProceduralExpressionValueRequest) obj;
81 if (componentType == null) {
82 if (other.componentType != null)
84 } else if (!componentType.equals(other.componentType))
86 if (expression == null) {
87 if (other.expression != null)
89 } else if (!expression.equals(other.expression))
91 if (indexRoot == null) {
92 if (other.indexRoot != null)
94 } else if (!indexRoot.equals(other.indexRoot))