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