Free cookie consent management tool by TermsFeed Policy Generator

source: branches/DataAnalysis.Extensions/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding.Formatters/3.3/SymbolicExpressionTreeSmalltalkStringFormatter.cs @ 4829

Last change on this file since 4829 was 4823, checked in by swinkler, 14 years ago

Implemented first version of Smalltalk representation for most symbols. (#1270)

File size: 6.8 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() : base() { }
39
40    public string Format(SymbolicExpressionTree symbolicExpressionTree) {
41      return FormatRecursively(symbolicExpressionTree.Root);
42    }
43
44    private string FormatRecursively(SymbolicExpressionTreeNode node) {
45      StringBuilder stringBuilder = new StringBuilder();
46
47      Symbol symbol = node.Symbol;
48      if (symbol is Addition) {
49        for (int i = 0; i < node.SubTrees.Count; i++) {
50          if (i > 0) stringBuilder.Append("+");
51          stringBuilder.Append("(");
52          stringBuilder.Append(FormatRecursively(node.SubTrees[i]));
53          stringBuilder.Append(")");
54        }
55      } else if (symbol is And) {
56        for (int i = 0; i < node.SubTrees.Count; i++) {
57          if (i > 0) stringBuilder.Append("&");
58          stringBuilder.Append("(");
59          stringBuilder.Append(FormatRecursively(node.SubTrees[i]));
60          stringBuilder.Append(")");
61        }
62      } else if (symbol is Average) {
63        stringBuilder.Append("(1/");
64        stringBuilder.Append(node.SubTrees.Count);
65        stringBuilder.Append(")*(");
66        for (int i = 0; i < node.SubTrees.Count; i++) {
67          if (i > 0) stringBuilder.Append("+");
68          stringBuilder.Append("(");
69          stringBuilder.Append(FormatRecursively(node.SubTrees[i]));
70          stringBuilder.Append(")");
71        }
72        stringBuilder.Append(")");
73      } else if (symbol is Constant) {
74        ConstantTreeNode constantTreeNode = node as ConstantTreeNode;
75        stringBuilder.Append(constantTreeNode.Value);
76      } else if (symbol is Cosine) {
77        stringBuilder.Append(FormatRecursively(node.SubTrees[0]));
78        stringBuilder.Append("cos");
79      } else if (symbol is Division) {
80        for (int i = 0; i < node.SubTrees.Count; i++) {
81          if (i > 0) stringBuilder.Append("/");
82          stringBuilder.Append("(");
83          stringBuilder.Append(FormatRecursively(node.SubTrees[i]));
84          stringBuilder.Append(")");
85        }
86      } else if (symbol is Exponential) {
87        stringBuilder.Append("not yet implemented");
88      } else if (symbol is GreaterThan) {
89        stringBuilder.Append("(");
90        stringBuilder.Append(FormatRecursively(node.SubTrees[0]));
91        stringBuilder.Append(">");
92        stringBuilder.Append(FormatRecursively(node.SubTrees[1]));
93        stringBuilder.Append(")");
94      } else if (symbol is IfThenElse) {
95        stringBuilder.Append("(");
96        stringBuilder.Append(FormatRecursively(node.SubTrees[0]));
97        stringBuilder.Append(" ifTrue: [");
98        stringBuilder.Append(FormatRecursively(node.SubTrees[1]));
99        stringBuilder.Append("] ifFalse: [");
100        stringBuilder.Append(FormatRecursively(node.SubTrees[2]));
101        stringBuilder.Append("] )");
102      } else if (symbol is LaggedVariable) {
103        stringBuilder.Append("not implemented");
104      } else if (symbol is LessThan) {
105        stringBuilder.Append("(");
106        stringBuilder.Append(FormatRecursively(node.SubTrees[0]));
107        stringBuilder.Append("<");
108        stringBuilder.Append(FormatRecursively(node.SubTrees[1]));
109        stringBuilder.Append(")");
110      } else if (symbol is Logarithm) {
111        stringBuilder.Append(FormatRecursively(node.SubTrees[0]));
112        stringBuilder.Append("log");
113      } else if (symbol is Multiplication) {
114        for (int i = 0; i < node.SubTrees.Count; i++) {
115          if (i > 0) stringBuilder.Append("*");
116          stringBuilder.Append("(");
117          stringBuilder.Append(FormatRecursively(node.SubTrees[i]));
118          stringBuilder.Append(")");
119        }
120      } else if (symbol is Not) {
121        stringBuilder.Append("!");
122        stringBuilder.Append(FormatRecursively(node.SubTrees[0]));
123      } else if (symbol is Or) {
124        for (int i = 0; i < node.SubTrees.Count; i++) {
125          if (i > 0) stringBuilder.Append("|");
126          stringBuilder.Append("(");
127          stringBuilder.Append(FormatRecursively(node.SubTrees[i]));
128          stringBuilder.Append(")");
129        }
130      } else if (symbol is Sine) {
131        stringBuilder.Append(FormatRecursively(node.SubTrees[0]));
132        stringBuilder.Append("sin");
133      } else if (symbol is Subtraction) {
134        for (int i = 0; i < node.SubTrees.Count; i++) {
135          if (i > 0) stringBuilder.Append("-");
136          stringBuilder.Append("(");
137          stringBuilder.Append(FormatRecursively(node.SubTrees[i]));
138          stringBuilder.Append(")");
139        }
140      } else if (symbol is Tangent) {
141        stringBuilder.Append(FormatRecursively(node.SubTrees[0]));
142        stringBuilder.Append("tan");
143      } else if (symbol is HeuristicLab.Problems.DataAnalysis.Symbolic.Symbols.Variable) {
144        stringBuilder.Append("not yet implemented");
145      }
146
147      else {
148        stringBuilder.Append("ERROR");
149      }
150
151      return stringBuilder.ToString();
152    }
153
154    public override IDeepCloneable Clone(Cloner cloner) {
155      return new SymbolicExpressionTreeSmalltalkStringFormatter(this, cloner);
156    }
157
158  }
159
160}
Note: See TracBrowser for help on using the repository browser.