Free cookie consent management tool by TermsFeed Policy Generator

source: branches/3.1/sources/HeuristicLab.Functions/SymbolicExpressionExporter.cs @ 4539

Last change on this file since 4539 was 519, checked in by gkronber, 16 years ago

implemented #259 (BakedFunctionTree should provide an appropriate ToString() method)

File size: 4.7 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2008 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;
23using System.Collections.Generic;
24using System.Linq;
25using System.Text;
26using HeuristicLab.Data;
27
28namespace HeuristicLab.Functions {
29  public class SymbolicExpressionExporter : IFunctionVisitor {
30    private IFunctionTree currentBranch;
31    private StringBuilder builder;
32    private string currentIndent;
33    public SymbolicExpressionExporter() {
34      Reset();
35    }
36
37    public void Reset() {
38      currentIndent = "";
39      builder = new StringBuilder();
40    }
41
42    public string GetStringRepresentation() {
43      return builder.ToString();
44    }
45
46    private void VisitFunction(string name, IFunction f) {
47      builder.Append(currentIndent);
48      builder.Append("(" + name + " ");
49    }
50
51    #region IFunctionVisitor Members
52
53    public void Visit(IFunction function) {
54      builder.Append(function.Name);
55    }
56
57    public void Visit(Addition addition) {
58      VisitFunction("+", addition);
59    }
60
61    public void Visit(Constant constant) {
62      builder.Append(currentIndent+currentBranch.GetLocalVariable(Constant.VALUE).Value);
63    }
64
65    public void Visit(Cosinus cosinus) {
66      VisitFunction("cos", cosinus);
67    }
68
69    public void Visit(Division division) {
70      VisitFunction("/", division);
71    }
72
73    public void Visit(Exponential exponential) {
74      VisitFunction("exp", exponential);
75    }
76
77    public void Visit(Logarithm logarithm) {
78      VisitFunction("log", logarithm);
79    }
80
81    public void Visit(Multiplication multiplication) {
82      VisitFunction("*", multiplication);
83    }
84
85    public void Visit(Power power) {
86      VisitFunction("expt", power);
87    }
88
89    public void Visit(Signum signum) {
90      VisitFunction("sign", signum);
91    }
92
93    public void Visit(Sinus sinus) {
94      VisitFunction("sin", sinus);
95    }
96
97    public void Visit(Sqrt sqrt) {
98      VisitFunction("sqrt", sqrt);
99    }
100
101    public void Visit(Subtraction subtraction) {
102      VisitFunction("-", subtraction);
103    }
104
105    public void Visit(Tangens tangens) {
106      VisitFunction("tan", tangens);
107    }
108
109    public void Visit(Variable variable) {
110      builder.Append(currentIndent + "(variable " + currentBranch.GetLocalVariable(Variable.WEIGHT).Value + " " +
111        currentBranch.GetLocalVariable(Variable.INDEX).Value + " " + currentBranch.GetLocalVariable(Variable.OFFSET).Value + ")");
112    }
113    public void Visit(Differential differential) {
114      builder.Append(currentIndent+"(differential " + currentBranch.GetLocalVariable(Differential.WEIGHT).Value + " "+
115        currentBranch.GetLocalVariable(Differential.INDEX).Value+ " " + currentBranch.GetLocalVariable(Differential.OFFSET).Value+")");
116    }
117
118    public void Visit(And and) {
119      VisitFunction("and", and);
120    }
121
122    public void Visit(Average average) {
123      VisitFunction("mean", average);
124    }
125
126    public void Visit(IfThenElse ifThenElse) {
127      VisitFunction("if", ifThenElse);
128    }
129
130    public void Visit(Not not) {
131      VisitFunction("not", not);
132    }
133
134    public void Visit(Or or) {
135      VisitFunction("or", or);
136    }
137
138    public void Visit(Xor xor) {
139      VisitFunction("xor", xor);
140    }
141
142    public void Visit(Equal equal) {
143      VisitFunction("equ", equal);
144    }
145
146    public void Visit(LessThan lessThan) {
147      VisitFunction("<", lessThan);
148    }
149
150    public void Visit(GreaterThan greaterThan) {
151      VisitFunction(">", greaterThan);
152    }
153    #endregion
154
155    public void Visit(IFunctionTree functionTree) {
156      currentBranch = functionTree;
157      functionTree.Function.Accept(this);
158      currentIndent += "  ";
159
160      foreach(IFunctionTree subTree in functionTree.SubTrees) {
161        builder.Append("\n");
162        Visit(subTree);
163      }
164      if(functionTree.SubTrees.Count > 0) builder.Append(")");
165      currentIndent = currentIndent.Remove(0, 2);
166    }
167  }
168}
Note: See TracBrowser for help on using the repository browser.