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.utilities.owl;
021    
022    import java.util.Comparator;
023    
024    import org.dllearner.core.owl.DatatypeProperty;
025    import org.dllearner.core.owl.ObjectProperty;
026    import org.dllearner.core.owl.ObjectPropertyInverse;
027    import org.dllearner.core.owl.PropertyExpression;
028    
029    /**
030     * Compares two property expressions. The order is:
031     * datatype properties first, then inverse object properties, then
032     * object properties. For equal types, the URI or toString (inverses)
033     * is used to fix an order.
034     * 
035     * @author Jens Lehmann
036     *
037     */
038    public class RoleComparator implements Comparator<PropertyExpression> {
039    
040            public int compare(PropertyExpression r1, PropertyExpression r2) {
041                    
042                    if(r1 instanceof ObjectProperty) {
043                            if(r2 instanceof ObjectProperty) {
044                                    return ((ObjectProperty)r1).getName().compareTo(((ObjectProperty)r2).getName());
045                                    // second role is inverse or datatype property
046                            } else {
047                                    return -1;
048                            }
049                    // first property is an inverse object property
050                    } else if(r1 instanceof ObjectPropertyInverse){
051                            if(r2 instanceof ObjectProperty) {
052                                    return 1;
053                            } else if(r2 instanceof ObjectPropertyInverse){
054                                    return r1.toString().compareTo(r2.toString());
055                            } else {
056                                    return -1;
057                            }
058                    // r1 is datatype property
059                    } else {
060                            if(r2 instanceof DatatypeProperty) {
061                                    return ((DatatypeProperty)r1).getName().compareTo(((DatatypeProperty)r2).getName());
062                            } else {
063                                    return 1;
064                            }
065                    }
066                    
067            }
068    
069    }