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.io.Serializable;
023    import java.util.Set;
024    
025    import org.dllearner.core.EvaluatedDescription;
026    import org.dllearner.core.owl.Description;
027    import org.dllearner.core.owl.Individual;
028    import org.dllearner.utilities.owl.OWLAPIDescriptionConvertVisitor;
029    import org.dllearner.utilities.owl.OWLAPIRenderers;
030    import org.json.JSONArray;
031    import org.json.JSONException;
032    import org.json.JSONObject;
033    import org.semanticweb.owlapi.model.OWLClassExpression;
034    
035    /**
036     * An evaluated description for learning classes in ontologies.
037     * 
038     * @author Jens Lehmann
039     *
040     */
041    public class EvaluatedDescriptionClass extends EvaluatedDescription implements Serializable{
042    
043            /**
044             * 
045             */
046            private static final long serialVersionUID = -5907640793141522431L;
047            private ClassScore classScore;
048            
049            /**
050             * Constructs an evaluated description for learning classes in ontologies.
051             * @param description Description.
052             * @param score Score of description.
053             */
054            public EvaluatedDescriptionClass(Description description, ClassScore score) {
055                    super(description, score);
056                    classScore = score;
057            }
058            
059            /**
060             * @return The addition factor.
061             * @see org.dllearner.learningproblems.ClassScore#getAddition()
062             */
063            public double getAddition() {
064                    return classScore.getAddition();
065            }
066    
067            /**
068             * @return The instances of the class description, which are not instances
069             * of the class to learn.
070             * @see org.dllearner.learningproblems.ClassScore#getAdditionalInstances()
071             */
072            public Set<Individual> getAdditionalInstances() {
073                    return classScore.getAdditionalInstances();
074            }
075    
076            /**
077             * @return The coverage percentage.
078             * @see org.dllearner.learningproblems.ClassScore#getCoverage()
079             */
080            public double getCoverage() {
081                    return classScore.getCoverage();
082            }
083    
084            /**
085             * 
086             * @return The instances covered by the class description.
087             * @see org.dllearner.learningproblems.ClassScore#getCoveredInstances()
088             */
089            public Set<Individual> getCoveredInstances() {
090                    return classScore.getCoveredInstances();
091            }
092    
093            /**
094             * 
095             * @return The instances of the class not covered by the class description.
096             * @see org.dllearner.learningproblems.ClassScore#getCoveredInstances()
097             */
098            public Set<Individual> getNotCoveredInstances() {
099                    return classScore.getNotCoveredInstances();
100            }       
101            
102            /**
103             * 
104             * @return True if adding the axiom to the knowledge base leads to an inconsistent knowledge base. False otherwise.
105             */
106            public boolean isConsistent() {
107                    return classScore.isConsistent();
108            }
109            
110            /**
111             * 
112             * @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.
113             */
114            public boolean followsFromKB() {
115                    return classScore.followsFromKB();
116            }       
117            
118            public void setConsistent(boolean isConsistent) {
119                    classScore.setConsistent(isConsistent);
120            }
121    
122            public void setFollowsFromKB(boolean followsFromKB) {
123                    classScore.setFollowsFromKB(followsFromKB);
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("signature", new JSONArray(c.getSignature()));
138                            object.put("descriptionOWLXML", OWLAPIRenderers.toOWLXMLSyntax(c));
139                            object.put("descriptionKBSyntax", description.toKBSyntaxString());
140                            object.put("scoreValue", score.getAccuracy());  
141                            object.put("additionalInstances", new JSONArray(getAdditionalInstances()));
142                            object.put("coveredInstances", new JSONArray(getCoveredInstances()));
143                            object.put("isConsistent", isConsistent());
144                            object.put("coverage", getCoverage());
145                            object.put("addition", getAddition());
146                            return object.toString(3);
147                    } catch (JSONException e) {
148                            e.printStackTrace();
149                            return null;
150                    }
151            }               
152    }