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.io.Serializable;
022import java.util.Set;
023
024import org.dllearner.core.EvaluatedDescription;
025import org.dllearner.utilities.owl.OWLAPIRenderers;
026import org.json.JSONArray;
027import org.json.JSONException;
028import org.json.JSONObject;
029import org.semanticweb.owlapi.model.OWLClassExpression;
030import org.semanticweb.owlapi.model.OWLEntity;
031import org.semanticweb.owlapi.model.OWLIndividual;
032
033/**
034 * An evaluated OWLClassExpression for learning classes in ontologies.
035 * 
036 * @author Jens Lehmann
037 *
038 */
039public class EvaluatedDescriptionClass extends EvaluatedDescription<ClassScore> implements Serializable{
040
041        private static final long serialVersionUID = -5907640793141522431L;
042        private ClassScore classScore;
043        
044        /**
045         * Constructs an evaluated class expression for learning classes in ontologies.
046         * @param description Description.
047         * @param score Score of description.
048         */
049        public EvaluatedDescriptionClass(OWLClassExpression description, ClassScore score) {
050                super(description, score);
051                classScore = score;
052        }
053        
054        /**
055         * @return The addition factor.
056         * @see org.dllearner.learningproblems.ClassScore#getAddition()
057         */
058        public double getAddition() {
059                return classScore.getAddition();
060        }
061
062        /**
063         * @return The instances of the class description, which are not instances
064         * of the class to learn.
065         * @see org.dllearner.learningproblems.ClassScore#getAdditionalInstances()
066         */
067        public Set<OWLIndividual> getAdditionalInstances() {
068                return classScore.getAdditionalInstances();
069        }
070
071        /**
072         * @return The coverage percentage.
073         * @see org.dllearner.learningproblems.ClassScore#getCoverage()
074         */
075        public double getCoverage() {
076                return classScore.getCoverage();
077        }
078
079        /**
080         * 
081         * @return The instances covered by the class description.
082         * @see org.dllearner.learningproblems.ClassScore#getCoveredInstances()
083         */
084        public Set<OWLIndividual> getCoveredInstances() {
085                return classScore.getCoveredInstances();
086        }
087
088        /**
089         * 
090         * @return The instances of the class not covered by the class description.
091         * @see org.dllearner.learningproblems.ClassScore#getCoveredInstances()
092         */
093        public Set<OWLIndividual> getNotCoveredInstances() {
094                return classScore.getNotCoveredInstances();
095        }       
096        
097        /**
098         * 
099         * @return True if adding the axiom to the knowledge base leads to an inconsistent knowledge base. False otherwise.
100         */
101        public boolean isConsistent() {
102                return classScore.isConsistent();
103        }
104        
105        /**
106         * 
107         * @return True if adding the axiom to the knowledge base does not logically change the knowledge base (i.e. the axiom already follows from it). False otherwise.
108         */
109        public boolean followsFromKB() {
110                return classScore.followsFromKB();
111        }       
112        
113        public void setConsistent(boolean isConsistent) {
114                classScore.setConsistent(isConsistent);
115        }
116
117        public void setFollowsFromKB(boolean followsFromKB) {
118                classScore.setFollowsFromKB(followsFromKB);
119        }
120
121        /**
122         * This convenience method can be used to store and exchange evaluated
123         * descriptions by transforming them to a JSON string.
124         * @return A JSON representation of an evaluated description.
125         */
126        @Override
127        public String asJSON() {
128                JSONObject object = new JSONObject();
129                try {
130                        object.put("descriptionManchesterSyntax", OWLAPIRenderers.toManchesterOWLSyntax(hypothesis));
131                        JSONArray array = new JSONArray();
132                        for (OWLEntity entity : hypothesis.getSignature()) {
133                                array.put(entity.toStringID());
134                        }
135                        object.put("signature", array);
136                        object.put("descriptionOWLXML", OWLAPIRenderers.toOWLXMLSyntax(hypothesis));
137                        object.put("scoreValue", score.getAccuracy());  
138                        array = new JSONArray();
139                        for (OWLIndividual ind : getAdditionalInstances()) {
140                                array.put(ind.toStringID());
141                        }
142                        object.put("additionalInstances", array);
143                        array = new JSONArray();
144                        for (OWLIndividual ind : getCoveredInstances()) {
145                                array.put(ind.toStringID());
146                        }
147                        object.put("coveredInstances", array);
148                        object.put("isConsistent", isConsistent());
149                        object.put("coverage", getCoverage());
150                        object.put("addition", getAddition());
151                        return object.toString(3);
152                } catch (JSONException e) {
153                        e.printStackTrace();
154                        return null;
155                }
156        }               
157}