/******************************************************************************* * Copyright (c) 2016 Association for Decentralized Information Management * in Industry THTH ry. * All rights reserved. This program and the accompanying materials * are made available under the terms of the Eclipse Public License v1.0 * which accompanies this distribution, and is available at * http://www.eclipse.org/legal/epl-v10.html * * Contributors: * THTH ry - initial API and implementation *******************************************************************************/ package org.simantics.debug.browser.sections; import gnu.trove.map.hash.THashMap; import gnu.trove.map.hash.TObjectIntHashMap; import java.io.PrintWriter; import java.util.ArrayList; import java.util.Collection; import java.util.Collections; import java.util.Comparator; import java.util.Map; import org.simantics.db.ReadGraph; import org.simantics.db.Resource; import org.simantics.db.Statement; import org.simantics.db.exception.DatabaseException; import org.simantics.debug.browser.utils.ResourceInfo; import org.simantics.debug.browser.utils.StatementInfo; import org.simantics.layer0.Layer0; public class RawStatementsSection implements ResourceBrowserSection { public Resource resource; public THashMap> statementsByPredicates = new THashMap>(); @Override public double getPriority() { return 100.0; } @Override public void toHtml(ReadGraph graph, PrintWriter out) throws DatabaseException { THashMap>> predicateClassification = new THashMap>>(); Layer0 L0 = Layer0.getInstance(graph); for(Map.Entry> entry : statementsByPredicates.entrySet()) { Resource domain = getDomain(graph, L0, resource, entry.getKey()); THashMap> map = predicateClassification.get(domain); if(map == null) { map = new THashMap>(); predicateClassification.put(domain, map); } map.put(entry.getKey(), entry.getValue()); } ArrayList domains = new ArrayList(predicateClassification.keySet()); Collections.sort(domains, new TypeComparator(graph, L0)); for(Resource domain : domains) { out.println("

" + new ResourceInfo(graph, domain) + "

"); generateStatementTable(graph, out, predicateClassification.get(domain)); } } private static class TypeComparator implements Comparator { private final ReadGraph graph; private final Layer0 L0; private TObjectIntHashMap depthCache = new TObjectIntHashMap(); public TypeComparator(ReadGraph graph, Layer0 L0) { this.graph = graph; this.L0 = L0; depthCache.put(L0.Entity, 0); } private int depth(Resource type) { if(depthCache.containsKey(type)) return depthCache.get(type); depthCache.put(type, 0); // loop guard try { int depth = 0; for(Resource superType : graph.getObjects(type, L0.Inherits)) depth = Math.max(depth, depth(superType)); ++depth; depthCache.put(type, depth); return depth; } catch(DatabaseException e) { e.printStackTrace(); return 0; } } @Override public int compare(Resource type1, Resource type2) { return Integer.compare(depth(type2), depth(type1)); } } public void generateStatementTable(ReadGraph graph, PrintWriter out, THashMap> statementsByPredicates) throws DatabaseException { ArrayList predicateInfos = new ArrayList(statementsByPredicates.size()); for(Resource predicate : statementsByPredicates.keySet()) predicateInfos.add(new ResourceInfo(graph, predicate)); Collections.sort(predicateInfos); if (!predicateInfos.isEmpty()) { out.println("
"); out.println(""); out.println(""); for(ResourceInfo predicateInfo : predicateInfos) { ArrayList statements = statementsByPredicates.get(predicateInfo.resource); ArrayList statementInfos = new ArrayList(statements.size()); for(Statement statement : statements) statementInfos.add(new StatementInfo(graph, resource, statement)); Collections.sort(statementInfos); boolean first = true; for(StatementInfo statementInfo : statementInfos) { out.println("\t"); if(first) { out.println("\t\t"); first = false; } out.print("\t\t" + statementInfo.object); if(statementInfo.objectTypes != null) { out.print(" : "); for(int i=0;i 0) out.print(", "); out.print(statementInfo.objectTypes[i]); } out.print(""); } out.println(""); if(statementInfo.graph != null || statementInfo.subject != null) { out.print("\t\t"); } out.println("\t"); } } out.println("
PredicateObjectNotes
" + predicateInfo + ""); if(statementInfo.graph != null) out.print(" from graph " + statementInfo.graph); if(statementInfo.subject != null) out.print(" from type " + statementInfo.subject); out.println("
"); out.println("
"); } } public static Resource getDomain(ReadGraph graph, Layer0 L0, Resource subject, Resource predicate) throws DatabaseException { Collection domains = graph.getObjects(predicate, L0.HasDomain); switch(domains.size()) { case 0: return L0.Entity; case 1: return domains.iterator().next(); } for(Resource domain : domains) if(graph.isInstanceOf(subject, domain)) return domain; return domains.iterator().next(); } /* out.println("
"); out.println(""); out.println("
"); */ }