1 |
|
---|
2 | using System.IO;
|
---|
3 | using HeuristicLab.Common;
|
---|
4 | using HeuristicLab.Core;
|
---|
5 |
|
---|
6 | namespace HeuristicLab.Encodings.SymbolicExpressionTreeEncoding {
|
---|
7 | [Item("Hierarchical Formatter", "Formatter for symbolic expression trees that uses special characters for drawing a tree in text-mode.")]
|
---|
8 | public sealed class SymbolicExpressionTreeHierarchicalFormatter : NamedItem, ISymbolicExpressionTreeStringFormatter {
|
---|
9 | private SymbolicExpressionTreeHierarchicalFormatter(SymbolicExpressionTreeHierarchicalFormatter original, Cloner cloner)
|
---|
10 | : base(original, cloner) {
|
---|
11 | }
|
---|
12 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
13 | return new SymbolicExpressionTreeHierarchicalFormatter(this, cloner);
|
---|
14 | }
|
---|
15 |
|
---|
16 | public SymbolicExpressionTreeHierarchicalFormatter() :
|
---|
17 | base("Hierarchical Formatter", "Formatter for symbolic expression trees that uses special characters for drawing a tree in text-mode.") { }
|
---|
18 |
|
---|
19 | public string Format(ISymbolicExpressionTree symbolicExpressionTree) {
|
---|
20 | var sw = new StringWriter();
|
---|
21 | RenderTree(sw, symbolicExpressionTree);
|
---|
22 | return sw.ToString();
|
---|
23 | }
|
---|
24 |
|
---|
25 | private static void RenderTree(TextWriter writer, ISymbolicExpressionTree tree) {
|
---|
26 | RenderNode(writer, tree.Root, string.Empty);
|
---|
27 | }
|
---|
28 |
|
---|
29 | public static void RenderNode(TextWriter writer, ISymbolicExpressionTreeNode node, string prefix) {
|
---|
30 | string label = node.ToString();
|
---|
31 | writer.Write(label);
|
---|
32 | if (node.SubtreeCount > 0) {
|
---|
33 | var padding = prefix + new string(' ', label.Length);
|
---|
34 | for (int i = 0; i != node.SubtreeCount; ++i) {
|
---|
35 | char connector, extender = ' ';
|
---|
36 | if (i == 0) {
|
---|
37 | if (node.SubtreeCount > 1) {
|
---|
38 | connector = RenderChars.JunctionDown;
|
---|
39 | extender = RenderChars.VerticalLine;
|
---|
40 | } else {
|
---|
41 | connector = RenderChars.HorizontalLine;
|
---|
42 | extender = ' ';
|
---|
43 | }
|
---|
44 | } else {
|
---|
45 | writer.Write(padding);
|
---|
46 | if (i == node.SubtreeCount - 1) {
|
---|
47 | connector = RenderChars.CornerRight;
|
---|
48 | extender = ' ';
|
---|
49 | } else {
|
---|
50 | connector = RenderChars.JunctionRight;
|
---|
51 | extender = RenderChars.VerticalLine;
|
---|
52 | }
|
---|
53 | }
|
---|
54 | writer.Write(string.Concat(connector, RenderChars.HorizontalLine));
|
---|
55 | var newPrefix = string.Concat(padding, extender, ' ');
|
---|
56 | RenderNode(writer, node.GetSubtree(i), newPrefix);
|
---|
57 | }
|
---|
58 | } else
|
---|
59 | writer.WriteLine();
|
---|
60 | }
|
---|
61 |
|
---|
62 | // helper class providing characters for displaying a tree in the console
|
---|
63 | public static class RenderChars {
|
---|
64 | public const char JunctionDown = '┬';
|
---|
65 | public const char HorizontalLine = '─';
|
---|
66 | public const char VerticalLine = '│';
|
---|
67 | public const char JunctionRight = '├';
|
---|
68 | public const char CornerRight = '└';
|
---|
69 | }
|
---|
70 | }
|
---|
71 | }
|
---|