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.learningproblems;
020
021import java.util.Set;
022
023import org.dllearner.core.EvaluatedDescription;
024import org.dllearner.utilities.owl.OWLAPIRenderers;
025import org.json.JSONArray;
026import org.json.JSONException;
027import org.json.JSONObject;
028import org.semanticweb.owlapi.model.OWLClassExpression;
029import org.semanticweb.owlapi.model.OWLIndividual;
030
031/**
032 * This represents a class description, which has been
033 * evaluated by the learning algorithm, i.e. it has been checked
034 * which examples it covers. It can be used as return value for
035 * learning algorithms to make it easier for applications to
036 * assess how good an offered class description is and how it
037 * classifies particular examples.
038 * 
039 * @author Jens Lehmann
040 *
041 */
042public class EvaluatedDescriptionPosNeg extends EvaluatedDescription<ScorePosNeg> {
043        
044        private static final long serialVersionUID = -6962185910615506968L;
045        private ScorePosNeg score2;
046        
047        /**
048         * Constructs an evaluated description using its score.
049         * @param description The description, which was evaluated.
050         * @param score The score of the description.
051         */
052        public EvaluatedDescriptionPosNeg(OWLClassExpression description, ScorePosNeg score) {
053                super(description, score);
054                score2 = score;
055        }
056        
057        
058        /**
059         * @see org.dllearner.learningproblems.ScorePosNeg#getAccuracy()
060         * @return Accuracy of the description.
061         */
062        @Override
063        public double getAccuracy() {
064                return score2.getAccuracy();
065        }
066        
067        /**
068         * Gets the score of this description. This can be used to get
069         * further statistical values.
070         * @see org.dllearner.learningproblems.ScorePosNeg
071         * @return The score object associated with this evaluated description.
072         */
073        @Override
074        public ScorePosNeg getScore() {
075                return score2;
076        }
077
078        /**
079         * @see org.dllearner.learningproblems.ScorePosNeg#getCoveredNegatives()
080         * @return Negative examples covered by the description.
081         */
082        public Set<OWLIndividual> getCoveredNegatives() {
083                return score2.getCoveredNegatives();
084        }
085
086        /**
087         * @see org.dllearner.learningproblems.ScorePosNeg#getCoveredPositives()
088         * @return Positive examples covered by the description.
089         */
090        public Set<OWLIndividual> getCoveredPositives() {
091                return score2.getCoveredPositives();
092        }
093
094        /**
095         * @see org.dllearner.learningproblems.ScorePosNeg#getNotCoveredNegatives()
096         * @return Negative examples not covered by the description.
097         */
098        public Set<OWLIndividual> getNotCoveredNegatives() {
099                return score2.getNotCoveredNegatives();
100        }
101
102        /**
103         * @see org.dllearner.learningproblems.ScorePosNeg#getNotCoveredPositives()
104         * @return Positive examples not covered by the description.
105         */
106        public Set<OWLIndividual> getNotCoveredPositives() {
107                return score2.getNotCoveredPositives();
108        }
109        
110        /**
111         * This convenience method can be used to store and exchange evaluated
112         * descriptions by transforming them to a JSON string.
113         * @return A JSON representation of an evaluated description.
114         */
115        @Override
116        public String asJSON() {
117                JSONObject object = new JSONObject();
118                try {
119                        object.put("descriptionManchesterSyntax", OWLAPIRenderers.toManchesterOWLSyntax(hypothesis));
120                        object.put("descriptionOWLXML", OWLAPIRenderers.toOWLXMLSyntax(hypothesis));
121                        object.put("accuracy", score2.getAccuracy());
122                        object.put("coveredPositives", getJSONArray(score2.getCoveredPositives()));
123                        object.put("coveredNegatives", getJSONArray(score2.getCoveredNegatives()));
124                        object.put("notCoveredPositives", getJSONArray(score2.getNotCoveredPositives()));
125                        object.put("notCoveredNegatives", getJSONArray(score2.getNotCoveredNegatives()));
126                        return object.toString(3);
127                } catch (JSONException e) {
128                        e.printStackTrace();
129                        return null;
130                }
131        }
132        
133        @Override
134        public String toString() {
135                return hypothesis.toString() + "(accuracy: " + getAccuracy() + ")";
136        }
137        
138        // we need to use this method instead of the standard JSON array constructor,
139        // otherwise we'll get unexpected results (JSONArray does not take Individuals
140        // as arguments and does not use toString)
141        public static JSONArray getJSONArray(Set<OWLIndividual> individuals) {
142                JSONArray j = new JSONArray();
143                for(OWLIndividual i : individuals) {
144                        j.put(i.toStringID());
145                }
146                return j;
147        }
148
149}