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.core;
021
022 import java.io.Serializable;
023 import java.text.DecimalFormat;
024
025 import org.dllearner.core.owl.Description;
026 import org.dllearner.kb.sparql.SparqlQueryDescriptionConvertVisitor;
027 import org.dllearner.utilities.owl.OWLAPIDescriptionConvertVisitor;
028 import org.dllearner.utilities.owl.OWLAPIRenderers;
029 import org.json.JSONException;
030 import org.json.JSONObject;
031 import org.semanticweb.owlapi.model.OWLClassExpression;
032
033 /**
034 * An evaluated description is a description and its score (with some
035 * convenience method and serialisation formats).
036 *
037 * @author Jens Lehmann
038 *
039 */
040 public class EvaluatedDescription implements Serializable{
041
042 /**
043 *
044 */
045 private static final long serialVersionUID = 1106431570510815033L;
046 protected Description description;
047 protected Score score;
048
049 protected static DecimalFormat dfPercent = new DecimalFormat("0.00%");
050
051 /**
052 * Constructs an evaluated description using its score.
053 * @param description The description, which was evaluated.
054 * @param score The score of the description.
055 */
056 public EvaluatedDescription(Description description, Score score) {
057 this.description = description;
058 this.score = score;
059 }
060
061 /**
062 * Gets the description, which was evaluated.
063 * @return The underlying description.
064 */
065 public Description getDescription() {
066 return description;
067 }
068
069 /**
070 * Used for rewriting (simplification, beautification) of
071 * evaluated descriptions returned by the learning algorithm.
072 * @param description The description to set.
073 */
074 public void setDescription(Description description) {
075 this.description = description;
076 }
077
078 /**
079 * @see org.dllearner.core.owl.Description#getLength()
080 * @return Length of the description.
081 */
082 public int getDescriptionLength() {
083 return description.getLength();
084 }
085
086 /**
087 * @see org.dllearner.core.owl.Description#getDepth()
088 * @return Depth of the description.
089 */
090 public int getDescriptionDepth() {
091 return description.getDepth();
092 }
093
094 /**
095 * @see org.dllearner.core.Score#getScoreValue()
096 * @return Value in this score system.
097 */
098 public double getAccuracy() {
099 return score.getAccuracy();
100 }
101
102 /**
103 * Returns a SPARQL query to get instances of this description
104 * from a SPARQL endpoint. Of course, results may be incomplete,
105 * because no inference is done. The SPARQL query is a straightforward
106 * translation without any attempts to perform e.g. subclass
107 * inferencing.
108 *
109 * @param limit The maximum number of results. Corresponds to LIMIT
110 * in SPARQL.
111 * @return A SPARQL query of the underlying description.
112 */
113 public String getSparqlQuery(int limit) {
114 return SparqlQueryDescriptionConvertVisitor.getSparqlQuery(description, limit, false, false);
115 }
116
117 /**
118 * This convenience method can be used to store and exchange evaluated
119 * descriptions by transforming them to a JSON string.
120 * @return A JSON representation of an evaluated description.
121 */
122 public String asJSON() {
123 JSONObject object = new JSONObject();
124 try {
125 object.put("descriptionManchesterSyntax", description.toManchesterSyntaxString(null, null));
126 OWLClassExpression c = OWLAPIDescriptionConvertVisitor.getOWLClassExpression(description);
127 object.put("descriptionOWLXML", OWLAPIRenderers.toOWLXMLSyntax(c));
128 object.put("descriptionKBSyntax", description.toKBSyntaxString());
129 object.put("scoreValue", score.getAccuracy());
130 return object.toString(3);
131 } catch (JSONException e) {
132 e.printStackTrace();
133 return null;
134 }
135 }
136
137 @Override
138 public String toString() {
139 return description.toString() + " " + dfPercent.format(getAccuracy());
140 }
141
142 }