]> gerrit.simantics Code Review - simantics/platform.git/blobdiff - bundles/org.simantics.debug.browser/src/org/simantics/debug/browser/utils/StatementInfo.java
Migrated source code from Simantics SVN
[simantics/platform.git] / bundles / org.simantics.debug.browser / src / org / simantics / debug / browser / utils / StatementInfo.java
diff --git a/bundles/org.simantics.debug.browser/src/org/simantics/debug/browser/utils/StatementInfo.java b/bundles/org.simantics.debug.browser/src/org/simantics/debug/browser/utils/StatementInfo.java
new file mode 100644 (file)
index 0000000..606a604
--- /dev/null
@@ -0,0 +1,72 @@
+/*******************************************************************************\r
+ * Copyright (c) 2016 Association for Decentralized Information Management\r
+ * in Industry THTH ry.\r
+ * All rights reserved. This program and the accompanying materials\r
+ * are made available under the terms of the Eclipse Public License v1.0\r
+ * which accompanies this distribution, and is available at\r
+ * http://www.eclipse.org/legal/epl-v10.html\r
+ *\r
+ * Contributors:\r
+ *     THTH ry - initial API and implementation\r
+ *******************************************************************************/\r
+package org.simantics.debug.browser.utils;\r
+\r
+import java.util.Arrays;\r
+import java.util.Collection;\r
+\r
+import org.simantics.db.ReadGraph;\r
+import org.simantics.db.Resource;\r
+import org.simantics.db.Statement;\r
+import org.simantics.db.VirtualGraph;\r
+import org.simantics.db.exception.DatabaseException;\r
+import org.simantics.db.service.VirtualGraphSupport;\r
+\r
+public class StatementInfo implements Comparable<StatementInfo> {\r
+    public final ResourceInfo subject; // null, if not asserted\r
+    public final ResourceInfo object;\r
+    public final ResourceInfo[] objectTypes;\r
+    public final String graph; // null, if in DB\r
+    \r
+    public StatementInfo(ReadGraph graph, Resource defaultSubject, Statement statement) throws DatabaseException {\r
+        Resource subject = statement.getSubject();\r
+        Resource predicate = statement.getPredicate();\r
+        Resource object = statement.getObject();\r
+        Collection<Resource> objectTypes = graph.getPrincipalTypes(object);\r
+        \r
+        if(subject.equalsResource(defaultSubject))\r
+            this.subject = null;\r
+        else\r
+            this.subject = new ResourceInfo(graph, subject);\r
+        this.object = new ResourceInfo(graph, object);\r
+        if(objectTypes.isEmpty())\r
+            this.objectTypes = null;\r
+        else\r
+            this.objectTypes = mapTypes(graph, objectTypes);\r
+        this.graph = getGraphName(graph, subject, predicate, object);\r
+    }\r
+    \r
+\r
+    private static ResourceInfo[] mapTypes(ReadGraph graph, Collection<Resource> types) throws DatabaseException {\r
+        ResourceInfo[] result = new ResourceInfo[types.size()];\r
+        int i=0;\r
+        for(Resource type : types)\r
+            result[i++] = new ResourceInfo(graph, type);\r
+        Arrays.sort(result);\r
+        return result;\r
+    }\r
+\r
+\r
+    public static String getGraphName(ReadGraph graph, Resource s, Resource p, Resource o) throws DatabaseException {\r
+        VirtualGraphSupport vgs = graph.getService(VirtualGraphSupport.class);\r
+        VirtualGraph vg = vgs.getGraph(graph, s, p, o);\r
+        if(vg != null)\r
+            return vg.toString();\r
+        else\r
+            return null;\r
+    }\r
+\r
+    @Override\r
+    public int compareTo(StatementInfo o) {\r
+        return object.compareTo(o.object);\r
+    }\r
+}\r