]> gerrit.simantics Code Review - simantics/platform.git/blobdiff - bundles/org.simantics.db.common/src/org/simantics/db/common/TransferableGraphRequests.java
Migrated source code from Simantics SVN
[simantics/platform.git] / bundles / org.simantics.db.common / src / org / simantics / db / common / TransferableGraphRequests.java
diff --git a/bundles/org.simantics.db.common/src/org/simantics/db/common/TransferableGraphRequests.java b/bundles/org.simantics.db.common/src/org/simantics/db/common/TransferableGraphRequests.java
new file mode 100644 (file)
index 0000000..8f776b7
--- /dev/null
@@ -0,0 +1,171 @@
+package org.simantics.db.common;\r
+\r
+import java.util.HashSet;\r
+import java.util.Set;\r
+\r
+import org.simantics.databoard.adapter.AdaptException;\r
+import org.simantics.databoard.binding.Binding;\r
+import org.simantics.db.ReadGraph;\r
+import org.simantics.db.Resource;\r
+import org.simantics.db.exception.DatabaseException;\r
+import org.simantics.graph.representation.TransferableGraph1;\r
+import org.simantics.graph.representation.TransferableGraphUtils;\r
+import org.simantics.graph.representation.Value;\r
+import org.simantics.layer0.Layer0;\r
+\r
+public class TransferableGraphRequests {\r
+       \r
+       private TransferableGraph1 tg;\r
+       private ReadGraph graph;\r
+       \r
+       public TransferableGraphRequests(TransferableGraph1 tg, ReadGraph graph) {\r
+               this.tg = tg;\r
+               this.graph = graph;\r
+       }\r
+\r
+       public Resource getExternal(int id) throws DatabaseException {\r
+               String uri = TransferableGraphUtils.getURI(tg, id);\r
+               return graph.getPossibleResource(uri);\r
+       }\r
+       \r
+       public Set<Integer> getObjects(int subject, Resource relation) throws DatabaseException {\r
+\r
+               Set<Integer> result = new HashSet<Integer>();\r
+               for(int i=0;i<tg.statements.length;i+=4) {\r
+                       if(tg.statements[i] == subject) {\r
+                               Resource predicate = getExternal(tg.statements[i+1]);\r
+                               if(predicate != null)\r
+                                       if(graph.isSubrelationOf(predicate, relation)) result.add(tg.statements[i+3]); \r
+                       }\r
+               }\r
+               return result;\r
+               \r
+       }\r
+\r
+       public Set<Integer> getSubjects(int object, Resource inverseRelation) throws DatabaseException {\r
+\r
+               Set<Integer> result = new HashSet<Integer>();\r
+               for(int i=0;i<tg.statements.length;i+=4) {\r
+                       if(tg.statements[i+3] == object) {\r
+                               Resource predicate = getExternal(tg.statements[i+2]);\r
+                               if(predicate != null)\r
+                                       if(graph.isSubrelationOf(predicate, inverseRelation)) result.add(tg.statements[i]); \r
+                       }\r
+               }\r
+               return result;\r
+               \r
+       }\r
+\r
+       public int getPossibleObject(int subject, Resource relation) throws DatabaseException {\r
+               Set<Integer> objects = getObjects(subject, relation);\r
+               if(objects.size() == 1) return objects.iterator().next();\r
+               return -1;\r
+       }\r
+\r
+       public int getSingleObject(int subject, Resource relation) throws DatabaseException {\r
+\r
+               Set<Integer> objects = getObjects(subject, relation);\r
+               if(objects.size() == 1) return objects.iterator().next();\r
+               else if (objects.size() == 0) throw new DatabaseException("No objects for subject " + subject + " and relation " + graph.getURI(relation));\r
+               else throw new DatabaseException("Multiple (" + objects.size() + ") objects for subject " + subject + " and relation " + graph.getURI(relation));\r
+               \r
+       }\r
+\r
+       public int getPossibleSubject(int object, Resource inverseRelation) throws DatabaseException {\r
+               Set<Integer> subjects = getSubjects(object, inverseRelation);\r
+               if(subjects.size() == 1) return subjects.iterator().next();\r
+               return -1;\r
+       }\r
+\r
+       public int getSingleSubject(int object, Resource inverseRelation) throws DatabaseException {\r
+\r
+               Set<Integer> subjects = getObjects(object, inverseRelation);\r
+               if(subjects.size() == 1) return subjects.iterator().next();\r
+               else if (subjects.size() == 0) throw new DatabaseException("No subjects for object " + object + " and relation " + graph.getURI(inverseRelation));\r
+               else throw new DatabaseException("Multiple (" + subjects.size() + ") subjects for object " + object + " and relation " + graph.getURI(inverseRelation));\r
+               \r
+       }\r
+\r
+       public <T> T getRelatedValue(int subject, Resource relation, Binding binding) throws DatabaseException {\r
+               \r
+               int object = getSingleObject(subject, relation);\r
+               return getValue(object, binding);\r
+               \r
+       }\r
+\r
+       public <T> T getPossibleRelatedValue(int subject, Resource relation, Binding binding) throws DatabaseException {\r
+               int object = getPossibleObject(subject, relation);\r
+               if (object == -1)\r
+                       return null;\r
+               return getPossibleValue(object, binding);\r
+       }\r
+\r
+       @SuppressWarnings("unchecked")\r
+       public <T> T getValue(int subject, Binding binding) throws DatabaseException {\r
+\r
+               Value value = TransferableGraphUtils.findValue(tg, subject);\r
+               if(value == null) throw new DatabaseException("No value for subject " + subject);\r
+               try {\r
+                       return (T)value.value.getValue(binding);\r
+               } catch (AdaptException e) {\r
+                       throw new DatabaseException(e);\r
+               }\r
+               \r
+       }\r
+\r
+       @SuppressWarnings("unchecked")\r
+       public <T> T getPossibleValue(int subject, Binding binding) {\r
+               Value value = TransferableGraphUtils.findValue(tg, subject);\r
+               if(value == null) return null;\r
+               try {\r
+                       return (T)value.value.getValue(binding);\r
+               } catch (AdaptException e) {\r
+                       return null;\r
+               }\r
+       }\r
+\r
+       public boolean isInstanceOf(int subject, Resource type) throws DatabaseException {\r
+\r
+               Layer0 L0 = Layer0.getInstance(graph);\r
+               for(int t : getObjects(subject, L0.InstanceOf)) {\r
+                       Resource tr = getExternal(t);\r
+                       if(tr != null)\r
+                               if(graph.isInheritedFrom(tr, type)) return true;\r
+               }\r
+               \r
+               return false;\r
+               \r
+       }\r
+       \r
+\r
+       public String formatResource(int r) throws DatabaseException {\r
+               \r
+               String result = "" + r;\r
+               String uri = TransferableGraphUtils.getURI(tg, r);\r
+               if(!uri.isEmpty()) result = uri;\r
+               \r
+               Value value = TransferableGraphUtils.findValue(tg, r);\r
+               if(value != null) result += " (value " + value.value + ")";\r
+               \r
+               return result;\r
+               \r
+       }\r
+       \r
+       public String listStatements() throws DatabaseException {\r
+               \r
+               StringBuilder b = new StringBuilder();\r
+       for(int i=0;i<tg.statements.length;i+=4) {\r
+               int subject = tg.statements[i];\r
+               int predicate = tg.statements[i+1];\r
+               int object = tg.statements[i+3];\r
+               b.append(formatResource(subject) + " " + formatResource(predicate) + " " + formatResource(object) + "\n");\r
+       }\r
+       return b.toString();\r
+               \r
+       }\r
+       \r
+       public ReadGraph getGraph() {\r
+               return graph;\r
+       }\r
+       \r
+}\r