Free cookie consent management tool by TermsFeed Policy Generator

source: branches/HeuristicLab.EvolutionaryTracking/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/SymbolicDataAnalysisSolutionTextRenderer.cs @ 13401

Last change on this file since 13401 was 9241, checked in by bburlacu, 12 years ago

#1772: Merged trunk changes for HeuristicLab.Problems.DataAnalysis.Symbolic. Added SymbolicDataAnalysisSolutionTextRenderer, a class for displaying symbolic expression trees in the console.

File size: 2.5 KB
Line 
1using System;
2using System.IO;
3using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
4
5namespace HeuristicLab.Problems.DataAnalysis.Symbolic {
6  public static class SymbolicDataAnalysisSolutionTextRenderer {
7    public static void TextRender(this ISymbolicExpressionTree tree) {
8      RenderTree(Console.Out, tree);
9    }
10
11    private static void RenderTree(TextWriter writer, ISymbolicExpressionTree tree) {
12      RenderNode(writer, tree.Root, string.Empty);
13    }
14
15    public static void RenderNode(TextWriter writer, ISymbolicExpressionTreeNode node, string prefix) {
16      string label;
17      if (node is VariableTreeNode) {
18        var variable = node as VariableTreeNode;
19        label = string.Concat(string.Format("{0:0.000}", variable.Weight), '·', variable.VariableName);
20      } else if (node is ConstantTreeNode) {
21        var constant = node as ConstantTreeNode;
22        label = string.Format("{0:0.000}", constant.Value);
23      } else {
24        label = node.Symbol.ToString();
25      }
26
27      writer.Write(label);
28      if (node.SubtreeCount > 0) {
29        var padding = prefix + new string(' ', label.Length);
30        for (int i = 0; i != node.SubtreeCount; ++i) {
31          char connector, extender = ' ';
32          if (i == 0) {
33            if (node.SubtreeCount > 1) {
34              connector = RenderChars.JunctionDown;
35              extender = RenderChars.VerticalLine;
36            } else {
37              connector = RenderChars.HorizontalLine;
38              extender = ' ';
39            }
40          } else {
41            writer.Write(padding);
42            if (i == node.SubtreeCount - 1) {
43              connector = RenderChars.CornerRight;
44              extender = ' ';
45            } else {
46              connector = RenderChars.JunctionRight;
47              extender = RenderChars.VerticalLine;
48            }
49          }
50          writer.Write(string.Concat(connector, RenderChars.HorizontalLine));
51          var newPrefix = string.Concat(padding, extender, ' ');
52          RenderNode(writer, node.GetSubtree(i), newPrefix);
53        }
54      } else
55        writer.WriteLine();
56    }
57
58    // helper class providing characters for displaying a tree in the console
59    public static class RenderChars {
60      public const char JunctionDown = '┬';
61      public const char HorizontalLine = '─';
62      public const char VerticalLine = '│';
63      public const char JunctionRight = '├';
64      public const char CornerRight = '└';
65    }
66  }
67}
Note: See TracBrowser for help on using the repository browser.