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