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.algorithms.el; 020 021import java.util.Comparator; 022import java.util.Iterator; 023 024import org.semanticweb.owlapi.model.OWLClass; 025import org.semanticweb.owlapi.model.OWLProperty; 026 027/** 028 * Compares two EL OWLClassExpression trees. It is a lexicographic order 029 * according to the following criteria: 030 * - number of children 031 * - size of label 032 * - string comparison for each class in the label 033 * - recursive call on each child (first compare edge label, then child node) 034 * 035 * @author Jens Lehmann 036 * 037 */ 038public class ELDescriptionNodeComparator implements Comparator<ELDescriptionNode> { 039 040 /* (non-Javadoc) 041 * @see java.util.Comparator#compare(java.lang.Object, java.lang.Object) 042 */ 043 @Override 044 public int compare(ELDescriptionNode node1, ELDescriptionNode node2) { 045 int nrOfChildren1 = node1.getEdges().size(); 046 int nrOfChildren2 = node2.getEdges().size(); 047 if(nrOfChildren1 > nrOfChildren2) { 048 return 1; 049 } else if(nrOfChildren1 < nrOfChildren2) { 050 return -1; 051 } else { 052 int labelSize1 = node1.getLabel().size(); 053 int labelSize2 = node2.getLabel().size(); 054 if(labelSize1 > labelSize2) { 055 return 1; 056 } else if(labelSize1 < labelSize2) { 057 return -1; 058 } else { 059 int compare = 0; 060 if(node1.isClassNode() && node2.isClassNode()){ 061 // navigate through both labels 062 Iterator<OWLClass> it1 = node1.getLabel().descendingIterator(); 063 Iterator<OWLClass> it2 = node2.getLabel().descendingIterator(); 064 while(it1.hasNext()) { 065 OWLClass nc1 = it1.next(); 066 OWLClass nc2 = it2.next(); 067 compare = nc1.toStringID().compareTo(nc2.toStringID()); 068 069 } 070 } else if(!node1.isClassNode() && !node2.isClassNode()){ 071 compare = node1.getDataRange().toString().compareTo(node2.getDataRange().toString()); 072 } else { 073 compare = -1; 074 } 075 076 if(compare != 0) 077 return compare; 078 079 // recursively compare all edges 080 for(int i=0; i<nrOfChildren1; i++) { 081 // compare by edge name 082 OWLProperty op1 = node1.getEdges().get(i).getLabel(); 083 OWLProperty op2 = node2.getEdges().get(i).getLabel(); 084 int compare1 = op1.toStringID().compareTo(op2.toStringID()); 085 if(compare1 != 0) 086 return compare1; 087 088 // compare child nodes 089 ELDescriptionNode child1 = node1.getEdges().get(i).getNode(); 090 ELDescriptionNode child2 = node2.getEdges().get(i).getNode(); 091 int compare2 = compare(child1, child2); 092 if(compare2 != 0) 093 return compare2; 094 } 095 096 // trees are identical 097 return 0; 098 } 099 } 100 } 101 102}