001    /**
002     * Copyright (C) 2007-2008, 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    
021    package org.dllearner.algorithms.refinement2;
022    
023    import java.util.Comparator;
024    
025    import org.dllearner.utilities.owl.ConceptComparator;
026    
027    /**
028     * This comparator is stable, because it only takes covered examples, concept
029     * length and the concepts itself (using again a stable comparator) into
030     * account, which do not change during the run of the algorithm.
031     * 
032     * @author Jens Lehmann
033     *
034     */
035    public class NodeComparatorStable implements Comparator<ExampleBasedNode> {
036    
037            private ConceptComparator conceptComparator = new ConceptComparator();
038            
039            public int compare(ExampleBasedNode n1, ExampleBasedNode n2) {
040                    
041                    // make sure quality has been evaluated
042                    if(n1.isQualityEvaluated() && n2.isQualityEvaluated()) {
043                            if(!n1.isTooWeak() && !n2.isTooWeak()) {
044                                    int classificationPointsN1 =  n1.getCoveredPositives().size() - n1.getCoveredNegatives().size();
045                                    int classificationPointsN2 =  n2.getCoveredPositives().size() - n2.getCoveredNegatives().size();                                
046                                    
047                                    if(classificationPointsN1>classificationPointsN2) 
048                                            return 1;
049                                    else if(classificationPointsN1<classificationPointsN2)
050                                            return -1;
051                                    else {
052                                            int lengthN1 = n1.getConcept().getLength();
053                                            int lengthN2 = n2.getConcept().getLength();
054                                            
055                                            if(lengthN1<lengthN2)
056                                                    return 1;
057                                            else if(lengthN1>lengthN2)
058                                                    return -1;
059                                            else
060                                                    return conceptComparator.compare(n1.getConcept(), n2.getConcept());
061                                    }
062                            } else {
063                                    if(n1.isTooWeak() && !n2.isTooWeak())
064                                            return -1;
065                                    else if(!n1.isTooWeak() && n2.isTooWeak())
066                                            return 1;
067                                    else
068                                            return conceptComparator.compare(n1.getConcept(), n2.getConcept());
069                            }
070                    }
071                    
072                    throw new RuntimeException("Cannot compare nodes, which have no evaluated quality.");
073            }
074    
075            // all stable node comparators lead to the same order
076            @Override               
077            public boolean equals(Object o) {
078                    return (o instanceof NodeComparatorStable);
079            }
080    
081    }