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