001/** 002 * Copyright (C) 2007-2011, Jens Lehmann 003 * 004 * This file is part of DL-Learner. 005 * 006 * DL-Learner is free software; you can redistribute it and/or modify 007 * it under the terms of the GNU General Public License as published by 008 * the Free Software Foundation; either version 3 of the License, or 009 * (at your option) any later version. 010 * 011 * DL-Learner is distributed in the hope that it will be useful, 012 * but WITHOUT ANY WARRANTY; without even the implied warranty of 013 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 014 * GNU General Public License for more details. 015 * 016 * You should have received a copy of the GNU General Public License 017 * along with this program. If not, see <http://www.gnu.org/licenses/>. 018 */ 019 020package org.dllearner.scripts.analyse; 021 022import java.io.File; 023import java.util.HashMap; 024import java.util.Map; 025import java.util.SortedSet; 026import java.util.TreeSet; 027 028import org.apache.jena.rdf.model.Model; 029import org.apache.jena.rdf.model.ModelFactory; 030import org.apache.jena.rdf.model.RDFNode; 031import org.apache.jena.rdf.model.Resource; 032import org.apache.jena.rdf.model.Statement; 033 034public class Hierarchy { 035 public static String subclassof = "http://www.w3.org/2000/01/rdf-schema#subClassOf"; 036 037 public static void main(String[] args) { 038 printMap(new Hierarchy().getHierarchyDown("dbpedia_3.4.owl", "RDF/XML", subclassof, false)); 039 printMap(new Hierarchy().getHierarchyUp("dbpedia_3.4.owl", "RDF/XML", subclassof, false)); 040 } 041 042 043 044 public static void printMap(Map<String, SortedSet<String>> hierarchy){ 045 for(String key:hierarchy.keySet()){ 046 SortedSet<String> current = hierarchy.get(key); 047 System.out.print(key+"\t"); 048 System.out.println(current); 049 } 050 } 051 052 public Map<String, SortedSet<String>> getHierarchyDown (String filename, String format, String relationUri, boolean noExpand){ 053 long n = System.currentTimeMillis(); 054 Map<String, SortedSet<String>> m = getHierarchy(filename, format, relationUri, true, noExpand); 055 System.out.println("hierarchy down needed "+(System.currentTimeMillis()-n)); 056 return m; 057 } 058 public Map<String, SortedSet<String>> getHierarchyUp (String filename, String format, String relationUri, boolean noExpand){ 059 long n = System.currentTimeMillis(); 060 Map<String, SortedSet<String>> m = getHierarchy(filename, format, relationUri, false, noExpand); 061 System.out.println("hierarchy up needed "+(System.currentTimeMillis()-n)); 062 return m; 063 } 064 065 private Map<String, SortedSet<String>> getHierarchy (String filename, String format, String relationUri, boolean invert, boolean noExpand){ 066 067 068 Model m = read(filename, format); 069 Map<String, SortedSet<String>> hierarchy = new HashMap<>(); 070 for(Object o : m.listStatements(null, m.getProperty(relationUri), (RDFNode)null).toList()){ 071 Statement s = (Statement) o; 072 Resource sub = s.getSubject(); 073 Resource obj = (Resource) s.getObject(); 074 if(sub.isAnon()||obj.isAnon()){ 075 continue; 076 } 077 if(invert){ 078 put(hierarchy, obj.getURI(), sub.getURI() ); 079 }else{ 080 put(hierarchy, sub.getURI(), obj.getURI() ); 081 } 082 } 083 084 return (noExpand)?hierarchy:expandHierarchy(hierarchy); 085 } 086 087 public Map<String, SortedSet<String>> expandHierarchy(Map<String, SortedSet<String>> hierarchy){ 088 Map<String, SortedSet<String>> expandedHierarchy = new HashMap<>(); 089 for(String key : hierarchy.keySet()){ 090 boolean expanded = true; 091 SortedSet<String> current = hierarchy.get(key); 092 if(current == null){ 093 continue; 094 } 095 while(expanded){ 096 expanded = false; 097 SortedSet<String> tmp = new TreeSet<>(current); 098 for(String currentObject: current){ 099 SortedSet<String> toAddSet = hierarchy.get(currentObject); 100 if(toAddSet==null){ 101 continue; 102 } 103 for(String toAdd : toAddSet){ 104 if(tmp.add(toAdd)){ 105 expanded = true; 106 } 107 }//for2 108 }//for1 109 current = tmp; 110 }//while 111// System.out.println("finished "+current.size()+" "+key); 112 expandedHierarchy.put(key, current); 113 }//for 114 return expandedHierarchy; 115 } 116 117 private void put(Map<String, SortedSet<String>> hierarchy, String key, String value){ 118 if(hierarchy.get(key)==null){ 119 hierarchy.put(key, new TreeSet<>()); 120 } 121 hierarchy.get(key).add(value); 122 } 123 124 public Model read(String filename, String format){ 125 Model m = ModelFactory.createDefaultModel(); 126 long n = System.currentTimeMillis(); 127 m.read(new File(filename).toURI().toString(), format ); 128 System.out.println("reading "+filename+" needed "+(System.currentTimeMillis()-n)); 129 return m; 130 } 131 132// @SuppressWarnings("unchecked") 133// public Map<String, SortedSet<String>> getHierarchyUp (String filename, String format, String relationUri){ 134// Model m = read(filename, format); 135// Map<String, SortedSet<String>> hierarchy = new HashMap<String, SortedSet<String>>(); 136// 137// for(Object o : m.listSubjects().toList()){ 138// Resource c = (Resource) o; 139// if(c.isAnon()){ 140// continue; 141// } 142// SortedSet<String> set = new TreeSet<String>(); 143// 144// Resource next = c; 145// set.add(next.getURI()); 146// while(next != null ){ 147// List<Statement> l = next.listProperties().toList(); 148// next = null; 149// for(Statement s: l){ 150// if(s.getPredicate().getURI().contains(relationUri)){ 151// 152// Resource r = (Resource) s.getObject(); 153// set.add(r.getURI()); 154// next = r; 155// } 156// } 157// if(next != null && hierarchy.get(next.getURI())!=null){ 158// set.addAll(hierarchy.get(next.getURI())); 159// next = null; 160// } 161// } 162// hierarchy.put(c.getURI(), set ); 163// } 164// return hierarchy; 165// } 166 167 168 169 170 171}