Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Formatters/InfixExpressionFormatter.cs @ 17460

Last change on this file since 17460 was 17180, checked in by swagner, 5 years ago

#2875: Removed years in copyrights

File size: 8.6 KB
RevLine 
[14024]1#region License Information
2/* HeuristicLab
[17180]3 * Copyright (C) Heuristic and Evolutionary Algorithms Laboratory (HEAL)
[14024]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;
23using System.Globalization;
24using System.Linq;
25using System.Text;
26using HeuristicLab.Common;
27using HeuristicLab.Core;
28using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
[16565]29using HEAL.Attic;
[14024]30
31namespace HeuristicLab.Problems.DataAnalysis.Symbolic {
32  /// <summary>
33  /// Formats mathematical expressions in infix form. E.g. x1 * (3.0 * x2 + x3)
34  /// </summary>
[16565]35  [StorableType("6FE2C83D-A594-4ABF-B101-5AEAEA6D3E3D")]
[14024]36  [Item("Infix Symbolic Expression Tree Formatter", "A string formatter that converts symbolic expression trees to infix expressions.")]
37
[14026]38  public sealed class InfixExpressionFormatter : NamedItem, ISymbolicExpressionTreeStringFormatter {
[14024]39
40
41    [StorableConstructor]
[16565]42    private InfixExpressionFormatter(StorableConstructorFlag _) : base(_) { }
[14024]43    private InfixExpressionFormatter(InfixExpressionFormatter original, Cloner cloner) : base(original, cloner) { }
44    public InfixExpressionFormatter()
45      : base() {
46      Name = ItemName;
47      Description = ItemDescription;
48    }
49    public override IDeepCloneable Clone(Cloner cloner) {
50      return new InfixExpressionFormatter(this, cloner);
51    }
52
[16700]53    /// <summary>
54    /// Produces an infix expression for a given expression tree.
55    /// </summary>
56    /// <param name="symbolicExpressionTree">The tree representation of the expression.</param>
57    /// <param name="numberFormat">Number format that should be used for numeric parameters (e.g. NumberFormatInfo.InvariantInfo (default)).</param>
58    /// <param name="formatString">The format string for numeric parameters (e.g. \"G4\" to limit to 4 digits, default is \"G\")</param>
59    /// <returns>Infix expression</returns>
60    public string Format(ISymbolicExpressionTree symbolicExpressionTree, NumberFormatInfo numberFormat, string formatString="G") {
[14024]61      // skip root and start symbols
62      StringBuilder strBuilder = new StringBuilder();
[16700]63      FormatRecursively(symbolicExpressionTree.Root.GetSubtree(0).GetSubtree(0), strBuilder, numberFormat, formatString);
[14024]64      return strBuilder.ToString();
65    }
66
[16700]67    public string Format(ISymbolicExpressionTree symbolicExpressionTree) {
68      return Format(symbolicExpressionTree, NumberFormatInfo.InvariantInfo);
69    }
70
71    private static void FormatRecursively(ISymbolicExpressionTreeNode node, StringBuilder strBuilder, NumberFormatInfo numberFormat, string formatString) {
[14024]72      if (node.SubtreeCount > 1) {
73        var token = GetToken(node.Symbol);
[16359]74        // operators
75        if (token == "+" || token == "-" || token == "OR" || token == "XOR" ||
76            token == "*" || token == "/" || token == "AND" ||
77            token == "^") {
[14024]78          strBuilder.Append("(");
[16700]79          FormatRecursively(node.Subtrees.First(), strBuilder, numberFormat, formatString);
[14024]80
81          foreach (var subtree in node.Subtrees.Skip(1)) {
82            strBuilder.Append(" ").Append(token).Append(" ");
[16700]83            FormatRecursively(subtree, strBuilder, numberFormat, formatString);
[14024]84          }
85          strBuilder.Append(")");
[14347]86        } else {
87          // function with multiple arguments
88          strBuilder.Append(token).Append("(");
[16700]89          FormatRecursively(node.Subtrees.First(), strBuilder, numberFormat, formatString);
[14347]90          foreach (var subtree in node.Subtrees.Skip(1)) {
91            strBuilder.Append(", ");
[16700]92            FormatRecursively(subtree, strBuilder, numberFormat, formatString);
[14347]93          }
94          strBuilder.Append(")");
[14024]95        }
96      } else if (node.SubtreeCount == 1) {
97        var token = GetToken(node.Symbol);
98        if (token == "-" || token == "NOT") {
99          strBuilder.Append("(").Append(token).Append("(");
[16700]100          FormatRecursively(node.GetSubtree(0), strBuilder, numberFormat, formatString);
[14024]101          strBuilder.Append("))");
102        } else if (token == "/") {
103          strBuilder.Append("1/");
[16700]104          FormatRecursively(node.GetSubtree(0), strBuilder, numberFormat, formatString);
[14024]105        } else if (token == "+" || token == "*") {
[16700]106          FormatRecursively(node.GetSubtree(0), strBuilder, numberFormat, formatString);
[14024]107        } else {
[14347]108          // function with only one argument
[14024]109          strBuilder.Append(token).Append("(");
[16700]110          FormatRecursively(node.GetSubtree(0), strBuilder, numberFormat, formatString);
[14024]111          strBuilder.Append(")");
112        }
113      } else {
114        // no subtrees
[14350]115        if (node.Symbol is LaggedVariable) {
116          var varNode = node as LaggedVariableTreeNode;
117          if (!varNode.Weight.IsAlmost(1.0)) {
118            strBuilder.Append("(");
[16700]119            strBuilder.Append(varNode.Weight.ToString(formatString, numberFormat));
[14350]120            strBuilder.Append("*");
121          }
122          strBuilder.Append("LAG(");
123          if (varNode.VariableName.Contains("'")) {
124            strBuilder.AppendFormat("\"{0}\"", varNode.VariableName);
125          } else {
126            strBuilder.AppendFormat("'{0}'", varNode.VariableName);
127          }
128          strBuilder.Append(", ")
[16700]129            .AppendFormat(numberFormat, "{0}", varNode.Lag)
[14350]130            .Append(")");
131        } else if (node.Symbol is Variable) {
[14024]132          var varNode = node as VariableTreeNode;
133          if (!varNode.Weight.IsAlmost(1.0)) {
134            strBuilder.Append("(");
[16700]135            strBuilder.Append(varNode.Weight.ToString(formatString, numberFormat));
[14024]136            strBuilder.Append("*");
137          }
138          if (varNode.VariableName.Contains("'")) {
139            strBuilder.AppendFormat("\"{0}\"", varNode.VariableName);
140          } else {
141            strBuilder.AppendFormat("'{0}'", varNode.VariableName);
142          }
143          if (!varNode.Weight.IsAlmost(1.0)) {
144            strBuilder.Append(")");
145          }
[14826]146        } else if (node.Symbol is FactorVariable) {
147          var factorNode = node as FactorVariableTreeNode;
148          if (factorNode.VariableName.Contains("'")) {
149            strBuilder.AppendFormat("\"{0}\"", factorNode.VariableName);
150          } else {
151            strBuilder.AppendFormat("'{0}'", factorNode.VariableName);
152          }
153          strBuilder.AppendFormat("[{0}]",
[16700]154            string.Join(", ", factorNode.Weights.Select(w => w.ToString(formatString, numberFormat))));
[14826]155        } else if (node.Symbol is BinaryFactorVariable) {
156          var factorNode = node as BinaryFactorVariableTreeNode;
157          if (!factorNode.Weight.IsAlmost(1.0)) {
158            strBuilder.Append("(");
[16700]159            strBuilder.Append(factorNode.Weight.ToString(formatString, numberFormat));
[14826]160            strBuilder.Append("*");
161          }
162          if (factorNode.VariableName.Contains("'")) {
163            strBuilder.AppendFormat("\"{0}\"", factorNode.VariableName);
164          } else {
165            strBuilder.AppendFormat("'{0}'", factorNode.VariableName);
166          }
167          strBuilder.Append(" = ");
168          if (factorNode.VariableValue.Contains("'")) {
169            strBuilder.AppendFormat("\"{0}\"", factorNode.VariableValue);
170          } else {
171            strBuilder.AppendFormat("'{0}'", factorNode.VariableValue);
172          }
173
174          if (!factorNode.Weight.IsAlmost(1.0)) {
175            strBuilder.Append(")");
176          }
177
[14024]178        } else if (node.Symbol is Constant) {
179          var constNode = node as ConstantTreeNode;
180          if (constNode.Value >= 0.0)
[16700]181            strBuilder.Append(constNode.Value.ToString(formatString, numberFormat));
[14024]182          else
[16700]183            strBuilder.Append("(").Append(constNode.Value.ToString(formatString, numberFormat)).Append(")"); // (-1
[14024]184        }
185      }
186    }
187
[16700]188    private static string GetToken(ISymbol symbol) {
[16359]189      var tok = InfixExpressionParser.knownSymbols.GetBySecond(symbol).FirstOrDefault();
[14024]190      if (tok == null)
191        throw new ArgumentException(string.Format("Unknown symbol {0} found.", symbol.Name));
192      return tok;
193    }
194  }
195}
Note: See TracBrowser for help on using the repository browser.