Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 17530 was 17180, checked in by swagner, 5 years ago

#2875: Removed years in copyrights

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