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 object properties (roles in Description Logics).
031     * 
032     * @author Jens Lehmann
033     *
034     */
035    public class ObjectPropertyHierarchy {
036    
037            RoleComparator rc = new RoleComparator();
038            TreeMap<ObjectProperty,SortedSet<ObjectProperty>> roleHierarchyUp;
039            TreeMap<ObjectProperty,SortedSet<ObjectProperty>> roleHierarchyDown;        
040            TreeSet<ObjectProperty> mostGeneralRoles = new TreeSet<ObjectProperty>(rc);
041            TreeSet<ObjectProperty> mostSpecialRoles = new TreeSet<ObjectProperty>(rc);
042            
043            ObjectProperty topRole = new ObjectProperty("http://www.w3.org/2002/07/owl#topObjectProperty");
044            ObjectProperty botRole = new ObjectProperty("http://www.w3.org/2002/07/owl#bottomObjectProperty");
045            
046            public ObjectPropertyHierarchy(Set<ObjectProperty> atomicRoles, TreeMap<ObjectProperty,SortedSet<ObjectProperty>> roleHierarchyUp , TreeMap<ObjectProperty,SortedSet<ObjectProperty>> roleHierarchyDown) {
047                    this.roleHierarchyUp = roleHierarchyUp;
048                    this.roleHierarchyDown = roleHierarchyDown;
049                    
050                    // find most general and most special roles
051                    for(ObjectProperty role : atomicRoles) {
052                            SortedSet<ObjectProperty> moreGen = getMoreGeneralRoles(role);
053                            SortedSet<ObjectProperty> moreSpec = getMoreSpecialRoles(role);
054                            if(moreGen.size()==0 || (moreGen.size()==1 && moreGen.first().equals(topRole)))
055                                    mostGeneralRoles.add(role);
056                            if(moreSpec.size()==0 || (moreSpec.size()==1 && moreSpec.first().equals(botRole)))
057                                    mostSpecialRoles.add(role);                     
058                    }
059            }
060            
061            public SortedSet<ObjectProperty> getMoreGeneralRoles(ObjectProperty role) {
062                    // we clone all concepts before returning them such that they cannot be
063                    // modified externally
064                    return new TreeSet<ObjectProperty>(roleHierarchyUp.get(role));    
065            }
066            
067            public SortedSet<ObjectProperty> getMoreSpecialRoles(ObjectProperty role) {
068                    return new TreeSet<ObjectProperty>(roleHierarchyDown.get(role));
069            }
070            
071            /**
072             * Implements a subsumption check using the hierarchy (no further
073             * reasoning checks are used).
074             * @param subProperty The (supposedly) more special property.
075             * @param superProperty The (supposedly) more general property.
076             * @return True if <code>subProperty</code> is a subproperty of <code>superProperty</code>.
077             */
078            public boolean isSubpropertyOf(ObjectProperty subProperty, ObjectProperty superProperty) {
079                    if(subProperty.equals(superProperty)) {
080                            return true;
081                    } else {
082    //                      System.out.println("oph: " + subProperty + " " + superProperty);
083                            for(ObjectProperty moreGeneralProperty : roleHierarchyUp.get(subProperty)) {    
084                                    if(isSubpropertyOf(moreGeneralProperty, superProperty)) {
085                                            return true;
086                                    }
087                            }
088                            // we cannot reach the class via any of the upper classes,
089                            // so it is not a super class
090                            return false;
091                    }
092            }       
093            
094            @Override
095            public String toString() {
096                    String str = "";
097                    for(ObjectProperty role : mostGeneralRoles) {
098                            str += toString(roleHierarchyDown, role, 0);
099                    }
100                    return str;
101            }
102            
103            private String toString(TreeMap<ObjectProperty,SortedSet<ObjectProperty>> hierarchy, ObjectProperty role, int depth) {
104                    String str = "";
105                    for(int i=0; i<depth; i++)
106                            str += "  ";
107                    str += role.toString() + "\n";
108                    Set<ObjectProperty> tmp = hierarchy.get(role);
109                    if(tmp!=null) {
110                            for(ObjectProperty c : tmp)
111                                    str += toString(hierarchy, c, depth+1);
112                    }
113                    return str;
114            }
115    
116            /**
117             * @return The most general roles.
118             */
119            public TreeSet<ObjectProperty> getMostGeneralRoles() {
120                    return mostGeneralRoles;
121            }
122    
123            /**
124             * @return The most special roles.
125             */
126            public TreeSet<ObjectProperty> getMostSpecialRoles() {
127                    return mostSpecialRoles;
128            }
129    
130    }