Free cookie consent management tool by TermsFeed Policy Generator

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

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