]> gerrit.simantics Code Review - simantics/platform.git/blobdiff - bundles/org.simantics.structural2/src/org/simantics/structural2/scl/CompileStructuralValueRequest.java
Simantics Console
[simantics/platform.git] / bundles / org.simantics.structural2 / src / org / simantics / structural2 / scl / CompileStructuralValueRequest.java
index 801c0c5f3785d7c9e12b7e37197e34f7ea07f004..ce824090dce3f686af4a37d77096218f4e9e6f8b 100644 (file)
-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
-        CompileStructuralValueRequest request = new CompileStructuralValueRequest(graph, context);\r
-        try {\r
-            Function1<Variable,Object> exp = graph.syncRequest(request, TransientCacheListener.<Function1<Variable,Object>>instance());\r
-            sclContext.put("graph", graph);\r
-            return exp.apply(context);\r
-        } catch (Throwable t) {\r
-            throw new DatabaseException("Compiling structural value request for component=" + request.component + ", literal=" + request.literal + " and relation " + request.relation + " failed!", 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
+package org.simantics.structural2.scl;
+
+import org.simantics.databoard.Bindings;
+import org.simantics.db.ReadGraph;
+import org.simantics.db.Resource;
+import org.simantics.db.common.procedure.adapter.TransientCacheListener;
+import org.simantics.db.common.request.IndexRoot;
+import org.simantics.db.common.utils.NameUtils;
+import org.simantics.db.exception.DatabaseException;
+import org.simantics.db.layer0.variable.Variable;
+import org.simantics.layer0.Layer0;
+import org.simantics.scl.runtime.SCLContext;
+import org.simantics.scl.runtime.function.Function1;
+
+/**
+ * Compiles an SCL expression that is attached to a literal
+ * whose parent is a component that is a part of a component type.
+ * 
+ * @author Hannu Niemist&ouml;
+ */
+public class CompileStructuralValueRequest extends AbstractCompileStructuralValueRequest {
+    
+    protected final Resource component;
+    protected final Resource literal;
+    
+    public CompileStructuralValueRequest(Resource component, Resource literal, Resource relation) {
+       super(relation);
+        this.component = component;
+        this.literal = literal;
+    }
+    
+    public CompileStructuralValueRequest(ReadGraph graph, Variable context) throws DatabaseException {
+        this(context.getParent(graph).getRepresents(graph),
+                context.getRepresents(graph),
+                context.getPredicateResource(graph));
+    }
+    
+    public static Object compileAndEvaluate(ReadGraph graph, Variable context) throws DatabaseException {
+        SCLContext sclContext = SCLContext.getCurrent();
+        Object oldGraph = sclContext.get("graph");
+        CompileStructuralValueRequest request = new CompileStructuralValueRequest(graph, context);
+        try {
+            Function1<Object,Object> exp = graph.syncRequest(request, TransientCacheListener.instance());
+            sclContext.put("graph", graph);
+            return exp.apply(context);
+        } catch (Throwable t) {
+            String componentName = NameUtils.getSafeName(graph, request.component);
+            String literalName = NameUtils.getSafeName(graph, request.literal);
+            String relationName = NameUtils.getSafeName(graph, request.relation);
+            StringBuilder sb = new StringBuilder("Compiling structural value request for component ")
+                    .append(componentName).append(" ").append(request.component).append(" , literal ")
+                    .append(literalName).append(" ").append(request.literal).append(" and relation ")
+                    .append(relationName).append(" ").append(request.relation).append(" failed!");
+            throw new DatabaseException(sb.toString(), t);
+        } finally {
+            sclContext.put("graph", oldGraph);
+        }
+    }
+    
+    public static Function1<Object, Object> compile(ReadGraph graph, Resource s, Resource o, Resource p) throws DatabaseException {
+        return graph.syncRequest(new CompileStructuralValueRequest(s, o, p), TransientCacheListener.instance());
+    }
+    
+    @Override
+    protected String getExpressionText(ReadGraph graph)
+            throws DatabaseException {
+        Layer0 L0 = Layer0.getInstance(graph);
+        return graph.getRelatedValue(literal, L0.SCLValue_expression, Bindings.STRING);
+    }
+
+    @Override
+    protected Resource getIndexRoot(ReadGraph graph) throws DatabaseException {
+       return graph.syncRequest(new IndexRoot(literal));
+    }
+
+    @Override
+    protected Resource getComponentType(ReadGraph graph)
+               throws DatabaseException {
+       // This is possible e.g. for interface expressions inside procedurals
+       if(component == null) return null;
+       return graph.syncRequest(new FindPossibleComponentTypeRequest(component));
+    }
+
+       @Override
+       public int hashCode() {
+               final int prime = 31;
+               int result = 1;
+               result = prime * result + ((relation == null) ? 0 : relation.hashCode());
+               result = prime * result + ((component == null) ? 0 : component.hashCode());
+               result = prime * result + ((literal == null) ? 0 : literal.hashCode());
+               return result;
+       }
+
+       @Override
+       public boolean equals(Object obj) {
+               if (this == obj)
+                       return true;
+               if (obj == null)
+                       return false;
+               if (getClass() != obj.getClass())
+                       return false;
+               CompileStructuralValueRequest other = (CompileStructuralValueRequest) obj;
+               if (relation == null) {
+                       if (other.relation != null)
+                               return false;
+               } else if (!relation.equals(other.relation))
+                       return false;
+               if (component == null) {
+                       if (other.component != null)
+                               return false;
+               } else if (!component.equals(other.component))
+                       return false;
+               if (literal == null) {
+                       if (other.literal != null)
+                               return false;
+               } else if (!literal.equals(other.literal))
+                       return false;
+               return true;
+       }
+       
+       @Override
+       protected String getContextDescription(ReadGraph graph) throws DatabaseException {
+           if(component != null) {
+               String uri = graph.getPossibleURI(component);
+               if(uri != null) {
+                   String propertyName = NameUtils.getSafeName(graph, relation);
+                   return uri + "#" + propertyName;
+               }
+           }
+           return super.getContextDescription(graph);
+       }
+       
+}