Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Formatters/SymbolicDataAnalysisExpressionSmalltalkFormatter.cs @ 10186

Last change on this file since 10186 was 9821, checked in by gkronber, 11 years ago

#1270 made changes as suggested in the review comments

  • initially select default string formatter in the symbolic expression view
  • renamed smalltalk formatter and fixed the namespace
  • fixed namespace of GraphViz formatter
  • removed unused usings
File size: 8.0 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2013 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.Globalization;
23using System.Text;
24using HeuristicLab.Common;
25using HeuristicLab.Core;
26using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
27
28namespace HeuristicLab.Problems.DataAnalysis.Symbolic {
29
30  [Item("Smalltalk String Formatter", "String formatter for string representations of symbolic expression trees in Smalltalk syntax.")]
31  public class SymbolicDataAnalysisExpressionSmalltalkFormatter : NamedItem, ISymbolicExpressionTreeStringFormatter {
32
33    protected SymbolicDataAnalysisExpressionSmalltalkFormatter(SymbolicDataAnalysisExpressionSmalltalkFormatter original, Cloner cloner) : base(original, cloner) { }
34    public SymbolicDataAnalysisExpressionSmalltalkFormatter()
35      : base() {
36      Name = ItemName;
37      Description = ItemDescription;
38    }
39
40    public string Format(ISymbolicExpressionTree symbolicExpressionTree) {
41      return FormatRecursively(symbolicExpressionTree.Root);
42    }
43
44    // returns the smalltalk expression corresponding to the node
45    // smalltalk expressions are always surrounded by parantheses "(<expr>)"
46    private string FormatRecursively(ISymbolicExpressionTreeNode node) {
47
48      ISymbol symbol = node.Symbol;
49
50      if (symbol is ProgramRootSymbol || symbol is StartSymbol)
51        return FormatRecursively(node.GetSubtree(0));
52
53      StringBuilder stringBuilder = new StringBuilder(20);
54
55      stringBuilder.Append("(");
56
57      if (symbol is Addition) {
58        for (int i = 0; i < node.SubtreeCount; i++) {
59          if (i > 0) stringBuilder.Append("+");
60          stringBuilder.Append(FormatRecursively(node.GetSubtree(i)));
61        }
62      } else if (symbol is And) {
63        stringBuilder.Append("(");
64        for (int i = 0; i < node.SubtreeCount; i++) {
65          if (i > 0) stringBuilder.Append("&");
66          stringBuilder.Append("(");
67          stringBuilder.Append(FormatRecursively(node.GetSubtree(i)));
68          stringBuilder.Append(" > 0)");
69        }
70        stringBuilder.Append(") ifTrue:[1] ifFalse:[-1]");
71      } else if (symbol is Average) {
72        stringBuilder.Append("(1/");
73        stringBuilder.Append(node.SubtreeCount);
74        stringBuilder.Append(")*(");
75        for (int i = 0; i < node.SubtreeCount; i++) {
76          if (i > 0) stringBuilder.Append("+");
77          stringBuilder.Append(FormatRecursively(node.GetSubtree(i)));
78        }
79        stringBuilder.Append(")");
80      } else if (symbol is Constant) {
81        ConstantTreeNode constantTreeNode = node as ConstantTreeNode;
82        stringBuilder.Append(constantTreeNode.Value.ToString(CultureInfo.InvariantCulture));
83      } else if (symbol is Cosine) {
84        stringBuilder.Append(FormatRecursively(node.GetSubtree(0)));
85        stringBuilder.Append(" cos");
86      } else if (symbol is Division) {
87        if (node.SubtreeCount == 1) {
88          stringBuilder.Append("1/");
89          stringBuilder.Append(FormatRecursively(node.GetSubtree(0)));
90        } else {
91          stringBuilder.Append(FormatRecursively(node.GetSubtree(0)));
92          stringBuilder.Append("/(");
93          for (int i = 1; i < node.SubtreeCount; i++) {
94            if (i > 1) stringBuilder.Append("*");
95            stringBuilder.Append(FormatRecursively(node.GetSubtree(i)));
96          }
97          stringBuilder.Append(")");
98        }
99      } else if (symbol is Exponential) {
100        stringBuilder.Append(FormatRecursively(node.GetSubtree(0)));
101        stringBuilder.Append(" exp");
102      } else if (symbol is GreaterThan) {
103        stringBuilder.Append("(");
104        stringBuilder.Append(FormatRecursively(node.GetSubtree(0)));
105        stringBuilder.Append(" > ");
106        stringBuilder.Append(FormatRecursively(node.GetSubtree(1)));
107        stringBuilder.Append(") ifTrue: [1] ifFalse: [-1]");
108      } else if (symbol is IfThenElse) {
109        stringBuilder.Append("(");
110        stringBuilder.Append(FormatRecursively(node.GetSubtree(0)));
111        stringBuilder.Append(" > 0) ifTrue: [");
112        stringBuilder.Append(FormatRecursively(node.GetSubtree(1)));
113        stringBuilder.Append("] ifFalse: [");
114        stringBuilder.Append(FormatRecursively(node.GetSubtree(2)));
115        stringBuilder.Append("]");
116      } else if (symbol is LaggedVariable) {
117        stringBuilder.Append("lagged variable not implemented");
118      } else if (symbol is LessThan) {
119        stringBuilder.Append("(");
120        stringBuilder.Append(FormatRecursively(node.GetSubtree(0)));
121        stringBuilder.Append(" < ");
122        stringBuilder.Append(FormatRecursively(node.GetSubtree(1)));
123        stringBuilder.Append(") ifTrue: [1] ifFalse: [-1]");
124      } else if (symbol is Logarithm) {
125        stringBuilder.Append(FormatRecursively(node.GetSubtree(0)));
126        stringBuilder.Append("ln");
127      } else if (symbol is Multiplication) {
128        for (int i = 0; i < node.SubtreeCount; i++) {
129          if (i > 0) stringBuilder.Append("*");
130          stringBuilder.Append(FormatRecursively(node.GetSubtree(i)));
131        }
132      } else if (symbol is Not) {
133        stringBuilder.Append("(");
134        stringBuilder.Append(FormatRecursively(node.GetSubtree(0)));
135        stringBuilder.Append(">0) ifTrue: [-1] ifFalse: [1.0]");
136      } else if (symbol is Or) {
137        stringBuilder.Append("(");
138        for (int i = 0; i < node.SubtreeCount; i++) {
139          if (i > 0) stringBuilder.Append("|");
140          stringBuilder.Append("(");
141          stringBuilder.Append(FormatRecursively(node.GetSubtree(i)));
142          stringBuilder.Append(">0)");
143        }
144        stringBuilder.Append(") ifTrue:[1] ifFalse:[-1]");
145      } else if (symbol is Sine) {
146        stringBuilder.Append(FormatRecursively(node.GetSubtree(0)));
147        stringBuilder.Append(" sin");
148      } else if (symbol is Subtraction) {
149        if (node.SubtreeCount == 1) {
150          stringBuilder.Append("-1*");
151          stringBuilder.Append(FormatRecursively(node.GetSubtree(0)));
152        } else {
153          stringBuilder.Append(FormatRecursively(node.GetSubtree(0)));
154          for (int i = 1; i < node.SubtreeCount; i++) {
155            stringBuilder.Append(" - ");
156            stringBuilder.Append(FormatRecursively(node.GetSubtree(i)));
157          }
158        }
159      } else if (symbol is Tangent) {
160        stringBuilder.Append(FormatRecursively(node.GetSubtree(0)));
161        stringBuilder.Append(" tan");
162      } else if (symbol is Variable) {
163        VariableTreeNode variableTreeNode = node as VariableTreeNode;
164        stringBuilder.Append(variableTreeNode.Weight.ToString(CultureInfo.InvariantCulture));
165        stringBuilder.Append("*");
166        stringBuilder.Append(variableTreeNode.VariableName);
167      } else {
168        stringBuilder.Append("(");
169        for (int i = 0; i < node.SubtreeCount; i++) {
170          if (i > 0) stringBuilder.Append(", ");
171          stringBuilder.Append(FormatRecursively(node.GetSubtree(i)));
172        }
173        stringBuilder.AppendFormat(" {0} [Not Supported] )", node.Symbol.Name);
174      }
175
176      stringBuilder.Append(")");
177
178      return stringBuilder.ToString();
179    }
180
181    public override IDeepCloneable Clone(Cloner cloner) {
182      return new SymbolicDataAnalysisExpressionSmalltalkFormatter(this, cloner);
183    }
184  }
185}
Note: See TracBrowser for help on using the repository browser.