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.el;
020
021import java.util.LinkedList;
022import java.util.List;
023
024import org.dllearner.core.Score;
025import org.semanticweb.owlapi.io.OWLObjectRenderer;
026
027/**
028 * A node in the search tree of an EL algorithm.
029 * 
030 * @author Jens Lehmann
031 *
032 */
033public class SearchTreeNode {
034
035        private ELDescriptionTree descriptionTree;
036        
037        private List<SearchTreeNode> children = new LinkedList<>();
038        
039        private int coveredPositives;
040        private int coveredNegatives;
041        private boolean tooWeak = false;
042        
043        private Score score;
044        protected double accuracy;
045        
046        public SearchTreeNode(ELDescriptionTree descriptionTree) {
047                this.descriptionTree = descriptionTree;
048        }
049
050        /**
051         * @return whether the node is too weak
052         */
053        public boolean isTooWeak() {
054                return tooWeak;
055        }
056
057        /**
058         * set if the node is too weak
059         */
060        public void setTooWeak() {
061                tooWeak = true;
062        }
063        
064        /**
065         * @param coveredPositives the coveredPositives to set
066         */
067        public void setCoveredPositives(int coveredPositives) {
068                this.coveredPositives = coveredPositives;
069        }
070        
071        /**
072         * @return the coveredPositives
073         */
074        public int getCoveredPositives() {
075                return coveredPositives;
076        }
077
078        /**
079         * @param coveredNegatives the coveredNegatives to set
080         */
081        public void setCoveredNegatives(int coveredNegatives) {
082                this.coveredNegatives = coveredNegatives;
083                tooWeak = false;
084        }
085
086        /**
087         * @return the coveredNegatives
088         */
089        public int getCoveredNegatives() {
090                return coveredNegatives;
091        }
092
093        public void addChild(SearchTreeNode node) {
094                children.add(node);
095        }
096        
097        /**
098         * @return the descriptionTree
099         */
100        public ELDescriptionTree getDescriptionTree() {
101                return descriptionTree;
102        }
103
104        /**
105         * @return the children
106         */
107        public List<SearchTreeNode> getChildren() {
108                return children;
109        }
110        
111        @Override               
112        public String toString() {
113                String ret = descriptionTree.transformToClassExpression().toString() + " [q:";
114                if(tooWeak)
115                        ret += "tw";
116                else
117                        ret += coveredNegatives;
118                ret += ", children:" + children.size() + "]";
119                ret += " score: " + score;
120                return ret;
121        }
122        
123        public String toString(OWLObjectRenderer renderer) {
124                String ret = renderer.render(descriptionTree.transformToClassExpression()) + " [q:";
125                if(tooWeak)
126                        ret += "tw";
127                else
128                        ret += coveredNegatives;
129                ret += ", children:" + children.size() + "]";
130                ret += " score: " + score;
131                return ret;
132        }
133        
134        public String getTreeString(OWLObjectRenderer renderer) {
135                return getTreeString(0, renderer).toString();
136        }
137        
138        private StringBuilder getTreeString(int depth, OWLObjectRenderer renderer) {
139                StringBuilder treeString = new StringBuilder();
140                for(int i=0; i<depth-1; i++)
141                        treeString.append("  ");
142                if(depth!=0)
143                        treeString.append("|--> ");
144                treeString.append(toString(renderer)).append("\n");
145                for(SearchTreeNode child : children) {
146                        treeString.append(child.getTreeString(depth+1, renderer));
147                }
148                return treeString;
149        }
150
151        /**
152         * @return the score of the node
153         */
154        public Score getScore() {
155                return score;
156        }
157
158        /**
159         * @param score the score of the node to set
160         */
161        public void setScore(Score score) {
162                this.score = score;
163        }       
164        
165        /**
166         * @return the accuracy
167         */
168        public double getAccuracy() {
169                return accuracy;
170        }
171        
172        /**
173         * @param accuracy the accuracy to set
174         */
175        public void setAccuracy(double accuracy) {
176                this.accuracy = accuracy;
177        }
178        
179}