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.algorithms.celoe;
020
021import org.dllearner.core.AbstractSearchTreeNode;
022import org.dllearner.utilities.datastructures.SearchTreeNode;
023import org.dllearner.utilities.owl.OWLAPIRenderers;
024import org.dllearner.utilities.owl.OWLClassExpressionUtils;
025import org.semanticweb.owlapi.model.OWLClassExpression;
026
027import java.text.DecimalFormat;
028import java.util.Map;
029
030/**
031 * A node in the search tree of the ontology engineering algorithm.
032 * 
033 * Differences to the node structures in other algorithms (this may change):
034 * - covered examples are not stored in node (i.e. coverage needs to be recomputed
035 * for child nodes, which costs time but saves memory)
036 * - only evaluated nodes are stored
037 * - too weak nodes are not stored
038 * - redundant nodes are not stored (?)
039 * - only accuracy is stored to make the node structure reusable for different
040 *   learning problems and -algorithms
041 * 
042 * @author Jens Lehmann
043 *
044 */
045public class OENode extends AbstractSearchTreeNode<OENode> implements SearchTreeNode {
046
047        protected OWLClassExpression description;
048        
049        protected double accuracy;
050        
051        protected int horizontalExpansion;
052                
053        // the refinement count corresponds to the number of refinements of the
054        // OWLClassExpression in this node - it is a better heuristic indicator than child count
055        // (and avoids the problem that adding children changes the heuristic value)
056        private int refinementCount = 0;
057        
058        private static DecimalFormat dfPercent = new DecimalFormat("0.00%");
059        
060        public OENode(OWLClassExpression description, double accuracy) {
061                this.description = description;
062                this.accuracy = accuracy;
063                this.horizontalExpansion = OWLClassExpressionUtils.getLength(description);
064        }
065        
066//      public OENode(OENode parentNode, OWLClassExpression description, double accuracy) {
067//              this(description, accuracy);
068//              this.setParent(parentNode);
069//      }
070
071        public void incHorizontalExpansion() {
072                horizontalExpansion++;
073        }
074        
075        public boolean isRoot() {
076                return (parent == null);
077        }
078        
079        /**
080         * @return the description
081         */
082        public OWLClassExpression getDescription() {
083                return description;
084        }
085
086        @Override
087        public OWLClassExpression getExpression() {
088                return getDescription();
089        }
090        
091        /**
092         * @return the accuracy
093         */
094        public double getAccuracy() {
095                return accuracy;
096        }
097
098        /**
099         * @return the horizontalExpansion
100         */
101        public int getHorizontalExpansion() {
102                return horizontalExpansion;
103        }
104        
105        public String getShortDescription(String baseURI) {
106                return getShortDescription(baseURI, null);
107        }
108        
109        public String getShortDescription(String baseURI, Map<String, String> prefixes) {
110                String ret = OWLAPIRenderers.toDLSyntax(description) + " [";
111//              String ret = OWLAPIRenderers.toManchesterOWLSyntax(description) + " [";
112//              ret += "score" + NLPHeuristic.getNodeScore(this) + ",";
113                ret += "acc:" + dfPercent.format(accuracy) + ", ";
114                ret += "he:" + horizontalExpansion + ", ";
115                ret += "c:" + children.size() + ", ";
116                ret += "ref:" + refinementCount + "]";
117                return ret;
118        }
119        
120        @Override
121        public String toString() {
122                return getShortDescription(null);
123        }
124
125        /**
126         * @return the refinementCount
127         */
128        public int getRefinementCount() {
129                return refinementCount;
130        }
131
132        /**
133         * @param refinementCount the refinementCount to set
134         */
135        public void setRefinementCount(int refinementCount) {
136                this.refinementCount = refinementCount;
137        }
138}