1 | #region License Information
|
---|
2 | /* HeuristicLab
|
---|
3 | * Copyright (C) 2002-2015 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;
|
---|
23 | using System.Collections.Generic;
|
---|
24 | using System.Drawing;
|
---|
25 | using System.Globalization;
|
---|
26 | using System.Linq;
|
---|
27 | using System.Text;
|
---|
28 | using HeuristicLab.Common;
|
---|
29 | using HeuristicLab.Core;
|
---|
30 |
|
---|
31 | namespace HeuristicLab.Encodings.SymbolicExpressionTreeEncoding.Views {
|
---|
32 | [Item("LaTeX/PDF Formatter", "Formatter for symbolic expression trees for use with latex package tikz.")]
|
---|
33 | public class SymbolicExpressionTreeLatexFormatter : NamedItem, ISymbolicExpressionTreeStringFormatter {
|
---|
34 | private readonly static Dictionary<string, string> symbolNameMap = new Dictionary<string, string>
|
---|
35 | {
|
---|
36 | {"ProgramRootSymbol", "Prog"},
|
---|
37 | {"StartSymbol","RPB"}
|
---|
38 | };
|
---|
39 |
|
---|
40 | private readonly ReingoldTilfordLayoutEngine<ISymbolicExpressionTreeNode> layoutEngine;
|
---|
41 |
|
---|
42 | public SymbolicExpressionTreeLatexFormatter()
|
---|
43 | : base("LaTeX/PDF Formatter", "Formatter for symbolic expression trees for use with latex package tikz.") {
|
---|
44 | layoutEngine = new ReingoldTilfordLayoutEngine<ISymbolicExpressionTreeNode>(n => n.Subtrees) {
|
---|
45 | HorizontalSpacing = 2,
|
---|
46 | VerticalSpacing = 2,
|
---|
47 | NodeWidth = 8,
|
---|
48 | NodeHeight = 4
|
---|
49 | };
|
---|
50 | }
|
---|
51 |
|
---|
52 | protected SymbolicExpressionTreeLatexFormatter(SymbolicExpressionTreeLatexFormatter original, Cloner cloner)
|
---|
53 | : base(original, cloner) {
|
---|
54 | }
|
---|
55 |
|
---|
56 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
57 | return new SymbolicExpressionTreeLatexFormatter(this, cloner);
|
---|
58 | }
|
---|
59 |
|
---|
60 | public string Format(ISymbolicExpressionTree symbolicExpressionTree) {
|
---|
61 | var root = symbolicExpressionTree.Root;
|
---|
62 | var actualRoot = root.SubtreeCount == 0 ? root.GetSubtree(0) : root;
|
---|
63 | var nodeCoordinates = layoutEngine.CalculateLayout(actualRoot).ToDictionary(n => n.Content, n => new PointF(n.X, n.Y));
|
---|
64 | var sb = new StringBuilder();
|
---|
65 | var nl = Environment.NewLine;
|
---|
66 | double ws = 1;
|
---|
67 | double hs = 0.7;
|
---|
68 |
|
---|
69 | sb.Append("\\documentclass[class=minimal,border=0pt]{standalone}" + nl +
|
---|
70 | "\\usepackage{tikz}" + nl +
|
---|
71 | "\\begin{document}" + nl +
|
---|
72 | "\\begin{tikzpicture}" + nl +
|
---|
73 | "\\def\\ws{1}" + nl +
|
---|
74 | "\\def\\hs{0.7}" + nl);
|
---|
75 |
|
---|
76 | var nodeIndices = new Dictionary<ISymbolicExpressionTreeNode, int>();
|
---|
77 | var nodes = symbolicExpressionTree.IterateNodesBreadth().ToList();
|
---|
78 | for (int i = 0; i < nodes.Count; ++i) {
|
---|
79 | var node = nodes[i];
|
---|
80 | nodeIndices.Add(node, i);
|
---|
81 | var coord = nodeCoordinates[node];
|
---|
82 | var nodeName = symbolNameMap.ContainsKey(node.Symbol.Name) ? symbolNameMap[node.Symbol.Name] : node.ToString();
|
---|
83 | sb.AppendLine(string.Format(CultureInfo.InvariantCulture, "\\node ({0}) at (\\ws*{1},\\hs*{2}) {{{3}}};", i, ws * coord.X, -hs * coord.Y, EscapeLatexString(nodeName)));
|
---|
84 | }
|
---|
85 |
|
---|
86 | for (int i = 0; i < nodes.Count; ++i) {
|
---|
87 | foreach (var s in nodes[i].Subtrees) {
|
---|
88 | sb.AppendLine(string.Format(CultureInfo.InvariantCulture, "\\draw ({0}) -- ({1});", i, nodeIndices[s]));
|
---|
89 | }
|
---|
90 | }
|
---|
91 |
|
---|
92 | sb.Append("\\end{tikzpicture}" + nl +
|
---|
93 | "\\end{document}" + nl);
|
---|
94 | return sb.ToString();
|
---|
95 | }
|
---|
96 |
|
---|
97 | private static string EscapeLatexString(string s) {
|
---|
98 | return s.Replace("\\", "\\\\").Replace("{", "\\{").Replace("}", "\\}").Replace("_", "\\_");
|
---|
99 | }
|
---|
100 | }
|
---|
101 | }
|
---|