source: trunk/sources/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.4/Formatters/SymbolicExpressionTreeGraphvizFormatter.cs @ 9649

Last change on this file since 9649 was 9649, checked in by gkronber, 6 years ago

#1270 fixed compile errors in Smalltalk and GraphViz formatters

File size: 3.8 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2010 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.Text;
23using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
24using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
25using System.Collections.Generic;
26using HeuristicLab.Core;
27using HeuristicLab.Common;
28
29namespace HeuristicLab.Encodings.SymbolicExpressionTreeEncoding.Formatters {
30  [Item("GraphViz String Formatter", "Formatter for symbolic expression trees for visualization with GraphViz.")]
31  public sealed class SymbolicExpressionTreeGraphvizFormatter : NamedItem, ISymbolicExpressionTreeStringFormatter {
32    public bool Indent { get; set; }
33
34    private readonly static Dictionary<string, string> symbolNameMap = new Dictionary<string, string>() {
35      // match Koza style
36      {"ProgramRootSymbol", "Prog"},
37      {"StartSymbol", "RPB"}, 
38    };
39
40    public SymbolicExpressionTreeGraphvizFormatter()
41      : base("GraphViz String Formatter", "Formatter for symbolic expression trees for visualization with GraphViz.") {
42      Indent = true;
43    }
44    private SymbolicExpressionTreeGraphvizFormatter(SymbolicExpressionTreeGraphvizFormatter original, Cloner cloner)
45      : base(original, cloner) {
46      this.Indent = original.Indent;
47    }
48
49    public override IDeepCloneable Clone(Cloner cloner) {
50      return new SymbolicExpressionTreeGraphvizFormatter(this, cloner);
51    }
52
53    public string Format(ISymbolicExpressionTree symbolicExpressionTree) {
54      int nodeCounter = 1;
55      StringBuilder strBuilder = new StringBuilder();
56      strBuilder.AppendLine("graph {");
57      strBuilder.AppendLine(FormatRecursively(symbolicExpressionTree.Root, 0, ref nodeCounter));
58      strBuilder.AppendLine("}");
59      return strBuilder.ToString();
60    }
61
62    private string FormatRecursively(ISymbolicExpressionTreeNode node, int indentLength, ref int nodeId) {
63      // save id of current node
64      int currentNodeId = nodeId;
65      // increment id for next node
66      nodeId++;
67
68      StringBuilder strBuilder = new StringBuilder();
69      if (Indent) strBuilder.Append(' ', indentLength);
70
71      // get label for node and map if necessary
72      string nodeLabel = node.ToString();
73      if (symbolNameMap.ContainsKey(nodeLabel)) {
74        nodeLabel = symbolNameMap[nodeLabel];
75      }
76
77      strBuilder.Append("node" + currentNodeId + "[label=\"" + nodeLabel + "\"");
78      // leaf nodes should have box shape
79      if (node.SubtreeCount == 0) {
80        strBuilder.AppendLine(", shape=\"box\"];");
81      } else {
82        strBuilder.AppendLine("];");
83      }
84
85      // internal nodes or leaf nodes?
86      foreach (ISymbolicExpressionTreeNode subTree in node.Subtrees) {
87        // add an edge
88        if (Indent) strBuilder.Append(' ', indentLength);
89        strBuilder.AppendLine("node" + currentNodeId + " -- node" + nodeId + ";");
90        // format the whole subtree
91        strBuilder.Append(FormatRecursively(subTree, indentLength + 2, ref nodeId));
92      }
93
94      return strBuilder.ToString();
95    }
96  }
97}
Note: See TracBrowser for help on using the repository browser.