]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.debug.browser/src/org/simantics/debug/browser/sections/RawStatementsSection.java
Fixed all line endings of the repository
[simantics/platform.git] / bundles / org.simantics.debug.browser / src / org / simantics / debug / browser / sections / RawStatementsSection.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.sections;
13
14 import gnu.trove.map.hash.THashMap;
15 import gnu.trove.map.hash.TObjectIntHashMap;
16
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;
22 import java.util.Map;
23
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;
31
32 public class RawStatementsSection implements ResourceBrowserSection {
33     public Resource resource;
34     public THashMap<Resource, ArrayList<Statement>> statementsByPredicates =
35             new THashMap<Resource, ArrayList<Statement>>();
36
37     @Override
38     public double getPriority() {
39         return 100.0;
40     }
41
42     @Override
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>>>();
46         
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);
52             if(map == null) {
53                 map = new THashMap<Resource, ArrayList<Statement>>();
54                 predicateClassification.put(domain, map);
55             }
56             map.put(entry.getKey(), entry.getValue());
57         }
58         
59         ArrayList<Resource> domains = new ArrayList<Resource>(predicateClassification.keySet());
60         Collections.sort(domains, new TypeComparator(graph, L0));
61         
62         for(Resource domain : domains) {
63             out.println("<h2>" + new ResourceInfo(graph, domain) + "</h2>");
64             generateStatementTable(graph, out, predicateClassification.get(domain));
65         }
66     }
67     
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>();
72         
73         public TypeComparator(ReadGraph graph, Layer0 L0) {
74             this.graph = graph;
75             this.L0 = L0;
76             depthCache.put(L0.Entity, 0);
77         }
78
79         private int depth(Resource type) {
80             if(depthCache.containsKey(type))
81                 return depthCache.get(type);
82             
83             depthCache.put(type, 0); // loop guard
84             try {
85                 int depth = 0;
86                 for(Resource superType : graph.getObjects(type, L0.Inherits))
87                     depth = Math.max(depth, depth(superType));
88                 ++depth;
89                 depthCache.put(type, depth);
90                 return depth;
91             } catch(DatabaseException e) {
92                 e.printStackTrace();
93                 return 0;
94             }
95         }
96         
97         @Override
98         public int compare(Resource type1, Resource type2) {
99             return Integer.compare(depth(type2), depth(type1));
100         }
101     }
102     
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);
110
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);
121                 
122                 boolean first = true;
123                 for(StatementInfo statementInfo : statementInfos) {
124                     out.println("\t<tr>");
125                     if(first) {
126                         out.println("\t\t<td rowspan=\""+statementInfos.size()+"\">" + predicateInfo + "</td>");
127                         first = false;
128                     }
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) {
136                             if(i > 0)
137                                 out.print(", ");
138                             out.print(statementInfo.objectTypes[i]);
139                         }
140                         out.print("</span>");
141                     }
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>");
150                     }
151                     out.println("\t</tr>");
152                 }
153             }
154             out.println("</table>");
155             out.println("</div>");
156         }
157     }
158     
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();
164         }
165         
166         for(Resource domain : domains)
167             if(graph.isInstanceOf(subject, domain))
168                 return domain;
169
170         return domains.iterator().next();
171     }
172
173     /*
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>',");
187
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],");
195         }
196         out.println("\t\t\t],");
197
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],");
203
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},");
213
214         out.println("\t\t\t\"oSearch\": {");
215         out.println("\t\t\t\t\"sSearch\": \"\"");
216         out.println("\t\t\t}");
217
218         out.println("\t\t});");
219         out.println("\t});");
220         out.println("</script>");
221         out.println("</div>");
222      */
223 }