]> gerrit.simantics Code Review - simantics/platform.git/blobdiff - bundles/org.simantics.structural2/src/org/simantics/structural2/scl/CompileStructuralValueRequest.java
Migrated source code from Simantics SVN
[simantics/platform.git] / bundles / org.simantics.structural2 / src / org / simantics / structural2 / scl / CompileStructuralValueRequest.java
diff --git a/bundles/org.simantics.structural2/src/org/simantics/structural2/scl/CompileStructuralValueRequest.java b/bundles/org.simantics.structural2/src/org/simantics/structural2/scl/CompileStructuralValueRequest.java
new file mode 100644 (file)
index 0000000..8f68fa9
--- /dev/null
@@ -0,0 +1,105 @@
+package org.simantics.structural2.scl;\r
+\r
+import org.simantics.databoard.Bindings;\r
+import org.simantics.db.ReadGraph;\r
+import org.simantics.db.Resource;\r
+import org.simantics.db.common.procedure.adapter.TransientCacheListener;\r
+import org.simantics.db.common.request.IndexRoot;\r
+import org.simantics.db.exception.DatabaseException;\r
+import org.simantics.db.layer0.variable.Variable;\r
+import org.simantics.layer0.Layer0;\r
+import org.simantics.scl.runtime.SCLContext;\r
+import org.simantics.scl.runtime.function.Function1;\r
+\r
+/**\r
+ * Compiles an SCL expression that is attached to a literal\r
+ * whose parent is a component that is a part of a component type.\r
+ * \r
+ * @author Hannu Niemistö\r
+ */\r
+public class CompileStructuralValueRequest extends AbstractCompileStructuralValueRequest {\r
+    \r
+    protected final Resource component;\r
+    protected final Resource literal;\r
+    \r
+    public CompileStructuralValueRequest(Resource component, Resource literal, Resource relation) {\r
+       super(relation);\r
+        this.component = component;\r
+        this.literal = literal;\r
+    }\r
+    \r
+    public CompileStructuralValueRequest(ReadGraph graph, Variable context) throws DatabaseException {\r
+        this(context.getParent(graph).getRepresents(graph),\r
+                context.getRepresents(graph),\r
+                context.getPredicateResource(graph));\r
+    }\r
+    \r
+    public static Object compileAndEvaluate(ReadGraph graph, Variable context) throws DatabaseException {\r
+        SCLContext sclContext = SCLContext.getCurrent();\r
+        Object oldGraph = sclContext.get("graph");\r
+        try {\r
+            Function1<Variable,Object> exp = graph.syncRequest(new CompileStructuralValueRequest(graph, context),\r
+                    TransientCacheListener.<Function1<Variable,Object>>instance());\r
+            sclContext.put("graph", graph);\r
+            return exp.apply(context);\r
+        } catch (DatabaseException e) {\r
+            throw (DatabaseException)e;\r
+        } catch (Throwable t) {\r
+            throw new DatabaseException(t);\r
+        } finally {\r
+            sclContext.put("graph", oldGraph);\r
+        }\r
+    }\r
+    \r
+    @Override\r
+    protected String getExpressionText(ReadGraph graph)\r
+            throws DatabaseException {\r
+        Layer0 L0 = Layer0.getInstance(graph);\r
+        return graph.getRelatedValue(literal, L0.SCLValue_expression, Bindings.STRING);\r
+    }\r
+\r
+    @Override\r
+    protected Resource getIndexRoot(ReadGraph graph) throws DatabaseException {\r
+       return graph.syncRequest(new IndexRoot(literal));\r
+    }\r
+\r
+    @Override\r
+    protected Resource getComponentType(ReadGraph graph)\r
+               throws DatabaseException {\r
+       // This is possible e.g. for interface expressions inside procedurals\r
+       if(component == null) return null;\r
+       return graph.syncRequest(new FindPossibleComponentTypeRequest(component));\r
+    }\r
+\r
+       @Override\r
+       public int hashCode() {\r
+               final int prime = 31;\r
+               int result = 1;\r
+               result = prime * result + ((component == null) ? 0 : component.hashCode());\r
+               result = prime * result + ((literal == null) ? 0 : literal.hashCode());\r
+               return result;\r
+       }\r
+\r
+       @Override\r
+       public boolean equals(Object obj) {\r
+               if (this == obj)\r
+                       return true;\r
+               if (obj == null)\r
+                       return false;\r
+               if (getClass() != obj.getClass())\r
+                       return false;\r
+               CompileStructuralValueRequest other = (CompileStructuralValueRequest) obj;\r
+               if (component == null) {\r
+                       if (other.component != null)\r
+                               return false;\r
+               } else if (!component.equals(other.component))\r
+                       return false;\r
+               if (literal == null) {\r
+                       if (other.literal != null)\r
+                               return false;\r
+               } else if (!literal.equals(other.literal))\r
+                       return false;\r
+               return true;\r
+       }\r
+       \r
+}\r