001/** 002 * Copyright (C) 2007 - 2016, 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 */ 019package org.dllearner.core.owl; 020 021import java.util.HashSet; 022import java.util.Set; 023import java.util.SortedMap; 024import java.util.SortedSet; 025import java.util.TreeSet; 026 027import org.semanticweb.owlapi.model.OWLAxiom; 028import org.semanticweb.owlapi.model.OWLClass; 029import org.semanticweb.owlapi.model.OWLClassExpression; 030import org.semanticweb.owlapi.model.OWLDataFactory; 031import org.semanticweb.owlapi.vocab.OWLRDFVocabulary; 032import org.slf4j.Logger; 033import org.slf4j.LoggerFactory; 034 035import uk.ac.manchester.cs.owl.owlapi.OWLClassImpl; 036import uk.ac.manchester.cs.owl.owlapi.OWLDataFactoryImpl; 037 038/** 039 * Represents a class subsumption hierarchy (ignoring equivalent concepts). 040 * 041 * @author Jens Lehmann 042 * 043 */ 044public class ClassHierarchy extends AbstractHierarchy<OWLClassExpression> { 045 046 public static Logger logger = LoggerFactory.getLogger(ClassHierarchy.class); 047 048 private static final OWLClass OWL_THING = new OWLClassImpl( 049 OWLRDFVocabulary.OWL_THING.getIRI()); 050 private static final OWLClass OWL_NOTHING = new OWLClassImpl( 051 OWLRDFVocabulary.OWL_NOTHING.getIRI()); 052 053 private OWLDataFactory df = new OWLDataFactoryImpl(); 054 055 /** 056 * The arguments specify the superclasses and subclasses of each class. This 057 * is used to build the subsumption hierarchy. 058 * @param subsumptionHierarchyUp Contains super classes for each class. 059 * @param subsumptionHierarchyDown Contains sub classes for each class. 060 */ 061 public ClassHierarchy( 062 SortedMap<OWLClassExpression, SortedSet<OWLClassExpression>> subsumptionHierarchyUp, 063 SortedMap<OWLClassExpression, SortedSet<OWLClassExpression>> subsumptionHierarchyDown) { 064 super(subsumptionHierarchyUp, subsumptionHierarchyDown); 065 } 066 067 /** 068 * Returns the all superclasses for the given class. 069 * @param concept the class 070 * @return all superclasses 071 */ 072 public SortedSet<OWLClassExpression> getSuperClasses(OWLClassExpression concept) { 073 return getSuperClasses(concept, false); 074 } 075 076 /** 077 * Returns the all superclasses for the given class. 078 * @param concept the class 079 * @param direct whether to return only direct superclasses or not 080 * @return all superclasses 081 */ 082 public SortedSet<OWLClassExpression> getSuperClasses(OWLClassExpression concept, boolean direct) { 083 if(concept.isOWLThing()) { 084 return new TreeSet<>(); 085 } 086 return getParents(concept, direct); 087 } 088 089 /** 090 * Returns the all subclasses. 091 * @param concept the class 092 * @return all subclasses 093 */ 094 public SortedSet<OWLClassExpression> getSubClasses(OWLClassExpression concept) { 095 return getSubClasses(concept, false); 096 } 097 098 /** 099 * Returns the all subclasses. 100 * @param concept the class 101 * @param direct whether to return only direct subclasses or not 102 * @return all subclasses 103 */ 104 public SortedSet<OWLClassExpression> getSubClasses(OWLClassExpression concept, boolean direct) { 105 if(concept.isOWLNothing()) { 106 return new TreeSet<>(); 107 } 108 return getChildren(concept, direct); 109 } 110 111 public SortedSet<OWLClassExpression> getSiblingClasses(OWLClassExpression concept) { 112 return getSiblings(concept); 113 } 114 115 public boolean isSubclassOf(OWLClass subClass, OWLClass superClass) { 116 return isChildOf(subClass, superClass); 117 } 118 119 public boolean isSubclassOf(OWLClassExpression subClass, OWLClassExpression superClass) { 120 return isChildOf(subClass, superClass); 121 } 122 123 @Override 124 public ClassHierarchy clone() { 125 return new ClassHierarchy(getHierarchyUp(), getHierarchyDown()); 126 } 127 128 public Set<OWLAxiom> toOWLAxioms(){ 129 return toOWLAxioms(OWL_THING); 130 } 131 132 public Set<OWLAxiom> toOWLAxioms(OWLClassExpression concept){ 133 Set<OWLAxiom> axioms = new HashSet<>(); 134 Set<OWLClassExpression> subConcepts = getChildren(concept); 135 if (subConcepts != null) { 136 for (OWLClassExpression sub : subConcepts){ 137 axioms.add(df.getOWLSubClassOfAxiom(sub, concept)); 138 axioms.addAll(toOWLAxioms(sub)); 139 } 140 } 141 return axioms; 142 } 143 144 public int getDepth2Root(OWLClassExpression concept){ 145 SortedSet<OWLClassExpression> superClasses = getParents(concept); 146 int depth = 0; 147 if(superClasses != null){ 148 depth = 1; 149 for(OWLClassExpression superClass : superClasses){ 150 depth += getDepth2Root(superClass); 151 } 152 } 153 return depth; 154 } 155 156 public SortedSet<OWLClassExpression> getMostGeneralClasses(){ 157 return getRoots(); 158 } 159 160 /* (non-Javadoc) 161 * @see org.dllearner.core.owl.AbstractHierarchy#getTopConcept() 162 */ 163 @Override 164 public OWLClassExpression getTopConcept() { 165 return OWL_THING; 166 } 167 168 /* (non-Javadoc) 169 * @see org.dllearner.core.owl.AbstractHierarchy#getBottomConcept() 170 */ 171 @Override 172 public OWLClassExpression getBottomConcept() { 173 return OWL_NOTHING; 174 } 175}