Free cookie consent management tool by TermsFeed Policy Generator

source: branches/DataAnalysis.Extensions/HeuristicLab.Problems.DataAnalysis/3.3/Symbolic/Formatters/SymbolicExpressionTreeSmalltalkStringFormatter.cs @ 4861

Last change on this file since 4861 was 4861, checked in by swinkler, 13 years ago

Added Smalltalk formatter in DataAnalysis.Symbolic.Formatters. (#1270)

File size: 6.9 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2010 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.Text;
23using HeuristicLab.Core;
24using HeuristicLab.Common;
25using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
26using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding.Symbols;
27using HeuristicLab.Problems.DataAnalysis.Symbolic.Symbols;
28
29namespace HeuristicLab.Encodings.SymbolicExpressionTreeEncoding.Formatters {
30
31  [Item("SymbolicExpressionTreeSmalltalkStringFormatter", "String formatter for string representations of symbolic expression trees in Smalltalk syntax.")]
32  [StorableClass]
33  public class SymbolicExpressionTreeSmalltalkStringFormatter : NamedItem, ISymbolicExpressionTreeStringFormatter {
34
35    [StorableConstructor]
36    protected SymbolicExpressionTreeSmalltalkStringFormatter(bool deserializing) : base(deserializing) { }
37    protected SymbolicExpressionTreeSmalltalkStringFormatter(SymbolicExpressionTreeSmalltalkStringFormatter original, Cloner cloner) : base(original, cloner) { }
38    public SymbolicExpressionTreeSmalltalkStringFormatter()
39        : base() {
40      Name = "Smalltalk String Formatter";
41    }
42
43    public string Format(SymbolicExpressionTree symbolicExpressionTree) {
44      return FormatRecursively(symbolicExpressionTree.Root);
45    }
46
47    private string FormatRecursively(SymbolicExpressionTreeNode node) {
48      StringBuilder stringBuilder = new StringBuilder();
49
50      Symbol symbol = node.Symbol;
51      if (symbol is Addition) {
52        for (int i = 0; i < node.SubTrees.Count; i++) {
53          if (i > 0) stringBuilder.Append("+");
54          stringBuilder.Append("(");
55          stringBuilder.Append(FormatRecursively(node.SubTrees[i]));
56          stringBuilder.Append(")");
57        }
58      } else if (symbol is And) {
59        for (int i = 0; i < node.SubTrees.Count; i++) {
60          if (i > 0) stringBuilder.Append("&");
61          stringBuilder.Append("(");
62          stringBuilder.Append(FormatRecursively(node.SubTrees[i]));
63          stringBuilder.Append(")");
64        }
65      } else if (symbol is Average) {
66        stringBuilder.Append("(1/");
67        stringBuilder.Append(node.SubTrees.Count);
68        stringBuilder.Append(")*(");
69        for (int i = 0; i < node.SubTrees.Count; i++) {
70          if (i > 0) stringBuilder.Append("+");
71          stringBuilder.Append("(");
72          stringBuilder.Append(FormatRecursively(node.SubTrees[i]));
73          stringBuilder.Append(")");
74        }
75        stringBuilder.Append(")");
76      } else if (symbol is Constant) {
77        ConstantTreeNode constantTreeNode = node as ConstantTreeNode;
78        stringBuilder.Append(constantTreeNode.Value);
79      } else if (symbol is Cosine) {
80        stringBuilder.Append(FormatRecursively(node.SubTrees[0]));
81        stringBuilder.Append("cos");
82      } else if (symbol is Division) {
83        for (int i = 0; i < node.SubTrees.Count; i++) {
84          if (i > 0) stringBuilder.Append("/");
85          stringBuilder.Append("(");
86          stringBuilder.Append(FormatRecursively(node.SubTrees[i]));
87          stringBuilder.Append(")");
88        }
89      } else if (symbol is Exponential) {
90        stringBuilder.Append("not yet implemented");
91      } else if (symbol is GreaterThan) {
92        stringBuilder.Append("(");
93        stringBuilder.Append(FormatRecursively(node.SubTrees[0]));
94        stringBuilder.Append(">");
95        stringBuilder.Append(FormatRecursively(node.SubTrees[1]));
96        stringBuilder.Append(")");
97      } else if (symbol is IfThenElse) {
98        stringBuilder.Append("(");
99        stringBuilder.Append(FormatRecursively(node.SubTrees[0]));
100        stringBuilder.Append(" ifTrue: [");
101        stringBuilder.Append(FormatRecursively(node.SubTrees[1]));
102        stringBuilder.Append("] ifFalse: [");
103        stringBuilder.Append(FormatRecursively(node.SubTrees[2]));
104        stringBuilder.Append("] )");
105      } else if (symbol is LaggedVariable) {
106        stringBuilder.Append("not implemented");
107      } else if (symbol is LessThan) {
108        stringBuilder.Append("(");
109        stringBuilder.Append(FormatRecursively(node.SubTrees[0]));
110        stringBuilder.Append("<");
111        stringBuilder.Append(FormatRecursively(node.SubTrees[1]));
112        stringBuilder.Append(")");
113      } else if (symbol is Logarithm) {
114        stringBuilder.Append(FormatRecursively(node.SubTrees[0]));
115        stringBuilder.Append("log");
116      } else if (symbol is Multiplication) {
117        for (int i = 0; i < node.SubTrees.Count; i++) {
118          if (i > 0) stringBuilder.Append("*");
119          stringBuilder.Append("(");
120          stringBuilder.Append(FormatRecursively(node.SubTrees[i]));
121          stringBuilder.Append(")");
122        }
123      } else if (symbol is Not) {
124        stringBuilder.Append("!");
125        stringBuilder.Append(FormatRecursively(node.SubTrees[0]));
126      } else if (symbol is Or) {
127        for (int i = 0; i < node.SubTrees.Count; i++) {
128          if (i > 0) stringBuilder.Append("|");
129          stringBuilder.Append("(");
130          stringBuilder.Append(FormatRecursively(node.SubTrees[i]));
131          stringBuilder.Append(")");
132        }
133      } else if (symbol is Sine) {
134        stringBuilder.Append(FormatRecursively(node.SubTrees[0]));
135        stringBuilder.Append("sin");
136      } else if (symbol is Subtraction) {
137        for (int i = 0; i < node.SubTrees.Count; i++) {
138          if (i > 0) stringBuilder.Append("-");
139          stringBuilder.Append("(");
140          stringBuilder.Append(FormatRecursively(node.SubTrees[i]));
141          stringBuilder.Append(")");
142        }
143      } else if (symbol is Tangent) {
144        stringBuilder.Append(FormatRecursively(node.SubTrees[0]));
145        stringBuilder.Append("tan");
146      } else if (symbol is HeuristicLab.Problems.DataAnalysis.Symbolic.Symbols.Variable) {
147        stringBuilder.Append("not yet implemented");
148      }
149
150      else {
151        stringBuilder.Append("ERROR");
152      }
153
154      return stringBuilder.ToString();
155    }
156
157    public override IDeepCloneable Clone(Cloner cloner) {
158      return new SymbolicExpressionTreeSmalltalkStringFormatter(this, cloner);
159    }
160
161  }
162
163}
Note: See TracBrowser for help on using the repository browser.