Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 16565 was 16565, checked in by gkronber, 5 years ago

#2520: merged changes from PersistenceOverhaul branch (r16451:16564) into trunk

File size: 3.8 KB
Line 
1#region License Information
2
3/* HeuristicLab
4 * Copyright (C) 2002-2019 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
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
24using System.IO;
25using HeuristicLab.Common;
26using HeuristicLab.Core;
27using HEAL.Attic;
28
29namespace HeuristicLab.Encodings.SymbolicExpressionTreeEncoding {
30  [Item("Hierarchical Formatter", "Formatter for symbolic expression trees that uses special characters for drawing a tree in text-mode.")]
31  [StorableType("0a9ad135-ced1-4c3b-94ff-b1fb41193515")]
32  public sealed class SymbolicExpressionTreeHierarchicalFormatter : NamedItem, ISymbolicExpressionTreeStringFormatter {
33    private SymbolicExpressionTreeHierarchicalFormatter(SymbolicExpressionTreeHierarchicalFormatter original, Cloner cloner)
34      : base(original, cloner) {
35    }
36    public override IDeepCloneable Clone(Cloner cloner) {
37      return new SymbolicExpressionTreeHierarchicalFormatter(this, cloner);
38    }
39
40    public SymbolicExpressionTreeHierarchicalFormatter() :
41      base("Hierarchical Formatter", "Formatter for symbolic expression trees that uses special characters for drawing a tree in text-mode.") { }
42
43    public string Format(ISymbolicExpressionTree symbolicExpressionTree) {
44      var sw = new StringWriter();
45      RenderTree(sw, symbolicExpressionTree);
46      return sw.ToString();
47    }
48
49    private static void RenderTree(TextWriter writer, ISymbolicExpressionTree tree) {
50      RenderNode(writer, tree.Root, string.Empty);
51    }
52
53    public static void RenderNode(TextWriter writer, ISymbolicExpressionTreeNode node, string prefix) {
54      string label = node.ToString();
55      writer.Write(label);
56      if (node.SubtreeCount > 0) {
57        var padding = prefix + new string(' ', label.Length);
58        for (int i = 0; i != node.SubtreeCount; ++i) {
59          char connector, extender = ' ';
60          if (i == 0) {
61            if (node.SubtreeCount > 1) {
62              connector = RenderChars.JunctionDown;
63              extender = RenderChars.VerticalLine;
64            } else {
65              connector = RenderChars.HorizontalLine;
66              extender = ' ';
67            }
68          } else {
69            writer.Write(padding);
70            if (i == node.SubtreeCount - 1) {
71              connector = RenderChars.CornerRight;
72              extender = ' ';
73            } else {
74              connector = RenderChars.JunctionRight;
75              extender = RenderChars.VerticalLine;
76            }
77          }
78          writer.Write(string.Concat(connector, RenderChars.HorizontalLine));
79          var newPrefix = string.Concat(padding, extender, ' ');
80          RenderNode(writer, node.GetSubtree(i), newPrefix);
81        }
82      } else
83        writer.WriteLine();
84    }
85
86    // helper class providing characters for displaying a tree in the console
87    public static class RenderChars {
88      public const char JunctionDown = '┬';
89      public const char HorizontalLine = '─';
90      public const char VerticalLine = '│';
91      public const char JunctionRight = '├';
92      public const char CornerRight = '└';
93    }
94  }
95}
Note: See TracBrowser for help on using the repository browser.