#region License Information /* HeuristicLab * Copyright (C) 2002-2013 Heuristic and Evolutionary Algorithms Laboratory (HEAL) * * This file is part of HeuristicLab. * * HeuristicLab is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * HeuristicLab is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with HeuristicLab. If not, see . */ #endregion using System; using System.Linq; using System.Text; using System.IO; using System.Globalization; using HeuristicLab.DataImporter.DataProcessor; using HeuristicLab.DataImporter.Data.Model; namespace HeuristicLab.DataImporter.Backend.Hl2ImporterFormat { public class Hl2Exporter : IExporter { public const string METATAG = "@"; public const string PROBLEMNAME = "PROBLEMNAME"; public const string VARIABLENAMES = "VARIABLENAMES"; public const string TARGETVARIABLE = "TARGETVARIABLE"; public const string NONINPUTVARIABLES = "NONINPUTVARIABLES"; public const string MINIMUMTIMEOFFSET = "MINIMUMTIMEOFFSET"; public const string MAXIMUMTIMEOFFSET = "MAXIMUMTIMEOFFSET"; public const string MAXIMUMTREEHEIGHT = "MAXIMUMTREEHEIGHT"; public const string MAXIMUMTREESIZE = "MAXIMUMTREESIZE"; public const string TRAININGSAMPLESSTART = "TRAININGSAMPLESSTART"; public const string TRAININGSAMPLESEND = "TRAININGSAMPLESEND"; public const string VALIDATIONSAMPLESSTART = "VALIDATIONSAMPLESSTART"; public const string VALIDATIONSAMPLESEND = "VALIDATIONSAMPLESEND"; public const string TESTSAMPLESSTART = "TESTSAMPLESSTART"; public const string TESTSAMPLESEND = "TESTSAMPLESEND"; public string Description { get { return "Export data as HL2 importer file"; } } private Hl2ExporterView view; public System.Windows.Forms.UserControl SettingsControl { get { if (view == null) view = new Hl2ExporterView(); return view; } } public void Export(DataSet dataSet) { StreamWriter stream = new StreamWriter(view.FileName, false); stream.WriteLine(METATAG + PROBLEMNAME + '=' + view.ProblemName); StringBuilder sb = new StringBuilder(); foreach (ColumnGroup grp in dataSet.ColumnGroups) { foreach (ColumnBase col in grp.Columns) sb.Append(col.Name + ';'); } sb = sb.Remove(sb.Length - 1, 1); //remove last ';' stream.WriteLine(METATAG + VARIABLENAMES + '=' + sb.ToString()); sb = sb.Remove(0, sb.Length); stream.WriteLine(METATAG + TARGETVARIABLE + '=' + view.TargetVariable); stream.WriteLine(METATAG + NONINPUTVARIABLES + '=' + view.NonInputVariables); stream.WriteLine(METATAG + MINIMUMTIMEOFFSET + '=' + view.MinimumTimeOffset); stream.WriteLine(METATAG + MAXIMUMTIMEOFFSET + '=' + view.MaximumTimeOffset); stream.WriteLine(METATAG + MAXIMUMTREEHEIGHT + '=' + view.MaximumTreeHeight); stream.WriteLine(METATAG + MAXIMUMTREESIZE + '=' + view.MaximumTreeSize); stream.WriteLine(METATAG + TRAININGSAMPLESSTART + '=' + view.TrainingStart); stream.WriteLine(METATAG + TRAININGSAMPLESEND + '=' + view.TrainingEnd); stream.WriteLine(METATAG + VALIDATIONSAMPLESSTART + '=' + view.ValidationStart); stream.WriteLine(METATAG + VALIDATIONSAMPLESEND + '=' + view.ValidationEnd); stream.WriteLine(METATAG + TESTSAMPLESSTART + '=' + view.TestStart); stream.WriteLine(METATAG + TESTSAMPLESEND + '=' + view.TestEnd); for (int i = 0; i < dataSet.ColumnGroups.Max(x => x.RowCount); i++) { foreach (ColumnGroup grp in dataSet.ColumnGroups) { foreach (ColumnBase col in grp.Columns) { if (col.GetValue(i) == null) sb.Append(';'); else if (col.DataType == typeof(double?)) sb.Append(((double)col.GetValue(i)).ToString("r", CultureInfo.InvariantCulture.NumberFormat) + ';'); else if (col.DataType == typeof(DateTime?)) sb.Append(((DateTime)col.GetValue(i)).ToString(CultureInfo.InvariantCulture.DateTimeFormat) + ';'); else sb.Append(col.GetValue(i).ToString() + ';'); } } //foreach columngroup sb = sb.Remove(sb.Length - 1, 1); sb.Append(Environment.NewLine); if (i % 100 == 0) { stream.Write(sb.ToString()); sb.Remove(0, sb.Length); } } //foreach row stream.Write(sb); stream.Close(); } } }