Free cookie consent management tool by TermsFeed Policy Generator

source: branches/HeuristicLab.EvolutionTracking/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.4/Formatters/SymbolicExpressionTreeStringFormatter.cs @ 14778

Last change on this file since 14778 was 14312, checked in by bburlacu, 8 years ago

#1772: Merge trunk changes. Delete unnecessary files (sliding window).

File size: 3.7 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2016 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.Collections.Generic;
23using System.Text;
24using HeuristicLab.Common;
25using HeuristicLab.Core;
26using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
27
28namespace HeuristicLab.Encodings.SymbolicExpressionTreeEncoding {
29
30  [Item("Default String Formatter", "The default string formatter for symbolic expression trees.")]
31  [StorableClass]
32  public class SymbolicExpressionTreeStringFormatter : NamedItem, ISymbolicExpressionTreeStringFormatter {
33
34    public bool Indent { get; set; }
35    public bool AppendNewLines { get; set; }
36
37    private static readonly Dictionary<string, string> PrettyNames = new Dictionary<string, string> {
38      { "Addition", "+"}, {"Subtraction", "-" }, {"Multiplication", "*" }, {"Division", "/" }, {"Logarithm", "log" }, {"Exponential", "exp" }
39    };
40
41    [StorableConstructor]
42    protected SymbolicExpressionTreeStringFormatter(bool deserializing) : base(deserializing) { }
43    protected SymbolicExpressionTreeStringFormatter(SymbolicExpressionTreeStringFormatter original, Cloner cloner)
44      : base(original, cloner) {
45      Indent = original.Indent;
46    }
47    public SymbolicExpressionTreeStringFormatter()
48      : base() {
49      Name = "Default String Formatter";
50      Indent = true;
51    }
52
53    public string Format(ISymbolicExpressionTree symbolicExpressionTree) {
54      return FormatRecursively(symbolicExpressionTree.Root, 0);
55    }
56
57    public string Format(ISymbolicExpressionTreeNode symbolicExpressionTreeNode) {
58      return FormatRecursively(symbolicExpressionTreeNode, 0);
59    }
60
61    private string FormatRecursively(ISymbolicExpressionTreeNode node, int indentLength) {
62      StringBuilder strBuilder = new StringBuilder();
63      if (Indent) strBuilder.Append(' ', indentLength);
64      strBuilder.Append("(");
65      // internal nodes or leaf nodes?
66      if (node.SubtreeCount > 0) {
67        // symbol on same line as '('
68        string name;
69        PrettyNames.TryGetValue(node.ToString(), out name);
70        if (name == null) name = node.ToString();
71        strBuilder.Append(name);
72        if (AppendNewLines)
73          strBuilder.AppendLine();
74        // each subtree expression on a new line
75        // and closing ')' also on new line
76        foreach (var subtree in node.Subtrees) {
77          strBuilder.Append(FormatRecursively(subtree, indentLength + 2));
78          if (AppendNewLines)
79            strBuilder.AppendLine();
80        }
81        if (Indent) strBuilder.Append(' ', indentLength);
82        strBuilder.Append(") ");
83      } else {
84        // symbol in the same line with as '(' and ')'
85        strBuilder.Append(node);
86        strBuilder.Append(") ");
87      }
88      return strBuilder.ToString();
89    }
90
91    public override IDeepCloneable Clone(Cloner cloner) {
92      return new SymbolicExpressionTreeStringFormatter(this, cloner);
93    }
94  }
95}
Note: See TracBrowser for help on using the repository browser.