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.sections;
14 import gnu.trove.map.hash.THashMap;
15 import gnu.trove.map.hash.TObjectIntHashMap;
17 import java.io.PrintWriter;
18 import java.util.ArrayList;
19 import java.util.Collection;
20 import java.util.Collections;
21 import java.util.Comparator;
24 import org.simantics.db.ReadGraph;
25 import org.simantics.db.Resource;
26 import org.simantics.db.Statement;
27 import org.simantics.db.exception.DatabaseException;
28 import org.simantics.debug.browser.utils.ResourceInfo;
29 import org.simantics.debug.browser.utils.StatementInfo;
30 import org.simantics.layer0.Layer0;
32 public class RawStatementsSection implements ResourceBrowserSection {
33 public Resource resource;
34 public THashMap<Resource, ArrayList<Statement>> statementsByPredicates =
35 new THashMap<Resource, ArrayList<Statement>>();
38 public double getPriority() {
43 public void toHtml(ReadGraph graph, PrintWriter out) throws DatabaseException {
44 THashMap<Resource, THashMap<Resource, ArrayList<Statement>>> predicateClassification =
45 new THashMap<Resource, THashMap<Resource, ArrayList<Statement>>>();
47 Layer0 L0 = Layer0.getInstance(graph);
48 for(Map.Entry<Resource, ArrayList<Statement>> entry
49 : statementsByPredicates.entrySet()) {
50 Resource domain = getDomain(graph, L0, resource, entry.getKey());
51 THashMap<Resource, ArrayList<Statement>> map = predicateClassification.get(domain);
53 map = new THashMap<Resource, ArrayList<Statement>>();
54 predicateClassification.put(domain, map);
56 map.put(entry.getKey(), entry.getValue());
59 ArrayList<Resource> domains = new ArrayList<Resource>(predicateClassification.keySet());
60 Collections.sort(domains, new TypeComparator(graph, L0));
62 for(Resource domain : domains) {
63 out.println("<h2>" + new ResourceInfo(graph, domain) + "</h2>");
64 generateStatementTable(graph, out, predicateClassification.get(domain));
68 private static class TypeComparator implements Comparator<Resource> {
69 private final ReadGraph graph;
70 private final Layer0 L0;
71 private TObjectIntHashMap<Resource> depthCache = new TObjectIntHashMap<Resource>();
73 public TypeComparator(ReadGraph graph, Layer0 L0) {
76 depthCache.put(L0.Entity, 0);
79 private int depth(Resource type) {
80 if(depthCache.containsKey(type))
81 return depthCache.get(type);
83 depthCache.put(type, 0); // loop guard
86 for(Resource superType : graph.getObjects(type, L0.Inherits))
87 depth = Math.max(depth, depth(superType));
89 depthCache.put(type, depth);
91 } catch(DatabaseException e) {
98 public int compare(Resource type1, Resource type2) {
99 return Integer.compare(depth(type2), depth(type1));
103 public void generateStatementTable(ReadGraph graph, PrintWriter out,
104 THashMap<Resource, ArrayList<Statement>> statementsByPredicates)
105 throws DatabaseException {
106 ArrayList<ResourceInfo> predicateInfos = new ArrayList<ResourceInfo>(statementsByPredicates.size());
107 for(Resource predicate : statementsByPredicates.keySet())
108 predicateInfos.add(new ResourceInfo(graph, predicate));
109 Collections.sort(predicateInfos);
111 if (!predicateInfos.isEmpty()) {
112 out.println("<div id=\"rawStatementContent\">");
113 out.println("<table>");
114 out.println("<tr><th>Predicate</th><th>Object</th><th>Notes</th></tr>");
115 for(ResourceInfo predicateInfo : predicateInfos) {
116 ArrayList<Statement> statements = statementsByPredicates.get(predicateInfo.resource);
117 ArrayList<StatementInfo> statementInfos = new ArrayList<StatementInfo>(statements.size());
118 for(Statement statement : statements)
119 statementInfos.add(new StatementInfo(graph, resource, statement));
120 Collections.sort(statementInfos);
122 boolean first = true;
123 for(StatementInfo statementInfo : statementInfos) {
124 out.println("\t<tr>");
126 out.println("\t\t<td rowspan=\""+statementInfos.size()+"\">" + predicateInfo + "</td>");
129 out.print("\t\t<td");
130 if (statementInfo.subject != null)
131 out.print(" class=\"asserted\"");
132 out.print(">" + statementInfo.object);
133 if(statementInfo.objectTypes != null) {
134 out.print("<span class=\"resourceType\"> : ");
135 for(int i=0;i<statementInfo.objectTypes.length;++i) {
138 out.print(statementInfo.objectTypes[i]);
140 out.print("</span>");
142 out.println("</td>");
143 if(statementInfo.graph != null || statementInfo.subject != null) {
144 out.print("\t\t<td>");
145 if(statementInfo.graph != null)
146 out.print(" from graph " + statementInfo.graph);
147 if(statementInfo.subject != null)
148 out.print(" from type " + statementInfo.subject);
149 out.println("</td>");
151 out.println("\t</tr>");
154 out.println("</table>");
155 out.println("</div>");
159 public static Resource getDomain(ReadGraph graph, Layer0 L0, Resource subject, Resource predicate) throws DatabaseException {
160 Collection<Resource> domains = graph.getObjects(predicate, L0.HasDomain);
161 switch(domains.size()) {
162 case 0: return L0.Entity;
163 case 1: return domains.iterator().next();
166 for(Resource domain : domains)
167 if(graph.isInstanceOf(subject, domain))
170 return domains.iterator().next();
174 out.println("<div id=\"rawStatementContent\">");
175 out.println("<script type=\"text/javascript\">");
176 out.println("$(document).ready(function() {");
177 out.println("\t$('#rawStatementContent').html( '<table id=\"rawStatementTable\" cellpadding=\"0\" cellspacing=\"0\" border=\"0\" class=\"tablesorter dataTables\"></table>' );");
178 out.println("\tvar oTable = $('#rawStatementTable')");
179 out.println("\t\t.dataTable({");
180 out.println("\t\t\t\"stateSave\": true,");
181 out.println("\t\t\t\"jQueryUI\": true,");
182 out.println("\t\t\t\"paging\": false,");
183 out.println("\t\t\t\"pagingType\": \"full_numbers\",");
184 out.println("\t\t\t\"filter\": true,");
185 out.println("\t\t\t\"info\": true,");
186 //out.println("\t\t\t\"dom\": '<\"H\"lr>t<\"F\"ip>',");
188 out.println("\t\t\t\"data\" : [");
189 for (Statement stm : statements) {
190 out.println("\t\t\t\t[");
191 out.println("\t\t\t\t\t'" + toLinkedLabel(graph, stm.getSubject()) + "',");
192 out.println("\t\t\t\t\t'" + toLinkedLabel(graph, stm.getPredicate()) + "',");
193 out.println("\t\t\t\t\t'" + toLinkedLabel(graph, stm.getObject()) + "'");
194 out.println("\t\t\t\t],");
196 out.println("\t\t\t],");
198 out.println("\t\t\t\"columns\": [");
199 out.println("\t\t\t\t{ \"sTitle\" : \"Subject\", \"sClass\": \"result\" },");
200 out.println("\t\t\t\t{ \"sTitle\" : \"Predicate\", \"sClass\": \"result\" },");
201 out.println("\t\t\t\t{ \"sTitle\" : \"Object\", \"sClass\": \"result\" }");
202 out.println("\t\t\t],");
204 out.println("\t\t\t\"oLanguage\": {");
205 out.println("\t\t\t\t\"oPaginate\": {");
206 out.println("\t\t\t\t\t\"sPrevious\": \"<\",");
207 out.println("\t\t\t\t\t\"sNext\": \">\",");
208 out.println("\t\t\t\t\t\"sLast\": \">>\",");
209 out.println("\t\t\t\t\t\"sFirst\": \"<<\"");
210 out.println("\t\t\t\t},");
211 out.println("\t\t\t\t\"sSearch\": \"Filter:\"");
212 out.println("\t\t\t},");
214 out.println("\t\t\t\"oSearch\": {");
215 out.println("\t\t\t\t\"sSearch\": \"\"");
216 out.println("\t\t\t}");
218 out.println("\t\t});");
219 out.println("\t});");
220 out.println("</script>");
221 out.println("</div>");