]> gerrit.simantics Code Review - simantics/platform.git/blob - 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
1 /*******************************************************************************\r
2  * Copyright (c) 2016 Association for Decentralized Information Management\r
3  * in Industry THTH ry.\r
4  * All rights reserved. This program and the accompanying materials\r
5  * are made available under the terms of the Eclipse Public License v1.0\r
6  * which accompanies this distribution, and is available at\r
7  * http://www.eclipse.org/legal/epl-v10.html\r
8  *\r
9  * Contributors:\r
10  *     THTH ry - initial API and implementation\r
11  *******************************************************************************/\r
12 package org.simantics.debug.browser.utils;\r
13 \r
14 import java.util.Arrays;\r
15 import java.util.Collection;\r
16 \r
17 import org.simantics.db.ReadGraph;\r
18 import org.simantics.db.Resource;\r
19 import org.simantics.db.Statement;\r
20 import org.simantics.db.VirtualGraph;\r
21 import org.simantics.db.exception.DatabaseException;\r
22 import org.simantics.db.service.VirtualGraphSupport;\r
23 \r
24 public class StatementInfo implements Comparable<StatementInfo> {\r
25     public final ResourceInfo subject; // null, if not asserted\r
26     public final ResourceInfo object;\r
27     public final ResourceInfo[] objectTypes;\r
28     public final String graph; // null, if in DB\r
29     \r
30     public StatementInfo(ReadGraph graph, Resource defaultSubject, Statement statement) throws DatabaseException {\r
31         Resource subject = statement.getSubject();\r
32         Resource predicate = statement.getPredicate();\r
33         Resource object = statement.getObject();\r
34         Collection<Resource> objectTypes = graph.getPrincipalTypes(object);\r
35         \r
36         if(subject.equalsResource(defaultSubject))\r
37             this.subject = null;\r
38         else\r
39             this.subject = new ResourceInfo(graph, subject);\r
40         this.object = new ResourceInfo(graph, object);\r
41         if(objectTypes.isEmpty())\r
42             this.objectTypes = null;\r
43         else\r
44             this.objectTypes = mapTypes(graph, objectTypes);\r
45         this.graph = getGraphName(graph, subject, predicate, object);\r
46     }\r
47     \r
48 \r
49     private static ResourceInfo[] mapTypes(ReadGraph graph, Collection<Resource> types) throws DatabaseException {\r
50         ResourceInfo[] result = new ResourceInfo[types.size()];\r
51         int i=0;\r
52         for(Resource type : types)\r
53             result[i++] = new ResourceInfo(graph, type);\r
54         Arrays.sort(result);\r
55         return result;\r
56     }\r
57 \r
58 \r
59     public static String getGraphName(ReadGraph graph, Resource s, Resource p, Resource o) throws DatabaseException {\r
60         VirtualGraphSupport vgs = graph.getService(VirtualGraphSupport.class);\r
61         VirtualGraph vg = vgs.getGraph(graph, s, p, o);\r
62         if(vg != null)\r
63             return vg.toString();\r
64         else\r
65             return null;\r
66     }\r
67 \r
68     @Override\r
69     public int compareTo(StatementInfo o) {\r
70         return object.compareTo(o.object);\r
71     }\r
72 }\r