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.utilities;
020
021import org.dllearner.algorithms.ocel.ExampleBasedNode;
022import org.dllearner.core.AbstractSearchTreeNode;
023import org.dllearner.core.Heuristic;
024import org.dllearner.utilities.datastructures.AbstractSearchTree;
025
026import javax.annotation.Nonnull;
027
028public class TreeUtils {
029
030        public static <T extends AbstractSearchTreeNode> String toTreeString(
031                        AbstractSearchTree<T> tree) {
032                return TreeUtils.<T>toTreeString(tree.getRoot(), tree.getHeuristic());
033        }
034        public static  <T extends AbstractSearchTreeNode> String toTreeString(T node, Heuristic<T> heuristic) {
035                return TreeUtils.toTreeString(node, heuristic, 0).toString();
036        }
037        public static <T extends AbstractSearchTreeNode> String getRefinementChainString(T node) {
038                if(node.getParent()!=null) {
039                        String ret = getRefinementChainString(node.getParent());
040                        ret += " => " + node.getExpression().toString();
041                        return ret;
042                } else {
043                        return node.getExpression().toString();
044                }
045        }
046
047        private static <T extends AbstractSearchTreeNode> StringBuilder toTreeString(T node,
048                                                  Heuristic<T> heuristic, int depth) {
049                StringBuilder treeString = new StringBuilder();
050                for(int i=0; i<depth-1; i++)
051                        treeString.append("  ");
052                if(depth!=0)
053                        treeString.append("|--> ");
054                treeString.append(node.toString())
055                                .append(addNodeScore(node, heuristic))
056                                .append("\n");
057                for (Object child :
058                     node.getChildren()) {
059                        treeString.append(TreeUtils.<T>toTreeString((T) child, heuristic,depth+1));
060                }
061                return treeString;
062        }
063
064        @Nonnull
065        private static <T extends AbstractSearchTreeNode> String addNodeScore(T node, Heuristic<T> heuristic) {
066                if (heuristic == null)
067                        return "";
068
069                if (node instanceof ExampleBasedNode) {
070                        if (!((ExampleBasedNode) node).isQualityEvaluated()) {
071                                return "";
072                        }
073                        if (((ExampleBasedNode) node).isTooWeak()) {
074                                return "[score:too weak]";
075                        }
076                }
077
078                return "[score: " + heuristic.getNodeScore(node) + "]";
079        }
080
081}