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;
020
021import java.io.Serializable;
022import java.text.DecimalFormat;
023
024import org.jetbrains.annotations.NotNull;
025import org.semanticweb.owlapi.model.OWLObject;
026
027import com.google.common.collect.ComparisonChain;
028
029/**
030 * An evaluated hypothesis is a hypothesis and its score.
031 * 
032 * @author Lorenz Buehmann
033 *
034 */
035public abstract class EvaluatedHypothesis<T extends OWLObject, S extends Score> implements Serializable, Comparable<EvaluatedHypothesis<T, S>>{
036
037        private static final long serialVersionUID = 1106431570510815033L;
038        
039        protected T hypothesis;
040        protected S score;
041        
042        protected static DecimalFormat dfPercent = new DecimalFormat("0.00%");
043        
044        /**
045         * Constructs an evaluated hypothesis using its score.
046         * @param hypothesis The hypothesis, which was evaluated.
047         * @param score The score of the hypothesis.
048         */
049        public EvaluatedHypothesis(T hypothesis, S score) {
050                this.hypothesis = hypothesis;
051                this.score = score;
052        }
053        
054        /**
055         * Gets the description, which was evaluated.
056         * @return The underlying description.
057         */
058        public T getDescription() {
059                return hypothesis;
060        }
061        
062        /**
063         * @return the score
064         */
065        public S getScore() {
066                return score;
067        }
068        
069        /**
070         * Used for rewriting (simplification, beautification) of 
071         * evaluated hypotheses returned by the learning algorithm.
072         * @param hypothesis The hypothesis to set.
073         */
074        public void setDescription(T hypothesis) {
075                this.hypothesis = hypothesis;
076        }       
077        
078        /**
079         * @see Score#getAccuracy()
080         * @return Value in this score system.
081         */
082        public double getAccuracy() {
083                return score.getAccuracy();
084        }
085        
086        @Override
087        public String toString() {
088                return hypothesis.toString() + " " + dfPercent.format(getAccuracy());
089        }
090
091        /* (non-Javadoc)
092         * @see java.lang.Comparable#compareTo(java.lang.Object)
093         */
094        @Override
095        public int compareTo(@NotNull EvaluatedHypothesis<T, S> o) {
096                return ComparisonChain.start()
097                                .compare(score.getAccuracy(), o.score.getAccuracy())
098                                .compare(hypothesis, o.getDescription())
099                                .result();
100        }
101
102}