Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.4/Formatters/SymbolicExpressionTreeHierarchicalFormatter.cs @ 14427

Last change on this file since 14427 was 14185, checked in by swagner, 8 years ago

#2526: Updated year of copyrights in license headers

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