1 /*******************************************************************************
2 * Copyright (c) 2016 Association for Decentralized Information Management
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
10 * THTH ry - initial API and implementation
11 *******************************************************************************/
12 package org.simantics.debug.browser.utils;
14 import java.util.Arrays;
15 import java.util.Collection;
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;
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
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);
36 if(subject.equalsResource(defaultSubject))
39 this.subject = new ResourceInfo(graph, subject);
40 this.object = new ResourceInfo(graph, object);
41 if(objectTypes.isEmpty())
42 this.objectTypes = null;
44 this.objectTypes = mapTypes(graph, objectTypes);
45 this.graph = getGraphName(graph, subject, predicate, object);
49 private static ResourceInfo[] mapTypes(ReadGraph graph, Collection<Resource> types) throws DatabaseException {
50 ResourceInfo[] result = new ResourceInfo[types.size()];
52 for(Resource type : types)
53 result[i++] = new ResourceInfo(graph, type);
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);
69 public int compareTo(StatementInfo o) {
70 return object.compareTo(o.object);