1 | #region License Information
|
---|
2 | /* HeuristicLab
|
---|
3 | * Copyright (C) 2002-2016 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
|
---|
4 | *
|
---|
5 | * This file is part of HeuristicLab.
|
---|
6 | *
|
---|
7 | * HeuristicLab is free software: you can redistribute it and/or modify
|
---|
8 | * it under the terms of the GNU General Public License as published by
|
---|
9 | * the Free Software Foundation, either version 3 of the License, or
|
---|
10 | * (at your option) any later version.
|
---|
11 | *
|
---|
12 | * HeuristicLab is distributed in the hope that it will be useful,
|
---|
13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
---|
15 | * GNU General Public License for more details.
|
---|
16 | *
|
---|
17 | * You should have received a copy of the GNU General Public License
|
---|
18 | * along with HeuristicLab. If not, see <http://www.gnu.org/licenses/>.
|
---|
19 | */
|
---|
20 | #endregion
|
---|
21 |
|
---|
22 | using System.IO;
|
---|
23 | using HeuristicLab.Common;
|
---|
24 | using HeuristicLab.Core;
|
---|
25 |
|
---|
26 | namespace HeuristicLab.Encodings.SymbolicExpressionTreeEncoding {
|
---|
27 | [Item("Hierarchical Formatter", "Formatter for symbolic expression trees that uses special characters for drawing a tree in text-mode.")]
|
---|
28 | public sealed class SymbolicExpressionTreeHierarchicalFormatter : NamedItem, ISymbolicExpressionTreeStringFormatter {
|
---|
29 | private SymbolicExpressionTreeHierarchicalFormatter(SymbolicExpressionTreeHierarchicalFormatter original, Cloner cloner)
|
---|
30 | : base(original, cloner) {
|
---|
31 | }
|
---|
32 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
33 | return new SymbolicExpressionTreeHierarchicalFormatter(this, cloner);
|
---|
34 | }
|
---|
35 |
|
---|
36 | public SymbolicExpressionTreeHierarchicalFormatter() :
|
---|
37 | base("Hierarchical Formatter", "Formatter for symbolic expression trees that uses special characters for drawing a tree in text-mode.") { }
|
---|
38 |
|
---|
39 | public string Format(ISymbolicExpressionTree symbolicExpressionTree) {
|
---|
40 | var sw = new StringWriter();
|
---|
41 | RenderTree(sw, symbolicExpressionTree);
|
---|
42 | return sw.ToString();
|
---|
43 | }
|
---|
44 |
|
---|
45 | private static void RenderTree(TextWriter writer, ISymbolicExpressionTree tree) {
|
---|
46 | RenderNode(writer, tree.Root, string.Empty);
|
---|
47 | }
|
---|
48 |
|
---|
49 | public static void RenderNode(TextWriter writer, ISymbolicExpressionTreeNode node, string prefix) {
|
---|
50 | string label = node.ToString();
|
---|
51 | writer.Write(label);
|
---|
52 | if (node.SubtreeCount > 0) {
|
---|
53 | var padding = prefix + new string(' ', label.Length);
|
---|
54 | for (int i = 0; i != node.SubtreeCount; ++i) {
|
---|
55 | char connector, extender = ' ';
|
---|
56 | if (i == 0) {
|
---|
57 | if (node.SubtreeCount > 1) {
|
---|
58 | connector = RenderChars.JunctionDown;
|
---|
59 | extender = RenderChars.VerticalLine;
|
---|
60 | } else {
|
---|
61 | connector = RenderChars.HorizontalLine;
|
---|
62 | extender = ' ';
|
---|
63 | }
|
---|
64 | } else {
|
---|
65 | writer.Write(padding);
|
---|
66 | if (i == node.SubtreeCount - 1) {
|
---|
67 | connector = RenderChars.CornerRight;
|
---|
68 | extender = ' ';
|
---|
69 | } else {
|
---|
70 | connector = RenderChars.JunctionRight;
|
---|
71 | extender = RenderChars.VerticalLine;
|
---|
72 | }
|
---|
73 | }
|
---|
74 | writer.Write(string.Concat(connector, RenderChars.HorizontalLine));
|
---|
75 | var newPrefix = string.Concat(padding, extender, ' ');
|
---|
76 | RenderNode(writer, node.GetSubtree(i), newPrefix);
|
---|
77 | }
|
---|
78 | } else
|
---|
79 | writer.WriteLine();
|
---|
80 | }
|
---|
81 |
|
---|
82 | // helper class providing characters for displaying a tree in the console
|
---|
83 | public static class RenderChars {
|
---|
84 | public const char JunctionDown = '┬';
|
---|
85 | public const char HorizontalLine = '─';
|
---|
86 | public const char VerticalLine = '│';
|
---|
87 | public const char JunctionRight = '├';
|
---|
88 | public const char CornerRight = '└';
|
---|
89 | }
|
---|
90 | }
|
---|
91 | }
|
---|