Free cookie consent management tool by TermsFeed Policy Generator

source: branches/HeuristicLab.DataImporter/HeuristicLab.DataImporter.Backend.Hl2ImporterFormat/Hl2Exporter.cs @ 13042

Last change on this file since 13042 was 9615, checked in by mkommend, 12 years ago

#1734: Updated copyright information in all DataImporter classes.

File size: 4.9 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2013 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 System.IO;
27using System.Globalization;
28using HeuristicLab.DataImporter.DataProcessor;
29using HeuristicLab.DataImporter.Data.Model;
30
31namespace HeuristicLab.DataImporter.Backend.Hl2ImporterFormat {
32  public class Hl2Exporter : IExporter {
33    public const string METATAG = "@";
34    public const string PROBLEMNAME = "PROBLEMNAME";
35    public const string VARIABLENAMES = "VARIABLENAMES";
36    public const string TARGETVARIABLE = "TARGETVARIABLE";
37    public const string NONINPUTVARIABLES = "NONINPUTVARIABLES";
38    public const string MINIMUMTIMEOFFSET = "MINIMUMTIMEOFFSET";
39    public const string MAXIMUMTIMEOFFSET = "MAXIMUMTIMEOFFSET";
40    public const string MAXIMUMTREEHEIGHT = "MAXIMUMTREEHEIGHT";
41    public const string MAXIMUMTREESIZE = "MAXIMUMTREESIZE";
42    public const string TRAININGSAMPLESSTART = "TRAININGSAMPLESSTART";
43    public const string TRAININGSAMPLESEND = "TRAININGSAMPLESEND";
44    public const string VALIDATIONSAMPLESSTART = "VALIDATIONSAMPLESSTART";
45    public const string VALIDATIONSAMPLESEND = "VALIDATIONSAMPLESEND";
46    public const string TESTSAMPLESSTART = "TESTSAMPLESSTART";
47    public const string TESTSAMPLESEND = "TESTSAMPLESEND";
48
49
50    public string Description {
51      get { return "Export data as HL2 importer file"; }
52    }
53
54    private Hl2ExporterView view;
55    public System.Windows.Forms.UserControl SettingsControl {
56      get {
57        if (view == null)
58          view = new Hl2ExporterView();
59        return view;
60      }
61    }
62
63    public void Export(DataSet dataSet) {
64      StreamWriter stream = new StreamWriter(view.FileName, false);
65      stream.WriteLine(METATAG + PROBLEMNAME + '=' + view.ProblemName);
66      StringBuilder sb = new StringBuilder();
67      foreach (ColumnGroup grp in dataSet.ColumnGroups) {
68        foreach (ColumnBase col in grp.Columns)
69          sb.Append(col.Name + ';');
70      }
71      sb = sb.Remove(sb.Length - 1, 1);  //remove last ';'
72      stream.WriteLine(METATAG + VARIABLENAMES + '=' + sb.ToString());
73      sb = sb.Remove(0, sb.Length);
74      stream.WriteLine(METATAG + TARGETVARIABLE + '=' + view.TargetVariable);
75      stream.WriteLine(METATAG + NONINPUTVARIABLES + '=' + view.NonInputVariables);
76      stream.WriteLine(METATAG + MINIMUMTIMEOFFSET + '=' + view.MinimumTimeOffset);
77      stream.WriteLine(METATAG + MAXIMUMTIMEOFFSET + '=' + view.MaximumTimeOffset);
78      stream.WriteLine(METATAG + MAXIMUMTREEHEIGHT + '=' + view.MaximumTreeHeight);
79      stream.WriteLine(METATAG + MAXIMUMTREESIZE + '=' + view.MaximumTreeSize);
80      stream.WriteLine(METATAG + TRAININGSAMPLESSTART + '=' + view.TrainingStart);
81      stream.WriteLine(METATAG + TRAININGSAMPLESEND + '=' + view.TrainingEnd);
82      stream.WriteLine(METATAG + VALIDATIONSAMPLESSTART + '=' + view.ValidationStart);
83      stream.WriteLine(METATAG + VALIDATIONSAMPLESEND + '=' + view.ValidationEnd);
84      stream.WriteLine(METATAG + TESTSAMPLESSTART + '=' + view.TestStart);
85      stream.WriteLine(METATAG + TESTSAMPLESEND + '=' + view.TestEnd);
86
87      for (int i = 0; i < dataSet.ColumnGroups.Max(x => x.RowCount); i++) {
88        foreach (ColumnGroup grp in dataSet.ColumnGroups) {
89          foreach (ColumnBase col in grp.Columns) {
90            if (col.GetValue(i) == null)
91              sb.Append(';');
92            else if (col.DataType == typeof(double?))
93              sb.Append(((double)col.GetValue(i)).ToString("r", CultureInfo.InvariantCulture.NumberFormat) + ';');
94            else if (col.DataType == typeof(DateTime?))
95              sb.Append(((DateTime)col.GetValue(i)).ToString(CultureInfo.InvariantCulture.DateTimeFormat) + ';');
96            else
97              sb.Append(col.GetValue(i).ToString() + ';');
98          }
99        } //foreach columngroup
100        sb = sb.Remove(sb.Length - 1, 1);
101        sb.Append(Environment.NewLine);
102        if (i % 100 == 0) {
103          stream.Write(sb.ToString());
104          sb.Remove(0, sb.Length);
105        }
106      } //foreach row
107      stream.Write(sb);
108      stream.Close();
109    }
110
111  }
112}
Note: See TracBrowser for help on using the repository browser.