]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.structural2/src/org/simantics/structural2/scl/CompileStructuralValueRequest.java
Merge "Testing SonarQube with Simantics Platform SDK"
[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         CompileStructuralValueRequest request = new CompileStructuralValueRequest(graph, context);\r
41         try {\r
42             Function1<Variable,Object> exp = graph.syncRequest(request, TransientCacheListener.<Function1<Variable,Object>>instance());\r
43             sclContext.put("graph", graph);\r
44             return exp.apply(context);\r
45         } catch (Throwable t) {\r
46             throw new DatabaseException("Compiling structural value request for component=" + request.component + ", literal=" + request.literal + " and relation " + request.relation + " failed!", t);\r
47         } finally {\r
48             sclContext.put("graph", oldGraph);\r
49         }\r
50     }\r
51     \r
52     @Override\r
53     protected String getExpressionText(ReadGraph graph)\r
54             throws DatabaseException {\r
55         Layer0 L0 = Layer0.getInstance(graph);\r
56         return graph.getRelatedValue(literal, L0.SCLValue_expression, Bindings.STRING);\r
57     }\r
58 \r
59     @Override\r
60     protected Resource getIndexRoot(ReadGraph graph) throws DatabaseException {\r
61         return graph.syncRequest(new IndexRoot(literal));\r
62     }\r
63 \r
64     @Override\r
65     protected Resource getComponentType(ReadGraph graph)\r
66                 throws DatabaseException {\r
67         // This is possible e.g. for interface expressions inside procedurals\r
68         if(component == null) return null;\r
69         return graph.syncRequest(new FindPossibleComponentTypeRequest(component));\r
70     }\r
71 \r
72         @Override\r
73         public int hashCode() {\r
74                 final int prime = 31;\r
75                 int result = 1;\r
76                 result = prime * result + ((component == null) ? 0 : component.hashCode());\r
77                 result = prime * result + ((literal == null) ? 0 : literal.hashCode());\r
78                 return result;\r
79         }\r
80 \r
81         @Override\r
82         public boolean equals(Object obj) {\r
83                 if (this == obj)\r
84                         return true;\r
85                 if (obj == null)\r
86                         return false;\r
87                 if (getClass() != obj.getClass())\r
88                         return false;\r
89                 CompileStructuralValueRequest other = (CompileStructuralValueRequest) obj;\r
90                 if (component == null) {\r
91                         if (other.component != null)\r
92                                 return false;\r
93                 } else if (!component.equals(other.component))\r
94                         return false;\r
95                 if (literal == null) {\r
96                         if (other.literal != null)\r
97                                 return false;\r
98                 } else if (!literal.equals(other.literal))\r
99                         return false;\r
100                 return true;\r
101         }\r
102         \r
103 }\r