]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.structural2/src/org/simantics/structural2/scl/CompileStructuralValueRequest.java
8f68fa98ffbcc13891c91b6fa8f78a31d12e01dc
[simantics/platform.git] / bundles / org.simantics.structural2 / src / org / simantics / structural2 / scl / CompileStructuralValueRequest.java
1 package org.simantics.structural2.scl;\r
2 \r
3 import org.simantics.databoard.Bindings;\r
4 import org.simantics.db.ReadGraph;\r
5 import org.simantics.db.Resource;\r
6 import org.simantics.db.common.procedure.adapter.TransientCacheListener;\r
7 import org.simantics.db.common.request.IndexRoot;\r
8 import org.simantics.db.exception.DatabaseException;\r
9 import org.simantics.db.layer0.variable.Variable;\r
10 import org.simantics.layer0.Layer0;\r
11 import org.simantics.scl.runtime.SCLContext;\r
12 import org.simantics.scl.runtime.function.Function1;\r
13 \r
14 /**\r
15  * Compiles an SCL expression that is attached to a literal\r
16  * whose parent is a component that is a part of a component type.\r
17  * \r
18  * @author Hannu Niemistö\r
19  */\r
20 public class CompileStructuralValueRequest extends AbstractCompileStructuralValueRequest {\r
21     \r
22     protected final Resource component;\r
23     protected final Resource literal;\r
24     \r
25     public CompileStructuralValueRequest(Resource component, Resource literal, Resource relation) {\r
26         super(relation);\r
27         this.component = component;\r
28         this.literal = literal;\r
29     }\r
30     \r
31     public CompileStructuralValueRequest(ReadGraph graph, Variable context) throws DatabaseException {\r
32         this(context.getParent(graph).getRepresents(graph),\r
33                 context.getRepresents(graph),\r
34                 context.getPredicateResource(graph));\r
35     }\r
36     \r
37     public static Object compileAndEvaluate(ReadGraph graph, Variable context) throws DatabaseException {\r
38         SCLContext sclContext = SCLContext.getCurrent();\r
39         Object oldGraph = sclContext.get("graph");\r
40         try {\r
41             Function1<Variable,Object> exp = graph.syncRequest(new CompileStructuralValueRequest(graph, context),\r
42                     TransientCacheListener.<Function1<Variable,Object>>instance());\r
43             sclContext.put("graph", graph);\r
44             return exp.apply(context);\r
45         } catch (DatabaseException e) {\r
46             throw (DatabaseException)e;\r
47         } catch (Throwable t) {\r
48             throw new DatabaseException(t);\r
49         } finally {\r
50             sclContext.put("graph", oldGraph);\r
51         }\r
52     }\r
53     \r
54     @Override\r
55     protected String getExpressionText(ReadGraph graph)\r
56             throws DatabaseException {\r
57         Layer0 L0 = Layer0.getInstance(graph);\r
58         return graph.getRelatedValue(literal, L0.SCLValue_expression, Bindings.STRING);\r
59     }\r
60 \r
61     @Override\r
62     protected Resource getIndexRoot(ReadGraph graph) throws DatabaseException {\r
63         return graph.syncRequest(new IndexRoot(literal));\r
64     }\r
65 \r
66     @Override\r
67     protected Resource getComponentType(ReadGraph graph)\r
68                 throws DatabaseException {\r
69         // This is possible e.g. for interface expressions inside procedurals\r
70         if(component == null) return null;\r
71         return graph.syncRequest(new FindPossibleComponentTypeRequest(component));\r
72     }\r
73 \r
74         @Override\r
75         public int hashCode() {\r
76                 final int prime = 31;\r
77                 int result = 1;\r
78                 result = prime * result + ((component == null) ? 0 : component.hashCode());\r
79                 result = prime * result + ((literal == null) ? 0 : literal.hashCode());\r
80                 return result;\r
81         }\r
82 \r
83         @Override\r
84         public boolean equals(Object obj) {\r
85                 if (this == obj)\r
86                         return true;\r
87                 if (obj == null)\r
88                         return false;\r
89                 if (getClass() != obj.getClass())\r
90                         return false;\r
91                 CompileStructuralValueRequest other = (CompileStructuralValueRequest) obj;\r
92                 if (component == null) {\r
93                         if (other.component != null)\r
94                                 return false;\r
95                 } else if (!component.equals(other.component))\r
96                         return false;\r
97                 if (literal == null) {\r
98                         if (other.literal != null)\r
99                                 return false;\r
100                 } else if (!literal.equals(other.literal))\r
101                         return false;\r
102                 return true;\r
103         }\r
104         \r
105 }\r