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
020 package org.dllearner.core.owl;
021
022 import java.util.Set;
023 import java.util.SortedSet;
024 import java.util.TreeMap;
025 import java.util.TreeSet;
026
027 import org.dllearner.utilities.owl.RoleComparator;
028
029 /**
030 * Represents a hierarchy of datatype properties.
031 *
032 * TODO: Currently, the role hierarchy pruning algorithm (analogous to the
033 * subsumption hierarchy) is not implemented.
034 *
035 * @author Jens Lehmann
036 *
037 */
038 public class DatatypePropertyHierarchy {
039
040 RoleComparator rc = new RoleComparator();
041 TreeMap<DatatypeProperty,SortedSet<DatatypeProperty>> roleHierarchyUp;
042 TreeMap<DatatypeProperty,SortedSet<DatatypeProperty>> roleHierarchyDown;
043 TreeSet<DatatypeProperty> mostGeneralRoles = new TreeSet<DatatypeProperty>(rc);
044 TreeSet<DatatypeProperty> mostSpecialRoles = new TreeSet<DatatypeProperty>(rc);
045
046 public DatatypePropertyHierarchy(Set<DatatypeProperty> atomicRoles, TreeMap<DatatypeProperty,SortedSet<DatatypeProperty>> roleHierarchyUp , TreeMap<DatatypeProperty,SortedSet<DatatypeProperty>> roleHierarchyDown) {
047 this.roleHierarchyUp = roleHierarchyUp;
048 this.roleHierarchyDown = roleHierarchyDown;
049
050 // find most general and most special roles
051 for(DatatypeProperty role : atomicRoles) {
052 if(getMoreGeneralRoles(role).size()==0)
053 mostGeneralRoles.add(role);
054 if(getMoreSpecialRoles(role).size()==0)
055 mostSpecialRoles.add(role);
056 }
057 }
058
059 public SortedSet<DatatypeProperty> getMoreGeneralRoles(DatatypeProperty role) {
060 // we clone all concepts before returning them such that they cannot be
061 // modified externally
062 return new TreeSet<DatatypeProperty>(roleHierarchyUp.get(role));
063 }
064
065 public SortedSet<DatatypeProperty> getMoreSpecialRoles(DatatypeProperty role) {
066 return new TreeSet<DatatypeProperty>(roleHierarchyDown.get(role));
067 }
068
069 @Override
070 public String toString() {
071 String str = "";
072 for(DatatypeProperty role : mostGeneralRoles) {
073 str += toString(roleHierarchyDown, role, 0);
074 }
075 return str;
076 }
077
078 private String toString(TreeMap<DatatypeProperty,SortedSet<DatatypeProperty>> hierarchy, DatatypeProperty role, int depth) {
079 String str = "";
080 for(int i=0; i<depth; i++)
081 str += " ";
082 str += role.toString() + "\n";
083 Set<DatatypeProperty> tmp = hierarchy.get(role);
084 if(tmp!=null) {
085 for(DatatypeProperty c : tmp)
086 str += toString(hierarchy, c, depth+1);
087 }
088 return str;
089 }
090
091 /**
092 * @return The most general roles.
093 */
094 public TreeSet<DatatypeProperty> getMostGeneralRoles() {
095 return mostGeneralRoles;
096 }
097
098 /**
099 * @return The most special roles.
100 */
101 public TreeSet<DatatypeProperty> getMostSpecialRoles() {
102 return mostSpecialRoles;
103 }
104
105
106 }