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