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.algorithms.refinement;
021    
022    import java.util.Set;
023    import java.util.TreeSet;
024    
025    import org.dllearner.core.owl.Description;
026    import org.dllearner.utilities.owl.ConceptComparator;
027    
028    public class Node {
029    
030            // TOP ist einfach das TOP-Konzept, also das einzige welches nicht evaluiert wird
031            public enum QualityEvaluationMethod { TOP, REASONER, TOO_WEAK_LIST, OVERLY_GENERAL_LIST };
032            
033            private QualityEvaluationMethod qualityEvaluationMethod = QualityEvaluationMethod.TOP;
034            
035            // alle Eigenschaften eines Knotens im Suchbaum
036            private Description concept;
037            private int horizontalExpansion;
038            private int coveredNegativeExamples;
039            private boolean isTooWeak;
040            private boolean isQualityEvaluated;
041            private boolean isRedundant;
042            
043            private static ConceptComparator conceptComparator = new ConceptComparator();
044            private static NodeComparatorStable nodeComparator = new NodeComparatorStable();
045            
046            // Einbettung in Suchbaum
047            private Node parent = null;
048            // private Set<Node> children = new HashSet<Node>();
049            private Set<Node> children = new TreeSet<Node>(nodeComparator);
050            // es wird auch eine Liste von Kindern gehalten
051            private Set<Description> childConcepts = new TreeSet<Description>(conceptComparator);
052            
053            // verwendeter Operator für Expansion des Knotens
054            // private RefinementOperator operator;
055            
056            public Node(Description concept) {
057                    this.concept = concept;
058                    horizontalExpansion = 0;
059                    isQualityEvaluated = false;
060            }
061            
062            public void setCoveredNegativeExamples(int coveredNegativeExamples) {
063                    if(isQualityEvaluated)
064                            throw new RuntimeException("Cannot set quality of a node more than once.");
065                    this.coveredNegativeExamples = coveredNegativeExamples;
066                    isQualityEvaluated = true;
067            }
068    
069            public void setHorizontalExpansion(int horizontalExpansion) {
070                    this.horizontalExpansion = horizontalExpansion;
071            }
072    
073            public void setRedundant(boolean isRedundant) {
074                    this.isRedundant = isRedundant;
075            }
076    
077            public void setTooWeak(boolean isTooWeak) {
078                    if(isQualityEvaluated)
079                            throw new RuntimeException("Cannot set quality of a node more than once.");
080                    this.isTooWeak = isTooWeak;
081                    isQualityEvaluated = true;
082            }
083    
084        public boolean addChild(Node child) {
085            // child.setParent(this);
086            child.parent = this;
087            childConcepts.add(child.concept);
088            return children.add(child);
089        }
090            
091            public Description getConcept() {
092                    return concept;
093            }
094            public int getCoveredNegativeExamples() {
095                    return coveredNegativeExamples;
096            }
097            public int getHorizontalExpansion() {
098                    return horizontalExpansion;
099            }
100            public boolean isQualityEvaluated() {
101                    return isQualityEvaluated;
102            }
103            public boolean isRedundant() {
104                    return isRedundant;
105            }
106            public boolean isTooWeak() {
107                    return isTooWeak;
108            }
109            
110            @Override               
111            public String toString() {
112                    String ret = concept.toString() + " [q:";
113                    if(isTooWeak)
114                            ret += "tw";
115                    else
116                            ret += coveredNegativeExamples;
117                    ret += ", he:" + horizontalExpansion + ", children:" + children.size() + "]";
118                    return ret;
119            }
120            
121            // gibt die Refinement-Chain zurück, die zu dem Knoten geführt hat
122            public String getRefinementChainString() {
123                    if(parent!=null) {
124                            String ret = parent.getRefinementChainString();
125                            ret += " => " + concept.toString();
126                            return ret;
127                    } else {
128                            return concept.toString();
129                    }
130            }
131    
132            public String getTreeString() {
133                    return getTreeString(0).toString();
134            }
135            
136            private StringBuilder getTreeString(int depth) {
137                    StringBuilder treeString = new StringBuilder();
138                    for(int i=0; i<depth-1; i++)
139                            treeString.append("  ");
140                    if(depth!=0)
141                            // treeString.append("|-→ ");
142                            treeString.append("|--> ");
143                    treeString.append(getShortDescription()+"\n");
144                    for(Node child : children) {
145                            treeString.append(child.getTreeString(depth+1));
146                    }
147                    return treeString;
148            }
149            
150            private String getShortDescription() {
151                    String ret = concept.toString() + " [q:";
152                    
153                    if(isTooWeak)
154                            ret += "tw";
155                    else
156                            ret += coveredNegativeExamples;
157                    
158                    ret += " ("+qualityEvaluationMethod+"), he:" + horizontalExpansion + "]";
159                    return ret;
160            }
161            
162            public Set<Node> getChildren() {
163                    return children;
164            }
165    
166            public Set<Description> getChildConcepts() {
167                    return childConcepts;
168            }
169    
170            public QualityEvaluationMethod getQualityEvaluationMethod() {
171                    return qualityEvaluationMethod;
172            }
173    
174            public void setQualityEvaluationMethod(QualityEvaluationMethod qualityEvaluationMethod) {
175                    this.qualityEvaluationMethod = qualityEvaluationMethod;
176            }
177            
178    }