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