Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.GP.StructureIdentification/3.3/ModelAnalyzerExporter.cs @ 3390

Last change on this file since 3390 was 2629, checked in by gkronber, 15 years ago

Fixed display of unknown functions and removed UnknownFunctionException. #845 (In the FunctionTreeView only the string N/A is displayed for unknown functions)

File size: 8.5 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 HeuristicLab.GP.Interfaces;
23using System;
24using System.Collections.Generic;
25
26namespace HeuristicLab.GP.StructureIdentification {
27  public class ModelAnalyzerExporter : IFunctionTreeSerializer {
28    #region IFunctionTreeExporter Members
29
30    public string Name {
31      get {
32        return "HL2 ModelAnalyzer Exporter";
33      }
34    }
35
36    public bool TryExport(IFunctionTree tree, out string exported) {
37      foreach (IFunctionTree t in FunctionTreeIterator.IteratePrefix(tree))
38        if (!functionExporter.ContainsKey(t.Function.GetType())) {
39          exported = null;
40          return false;
41        }
42      exported = Export(tree);
43      return true;
44    }
45
46    public string Export(IFunctionTree tree) {
47      string result = ExportFunction(tree.Function, tree);
48      if (tree.SubTrees.Count > 0)
49        result += "(\n";
50      foreach (IFunctionTree subTree in tree.SubTrees) {
51        result += Export(subTree);
52        result += ";\n";
53      }
54      result = result.TrimEnd(';', '\n');
55      if (tree.SubTrees.Count > 0) result += ")";
56      return result;
57    }
58    #endregion
59
60    // try-export checks the keys of this dictionary
61    private static Dictionary<Type, Func<IFunction, IFunctionTree, string>> functionExporter = new Dictionary<Type, Func<IFunction, IFunctionTree, string>>() {
62      { typeof(Addition), (function, tree) => ((Addition)function).ExportToHL2(tree)},
63      { typeof(And), (function, tree) => ((And)function).ExportToHL2(tree)},
64      { typeof(Average), (function, tree) => ((Average)function).ExportToHL2(tree)},
65      { typeof(Constant), (function, tree) => ((Constant)function).ExportToHL2(tree)},
66      { typeof(Cosinus), (function, tree) => ((Cosinus)function).ExportToHL2(tree)},
67      { typeof(Differential), (function, tree) => ((Differential)function).ExportToHL2(tree)},
68      { typeof(Division), (function, tree) => ((Division)function).ExportToHL2(tree)},
69      { typeof(Equal), (function, tree) => ((Equal)function).ExportToHL2(tree)},
70      { typeof(Exponential), (function, tree) => ((Exponential)function).ExportToHL2(tree)},
71      { typeof(GreaterThan), (function, tree) => ((GreaterThan)function).ExportToHL2(tree)},
72      { typeof(IfThenElse), (function, tree) => ((IfThenElse)function).ExportToHL2(tree)},
73      { typeof(LessThan), (function, tree) => ((LessThan)function).ExportToHL2(tree)},
74      { typeof(Logarithm), (function, tree) => ((Logarithm)function).ExportToHL2(tree)},
75      { typeof(Multiplication), (function, tree) => ((Multiplication)function).ExportToHL2(tree)},
76      { typeof(Not), (function, tree) => ((Not)function).ExportToHL2(tree)},
77      { typeof(Or), (function, tree) => ((Or)function).ExportToHL2(tree)},
78      { typeof(Power), (function, tree) => ((Power)function).ExportToHL2(tree)},
79      { typeof(Signum), (function, tree) => ((Signum)function).ExportToHL2(tree)},
80      { typeof(Sinus), (function, tree) => ((Sinus)function).ExportToHL2(tree)},
81      { typeof(Sqrt), (function, tree) => ((Sqrt)function).ExportToHL2(tree)},
82      { typeof(Subtraction), (function, tree) => ((Subtraction)function).ExportToHL2(tree)},
83      { typeof(Tangens), (function, tree) => ((Tangens)function).ExportToHL2(tree)},
84      { typeof(Variable), (function, tree) => ((Variable)function).ExportToHL2(tree)},
85      { typeof(Xor), (function, tree) => ((Xor)function).ExportToHL2(tree)},
86    };
87    private static string ExportFunction(IFunction function, IFunctionTree tree) {
88      // this is smelly, if there is a cleaner way to have a 'dynamic' visitor
89      // please let me know! (gkronber 14.10.2008)
90      if (functionExporter.ContainsKey(function.GetType())) return functionExporter[function.GetType()](function, tree);
91      else return function.Name;
92    }
93
94    public static string GetName(IFunctionTree tree) {
95      return ExportFunction(tree.Function, tree);
96    }
97  }
98
99  internal static class HL2ExporterExtensions {
100    private static string GetHL2FunctionName(string name) {
101      return "[F]" + name;
102    }
103
104    public static string ExportToHL2(this Addition addition, IFunctionTree tree) {
105      return GetHL2FunctionName("Addition[0]");
106    }
107
108    public static string ExportToHL2(this Constant constant, IFunctionTree tree) {
109      double value = ((ConstantFunctionTree)tree).Value;
110      return "[T]Constant(" + value.ToString("r") + ";0;0)";
111    }
112
113    public static string ExportToHL2(this Cosinus cosinus, IFunctionTree tree) {
114      return GetHL2FunctionName("Trigonometrics[1]");
115    }
116
117    public static string ExportToHL2(this Differential differential, IFunctionTree tree) {
118      var varTree = (VariableFunctionTree)tree;
119      return "[T]Differential(" + varTree.Weight.ToString("r") + ";" + varTree.VariableName + ";" + -varTree.SampleOffset + ")";
120    }
121
122    public static string ExportToHL2(this Division division, IFunctionTree tree) {
123      return GetHL2FunctionName("Division[0]");
124    }
125
126    public static string ExportToHL2(this Exponential exponential, IFunctionTree tree) {
127      return GetHL2FunctionName("Exponential[0]");
128    }
129
130    public static string ExportToHL2(this Logarithm logarithm, IFunctionTree tree) {
131      return GetHL2FunctionName("Logarithm[0]");
132    }
133
134    public static string ExportToHL2(this Multiplication multiplication, IFunctionTree tree) {
135      return GetHL2FunctionName("Multiplication[0]");
136    }
137
138    public static string ExportToHL2(this Power power, IFunctionTree tree) {
139      return GetHL2FunctionName("Power[0]");
140    }
141
142    public static string ExportToHL2(this Signum signum, IFunctionTree tree) {
143      return GetHL2FunctionName("Signum[0]");
144    }
145
146    public static string ExportToHL2(this Sinus sinus, IFunctionTree tree) {
147      return GetHL2FunctionName("Trigonometrics[0]");
148    }
149
150    public static string ExportToHL2(this Sqrt sqrt, IFunctionTree tree) {
151      return GetHL2FunctionName("Sqrt[0]");
152    }
153
154    public static string ExportToHL2(this Subtraction substraction, IFunctionTree tree) {
155      return GetHL2FunctionName("Subtraction[0]");
156    }
157
158    public static string ExportToHL2(this Tangens tangens, IFunctionTree tree) {
159      return GetHL2FunctionName("Trigonometrics[2]");
160    }
161
162    public static string ExportToHL2(this Variable variable, IFunctionTree tree) {
163      var varTree = (VariableFunctionTree)tree;
164      return "[T]Variable(" + varTree.Weight.ToString("r") + ";" + varTree.VariableName + ";" + -varTree.SampleOffset + ")";
165    }
166
167    public static string ExportToHL2(this And and, IFunctionTree tree) {
168      return GetHL2FunctionName("Logical[0]");
169    }
170
171    public static string ExportToHL2(this Average average, IFunctionTree tree) {
172      return GetHL2FunctionName("Average[0]");
173    }
174
175    public static string ExportToHL2(this IfThenElse ifThenElse, IFunctionTree tree) {
176      return GetHL2FunctionName("Conditional[0]");
177    }
178
179    public static string ExportToHL2(this Not not, IFunctionTree tree) {
180      return GetHL2FunctionName("Logical[2]");
181    }
182
183    public static string ExportToHL2(this Or or, IFunctionTree tree) {
184      return GetHL2FunctionName("Logical[1]");
185    }
186
187    public static string ExportToHL2(this Xor xor, IFunctionTree tree) {
188      return GetHL2FunctionName("Logical[3]");
189    }
190
191    public static string ExportToHL2(this Equal equal, IFunctionTree tree) {
192      return GetHL2FunctionName("Boolean[2]");
193    }
194
195    public static string ExportToHL2(this LessThan lessThan, IFunctionTree tree) {
196      return GetHL2FunctionName("Boolean[0]");
197    }
198
199    public static string ExportToHL2(this GreaterThan greaterThan, IFunctionTree tree) {
200      return GetHL2FunctionName("Boolean[4]");
201    }
202  }
203}
Note: See TracBrowser for help on using the repository browser.