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