Free cookie consent management tool by TermsFeed Policy Generator

source: branches/1772_HeuristicLab.EvolutionTracking/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.4/Formatters/SymbolicExpressionTreeHierarchicalFormatter.cs @ 18242

Last change on this file since 18242 was 17434, checked in by bburlacu, 5 years ago

#1772: Merge trunk changes and fix all errors and compilation warnings.

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